Article Index

A First IoT Program

This purpose of this program is to just let you check that everything is working - the functions used will be explained in detail later. 

Start a new project, call it Blink, and make sure you add the bcm2835 library to the linker options - right click on the project and select properties, select Build,Linker in the dialog box that appears, click the three dots in the Libraries section, click Add Library and specify bcm2835.

Then enter the program:

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

int main(int argc, char **argv) {
 if (!bcm2835_init())
   return 1;
 while (1) {
  bcm2835_gpio_write(RPI_GPIO_P1_07, HIGH);
  bcm2835_gpio_write(RPI_GPIO_P1_07, LOW);
 return 0;

Don't panic when NetBeans initially shows you lots of errors. If you build or run the project NetBeans will take notice of the header files and stop worrying about identifiers like bcm2835_init(). Once the project has been built NetBeans knows all about the new library and will syntax check and offer code completion hints. 

Even though you don't know much about the library just yet you can see that the general idea. First we try to initalize the library. If this works GPIO pin 7  is set to be an output and then the loop turns it on and off (high voltage then low) with a dearly of half a second. 

The library uses physical pins rather than logical GPIO numbers so RPI_GPIO_P1_07 is pin 7 on connector P1. 

If you want to connect an LED to see the "blinking" for real then this is easy enough but you do need a current limiting resistor - 270Ohm is a good choice. 


How you build the circuit is up to you. You can use a prototyping board or just a pair of jumper wires. 


If you can't be bothered to go though the ritual of testing "blinky" with a real LED then just connect a logic analyzer to pin 7 and you will see the one second pulses.  

Running Programs On NetBeans

All you have to do to run the program is click the Run icon or press F6. This downloads the program to the Pi that is the Build Server for the project and then runs it on the same Pi. If you try this with Blink you will see the LED blink or the Logic Analyzer will show the output pulse train. 

The next question is how do you stop the program. 

If the program isn't in the form of an infinite loop it will terminate naturally and there isn't anything to do but blink is an infinite loop which never ends. If you click the run icon a second time a second copy of the program will be created and will start to run. 

This clearly isn't a good idea for programs that are interacting with GPIO lines. 

You have to explicitly stop the program before you run it again. To do this simply click the red stop button down in the output window:


You can also see that there is a program running from the status bar at the bottom of the window:


If you do run multiple programs, either by design or accident, you can stop any of them by double clicking on the run status at the bottom of the window. Notice that the message to the right tells you how many programs are running. You can stop any or all by clicking on the corresponding stop buttons. 


This is the minimum you need to know to run a program using NetBeans. The important topics of debugging and creating templates will be discussed in a later chapter. 

Pin Numbering

The one thing that drives every programmer mad are the different ways of referring to the numbering of the GPIO pins. 

There is the physical pin number i.e. the number of the pin on the physical connector and the logical GPIO number that is assigned deep in the hardware. 

Another problem is that different generations of Pi have modified the pin layout slightly and there was an extension in the move from Pi 1 to Pi 2 and 3. In most cases the changes are slight and if you stay within the first 26 pins of the GPIO connector then you don't even have to worry if your program is running on a Pi 1, 2 or 3. 

The standard connector for a late generation P1 is:


and for a Pi 2 or 3:



The bcm2835 library works in terms of the GPIO numbers defined by the processor. However it also provides an enumeration which allows you to work in terms of the physical pins. For example in the blinky program the instruction:

 bcm2835_gpio_write(RPI_GPIO_P1_07, HIGH);

set the GPIO line connected to pin 7 on connector P1 high. If you look at the code for the enumeration you will discover that RPI_GPIO_P1_07 is defined to be 4 which corresponds to GPIO 4 which is indeed connected to pin 7. You could have used:

 bcm2835_gpio_write(4, HIGH);

 to set the same GPIO line high. 

It is up to you which way you choose to specify pin numbers - by logical GPIO number or physical pin number. In most cases the physical pin numbers are more direct because they specify what you are actually connecting wires to. However notice that you do need to specify the model of Pi that you are working with. For example the library defines three types of pin 3 RPI_GPIO_P1_03 which is the Pi 1 revision 1 version of pin 3 or GPIO 0 and RPI_V2_GPIO_P1_03 which is the Pi 1 revision 2 version of pin 3 or GPIO 2 and RPI_BPLUS_GPIO_J8_03 for all models after the B+. 

You can see that this can be confusing but the change in the pin outs is also confusing. If you are using the Pi Compute module then don't just he enumeration simply use the GPIO number. 

In general you have to decide if you should use enumeration values that start with 

RPI_GPIO for Pi 1 revision 1

RPI_V2_GPIO for Pi 1 revision 2


RPI_BPLUS_GPIO for all modern Pis from the B+ on.