Evolution of a Blog

This blog has evolved as I have as a maker. It starts at the beginning of my journey where I began to re-tread my tires in the useful lore of micro electronics and the open-source software that can drive them. While building solutions around micro-electronics are still an occasional topic my more recent focus has been on the 3D Printing side of making.

Wednesday, November 7, 2012

Raspberry Pi and Basic GPIO

Might be a bit silly but I decided that my 'bot needs some way to signal a status...for example...that the network has dropped but the RPi is still there.   I got myself an RGB LED and am thinking that a green strobe actuated at the end of each process loop will mean that all is well.   If the network drops the color will change to orange or yellow.  If the whole lot of code running the 'bot fails a watchdog daemon will flash an "SOS" across the LED.
It turns out that working with GPIO on the RPi is simple.  As you can see in the photo to the right I don't have the ribbon cable adapter for the RPi so I used five jumper cables.   Three of them are for the pins that I will use for driving the LED colors (red, green, and orange = blue since I didn't have a blue cable).   The ground is the black jumper.   The extra red jumper is used to measure the power on the RPi 5v bus (more here).

For now I only have this setup for testing.   I am going to put the LED inside a ping pong ball and stick it on a mast on the stern of the 'bot across from the compass, ideally with a reset switch for the Arduino as things are getting crowded.



The code to drive this RGB LED as a Morse Code signaler is here.  I don't anticipate sending any other message other than SOS but I got carried away.   This sample code also illustrates the use of option parsing in Python but I will talk to that later.  Some of the key snippets from this code are here:

    # Initialize the GPIO module and our three pins     
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(GREEN, GPIO.OUT)
    GPIO.setup(RED, GPIO.OUT)
    GPIO.setup(BLUE, GPIO.OUT)
 

    # Do the actual blinking 
    def blink(r, g, b, duration):
   
    global RED, GREEN, BLUE
   
    GPIO.output(RED, r)
   
    GPIO.output(GREEN, g)
   
    GPIO.output(BLUE, b)
   
    time.sleep(duration)
   
    GPIO.output(RED, 0)
   
    GPIO.output(GREEN, 0)
   
    GPIO.output(BLUE, 0)
   
    time.sleep(INTERVAL)
 

    # Reset the pins we used
    GPIO.cleanup()


Obviously this is the most simple example possible but it shows the initialization, writing to a pin, and the all important cleanup.  

BTW, you need to run this under sudo to access the GPIO.  There is a discussion of alternatives here.

Almost all the code related to the Morse Code functionality came from a post created by Stephen Chappell on Thu, 12 Jan 2012 (MIT)

No comments:

Post a Comment