We’ll add 2 more LEDs and a button now and here is the final photo:
So we now have a traffic light/pelican (Tux!) crossing simulator.
The 2nd Red LED is connected to wiringPi pin 3, (GPIO-22), and the 2nd Green LED is connected to wiringPi pin 4, (GPIO-23).
Test them as before with the gpio command, e.g.
for i in 0 1 2 3 4 ; do gpio mode $i out; done for i in 0 1 2 3 4 ; do gpio write $i 1; done for i in 0 1 2 3 4 ; do gpio write $i 0; done
The button is connected to wiringPi pin 8, (GPIO-0/SDA0), and the other side is connected to 0v. This pin has a 1.8KΩ resistor connecting it to +3.3v on the Raspberry Pi board. What this means is that when we set the pin to input mode and read the value, we are going to read in a logic high value (or 1 in our number terms), and when we push the button we connect the pin to 0v then the input will read a logic low value, or zero. This may appear to be the opposite way round to what we might expect, but we can easily cater for it in our programs.
Some designs for connecting buttons to the Rasberry Pi may include additional resistors, designed to protect the chips pin should you accidentally program it as an output. It’s not strictly necessary, but if you do program it as output, and write a high value to the pin, then you run the risk of damaging the pin, and possibly the chip.
We read the button as follows:
gpio mode 8 input gpio read 8
The gpio program will print 1 or 0 depending on the state of the button. Expect it to print 1 when the button is not pressed, and print 0 when it is pressed.
To complete this we need to write some software to control it all