Flashing the onboard LED

Ever since going to the TechnoTeach in the Village where someone mentioned you could flash the onboard Status LED from the commandline I wanted to have a go.  Instant hardware hacking without waiting for ‘bits’ to arrive.

There’s some guidelines here (ignore the first answer which says ‘not possible’ Those Who Can appear later in the thread.  The gist is that it, by default shows the activity on the SD card, but you can override that and control it from GPIO pin 16.

echo none >/sys/class/leds/led0/trigger to turn off the default behaviour
echo mmc0 /sys/class/leds/led0/trigger to turn it back on (although I haven't tested that yet!)

Once that’s done you can control it from the commandline using the following commands

echo 1 >/sys/class/leds/led0/brightness will turn the LED on
echo 0 /sys/class/leds/led0/brightness will turn it off.

Or to flash it the following module and command will work:

modprobe ledtrig_heartbeat
echo heartbeat >/sys/class/leds/led0/trigger

Having got that working to my immense amusement it was time to look at incorporating this into a program.

Python was what most of the examples were written in – something I’d never used.  Happily the Raspberry Pi Users Guide was at hand (in Kindle form floating round the classroom on a couple of tablets) and that plus the sample code here was enough to get started.

Initial hurdle was to meddle withe GPIO you need to be running as root and the IDLE ‘Run module’ command doesn’t by default.  Relaunching the editor from commandline with sudo sorted that bit.

I fiddled with the sample code to try out timing then started wondering about more uses.  Flashing a Morse code seemed the obvious thing to try and since I only know two (BCN or STU for the Brecon and Strumble VORs!) I decided to try and make it flash the code for Brecon.

Things I instantly didn’t know…

How to write to the screen in Python (easy and guessable – print “Whatever”)

The timings for Morse (how much longer than a dot a dash is, how much space is between letters etc. (Wikipedia to the rescue there)

Things that worked well

It worked!  and I tweaked it a bit to put some info on the screen as well.

I needed to comment the code heavily to not loose track of which letter I was on.

Stuff to improve /add

More realistic timing.  It’s vvveerrryyy veeerryyy slow at the moment 1 second per dit.

Variables for the lengths of the ons and offs.  At the moment I’ve coded each characters individually.  Very inefficient!

More than one station!

Free text conversion into Morse sequences.

Find out if I can run those commandline setup and close down bits to make the LED accessible from within the program itself.

Code below (I learned about the WordPress shortcode wrapper for posting code without mangling it too!


#!/user/bin/python

#Needs the LED0 (OK light) to be uncoupled from the mmc0 trigger using:
# echo none >/sys/class/LEDs/led0/trigger prior to start

import RPi.GPIO as GPIO
from time import sleep

# Needs to be BCM. GPIOBOARD lets you address GPIO ports by peripheral
# connector pin number, and the LED GPIO isn't on the connector
GPIO.setmode(GPIO.BCM)

#disable warning about channel in use
GPIO.setwarnings(False)

# set up GPIO output channel
GPIO.setup(16, GPIO.OUT)
# write the Brecon VOR details to the screen
print "The Brecon VOR is 117.45 BCN _.../_._./_."
print "Now watch the LED!"
sleep(3)

# Flash the Brecon Morse pattern

# B
print "B..."
GPIO.output(16, GPIO.LOW) #dah
sleep(3)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dit
sleep(1)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dit
sleep(1)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dit
sleep(1)
GPIO.output(16, GPIO.HIGH)

#next letter
sleep(3)

#C
print "...C..."
GPIO.output(16, GPIO.LOW) #dah
sleep(3)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dit
sleep(1)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dah
sleep(3)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dit
sleep(1)
GPIO.output(16, GPIO.HIGH)

#next letter
sleep(3)

#N
print "...N"
GPIO.output(16, GPIO.LOW) #dah
sleep(3)
GPIO.output(16, GPIO.HIGH)
sleep(1)
GPIO.output(16, GPIO.LOW) #dit
sleep(1)
GPIO.output(16, GPIO.HIGH)

# On
#GPIO.output(16, GPIO.LOW)
# wait a bit
# sleep(5)
# off
#GPIO.output(16, GPIO.HIGH)

# For tidiness set the mmc0 trigger back to default using
# echo /mmc0 >/sys/class/leds/led0/trigger

 

 

Advertisements

One thought on “Flashing the onboard LED

  1. Minor addendum – the above scripts didn’t work on one of the Pis we bought earlier in the project – the module RPi.GPIO needed installing (and/or updates to Raspbian were eneded. Solution found here: http://diigo.com/0uoe6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s