Raspberry Pi Traffic Light Circuit with GPIO Part 1

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:

 

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

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.

Leave a Comment

Your email address will not be published.

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