The command to write a value to a pin in output mode is:
gpio write <pin> <value>
for example:
gpio write 0 1
for example:
gpio write 0 1
Now, as stated at the end of the last Raspberry PI post, you need to be VERY careful with your PI because your pins are not buffered. You can fry your whole board by overloading a pin! You do not want this!
I found a pin diagram at
http://www.raspberrypi-spy.co.uk/2012/09/raspberry-pi-p5-header/raspberry-pi-gpio-layout-revision-2/
which can help you to see which pins are designed for 5 volts, which are designed for 3.3 volts. We will mostly be using 5 volts as that is easiest to use with microprocessors and is the digital standard.
To test your writing capabilities (and just to have a little fun!) try setting up a simple LED circuit and writing a Python script. Hook up your LED with the anode (long lead) to pin 0 and the cathode (the short lead) to the ground pin. Turn on your PI and write the program. Here in team PorkPI, we use the Linux text editor for real men: VIM. Do the following command:
vim ledTest.py
In this file, we need to make a simple script to flash the light on and off. You can use my code as a template or if you're lazy, feel free to copy and paste for a simple test. The code is as follows:
#strobe_lights
import subprocess
import time
subprocess.call(["clear"])
print('waiting')
#time.sleep(5)
for x in range(0,10):
subprocess.call(["gpio", "write", "0", "0"])
time.sleep(.5)
subprocess.call(["gpio", "write", "0", "1"])
time.sleep(.5)
print(x)
subprocess.call(["gpio", "write", "0", "1"])
import subprocess
import time
subprocess.call(["clear"])
print('waiting')
#time.sleep(5)
for x in range(0,10):
subprocess.call(["gpio", "write", "0", "0"])
time.sleep(.5)
subprocess.call(["gpio", "write", "0", "1"])
time.sleep(.5)
print(x)
subprocess.call(["gpio", "write", "0", "1"])
So, a quick explanation of what's happening here:
We import subprocess which allows us to call bash commands from a python script. If there's a returned value, you can store it in a variable. However, we don't look for return values so we won't worry about it. We also import time so we can slow down the script enough so we can actually see the light flashing. (if you don't know what i mean, remove all the time.sleep(.5) lines and watch how fast it flashes. It will likely look like it's just solid and on.)
So now we are able to control a light that's attached to the GPIO pins! From here, to go on a larger scale, we would need to use a relay instead of an LED and we could control much larger things. But for now, this will be enough for demonstration and learning purposes.
No comments:
Post a Comment