3: More LEDs and a Button

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.

3 More LEDs and a Buttonfor 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.

3 More LEDs and a Button SchematicSome 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.

 

For more detail: 3 More LEDs and a Button


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top