Rule the pins of Raspberry Pi 3

To easily drive the pins of RPi use library named bcm2835. It will work with Raspberry Pi 3 despite the fact this model uses BCM2837 chip.

Visit http://www.airspayce.com/mikem/bcm2835/

Check the latest version of bcm2835 library.
Open terminal and do what is below. wget command will fetch the choosen version of the library, say bcm2835-1.55.tar.gz and the others commands will unpack it and install:

wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.xx.tar.gz
tar zxvf bcm2835-1.xx.tar.gz
cd bcm2835-1.xx
./configure
make
sudo make check
sudo make install

It is time to test the library now. Using Geany (should be installed by default) go to File -> New and place the code which could be find on http://www.airspayce.com/mikem/bcm2835/blink_8c-example.html The below example has some modification to work with RPi3: instead of original #define PIN RPI_GPIO_P1_11 there is #define PIN RPI_BPLUS_GPIO_J8_11 what is a correct PIN definition when using RPi 3. To check how correctly make an PIN assignment open file named bcm2835.h and look from line 771 (described as GPIO Pin Numbers) There are definitions for all Raspberry models pins.

#include <bcm2835.h>
#include <stdio.h>

//Blinks in diode connected to PIN11 
#define PIN RPI_BPLUS_GPIO_J8_11
int main (void){
  if(!bcm2835_init()) //if initialisation of bcm2835 is unsuccesful
    return 1; //end the program
  
  bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP); //set the pin to be an output
  while(1){ //starting endless loop
    bcm2835_gpio_write(PIN, HIGH); 
    delay(500); //wait 500ms
    bcm2835_gpio_write(PIN, LOW);
    delay(500);
  }
return 0;
}
Now this file has to be saved, let's say under blink.c and compiled (in terminal):

gcc -o blink blink.c -l bcm2835

Next, try it:

sudo ./blink

It can be quite boring to move to terminal every time when finishing and upgrading your code, so we can use Compile and Build from Geany menu. Anyway trying to build gives us a lot of errors, and this is due to lack of library linking. To sort it out, go to Geany's menu Build-->Set Build Commands. 

New window will appear and there you can find C commands section, with number 2 Build and fill Command with gcc -Wall -o "%e" "%f". At the end of this line just add -lbcm2835, so whole line looks like this: gcc -Wall -o "%e" "%f" -lbcm2835
To have a possibility of executing from Geany, edit the 'Execute' line, and add 'sudo' before "./%e" 

From now, you can use Compile (F8), Build (F9) and Run commands straight from Geany. 





Comments

Popular posts from this blog

Hello world with ADS1298

matplotlib and Raspberry Pi 3 - show my 'Hello world!'

Data collection from ADS1298