• 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 1

Raspberry Pi Traffic Light Circuit with GPIO Part 1

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

This tutorial is about a simple Traffic Light Circuit using the programmable GPIO Pins via Python. In the next tutorial, we will extend the circuit by a few functions (including an Input).

Accessories

We need the following:

  • Breadboard
  • Jumper Cable
  • LED’s red-green-yellow Ø 5mm
  • Transistors (BC547)
  • Resistors (470 Ω and 10k Ω)
  • regular Wire (for example for the Garden)

 

Preparation

First, we need to update the packages and paths.

sudo apt-get update
sudo apt-get upgrade

So that the library works properly afterwards, we need python-dev.

sudo apt-get install python-dev

Afterwards, we load the Python GPIO library (the newest version is available here, maybe adapt version: https://pypi.python.org/pypi/RPi.GPIO)

sudo wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.6.1.tar.gz

Unpack now

tar zxvf RPi.GPIO-*

and switch to the Directory

cd RPi.GPIO-*

Now the Library can be installed.

sudo python setup.py install

Should it come to Mistakes, they are written in the Comments. Luckily, most Mistakes are pretty well described.

Setup

The setup of the first Circuit looks like this:

ampel1_Steckplatine

As a tip: each LED has a long and a shorter end. The longer belongs to the Positive (3.3V), the shorter to the Negative (GND). As a series resistor, the LED gets 470 Ω.

A Transistor works in such a way that as soon as the middle Pin has Power, the Line is released from left to right. However, to make sure that the Power is not too high, we will switch a 10k Ω Resistor.

Before connecting the Cables to the Pi, check the Circuit again.

 

Programming

After everything is set up and connected, we need a Script that makes the LED blink. We create a Script

sudo nano ampel_skript1.py

with the following content

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#Bibliotheken einbinden
import RPi.GPIO as GPIO
import time
 
#GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BOARD)
 
#Richtung der GPIO-Pins festlegen (IN / OUT)
GPIO.setup(26, GPIO.OUT)
 
#unendliche Schleife
while True:
    #Pin 26 HIGH Pegel
    GPIO.output(26, True)
    #eine halbe Sekunde warten
    time.sleep(0.5)
    #Pin 26 LOW Pegel
    GPIO.output(26, False)
    #eine halbe Sekunde warten
    time.sleep(0.5)

Attention: Here the number of the Pin is important and not the GPIO Number. The Pin Number is 26, where it is GPIO Pin 07. If the GPIO Number is to be specified, line 6 must read:

GPIO.setmode(GPIO.BCM)

We save and exit the Editor (CTRL + O, CTRL + X) and execute the Script.

sudo python ampel_skript1.py

To abort the Script, press CTRL + C.

In Part 2 we build the Phases of a Traffic Light.

python traffic light circuit
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous Article7 Segment Display (Anode) with MAX7219 / MAX7221 and Raspberry Pi
Next Article Raspberry Pi Traffic Light Circuit with GPIO Part 2

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 GSM Module – Mobile Internet (LTE, 3G, UMTS)

Let Raspberry Pi’s communicate with each other per 433MHz wireless signals

Setting up Raspberry Pi 4 Wi-Fi and Bluetooth

MCP3008: Read out Analog Signals on the Raspberry Pi

8×8 LED Dot Matrix – Assembly and Installation (Raspberry Pi)

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

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.