RaspberryPi: Digital IOs

Embed Size (px)

Citation preview

  • 8/13/2019 RaspberryPi: Digital IOs

    1/6

  • 8/13/2019 RaspberryPi: Digital IOs

    2/6

    Instructions handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 2

    DIGITAL INPUTS AND OUTPUTS

    One of the great features of Raspberry Pi is its ability to communicate with external inputsthrough 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 RaspberryPi and being able to turn the LED on and off using an external switch.

    Instructions

    PinoutThe pinout varies between the differentmodels of the Raspberry Pi. In Picture 1you can see the pinout of Model BRevision 1. If you have a Model B

    Revision 2 there are no DNC (do notconnect) pins left. All DNC Pins exceptPin 4 can be used for GND (ground). Pin4 provides 5 V.

    The Raspberry Pi works internally with3.3 V. The 3.3 V supply voltage at Pin 1can provide a maximum of 50 mA. The5 V supply voltage comes directly fromthe connected USB-power-supply. So ifyou use the 5 V, you have to considerthat there must be enough suppliedamperage for the board and your

    additional use of pin 2.

    Because the Raspberry Pi works with3.3 V internally we are going to usealways port 1 for power supply.

    You can see in picture 1 that every pinhas 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 theblack one.

    Installation add parts to breadboard as you can see in picture 2

    o calculate resistors as described in workpackage Basic Electronicso be careful to choose the right IOs for GND, port and power pins (pinout in picture1 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 DirectoryIt makes sense to work in your home directory on the Raspberry Pi. It is the default directoryafter 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.jsor sudo python

    your_programname.py.To create a new file where you can start programming enter sudo nano

    your_programname.jsor sudo nano your_programname.py.

    PICTURE 1: PINOUT

  • 8/13/2019 RaspberryPi: Digital IOs

    3/6

    Instructions handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 3

    python codeIf 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:importRPi.GPIO asGPIO;

    to define how you want to access the pins, decide for one of theseGPIO.setmode(GPIO.BOARD);-> black numbersGPIO.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 gpiosimportRPi.GPIO asGPIO;#set pin mode#GPIO.setmode(GPIO.BOARD); -> another possibilityGPIO.setmode(GPIO.BCM);#set pin 22 as outputGPIO.setup(25,GPIO.OUT);#set pin 11 as inputGPIO.setup(17,GPIO.IN);whiletrue:

    #if the button is pressed, turn on LEDifGPIO.input(17)==False:

    GPIO.output(25,True);#otherwise turn off LEDelse:

    GPIO.output(25,False);

    node.js CodeIf you did not install any libraries by now, do this like described in work sheet SetupRaspberry Pi with node.jsThere are two different ways of getting inputs and reading outputs: synchronous andasynchronous.

    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. Inour case you do not need a call-back function for reading/writing values if you make itsynchronous. 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:vargpio =require('onoff').Gpio

    to set pins to input or output (look up green numbers in picture 1)o varled =newgpio(25,'out');o varbutton =newgpio(17,'in','both',{persistentWatch:true});

    both: rising and falling interrupt edges should be configured (press andrelease)

    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);

  • 8/13/2019 RaspberryPi: Digital IOs

    4/6

    Trouble Shooting handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 4

    Full Code//"require" library onoff to get access to gpiosvargpio =require('onoff').Gpio/*create a new button where you define the port where the button isconnected to,define that it should work as an input and any event should react on 'both'

    changes (rising and falling)*/varbutton =newgpio(17,'in','both',{persistentWatch:true});//create a new variable led which defines your Pin 25 as an outputvarled =newgpio(25,'out');

    varcounter=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 CodeIf you have finished writing your code you can save your code in nano with Strg+O and thengo back to command line with Strg+X.

    Then, dependent on your selected programming language, type in

    (sudo) node your_programmname.jsor(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 lowwhen the button is pressed the code has to be adapted accordingly. This is done to avoid theso called floating pin behaviour, which can lead to false results.

    Error when importing LibrarypythonTo install enter the following commands:

    Download the RPi GPIO Librarywget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz

    Extract the filestar zxf RPi.GPIO-0.3.1a.tar.gz

    Browse to the extracted foldercd RPi.GPIO-0.3.1a

    Install the librarysudo python setup.py install

  • 8/13/2019 RaspberryPi: Digital IOs

    5/6

    Circuit Schematic handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 5

    nodejsGo to worksheet Setup Node.js on the Raspberry Pi and carefully read threw InstallLibraries.

    Circuit Schematic

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

    PICTURE 2: CIRCUIT INSTALLATION ON BREADBOARD

    PICTURE 3: CIRCUIT DIAGRAM

  • 8/13/2019 RaspberryPi: Digital IOs

    6/6

    Useful Resources handson.dmt.fh-joanneum.at

    Carelse, Pauger, Pilz 6

    Useful Resources

    Library RPi.GPIO (python)http://blog.oscarliang.net/use-gpio-pins-on-raspberry-pi/

    Library onoff (nodejs)

    https://npmjs.org/package/onoffOther nodejs Libraries to access gpios

    https://npmjs.org/package/pi-gpiohttps://npmjs.org/package/rpi-gpio