As an application of L Chika, I tried to reproduce the movement of the traffic light.
Reproduce the traffic light attached to the pedestrian crossing. Let's try using "RGB LED".
** <What is RGB LED? > ** It is a 4-legged LED that can express color. Based on red, green, and blue, you can also use the switch to output purple, yellow, and light blue. This time, I mainly use "RGB LED". Please note that the role changes depending on the length of the foot.
Wire like this.
$ sudo nano walk_shingo.py
Fill in the following in it
walk_shingo.py
import RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BCM)
PORT_L = 27 #red
PORT_R = 17 #Blue
GPIO.setup(PORT_L, GPIO.OUT)
GPIO.setup(PORT_R, GPIO.OUT)
try:
while True:
GPIO.output(PORT_L, GPIO.LOW) #Green light
GPIO.output(PORT_R, GPIO.HIGH)
time.sleep(5)
for i in range(10): #Flashing green light
GPIO.output(PORT_R, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(PORT_R, GPIO.LOW)
time.sleep(0.2)
GPIO.output(PORT_L, GPIO.HIGH) #Red light
GPIO.output(PORT_R, GPIO.LOW)
time.sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit()
After pressing "control" + "X", press "y" and press Enter to save.
$ python walk_shingo.py
Activate the traffic light with. When you want to quit, press "control" + "C". In this program, green light 5 seconds → green light blinking 0.2 second interval x 10 times → red light 5 seconds is repeated. Feel free to customize from "time.sleep ()".
Here, let's use the tact switch as a push button to reproduce a push button type pedestrian traffic light.
Add a tact switch and jumper wire to the wiring in 1 above.
Wire like this.
$ sudo nano push_shingo.py
Fill in the following in it
push_shingo.py
import RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BCM)
SWITCH = 4 #Tact switch
PORT_L = 27 #red
PORT_R = 17 #Blue
GPIO.setup(PORT_L, GPIO.OUT)
GPIO.setup(PORT_R, GPIO.OUT)
GPIO.setup(SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
while True:
if (GPIO.input(SWITCH) == GPIO.LOW): #Red light
GPIO.output(PORT_L, GPIO.LOW)
GPIO.output(PORT_R, GPIO.HIGH)
if (GPIO.input(SWITCH) == GPIO.HIGH): #Green light
GPIO.output(PORT_L, GPIO.LOW)
GPIO.output(PORT_R, GPIO.HIGH)
time.sleep(1)
GPIO.output(PORT_L, GPIO.HIGH)
GPIO.output(PORT_R, GPIO.LOW)
time.sleep(5)
for i in range(10): #Flashing green light
GPIO.output(PORT_L, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(PORT_L, GPIO.LOW)
time.sleep(0.2)
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit()
Save ...
$ python push_shingo.py
Run with! Basically, it is a red light, and when the switch is turned on, the green light is 5 seconds → the green light blinks 0.2 seconds interval x 10 times. Also, by changing the signal 1 second after pressing the switch, it is made into a shape close to the actual machine.
Next, reproduce the traffic light on the road. This time, red and blue are "RGB LEDs", and yellow is a commercially available yellow LED. I think you can paint the white LED with a marker.
Remove the tact switch from the wiring in 2 above. Add another set of LEDs, etc. there.
(0. When using a yellow LED ... Replace "RGB LED" in "PORT_R" with yellow LED. At this time, leave one space next to both feet of the LED. )
Wire like this.
$ sudo nano car_shingo.py
Fill in the following in it
car_shingo.py
import RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BCM)
PORT_L = 17 #red
PORT_C = 27 #yellow
PORT_R = 22 #Blue
GPIO.setup(PORT_L, GPIO.OUT)
GPIO.setup(PORT_C, GPIO.OUT)
GPIO.setup(PORT_R, GPIO.OUT)
try:
while True:
GPIO.output(PORT_L, GPIO.LOW) #Green light
GPIO.output(PORT_C, GPIO.LOW)
GPIO.output(PORT_R, GPIO.HIGH)
time.sleep(4)
GPIO.output(PORT_L, GPIO.LOW) #Yellow light
GPIO.output(PORT_C, GPIO.HIGH)
GPIO.output(PORT_R, GPIO.LOW)
time.sleep(0.5)
GPIO.output(PORT_L, GPIO.HIGH) #Red light
GPIO.output(PORT_C, GPIO.LOW)
GPIO.output(PORT_R, GPIO.LOW)
time.sleep(4)
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit()
Save it and use the command below to make it glow.
$ python car_shingo.py
In this program, green light 4 seconds → yellow light 0.5 seconds → red light 4 seconds is repeated.
Some traffic lights are moving by detecting the car with a sensor. It might be a good idea to attach a sensor to the Raspberry Pi and give it a try. Also, if you use a slightly smaller LED and incorporate it into the world of diorama and model railroads, it seems that you can enjoy it more realistically.
It is a frame to make it look like a traffic light. If you print with A4 according to the page, the size will be just right. Please enjoy it more realistically with this.