My programming ramblings

Raspberry Pi - Using C++ to control a group of LEDs

Posted on December 23, 2018 by Paul

In this article, I will show you how to use C++ to control a group of LEDs connected to a Raspberry Pi. You can see the final results in this short video:

List of components:

1 Raspberry Pi (any recent version should work);

1 Breadboard;

3 LEDs;

3 220 Ω resistors;

4 jump wires.

Note: In my setting I’ve used a T cobbler to connect the Raspberry Pi GPIO pins to a small breadboard, this has the advantage that I can easily see the named pins of the Pi. However, you can also directly connect the RPi pins to your main breadboard using a couple of female-male Dupont wires.

I’ve connected the positive leg, the anode, of each of the three LEDs to one of the Pi’s pins in this order: 17, 22 and 6. If you want to see your Pi pins layout, use this command in a Terminal on your RPi:

1 pinout

The negative leg, the cathode, of each LED is connected to the ground rail of my breadboard using a 220 Ω resistor. Also, I’ve used a jump wire to connect the ground rail of the breadboard to one of the RPi’s ground pins.

In the next diagram, made with Fritzing, you can see a more clear picture of how the circuit was wired:

You should have an up to date version of Raspbian installed on your RPi. In my case, I’ve used the latest available version of Raspbian Stretch with desktop and recommended software. The advantage of using the full Raspbian version is that you have everything included: a C++ compiler and the wiringPi library used to access the GPIO pins. If you are using Raspbian Lite, you will probably need to use the apt package manager to install GCC and wiringPi.

Assuming that you’ve finished assembling the above circuit, we can write the C++ code that we’ll use to control the three LEDs. I’ve saved the next code in a file named led_control.cpp:

The above code starts by registering a callback handler that will be called if the user presses CTRL-C, this is useful to do a clean exit from the program main loop.

Next, we initialize the wiringPi library using wiringPiSetupGpio. This function will let us use the BCM pin numbering layout, which is the numbering system you will find in the official Raspberry Pi GPIO documentation. As a side note, for historical reasons, wiringPi uses his own numbering layout.

If you are curious to see the difference between the BCM numbering layout and wiringPi, use the next command in a Terminal:

Next, we define the three pins we are going to use and set them as output pins.

Finally, we start the main loop of the program, this will run as long as the global flag RUNNING is true. Inside the main loop, we use the blink_led function to start an LED, wait one second, stop the LED, wait one second, start the next LED and so …

Please note how we are using the RUNNING variable to do a clean program exit. When the user presses CTRL-C the my_handler function is called which will set the RUNNING variable to false. Otherwise, without catching the SIGINT signal (which is triggered by CTRL-C), if you simply stop the program using CTRL-C you can, potentially, leave one of the output pins on and you will end up with some LED always on.

If you want to learn more about programming on the Raspberry Pi, a very good book is Exploring Raspberry Pi by Derek Molloy:

If you are interested to learn more about modern C++, I recommend A Tour of C++ by Bjarne Stroustroup:

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.