• First Steps
  • General
  • Projects
Facebook Twitter YouTube
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
  • Hardware & GPIO
  • Sensors & Components
  • Scale
  • Cases
  • Fingerprint
  • Gas Sensor
  • Heartbeat
  • German
  • French
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
Home»Hardware & GPIO»Raspberry Pi Traffic Light Circuit with GPIO Part 2

Raspberry Pi Traffic Light Circuit with GPIO Part 2

Facebook Twitter LinkedIn Tumblr Email Reddit
Raspberry Pi Kids Projekt
Phasen einer Ampelschaltung
Share
Facebook Twitter LinkedIn Email Tumblr Reddit Telegram WhatsApp

It continues with the second part and the actual Traffic Light Circuit. Let’s take a look at the various Traffic Light Signals on Wikipedia. We want to recreate them at the push of a Button.

(Source: Wikipedia)

First of all, we build the Circuit:

 ampel2_Steckplatine

So we have the following Phases:

  1. no Button pressed – permanently red (yellow and green off)
  2. Button is pressed – red and yellow light up (green off), duration: 3 seconds
  3. Change to green (red and yellow off), duration: 15 seconds
  4. Change to yellow (red and green off), duration: 3 seconds
  5. Back to Step 1.

This now has to be converted into Code. For example, I created a Script for this:

sudo nano ampel_skript2.py

This time, instead of the Pin Numbers, I use the GPIO Numbers.

Python
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Bibliotheken einbinden
import RPi.GPIO as GPIO
import time
 
#GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#Warnungen ausschalten
GPIO.setwarnings(False)
 
#GPIO Pin Belegung
ROT = 02
GELB = 14
GRUEN = 15
TASTER = 07
 
#Richtung der GPIO-Pins festlegen (IN / OUT)
GPIO.setup(ROT, GPIO.OUT) #rot
GPIO.setup(GELB, GPIO.OUT) #gelb
GPIO.setup(GRUEN, GPIO.OUT) #gruen
GPIO.setup(TASTER, GPIO.IN) #Taster
 
#Umschaltung definieren
def umschalten():
    #Phase 2
    GPIO.output(ROT, True)
    GPIO.output(GELB, True)
    GPIO.output(GRUEN, False)
    time.sleep(2)
    #Phase 3
    GPIO.output(GRUEN, True)
    GPIO.output(ROT, False)
    GPIO.output(GELB, False)
    time.sleep(15)
    #Phase 4
    GPIO.output(GELB, True)
    GPIO.output(GRUEN, False)
    time.sleep(3)
    #zurueck zu Phase 1
    GPIO.output(ROT, True)
    GPIO.output(GELB, False)
 
#Endlosschleife
while True:
    #Phase 1
    GPIO.output(ROT, True)
    GPIO.output(GELB, False)
    GPIO.output(GRUEN, False)
 
    #Status des Tasters einlesen
    tasterStatus = GPIO.input(TASTER)
    if (tasterStatus):
        umschalten()

It is called again with

sudo python ampel_skript2.py

The red LED lights up and pressing the Button calls up the Function and changes the Traffic Lights. Execution of the Script terminated with CTRL + C.

The Functions can be changed and extended. Here is a Picture of my Circuit.

20140325_210311

python traffic light circuit
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleRaspberry Pi Traffic Light Circuit with GPIO Part 1
Next Article Raspberry Pi Security Camera with Webcam

Related Posts

Control all GPIOs with the Raspberry Pi REST API via Python

How to build a ESP8266 Scale (with Weight Sensor HX711)

Using TensorFlow Lite with Google Coral TPU on Raspberry Pi 4

How-To: Bluetooth Connection between ESP32’s and Raspberry Pi’s

Leave A Reply Cancel Reply

Raspberry Pi Traffic Light Circuit with GPIO Part 1

Connect and control Raspberry Pi motion detector PIR

Installing OpenCV on the Raspberry Pi

Raspberry Pi GPIO Explanation for Beginners + Programming (Part 2)

Top 10 Raspberry Pi 3 Cases (with fan, screen, …)

Remote Control of Raspberry Pi Robots with an Xbox 360 Controller (Part 6)

Subscribe to Blog
Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!
Subscribe

Thank you!

We will contact you soon.

Tutorials for Raspberry Pi
Facebook Twitter YouTube
  • Contact & Disclaimer

Type above and press Enter to search. Press Esc to cancel.