Вы находитесь на странице: 1из 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
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Author:drcurzon DCWebTech
Hi 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 Notes
1. Controlled by the GPIO pins. :)
2. Raspberry Pi
Image Notes
1. Turn GPIO on or off
Step 1:GPIO Pinouts
Here 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 .
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Image Notes
1. Header pin number
Step 2:Root account
In 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: root
Password: Whatever you just set
Image Notes
1. sudo -i
Image Notes
1. passwd root
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Image Notes
1. root password set
Image Notes
1. 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 / Enter
echo "out" > /sys/class/gpio/gpio21/direction Hit Return / Enter
Now either:
To Turn GPIO 21 on; echo "1" > /sys/class/gpio/gpio21/value
or
To 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 Notes
1. start the GPIO pin
echo "21" > /sys/class/gpio/export
Image Notes
1. Set the pin direction (out, or in)
echo "out" > /sys/class/gpio/gpio21/direction
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Image Notes
1. Turn GPIO 21 on
echo "1" > /sys/class/gpio/gpio21/value
Image Notes
1. LED turned on :D
(Connected to GPIO 21, or pin 13)
2. GPIO header
Pin 1 is on the top left.
Image Notes
1. Turn GPIO 21 off
echo "0" > /sys/class/gpio/gpio21/value
Image Notes
1. The LED is now off :(
2. Conected to pin 13
GPIO 21
Image Notes
1. Clear the GPIO pin
echo "21" > /sys/class/gpio/unexport
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 / Enter
echo "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 Notes
1. Register the pin
echo "17" > /sys/class/gpio/export
Image Notes
1. Set the pin as input
echo "in" > /sys/class/gpio/gpio17/direction
Image Notes
1. Read the value of the pin (pin is off here)
cat /sys/class/gpio/gpio17/value
Image Notes
1. Read the value of the GPIO pin, which is on here.
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Image Notes
1. Clean up.
echo "17" > /sys/class/gpio/unexport
Image Notes
1. 3.3v from pin 1 on the GPIO header
2. Push to make switch
3. 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.sh
Copy 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 Notes
1. Run the script
./gpio.sh
Image Notes
1. Turn the output on (start) or off (stop)?
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Image Notes
1. Which GPIO do you want to modify?
Choose from the list above (showing GPIO Number - Pin Number)
Image Notes
1. Pin 21 is now on
Image Notes
1. Turn a GPIO pin off
Image Notes
1. Pin 21
Image Notes
1. 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']
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 Notes
1. Turn on or off the GPIO pin
Related Instructables
LED Blinking
with Raspberry
Pi by rahulkar
Controlling
Raspberry Pi
with Pi Buddy
by
TheGreenToaster
A Really
Inexpensive
Raspberry Pi
GPIO cable by
philip42
Raspberry Pi
Halloween
Lights and
Music Show by
noelportugal
Control Stuff
with your
Raspberry Pi
(GPIO) over the
internet by
drcurzon
Web Control of
Raspberry Pi
GPIO
(UPDATED) by
drcurzon
Advertisements
Comments
25 comments Add Comment
spafruit1 says: Feb 12, 2013. 6:27 AM REPLY
When i run the scrip it says:
./gpio.sh: line 34: echo: write error: Operation not permitted
drcurzon says: Feb 12, 2013. 9:39 AM REPLY
Hi there.
Are you running the script as root?
Many thanks, Dan.
7equivalents says: Nov 2, 2012. 10:27 PM REPLY
I'm having the same problem as garym1957..............
7equivalents says: Nov 4, 2012. 9:20 AM REPLY
I 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's
gpio.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
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
drcurzon says: Nov 3, 2012. 4:24 AM REPLY
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.
fergusondavid6 says: Oct 5, 2012. 9:46 AM REPLY
Hi,
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 REPLY
Hi there.
Nope, nothing extra to the instructable needs to be done :)
If you need any help, or have any problems, just ask :D
Daniel.
fergusondavid6 says: Oct 5, 2012. 1:52 PM REPLY
Hi 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 I
would 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 REPLY
Just 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 REPLY
Hi 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 REPLY
Just 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 REPLY
This 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's
connected or to a PC also can it check what information is passing threw the cord if so how.
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
garym1957 says: Aug 27, 2012. 8:30 PM REPLY
I'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 REPLY
Hi 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 REPLY
According 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 REPLY
That'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 REPLY
See my Setup here: http://langeder.no-ip.org/cms/index.php?p=raspberry-pi-server
BBerastegui says: Jun 29, 2012. 4:00 PM REPLY
First, 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 REPLY
Hi 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 :D
Daniel.
BBerastegui says: Jul 3, 2012. 3:56 PM REPLY
I 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 REPLY
Maybe you wired the LED to 3V3 instead of ground?
http://www.instructables.com/id/Control-Stuff-with-your-Raspberry-Pi-GPIO/
Dream Dragon says: Jun 8, 2012. 1:22 PM REPLY
I 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 it
now 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 REPLY
This 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 be
precise 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 fry
your pi!
drcurzon says: Jun 15, 2012. 8:55 AM REPLY
Nice :D Thanks for that - saved me trying it and damaging my Pi.
Thanks, Daniel.
drcurzon says: Jun 8, 2012. 2:59 PM REPLY
Hey, 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 save
power.
I think I'll stick with 3.3v to be safe though. (Until I accidentally put 5v on it :D)
Thanks, Daniel.

Вам также может понравиться