You are on page 1of 6

handson.dmt.fh-joanneum.

at

RASPBERRY PI
Workpackage: Description: Difficulty (1-10): Overview: Digitale Inputs & Outputs This workpackage is about how to access the GPIO pins as simple in- and output to get values from a button and turn a LED on/off. 3 Instructions Pinout python Code node.js Code Trouble Shooting Circuit Schematic Useful Resources Hardware: Raspberry Pi Pi Cobbler (optional but very helpful) Bread board Hook up Wires LED Resistors (10 k, 47 ) Button Software: Raspbian OS GPIO Library node.js (described in Workpackage Programming on the Raspberry Pi)

Requirements:

Carelse, Pauger, Pilz

Instructions

handson.dmt.fh-joanneum.at

DIGITAL INPUTS AND OUTPUTS


One of the great features of Raspberry Pi is its ability to communicate with external inputs through the built in General Purpose Input/Output connector. This connector has integrated power pins (5v, 3.3v), ground, and inputs/outputs. In this tutorial we will guide you through the basics of connecting up a LED to your Raspberry Pi and being able to turn the LED on and off using an external switch.

Instructions
Pinout
The pinout varies between the different models of the Raspberry Pi. In Picture 1 you can see the pinout of Model B Revision 1. If you have a Model B Revision 2 there are no DNC (do not connect) pins left. All DNC Pins except Pin 4 can be used for GND (ground). Pin 4 provides 5 V. The Raspberry Pi works internally with 3.3 V. The 3.3 V supply voltage at Pin 1 can provide a maximum of 50 mA. The 5 V supply voltage comes directly from the connected USB-power-supply. So if you use the 5 V, you have to consider that there must be enough supplied amperage for the board and your additional use of pin 2. Because the Raspberry Pi works with 3.3 V internally we are going to use always port 1 for power supply. P ICTURE 1: P INOUT You can see in picture 1 that every pin has two different labels (green and black). Depending on the library you use, or (in python) what you define, you have to provide sometimes the green pin number and sometimes the black one.

Installation
add parts to breadboard as you can see in picture 2 o calculate resistors as described in workpackage Basic Electronics o be careful to choose the right IOs for GND, port and power pins (pinout in picture 1 is for Raspberry Pi Model B, Revision 1) connect breadboard via the pi cobbler with Raspberry Pi start Raspberry Pi by connecting to the power supply decide for programming language (python/node.js)

Working Directory
It makes sense to work in your home directory on the Raspberry Pi. It is the default directory after starting your Raspberry Pi. Maybe you can create a subfolder node with the command mkdir node. In this directory you should not have problems when you run your node program with command sudo node your_programname.js or sudo python your_programname.py. To create a new file where you can start programming enter sudo nano your_programname.js or sudo nano your_programname.py.
Carelse, Pauger, Pilz 2

Instructions

handson.dmt.fh-joanneum.at

python code
If you did not install any librarys by now, do this like described in Trouble Shooting below.

Hints
to access GPIO-Ports of Raspberry Pi you need to import a library:
import RPi.GPIO as GPIO;

to define how you want to access the pins, decide for one of these GPIO.setmode(GPIO.BOARD); -> black numbers GPIO.setmode(GPIO.BCM); -> green numbers (I take this one) to set pins to input or output (look up numbers in schematic above)
GPIO.setup(25, GPIO.OUT); GPIO.setup(17, GPIO.IN);

to get signal of input (true/false)


GPIO.input(17):

to set signal of output (true/false)


GPIO.output(25, True);

Full Code
#import library to get acces to gpios import RPi.GPIO as GPIO; #set pin mode #GPIO.setmode(GPIO.BOARD); -> another possibility GPIO.setmode(GPIO.BCM); #set pin 22 as output GPIO.setup(25, GPIO.OUT); #set pin 11 as input GPIO.setup(17, GPIO.IN); while true: #if the button is pressed, turn on LED if GPIO.input(17) == False: GPIO.output(25, True); #otherwise turn off LED else: GPIO.output(25, False);

node.js Code
If you did not install any libraries by now, do this like described in work sheet Setup Raspberry Pi with node.js There are two different ways of getting inputs and reading outputs: synchronous and asynchronous. Synchronous waits for each operation to complete, after executing the next operation. Asynchronous never waits for each operation to complete and starts as soon as possible. In our case you do not need a call-back function for reading/writing values if you make it synchronous. Find more about the two ways at https://npmjs.org/package/onoff.

Hints

to access GPIO-Ports of Raspberry Pi you need to import a library:


var gpio = require('onoff').Gpio

to set pins to input or output (look up green numbers in picture 1)


o o var led = new gpio(25, 'out'); var button = new gpio(17, 'in', 'both', {persistentWatch: true});

both: rising and falling interrupt edges should be configured (press and release) persistentWatch: react on all events not only the first one to get signal of input (true/false)
button.watch(function(err, value){ console.log(value); });

to set signal of output (0/1)


o led.write(0);
3

Carelse, Pauger, Pilz

Trouble Shooting

handson.dmt.fh-joanneum.at

Full Code
//"require" library onoff to get access to gpios var gpio = require('onoff').Gpio /*create a new button where you define the port where the button is connected to, define that it should work as an input and any event should react on 'both' changes (rising and falling)*/ var button = new gpio(17, 'in', 'both', {persistentWatch: true}); //create a new variable led which defines your Pin 25 as an output var led = new gpio(25, 'out'); var counter=0; //add an event to your button which reacts on status change (pressing/releasing button) button.watch(function(err, value){ if (value===1){ //turn LED of if button is released led.write(0); } else { //turn LED on if button is pressed led.write(1); counter++; console.log("pressed"+counter.toString()); }; });

Test your Code


If you have finished writing your code you can save your code in nano with Strg+O and then go back to command line with Strg+X. Then, dependent on your selected programming language, type in (sudo) node your_programmname.js or (sudo) python your_programmname.py. After that you should be able to control your light with the help of the button.

Trouble Shooting
If you have problems running your application, read the printed error-message carefully. Most time in that way you can find out where the problem/error is located. Due to the fact that the input on GPIO 17 is high when the button is not pressed and is low when the button is pressed the code has to be adapted accordingly. This is done to avoid the so called floating pin behaviour, which can lead to false results.

Error when importing Library


python
To install enter the following commands: Download the RPi GPIO Library Extract the files
tar zxf RPi.GPIO-0.3.1a.tar.gz wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO0.3.1a.tar.gz

Browse to the extracted folder


cd RPi.GPIO-0.3.1a

Install the library


sudo python setup.py install

Carelse, Pauger, Pilz

Circuit Schematic

handson.dmt.fh-joanneum.at

nodejs
Go to worksheet Setup Node.js on the Raspberry Pi and carefully read threw Install Libraries.

Circuit Schematic
Use 10 k resistor for Button and 47 resistor for LED.

P ICTURE 2: C IRCUIT I NSTALLATION ON B READBOARD

P ICTURE 3: C IRCUIT D IAGRAM Carelse, Pauger, Pilz 5

Useful Resources

handson.dmt.fh-joanneum.at

Useful Resources
Library RPi.GPIO (python) http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/ Library onoff (nodejs) https://npmjs.org/package/onoff Other nodejs Libraries to access gpios https://npmjs.org/package/pi-gpio https://npmjs.org/package/rpi-gpio

Carelse, Pauger, Pilz