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.
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.
The C programming language code for blinking the led is: –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <wiringPi.h> #define LedPin 0 int main(void) { if(wiringPiSetup() == -1) { //when initialize wiringPi failed, print message to screen printf("setup wiringPi failed !\n"); return -1; } pinMode(LedPin, OUTPUT); while(1) //infinite loop { digitalWrite(LedPin, LOW); //led on printf("led on...\n"); delay(500); //gives a break of 500ms digitalWrite(LedPin, HIGH); //led off printf("...led off\n"); delay(500); //gives a break of 500ms } return 0; } |
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:
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:
The code for achieving this is:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> #include <wiringPi.h> // procedure to turn LEDs ON and Off void OnOff(int pin) { digitalWrite(pin, LOW); //led on delay(300); digitalWrite(pin, HIGH); //led off delay(300); } int main(void) { if(wiringPiSetup() == -1) { //when initialize wiringPi failed, print message to screen printf("setup wiringPi failed !\n"); return -1; } // assigning pin mode to GPIO 0 to 3 pinMode(0, OUTPUT); //red pinMode(1, OUTPUT); //blue pinMode(2, OUTPUT); //yellow pinMode(3, OUTPUT); //green while(1) { OnOff(0); OnOff(1); OnOff(2); OnOff(3); } return 0; } |
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!
Please educate me