The Getting Started book gives a brief set of commands to test whether all the modules are installed properly:
- sudo python: This runs Python in the command line.
- import RPi.GPIO as GPIO: This tells Python to import the module that runs the pins. An error means something is wrong.
Since everything was working so far, I was able to proceed directly to trying out the commands to turn the LED on.
- GPIO.setmode(GPIO.BCM): There are different ways to refer to the pins using Python. There's a numbering based on the physical layout (GPIO.BOARD) and then there's the numbering based on the labels that have been designated and the same ones that were used earlier (GPIO.BOARD).
- GPIO.setup(25,GPIO.OUT): Set pin 25 as an out.
- GPIO.output(25,GPIO.HIGH): LED on. (It worked!)
- GPIO.output(25,GPIO.LOW): LED off. (It also worked!)
So everything was ready to go, and I could start into programming. The Getting Started book uses VNC to get access to the graphical interface to create the files, but I wanted to use terminal method just because I feel that I should be able to make text documents through the terminal. I decided to use the Nano editor.
The code was pretty straightforward. It used the time module in Python to allow a sleep function to be called (which just pauses the program for a set period of time -- time.sleep(# of seconds)), and then flipped back and forth between the two settings using the commands above.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.OUT)
while True:
GPIO.output(25, GPIO.HIGH)
time.sleep(1)
GPIO.output(25, GPIO.LOW)
time.sleep(1)
This is all simple enough. I did get an error, but it looks like it's a pretty innocuous one:
blink.py:5: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(25,GPIO.OUT)I think this just means that I had already run GPIO.setup(25,GPIO.OUT) before. It probably means that I'll need to learn how to un-set the pins at some point.
I decided I would plug in another LED and make a binary counter that could count up to 3. One thing I discovered is that the amount of space on the breadboard is not enough to do anything too complex, so I'm probably going to need to make a run to Fry's and pick up a bigger breadboard. But this was big enough for a two bit counter. Here is the code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
while True:
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
time.sleep(1)
GPIO.output(24, GPIO.HIGH)
time.sleep(1)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.HIGH)
time.sleep(1)
GPIO.output(24, GPIO.HIGH)
time.sleep(1)
This felt like a good stopping place. Next time I'm going to try to read a button as an input using Python.
No comments:
Post a Comment