Raspberry Pi – Programming LEDs

Electronics are very fun to play with but adding computer science to it makes it more fun. Following the previous post of using a breadboard, we are now going to control program LEDs to flow by a certain pattern using the C programming language on the Raspberry Pi. The Raspberry Pi consists of General Purpose Input/Output (GPIO) pins which can be used as input or output to circuits, writes 1 or 0 where ‘1’ indicates a high voltage and ‘0’ represents a low voltage.

Materials Needed:-

  • A Raspberry Pi
  • LEDs (1 red, 1 blue, 1 yellow, 1 green)
  • 4 Resistors of 220Ω
  • Many jumper wires

Setting the Raspberry Pi

The pins on the Raspberry Pi 2 and 3 are numbered and named like in the picture.

wiringPi Diagram

Note: There are 2 ways to interface with the raspberry pi; either by the pre-set architecture of the processor (Broadcom 2835/2836) or by WiringPi (which makes the addressing similar to Arduino).

Using Raspbian as OS, download and install Wiring PI (a library for controlling GPIO pins) as per the instructions here. As we will be using the WiringPi library, we will use the WiringPi numbering.

Blinking LED

To blink a LED, we need to control the flow of voltage/current to the circuit. This can be done by connecting the positive side of a LED to a 3.3V pin on the Pi and the negative to a GPIO (in this example GPIO 0) as shown in the picture.

LED control Raspberry Pi

The C programming language code for blinking the led is: –

 

For further understanding of the functions used, please refer to WiringPi documentation. Don’t forget to add the command -l wiringPi to the compiler or the program won’t compile.

The output will be similar to this video:

 

First experiment with #Raspberry #Pi! Blinking Led

A video posted by Yashtir Gopee (@yashtir) on

Having understood these functions, let’s try doing the Mauritian flag colours flow with 4 LEDs (Red, Blue, Yellow and Green). Each pairs of LED and resistor is connected in parallel to the Pi (GPIO 0 – 3) as follows:

MU flag

The code for achieving this is:-

In this code, we are using a procedure named ‘OnOff’ (a function not returning any value) to switch each LED between ON and OFF.

To see the output of this one, try it for yourself!

One thought on “Raspberry Pi – Programming LEDs

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.