home |  electronics |  toolbox |  science club |  tuxtalk |  photos |  e-cards |  online-shop



Raspberry PI, beginner GPIO tutorial, a traffic light

The Raspberry PI is in many ways like a normal Linux computer but what makes it different from your laptop running Ubuntu is the GPIO connector. You can use those to build fun electronics projects. We are not going to use python or any special programing language. We will just write a small shell script. That's in my opinion easier than python not only if you know the Linux bash shell already but it is as well more straight forward if this is your first interaction with Linux.

I will only briefly show how to connect the raspberry pi. The only really important part is to get a good power supply (5V, 3A) and a decent micro usb power cable. It's important to have good and stable power. The USB power cable should be short. As for the OS installation: the easiest is to buy a SD card that has a NOOPs already on it and you just plug it in to install the full OS.

rasberrypi connections

rasberrypi connections


The connector in the upper left corner is the GPIO connector (general purpose input output connector). The pins for the raspberry pi hardware version 2 and version 3 are numbered as shown below. You can use any raspberry pi to follow this tutorial just make sure you have the right pinout for your hardware version.

rasberrypi gpio pinout, version 2 and 3
rasberrypi gpio pinout, finding pin 1

raspberry pi hardware version 2 and 3, GPIO pinout


We will build a little LED traffic light. You need a 3x 220 Ohm resistor plus one green, one yellow and one red LED. The minus pin of all LEDs connects to GND (GPIO pin 39). Each of resistors goes onto the plus pin of one of the LEDs and then the other side of that resistor connects to the raspberry pi GPIO port as follows. Each 220 Ohm resistor is in series with one LED.


The best is to get some cables that can be connected on one side to the GPIO port of raspberry pi and have on the other side a little pin that can be pushed into a bread board.

cables to connect a raspberry pi to a bread board

Cables to connect a raspberry pi to a bread board


The hardware part is ready. Now let's start the software part. Power up your PI and login (user pi, password raspberry, unless you changed that previously). Open a terminal window and see if a program called gpio is installed already (type gpio, it should print some usage info if it is installed). If it is not yet installed then open a web browser, go to http://wiringpi.com/download-and-install/ and follow what is called "plan B" on that page. That's the easiest method.

To use a GPIO pin you have to say first which direction that pin should have (input or output). We want outputs. So to make e.g the pin for the green LED an output you type:
gpio -g mode 17 out

To turn the LED on you type:
gpio -g write 27 1

... and to turn it off:
gpio -g write 27 0

Easy. That's basically all. Now we put this into a shell script with a loop inside and our traffic light will start to work.
#!/bin/sh
# script trafficlight.sh
#
# Instructions:
# open a text editor, copy/paste this
# and save it as trafficlight.sh, go to the shell and type
# chmod 755 trafficlight.sh 
# to make it an executable program. After that run
# ./trafficlight.sh
# enjoy

gpio -g mode 17 out #green
gpio -g mode 27 out #yellow
gpio -g mode 22 out #red

while true; do
gpio -g write 22 0
gpio -g write 17 1
sleep 2
gpio -g write 17 0
gpio -g write 27 1
sleep 1
gpio -g write 27 0
gpio -g write 22 1
sleep 3
done

You have now the basics working and you can easily modify this and make it more complicated.

This gpio program can as well be used to print your hardware version and details about the GPIO port:
pi@raspberrypi:~ $ gpio -v
gpio version: 2.32
Copyright (c) 2012-2015 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 3, Revision: 02, Memory: 1024MB, Maker: Embest 
  * Device tree is enabled.
  * This Raspberry Pi supports user-level GPIO access.
    -> See the man-page for more details
    -> ie. export WIRINGPI_GPIOMEM=1


pi@raspberrypi:~ $ gpio readall
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+



Back to: "No preservatives added"



© 2004-2024 Guido Socher