control stuff with your raspberry pi gpio

12
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/ Food Living Outside Play Technology Workshop Control Stuff with your Raspberry Pi (GPIO) by drcurzon on June 4, 2012 Table of Contents Control Stuff with your Raspberry Pi (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Intro: Control Stuff with your Raspberry Pi (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Step 1: GPIO Pinouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Step 2: Root account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Step 3: Terminal Control (Outputs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Step 4: Terminal Control (Inputs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Step 5: Shell Script (Outputs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Step 6: Web page to control the GPIO? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Upload: strongr

Post on 19-Apr-2017

249 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Food      Living       Outside        Play        Technology       Workshop

Control Stuff with your Raspberry Pi (GPIO)by drcurzon on June 4, 2012

Table of Contents

Control Stuff with your Raspberry Pi (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro:   Control Stuff with your Raspberry Pi (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1:   GPIO Pinouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 2:   Root account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 3:   Terminal Control (Outputs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 4:   Terminal Control (Inputs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Step 5:   Shell Script (Outputs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Step 6:   Web page to control the GPIO? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Page 2: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Author:drcurzon    DCWebTechHi there, I'm a engineering apprentice with a passion for computing.

Intro:  Control Stuff with your Raspberry Pi (GPIO)Hi there,In this Instructable I will show you how to get a very basic GPIO system working with your Raspberry Pi.For this I will focus on simply controlling an LED.

NOTE: For controlling GPIO via a web browser, click here .

Image Notes1. Controlled by the GPIO pins. :)2. Raspberry Pi

Image Notes1. Turn GPIO on or off

Step 1: GPIO PinoutsHere is a diagram of the GPIO pins on the Raspberry Pi.

Please note that the DNC connections are actually connected on the board - so don't put voltage on them :D

For example;To turn pin 7 of the header connector on, you set GPIO 4 high.

Note: You can find another pinout image, here .

Page 3: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Image Notes1. Header pin number

Step 2: Root accountIn this Instructable I am only covering how to control the GPIO pins via a terminal window. To see how to control them via a web browser, click here .

Firstly you must login to your Raspberry Pi as root. This step will cover how to enable the root account, skip to the next step if you can already do this.

To enable the root account, login to your raspberry pi, then type "sudo -i " and hit Return / Enter. (you'll need to type your password in).Then type "passwd root " and hit Return / Enter.You will be asked to type a password, then confirm it.It will say that the password was successfully changed.Now you need to completely log out of your Raspberry Pi (Restarting it works as well :D), then login with:Username: rootPassword: Whatever you just set

Image Notes1. sudo -i

Image Notes1. passwd root

Page 4: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Image Notes1. root password set

Image Notes1. Logged in as root

Step 3: Terminal Control (Outputs)Firstly I'll show you how to control the GPIO pins via the echo command, then how to write a script to do it all for you :D

Using the pinout (step 1), decide on which pin you want to use. (I'm going to use GPIO 21, pin 13)

Then type the following commands, replacing 21 with your GPIO number (NOT pin number )

echo "21" > /sys/class/gpio/export Hit Return / Enterecho "out" > /sys/class/gpio/gpio21/direction Hit Return / Enter

Now either:To Turn GPIO 21 on; echo "1" > /sys/class/gpio/gpio21/valueorTo Turn GPIO 21 off; echo "0" > /sys/class/gpio/gpio21/value

Once you are finished, you should clear the GPIO like so;echo "21" > /sys/class/gpio/unexport

Image Notes1. start the GPIO pin

echo "21" > /sys/class/gpio/export

Image Notes1. Set the pin direction (out, or in)

echo "out" > /sys/class/gpio/gpio21/direction

Page 5: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Image Notes1. Turn GPIO 21 onecho "1" > /sys/class/gpio/gpio21/value

Image Notes1. LED turned on :D(Connected to GPIO 21, or pin 13)2. GPIO headerPin 1 is on the top left.

Image Notes1. Turn GPIO 21 offecho "0" > /sys/class/gpio/gpio21/value

Image Notes1. The LED is now off :(2. Conected to pin 13GPIO 21

Image Notes1. Clear the GPIO pinecho "21" > /sys/class/gpio/unexport

Page 6: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Step 4: Terminal Control (Inputs)There is an image in this step showing a simple circuit for a digital I/O.

Using the pinout (step 1), decide on which pin you want to use. (I'm going to use GPIO 17, pin 11)

Then type the following commands, replacing 17 with your GPIO number (NOT pin number )

echo "17" > /sys/class/gpio/export Hit Return / Enterecho "in" > /sys/class/gpio/gpio17/direction Hit Return / Enter

Now to read the value of the GPIO pin, type;cat /sys/class/gpio/gpio17/value

PLEASE NOTE:I cannot find any documentation on it, however I have tested 3.3v as safe for the GPIO inputs. (You can use Pin 1 of hte GPIO block as permanent 3.3v). 5V may be okay- however I haven't tested it, and wouldn't recommend it.

Once you are finished, you should clear the GPIO like so;echo "17" > /sys/class/gpio/unexport

Image Notes1. Register the pinecho "17" > /sys/class/gpio/export

Image Notes1. Set the pin as inputecho "in" > /sys/class/gpio/gpio17/direction

Image Notes1. Read the value of the pin (pin is off here)cat /sys/class/gpio/gpio17/value

Image Notes1. Read the value of the GPIO pin, which is on here.

Page 7: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Image Notes1. Clean up.echo "17" > /sys/class/gpio/unexport

Image Notes1. 3.3v from pin 1 on the GPIO header2. Push to make switch3. Ground connection (pin 6 of the GPIO header)4. Some value of resistor ~10KOhm should be ok.5. Connect this to the GPIO pin you want to read from.

Step 5: Shell Script (Outputs)I've attached the shell script file that I have shown in the images for this step.

Firstly, the way in which this works is that you start the script, choose to start or stop an output (on or off), then enter the GPIO number.

To use this script, copy and paste the contents into a file on your Raspberry Pi;nano gpio.shCopy the text from the uploaded file into the script you have just created.Once you have copied it completely, hold Ctrl and press "x ", then release Ctrl and press "y ".Finally, type chmod +x gpio.sh

To run the script, type "./gpio.sh " without the quote marks.

Image Notes1. Run the script./gpio.sh

Image Notes1. Turn the output on (start) or off (stop)?

Page 8: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Image Notes1. Which GPIO do you want to modify?Choose from the list above (showing GPIO Number - Pin Number)

Image Notes1. Pin 21 is now on

Image Notes1. Turn a GPIO pin off

Image Notes1. Pin 21

Image Notes1. Pin 21 is now off

File Downloads

gpio.txt (1 KB)[NOTE: When saving, if you see .tmp as the file ext, rename it to 'gpio.txt']

Page 9: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

Step 6: Web page to control the GPIO?If you want to control the GPIO pin(s) via a web connection, you can find a way to do it here .

All criticisms and comments are welcome,Hope this helped,Daniel.

Image Notes1. Turn on or off the GPIO pin

Related Instructables

LED Blinkingwith RaspberryPi by rahulkar

ControllingRaspberry Piwith Pi BuddybyTheGreenToaster

A ReallyInexpensiveRaspberry PiGPIO cable byphilip42

Raspberry PiHalloweenLights andMusic Show bynoelportugal

Control Stuffwith yourRaspberry Pi(GPIO) over theinternet bydrcurzon

Web Control ofRaspberry PiGPIO(UPDATED) bydrcurzon

Advertisements

Comments

25 comments Add Comment

 spafruit1 says:  Feb 12, 2013. 6:27 AM  REPLYWhen i run the scrip it says:

./gpio.sh: line 34: echo: write error: Operation not permitted

 drcurzon says:  Feb 12, 2013. 9:39 AM  REPLYHi there.Are you running the script as root?Many thanks, Dan.

 7equivalents says:  Nov 2, 2012. 10:27 PM  REPLYI'm having the same problem as garym1957..............

 7equivalents says:  Nov 4, 2012. 9:20 AM  REPLYI have found my problem, the problem was difficult to find but easy to fix. It had to do with things called carriage returns, google them. I copied drcurzon'sgpio.txt into notepad on my Windows machine, I then used ftp to upload the file to my Rasp Pi

Well, Linux and Windows handle carriage returns("enter or return button") differently, and that was the source of my errors

Page 10: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

 drcurzon says:  Nov 3, 2012. 4:24 AM  REPLYhave you tried the following instructable? (It's the updated version of GPIO).

http://www.instructables.com/id/Web-Control-of-Raspberry-Pi-GPIO/

Let me know how it goes.Regards, Daniel.

 fergusondavid6 says:  Oct 5, 2012. 9:46 AM  REPLYHi,I was wondering if you needed to install any packages or python libraries before doing these steps.Thanks!

 drcurzon says:  Oct 5, 2012. 12:34 PM  REPLYHi there.Nope, nothing extra to the instructable needs to be done :)If you need any help, or have any problems, just ask :DDaniel.

 fergusondavid6 says:  Oct 5, 2012. 1:52 PM  REPLYHi again,Thanks for your quick response. Could you please tell me the pin numbers that I would connect the positive leg of the LED to and the pin number Iwould connect the negative leg of the LED to.Do I connect the positive leg to the 3.3.v pin, and the negative to any GPIO pin? Do I need any resistors?Thanks!

 ajf350d says:  Sep 23, 2012. 6:17 AM  REPLYJust started doing 'something interesting' with my Pi, and been following this and the other instructables for web control.

I don't understand the wiring you have done for the LED in this Instructable though?Can you clarify it or show a diagram?it looks like you have a connection to the USB port.

I have never done electronics but this seems odd and from other googling I cannot find a similar diagram.

Thanks.

 drcurzon says:  Sep 23, 2012. 6:35 AM  REPLYHi there.On one of the steps of this instructable is a diagram showing the pinout of the raspberry pi's gpio pins.The LED is connected to a GPIO output.What are you trying to power with the GPIO?Daniel.

 ajf350d says:  Sep 24, 2012. 11:49 AM  REPLYJust the LED lights as per the suggestion in this Instructable.The picture at the top of this page is the wiring I meant.I couldn't understand how it was wired in the Pi itself.

I've also attached the image for clarity and highlighted the area that confused me.

 cadams21 says:  Sep 11, 2012. 1:58 AM  REPLYThis may sound like a dumb question i am a idiot when it comes to external technology but how do you use a gpio and can i use it to control if a cord that'sconnected or to a PC also can it check what information is passing threw the cord if so how.

Page 11: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

 garym1957 says:  Aug 27, 2012. 8:30 PM  REPLYI'm having trouble getting your .sh file to execute. Are'nt shell scripts supposed to have #!bin/sh or something at the top of the file?Also, the variable "response" throws an error near the beginning of the script...

": not a valid identifier `response

...and some other errors before an ugly death. Unexpected end of file.

Is ther a corrected version of this script around? I'm not a shell expert at all.

 drcurzon says:  Aug 28, 2012. 10:49 AM  REPLYHi there,I've been running the script for a while and haven't had a problem.That said, have you tried the following instructable? (It's the updated version of GPIO).

http://www.instructables.com/id/Web-Control-of-Raspberry-Pi-GPIO/

Let me know how it goes.Regards, Daniel.

 drresearch says:  Aug 16, 2012. 9:56 AM  REPLYAccording to this page http://elinux.org/RPi_Low-level_peripherals , the GPIO pins are NOT 5v tolerant and are NOT over-voltage protected,

 drcurzon says:  Aug 16, 2012. 1:31 PM  REPLYThat's correct - they're 3.3v data connections.You'd need a driver board to do anything big with them.

That said - I haven't had any issues with 5v on them.Daniel.

 miicchhii says:  Jul 21, 2012. 1:57 AM  REPLYSee my Setup here: http://langeder.no-ip.org/cms/index.php?p=raspberry-pi-server

 BBerastegui says:  Jun 29, 2012. 4:00 PM  REPLYFirst, THANK YOU for all these tutorials ;)

Second, I'm having a ridiculous problem...

Why is the led turning ON when i do "echo "0" > /sys/class/gpio/gpio21/value" and turning OFF when I do "echo "1" > /sys/class/gpio/gpio21/value" ?

I don't know why it can be, and im getting a little bit mad, now I'm programming a little bit.

Thanks again.

 drcurzon says:  Jun 30, 2012. 2:30 AM  REPLYHi there,I don't know if you've just accidentally typed it incorrectly here, or whether you've used in with the pi as well, but the commands should be;

To Turn off:echo "0" > /sys/class/gpio/gpio21/value

To Turn on:echo "1" > /sys/class/gpio/gpio21/value

It's my fault in the way I wrote it in the instructable.You only need to encapsulate the 0, or 1 in speech marks, not the entire command.

Hope this helps, and have fun with it :DDaniel.

 BBerastegui says:  Jul 3, 2012. 3:56 PM  REPLYI know the command syntax :P

I just mean that its working but just in reverse. With number 1, it turns off, and with number 0 turns on.

Thanks !

 dlemke1 says:  Jul 10, 2012. 7:07 PM  REPLYMaybe you wired the LED to 3V3 instead of ground?

Page 12: Control Stuff With Your Raspberry Pi GPIO

http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/

 Dream Dragon says:  Jun 8, 2012. 1:22 PM  REPLYI remember reading specs of an early expectations of what they wanted the GPIO to do and I'm fairly sure they were limiting EVERYTHING to 3V3, though itnow looks like SOME of the pins can handle 5V.

You may find this page useful.

http://www.raspberrypi.org/archives/384

 abishur says:  Jun 15, 2012. 8:37 AM  REPLYThis is one of the mods on the Raspberry Pi Forum (Abishur to be exact ;-) ) There is only 1 pin that can take 5V and that's the one labeled 5V! To beprecise it's connected to the 5V rail so it can either source or sink 5 volts. If you try and connect 5V to any of the regular GPIO pins, you are going to fryyour pi!

 drcurzon says:  Jun 15, 2012. 8:55 AM  REPLYNice :D Thanks for that - saved me trying it and damaging my Pi.Thanks, Daniel.

 drcurzon says:  Jun 8, 2012. 2:59 PM  REPLYHey, thanks for the comment.I think you're right about the 5V connections to SOME of the pins, they also said that they wanted to keep the input voltages as low as possible to savepower.I think I'll stick with 3.3v to be safe though. (Until I accidentally put 5v on it :D)Thanks, Daniel.