Tuesday, August 7, 2012

LED Clock using Raspberry Pi

Okay, so if you were going to make a simple LED 4 digit clock, you probably wouldn't start with a Raspberry Pi.


But I wanted to test out I2C on the Pi and Adafruit's I2C LED displays looked like a good bet. I also used a level converter to convert the Pi's 3.3V I2C to the 5V I2C required by the LED module.

To recreate what I have done here you will need:
* Raspberry Pi
* Cobbler
* 4-digit 7 segment display and Backpack
* Level Converter
* Breadboard
* Jumper wires

Hardware
Connect up your Cobbler header as shown below. This is easier without the ribbon cable in place.
If you cannot make out the connections, they are:
* Cobbler GND to Level Shifter GND to Display GND
* Cobbler 5V to Level Shifter HV to Display VCC
* Cobbler 3.3V to Level Shifter LV
* Cobbler SDA0 to Level Shifter A1 (orange)
* Cobbler SCL0 to Level Shifter A2 (yellow)
* Level Shifter B1 to Display SDA (orange)
* Level Shifter B2 to Display SCL (yellow)

Software
I had to do the following to get I2C to work, following the instructions here:


sudo apt-get install python-smbus
sudo apt-get install i2c-tools (usefull but not essential)
sudo modprobe i2c-dev
sudo modprobe i2c-bcm2708

To simplify the process of using the display, I have created a Python library modelled on the Adafruit library for Arduino. You can download this from here and then cd into the extracted folder and do:

sudo python setup.py install


The clock program is included in the library as clock_example.py:


import i2c7segment as display
import time

disp = display.Adafruit7Segment()

while True:
    h = time.localtime().tm_hour
    m = time.localtime().tm_min
    disp.print_int(h * 100 + m)
    disp.draw_colon(True)
    disp.write_display()
    time.sleep(0.5)
    disp.draw_colon(False)
    disp.write_display()
    time.sleep(0.5)

For those looking for more information about the library, then just browse through the file i2c7segment.py, the methods are documented.

For more information about the display itself, including the segment mapping can be found at this useful thread.



About the Author
These are my books. Click on the image below to find out more about them.




                                                                                                                           


13 comments:

Dillon Nichols said...

Saw your first pic and I just stopped by to say nice USB cable! I bought one myself a while back, super high quality for as cheap as they are.

pdp-7 said...

Simon this is very helpful - thanks! I did have to change the import for the clock demo to:

pi@raspberrypi:~/i2c7segment-1.0$ head clock_example.py
import i2c7segment as display

After which, invoking it as root made those lovely segments light up!

pi@raspberrypi:~/i2c7segment-1.0$ sudo python ./clock_example.py

Also, I actually have the module powered via 3.3V on the Pi, so I skipped the logic level conversion.

pdp-7 said...

Another note: I'm running this on Adafruit's Occidentalis v0.1 image (http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-1)

Mr Routledge said...

Maybe give this a go next...

http://jeremyblythe.blogspot.co.uk/2012/07/raspberry-pi-7-segment-displays.html

Tim said...

What is the Pi sitting on? Is that some sort of plastic base?

Simon Monk said...

Pi is sitting on half a 'Modmypi' case.

city said...

thanks for sharing.

Gordon Henderson said...

Not quite sure why you're using a level convertor here... I2C is driven by open collector outputs with the master providing the pull-ups - which the Pi does via on-board 1.8K ohm resistors... The display should never drive the bus back to the Pi, so there really shouldn't be a need...

-Gordon

kiwinewt said...

Just wondering what sort of load there would be on the Pi driving this constantly? Would you just set up a cron job each minute to update it?
Another though - could you have 2 displays running together? That way you can use it to have 2 clocks... or 1 clock and another display (temp?) or 2 displays... or or or... :D

Simon Monk said...

@Gordon. I think you are right I don't think it is necessary. I had a level converter anyway and did the wiring before finding out about the protocol. I thought their may be some 'acks' going on.

No, the display should never be driving the bus.

Gordon Henderson said...

I guess a "belt & braces" approach never hurts!

Simon Monk said...

@ kiwinewt the load on the Pi is minimal, must of the time its sleeping, allowing other processes to do useful things.

Simon Monk said...

@Gordon, I just checked that the display doesn't do any nasty pulling up of the bus to 5V, and it doesn't. So I took the level converter out and it works just fine without it.