I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)

As an application of L Chika, I tried to reproduce the movement of the traffic light.

Things to prepare

1. Make the pedestrian traffic light L-flicker

Reproduce the traffic light attached to the pedestrian crossing. Let's try using "RGB LED".

wiring

  1. Connect the male-female cable of the jumper wire to ** pin 11 ** (6th from the top on the left) of "GPIO".
  2. Insert the other side into the breadboard.
  3. Insert one leg of the resistor in the same row as the jumper wire.
  4. Insert the ** red anode side ** of the "RGB LED" (after putting on the light diffusion cap) in the same row as the opposite foot of the resistor.

** <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. IMG_3809.jpg

  1. Insert the male-male jumper cable in the same row as the RGB LED ** cathode **. Insert the other side into the minus lane.
  2. Make another set of steps 1-5. At this time, connect the male-female cable of the jumper wire to ** pin 13 ** (6th from the top on the left) of "GPIO". Also, insert the ** blue anode side ** of the RGB LED in the same row as the resistor.
  3. Insert the male-female cable of the jumper wire into the minus lane, and connect the other side to ** pin 39 ** (first from the bottom left) of "GPIO".
  4. Turn off "1. Pedestrian traffic light" from the "traffic light-like frame" and attach it to the LED.

Wire like this. IMG_3805.jpg

program

$ 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 ()".

2. L-flicker the push-button pedestrian traffic light

Here, let's use the tact switch as a push button to reproduce a push button type pedestrian traffic light.

wiring

Add a tact switch and jumper wire to the wiring in 1 above.

  1. Insert the male-female cable of the jumper wire into ** pin 1 ** (first on the left). Insert the other side into the breadboard.
  2. Insert the tact switch so that it sandwiches the groove in the center of the breadboard. At this time, make sure that one of your legs is in the same row as the jumper wire that connects to pin 1.
  3. Insert the male-female cable of the jumper wire into ** pin 7 ** (first on the left). Insert the other side in the same row as the other leg of the tact switch.
  4. Turn off "2. Pedestrian traffic light (push button type)" from the "traffic light-like frame" and fit it on the LED.

Wire like this. IMG_3804.jpg

program

$ 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.

3. Make the vehicle traffic light L-flicker

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.

wiring

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. )

  1. Insert the male-female cable of the jumper wire into ** pin 15 ** (8th on the upper left). Insert the other side in the same row as the resistor.
  2. Insert the resistor in the same row as the ** blue anode ** of the "RGB LED".
  3. Connect the male-male jumper cable from the same row as the ** cathode ** to the minus lane.
  4. Turn off "3. Vehicle traffic light" from the "traffic light-like frame" and attach it to the LED.

Wire like this. IMG_3811.png

program

$ 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()

undefined

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.

More application!

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.

A frame that looks like a traffic light

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.

Recommended Posts