The bcm2835C library is the easiest way to get in touch with the Pi's GPIO lines. In this chapter we take a look at the basic operations involved in using the GPIO lines with an emphasis on output. How fast can you change a GPIO line, how do you generate pulses of a given duration and how can you change multiple lines in sync with each other? 

 

 

Now On Sale!

You can now buy a print edition of Raspberry Pi IoT in C

You can buy it from:

USA and World   Amazon.com
Canada              Amazon.ca
UK                     Amazon.co.uk
France               Amazon.fr
Germany            Amazon.de
Spain                 Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 

 

For Errata and Listings Visit: IO Press

 

 

This our ebook on using the Raspberry Pi to implement IoT devices using the C programming language. The full contents can be seen below. Notice this is a first draft and a work in progress. 

Chapter List

  1. Introducing Pi (paper book only)

  2. Getting Started With NetBeans In this chapter we look at why C is a good language to work in when you are creating programs for the IoT and how to get started using NetBeans. Of course this is where Hello C World makes an appearance.

  3. First Steps With The GPIO
    The bcm2835C library is the easiest way to get in touch with the Pi's GPIO lines. In this chapter we take a look at the basic operations involved in using the GPIO lines with an emphasis on output. How fast can you change a GPIO line, how do you generate pulses of a given duration and how can you change multiple lines in sync with each other? 

  4. GPIO The SYSFS Way
    There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the bcm2835 library. Sometimes you need this because you are working in a language for which direct access to memory isn't available. It is also the only way to make interrupts available in a C program.

  5. Input and Interrupts
    There is no doubt that input is more difficult than output. When you need to drive a line high or low you are in command of when it happens but input is in the hands of the outside world. If your program isn't ready to read the input or if it reads it at the wrong time then things just don't work. What is worse is that you have no idea what your program was doing relative to the event you are trying to capture - welcome to the world of input.

  6. Memory Mapped I/O
    The bcm2835 library uses direct memory access to the GPIO and other peripherals. In this chapter we look at how this works. You don't need to know this but if you need to modify the library or access features that the library doesn't expose this is the way to go. 

  7. Near Realtime Linux
    You can write real time programs using standard Linux as long as you know how to control scheduling. In fact it turns out to be relatively easy and it enables the Raspberry Pi to do things you might not think it capable of. There are also some surprising differences between the one and quad core Pis that make you think again about real time Linux programming.

  8. PWM
    One way around the problem of getting a fast response from a microcontroller is to move the problem away from the processor. In the case of the Pi's processor there are some builtin devices that can use GPIO lines to implement protocols without the CPU being involved. In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos.

  9. I2C Temperature Measurement
    The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one.

  10. A Custom Protocol - The DHT11/22
    In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in implementing a custom protocol directly in C. 

  11. One Wire Bus Basics
    The Raspberry Pi is fast enough to be used to directly interface to 1-Wire bus without the need for drivers. The advantages of programming our own 1-wire bus protocol is that it doesn't depend on the uncertainties of a Linux driver.

  12. iButtons
    If you haven't discovered iButtons then you are going to find of lots of uses for them. At its simples an iButton is an electronic key providing a unique coce stored in its ROM which can be used to unlock or simply record the presence of a particular button. What is good news is that they are easy to interface to a Pi. 

  13. The DS18B20
    Using the software developed in previous chapters we show how to connect and use the very popular DS18B20 temperature sensor without the need for external drivers. 

  14. The Multidrop 1-wire bus
    Some times it it just easier from the point of view of hardware to connect a set of 1-wire devices to the same GPIO line but this makes the software more complex. Find out how to discover what devices are present on a multi-drop bus and how to select the one you want to work with.

  15. SPI Bus
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  16. SPI MCP3008/4 AtoD  (paper book only)

  17. Serial (paper book only)

  18. Getting On The Web - After All It Is The IoT (paper book only)

  19. WiFi (paper book only)

 


The bcm2835 library is designed to make it easy to access the hardware - there are other ways that we will examine later. It has the advantage of being open source and it uses a direct approach so you don't need to worry about Linux drivers.

In this chapter we take a first look at using bcm2835, how to install it and how to control the GPIO pins as basic input output lines.   Later on we will look at how the library works and how to avoid using it if you really need to.

Setting Up bcm2835 With NetBeans

Exactly how to set up the library is a tiny bit tricker than most because it has to run as root to be able to access all of the GPIO facilities. It is also a little puzzling how to set up the library if you are using a remote build server - where do you copy the library for example? As it turns out the answer to this is easy.

First we need to set up a password for the stanard root user and allow the root user to log in using SSH.

To allow root to log on with a password we have to assign a password so log in as user pi and give the command:

sudo passwd root

you will be prompted to type the new password twice. 

Next use a text editor of your choice e,g nano to edit the ssh.config file to allow root to log on via SSH:

sudo nano /etc/ssh/sshd_config

Find 

PermitRootLogin without-password

and change it to read

PermitRootLogin yes

Reboot the system to make the changes take effect or restart the SSH agent:

sudo service ssh restart

Check that this works by logging on using SSH, Putty say, using the user name root and the password you supplied earlier. 

If you are using NetBeans on the Pi then simply log in as root when ever you are working with it. 

If you are using NetBeans with the Pi as a remote build host then you need to create a new build host with the user name root and the password you specified. This might have the side effect of moving the location where projects are created to root's home directory.

With these changes you can check that you can create and/or run a project with the new build host.

If you find that a project is simply failing to run and returns a -1 status with no reason given the chances are you are not runing the program as root. 

The final step is to download the bcm2835 library from:

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

At the time of writing this is at version 1.50 and works with all versions of the Raspberry Pi including the Zero and the Pi 3. 

To install the library you need to download it into root's home directory. You can do this from the command line using:

sudo wget "http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz"

Where you may well need to change the URL to reflect the latest version.  The wget command as given will download the tar ball into the current directory.

Then use the following commands:

tar zxvf bcm2835-1.xx.tar.gz
cd bcm2835-1.xx
./configure
make
sudo make check
sudo make install

where xx is the version number, 50 at the moment. When these commands are completed you will have a bcm2835-1.xx folder which contains copies of the library, the header file and all the source code.

The library and header file that you are going to use will have been installed in /usr/local/lib and /user/local/include where NetBeans will find them automatically. Notice that NetBeans will find them automatically even if you are using the Pi as a remote build host. 

That is you do not have to install the library on the machine running NetBeans only on the build host. 

To make use of the library in your program you have to do two final things. 

The first is you have to include the header file:

#include <bcm2835.h>

NetBeans will not only find the header on the remote build host it will also let you work with it as if it was a file within the project. 

You also have to specify the name of the library that you want the linker to add to your program. To do this 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. Don't make the mistake of trying to add a Library File. 

 

Note you don't have to specify where the library is stored or it version number NetBeans will work it out. 

Now you are ready to write your first IoT program which is nearly always to flash an LED.