Sunday, 7 October 2012

sending dynamic text from the Pi to the LoL Shield via Gertboard

Getting fixed scrolling text on the LoL Shield from the uploaded arduino program is one thing (see previous posting), but we really want to get the text delivered into the program dynamically. This is where the serial GPIOs on the Pi can help out.

I wrote a small arduino program to read characters from the serial port and display them on the LoL Shield. Note that I had to edit the Charlieplex.cpp file from the LoLShield library to remove a (apparently unused) Serial.end() call. Line 373 on my copy.

Once the program is uploaded to your ATmega on the Gertboard, you can deliver text via serial on the Pi using any method you like, e.g. interactively via one of:

  • minicom ama0
  • screen /dev/ttyAMA0 9600
(Press return to tell the program you've finished entering your characters.)

Alternatively, you can send your text programmatically via a python, C, bash script by writing to the /dev/ttyAMA0 device, e.g.:

  • $ echo 'weeee!' > /dev/ttyAMA0 
Remember, when sending/receiving serial data between the Pi and the Gertboard's ATmega, you must connect the GP14-15 pins in J2 to the MCTX and MCRX pins just above them.

// displays text sent via serial e.g.
// echo 'weeee!' > /dev/ttyAMA0
#include "Charliplexing.h"
#include "Myfont.h"
#include "Arduino.h"

unsigned char buffer[140];
int c;
int i = 0;
int len;

void setup() {

void doMsg() {
    Myfont::Banner(len, buffer);

void loop() {
    if(Serial.available()) {
        int c =;
        if(c != -1) {
            if(c == '\n' || c == '\r') {
                buffer[i] = '\0';
                len = i;
                i = 0;
            } else {
                buffer[i++] = c;
    } else {
        if(len > 0) {

Wednesday, 3 October 2012

LoL Shield on Gertboard

Bought a giant LoL (lots of LEDs) shield from Olimex on Ebay. This shield has a matrix of 9x14 LEDs which can be individually driven. The shield has pinouts for Arduino boards, but wiring up 12 digital outputs from the ATmega chip on the Gertboard (PD0-PB5) to 12 digital inputs on the shield (2-13) allows the arduino IDE on the pi to drive the shield perfectly. Take a look at arduino images on the web to see where the digital pins 2-13 would connect to your shield.

Download the library from  and place it in ~/sketchbook/libraries (create these directories if they don't exist),  then restart arduino IDE if open, and finally open the LolShield/examples directory in your arduino IDE and upload.

Note that I had to placate the compiler with a couple of 'const' references in the LolShield example/library code, and I had to remove the programming wires from the Gertboard after uploading to prevent the pi's GPIO pins from interfering with the ATmega chip at runtime.

The LoL Shield library lets you do loads of cool things from scrolling text, to vu-meters. I'll be having fun for some time, I think!

Monday, 24 September 2012

twitter feed on LCD

I used the python-twitter module from to add the ability to display the latest mention of a twitter account to the LCD, along with manual scrolling of the text to fit into the 16 character wide display. You'll need to log into with the account and enable an 'application' to get the oauth keys which you place into the initial twitter.Api() call, replacing the xxx entries with your own. The code fetches the latest mentions every 30 seconds. In between those fetches, it scrolls the text from right to left and repeats when the line has all been shown.

Here's the new main routine adapted from the original code as referenced in the post on LCD a couple of days ago:

def main():
  # Main program block
  api = twitter.Api(consumer_key='xxx',

  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers

  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable

  # Initialise display


  GPIO.output(LED_ON, True)

    nowTime = time.time()
    loopTime = nowTime + 30
    print 'loop'
    results = api.GetMentions()
    msg1 = '               from @' + results[0].user.screen_name + '                '
    msg2 = '               ' + results[0].text + '                 '
    msg1Idx = 0
    msg2Idx = 0
    while(time.time() < loopTime):
      view1 = msg1[msg1Idx:msg1Idx + 16]
      if(msg1Idx + 16 > len(msg1)):
        msg1Idx = 0
      view2 = msg2[msg2Idx:msg2Idx + 16]
      if(msg2Idx + 16 > len(msg2)):
        msg2Idx = 0
      lcd_byte(LCD_LINE_1, LCD_CMD)
      lcd_string(view1, 1)
      lcd_byte(LCD_LINE_2, LCD_CMD)
      lcd_string(view2, 1)
      msg1Idx += 1
      msg2Idx += 1

Sunday, 23 September 2012


Ordered a Gertboard from which arrived 2 days later, even with the 3-4 day shipping option. 

The Gertboard is a Raspberry Pi expansion board, which offers 12 buffered IO ports with LEDs, 3 pushbuttons, a DAC, an ADC, a motor controller, 6 open collector channels and probably the coolest of all, an Arduino microcontroller (Atmega328P) which can be programmed by the pi, using the standard Arduino IDE with tweaks from here. Here's a shot of the Gertboard running the 'blink' Arduino example sketch:

The Gertboard comes unassembled, and takes a few hours to put together. Tandy's packaging is excellent, with components in an ordered strip. There are a bunch of sample C programs to test each of the subsystems, which I'm enjoying working through to validate the soldering.

Thursday, 20 September 2012

LCD fun

I bought a cheap HD44780 LCD from ebay, and followed the excellent instructions at to great success:

2 Raspberry Pis communicating via morse code

Our second Pi arrived in the post today from Cool Components, which arrived the next day after they shipped it. Nice! But what on earth can you do with 2 raspberry pis?

You can set up one to be an LED morse code transmitter, and one to be a phototransistor morse code receiver:

I got the idea from a post on the forums about a project described on to monitor a power meter that indicates load by the frequency of a flashing LED. I took the phototransistor from there and found that it works very well with the LEDs contained in my SK Pang kit.

In my project, the sender is a typical GPIO output controlled LED, with python code to take an english message from the user and convert it into morse code LED pulses. I took the python morse code dictionary from to save a little work. 

The receiver setup is identical to the one in the power meter project, but with C code to monitor the LED pulses and convert them from morse code back into english. I used C because the python runtime turned out to be quite imprecise on the timing loops. With some tuning, I could get the C code to correctly interpret the active LED durations, and the pauses between. Because I was following the power meter project's C source, I also used the bcm2835 C library which can be found at

I prototyped this all on a single Pi initially, so don't let that stop you if you only have the one. 

The one minor issue with the phototransistor, is that it is quite sensitive to ambient light, so I covered the units with card during execution. Note that the current receiver code spits out the previous message when a new one arrives (i.e. pauses are currently only terminated with the next LED pulse).

Thursday, 13 September 2012

Following the excellent instructions at D. Bodnar's pi page: I successfully made a serial cable for the pi.

I bought an rs232 to ttl chip from very cheap, and it even arrived next day!

Initially, I soldered the chip right-to-left, which didn't work. It was only by noticing the layout of the resistors in D. Bodnar's page above that I realised the chip was inverted. Note that this chip has 6 resistors, unlike the above page. Click the image to enlarge it. I guess I'm lucky I didn't fry it, or the pi. The chip did get very hot. Ahem.

Once the cable was complete, I needed to test it via a terminal. I don't have a windows machine with a COM port, so I used a linux laptop and ran

screen /dev/ttyS0 115200

On my crunchbang 11 linux setup, this initially just returned '[screen is terminating]'. Suspecting permissions on /dev/ttyS0 I ran the command again as root, pressed Return a few times, and it worked! I'll work out what the permissions issue on /dev/ttyS0 is later.

Note the ttyAMA0 presented by the pi compared to the normal tty1 you see over e.g. HDMI console. Interestingly, the terminal stays connected after a reboot and you can see the pi's output as it is restarting.