This library also supports the RPi2 and upwards, with the bcm2836 and upwards chipsets

Installing The Library

The library homepage

In the commands below change the .XX to match the current library version number, e.g. “.37”.

If you are using the GUI then open the command prompt using Menu > Other > LXTerminal

Using your RPi download the .tar.gz file from the library page to your “/home/pi/” root directory.  You can do this using wget on the command line:

cd /home/pi/

wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.XX.tar.gz

Install the library files using the following commands

Unzip the downloaded file (Change the file version number to match your downloaded version)

tar zxvf bcm2835-1.XX.tar.gz

The files will be unzipped into a folder called “/home/pi/bcm2835-#.#” where # is the version number. (The following instructions are based on the instructions in the bcm2835.h file so if something doesn’t work check there to see if the instructions have changed)

Change to the directory the files we’re unzipped into (Change the directory version number to match your downloaded version)

cd bcm2835-1.XX

Run the configure exe

./configure

Execute the makefile

make

Then

sudo make check

Then

sudo make install

(The sudo is needed to elevate permissions to the root user)

The library is now ready to use.

Using the Library In A NetBeans Project

Including the library header file

#include <bcm2835.h>

When you compile you also need to include -lbcm2835 so the libraries object file is added to the final compilation.

Right click the project > Properties > Build > Linker > In the ‘Libraries’ section press the ‘…’ button > Add Option… > Other Option > Enter: -lbcm2835

Using the Library In A Geany Project

Including the library header file

#include <bcm2835.h>

When you compile you also need to include -lbcm2835 so the libraries object file is added to the final compilation.
For example at the command line:

gcc clk.c -o clk -lbcm2835

In a simple makefile for a project with a single file called main.c:


all: output_file_name

output_file_name: main.o
	gcc main.o -lbcm2835 -o output_file_name

main.o: main.c
	gcc -c main.c

clean:
	rm -rf *o output_file_name

Trying out an example project using the command line C compiler

You can compile one of the examples using this:


cd examples/blink
gcc -o blink -l rt blink.c -l bcm2835 

and then run the blink exe with this command:


sudo ./blink

If you connect the positive pin of a 5V LED to pin 11 of the P1 header it should be blinking. Use CTRL+Break or CTRL+SHIFT+C to stop the exe.

Problems With The Library

DelayMicroseconds() is a handy function but if you use it a lot you’ll find operation takes much longer than it should.  This is because the function allows the linux scheduler to know the process is sleeping and decide to move on to another process.  See here for a better solution if you need to avoid this.

Controlling Pins

See the header file for all of the functions.

RPi1 Model B+ and RPi2 Model B

//Setup
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_40, BCM2835_GPIO_FSEL_INPT);		//RPI1B+ & RPi2B <<Set as input
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_40, BCM2835_GPIO_FSEL_OUTP);		//RPI1B+ & RPi2B <<Set as output
bcm2835_gpio_set_pud(RPI_BPLUS_GPIO_J8_40, BCM2835_GPIO_PUD_UP);		//RPI1B+ & RPi2B <<Set pull up

//Outputs
#define LED_GREEN(state)				bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_40, !state)        //RPI1B+ & RPi2B

//Inputs
//bcm2835_gpio_lev returns uint8_t
#define SW_A_INPUT						bcm2835_gpio_lev(RPI_BPLUS_GPIO_J8_40)                    //RPI1B+ & RPi2B
RPi1 V2 (old)

//Outputs
#define LED_GREEN(state)				bcm2835_gpio_write(RPI_V2_GPIO_P1_24, !state)
#define LED_YELLOW(state)				bcm2835_gpio_write(RPI_V2_GPIO_P1_26, !state)

//Inputs
//bcm2835_gpio_lev returns uint8_t
#define SW_A_INPUT						bcm2835_gpio_lev(RPI_V2_GPIO_P1_03)

Upgrading to a newer version of the library

Just reinstall, no need to uninstall the old one.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. PaulD

    10 months ago

    Hi There,

    RPI-5 question

    I followed the installation as described above. However, I cannot get any IO to change state. bcm2835_delay(n) is functioning as expected so some of the library is operational. I’m using Netbeans remote build method.

  2. ubuntupunk

    11 months ago

    Will this work on a Pi3b+?

  3. Helim

    4 years ago

    Sorry C Sickles that I misspelled your name, I’m German and accidentally translated your name. It wasn’t bad intent.

  4. Helim

    4 years ago

    Thank you for all the helpful information!
    I have the same problem as “C Sicheln” in VSCode.
    Even if I enter the argument “-l bcm2835” in the configuration file “task.json”, gcc always compiles without bcm2835.
    Where can I enter the argument “-l bcm2835” in VSCode?

  5. C Sickles

    4 years ago

    Just CANT get visual studio to “see” it properly..
    No matter what I do i get “undefined reference to `bcm2835_init’ i2c”

    All “references” to bcm 2835 come back a un defined.
    It is installed on the Pi and Visual studio sees the API objects when writing the code but it wont debug / build… This is the EXACT i2c I need (write a buffer) that no other Lib seems to have..
    Help !!

  6. C Stanford

    4 years ago

    Excellent i2c library! I am able to do all the transactions I like. I see this as being superior to the wiringpi library as the delay between bus cycles is about 1us, where wiringpi is 20us. This library gives the raspberry pi i2c performance similar to the Wire library in Arduino.
    One more complaint about wiringpi… my python smbus has the same delay between cycles as wiringpi C++.

  7. Pascal Serveurperso

    4 years ago

    Raspberry PI 4 ?

  8. Norm Lane

    8 years ago

    bcm2835.h ends up in /usr/local/include. Is that right? Because netbeans isn’t finding it. Is it supposed to be transferred to the Windows machine?

  9. Rudolf Byker

    8 years ago

    What is the difference between this driver, and the BCM2835 drivers included in Vanilla Raspbian nowadays?

  10. Santokh

    9 years ago

    hi
    im trying to control the gpio through netbeans but the file /sys/class/gpio/gpio17/direction in not getting accessed by the FileWriter object..???? Does raspberry has special imports in netbeans??

  11. Davinci

    10 years ago

    I hope the gpio library can use the framework in the gpio.txt(under kernel’s directory “Documentation/devicetree/bindings/gpio/gio.txt”), kernel version 3.12

  12. Colin

    10 years ago

    I had to do “sudo make check” to get it to pass

    1. Ehren

      10 years ago

      Just done what you said after it failed and it now has passed it! thanks :D

  13. Enthusiastic

    11 years ago

    Hi! Assuming that I have a configured cross-compiler and I’m developing c++ apps in
    eclipse on windows, how can I add this nice stuff to my eclipse project.

  14. Sajal Nagwanshi

    12 years ago

    hi !
    I am trying to interface GPIO with Hello_video program. But it’s saying “it is not able to recognize the functions”.
    How do I make/compile the program using the bcm2835 header ?

    1. Amit R

      10 years ago

      Have you linked the library at compile time?
      e.g. : sudo example.c -o example -lbcm2835

    2. Sajal Nagwanshi

      10 years ago

      Thanks Amit !
      I had posted this 2 years ago. I had figured it out then.
      But good to see your reply.
      Where are you from ? Which engg college ?

  15. Pingback: Eclipse General Notes « Raspberry Pi Projects

  16. Pingback: .Getting Your RPi Ready For C Programming « Raspberry Pi Projects

Comments

Your email address will not be published. Required fields are marked *