A Dream of Programming an Alarm Clock with a Raspberry Pi and Python

I went to Value Village yesterday to look for a silly Halloween costume. I came out of there with a full-body Robin suit, a $4 dual-band wireless router and a $4 LCD alarm clock. A few weeks prior I had the idea of connecting my Raspberry Pi to something I could control with its GPIO pins, because I study Electronics Engineering as a hobby and haven't really done anything with that knowledge. Anyways, every time I get my hands onto a circuit I just love analyzing it. There's so much to learn from a real-world circuit. When I don't understand a configuration of components, I do some research to figure out why they could be setup like that. I highly recommend this to anyone who want to study EE. As a programmer I feel empowered to know how electronic hardware works.

After stripping a few screws and untangling the mess of cables inside the clock, the first thing that caught my eye was a microcontroller. I love microcontrollers. Everytime I see one I know there is a datasheet out there that holds the secrets to controlling a good portion of a circuit and understanding what it does. Turns out this one was the heart of the entire circuit. The LM8560 is designed to create alarm clocks.

Lets take a look at what this baby can do:

Such power! Much digital!

On my particular clock, the manufacturer removed the ability to set the Hz and switch between 12 and 24 hour time (or rather, didn't include it).

The manufacturer added an additional feature though: use your voice as an alarm.

I wasn't really interested though in controlling the LM8560. The real fun was with the LCD screen that came with it. Using the LM8560 datasheet I deciphered what most of the pins on the RTTL-1855 LCD screen do, since there seems to be no datasheet. If anyone has it or knows what all these pins do that'd be awesome. Here's what I've figured out so far:

Maybe I should start a LCD clock database where people submit clock LCD pinouts.

Remember, I bought a whole clock for $4. The cheapest LCD from Adafruit is $9.95, and that's before shipping and taxes. And mine is way bigger. Can't display pixel graphics on it though.

Before this I had no experience with the RPi's GPIO pins, so I looked around a few places and read some material. Most talked about pull-up/down resistors, which lead me to research more about important components in a digital circuit. I knew what a pull-up and pull-down resistor were, but these pages described other things that were necessary to know about when messing with the RPi GPIO pins. Here's what I learned:

  • You can access the pins either by their physical placement on the circuit, or by their assigned number. The numbers are assigned by the broadcom (BCM) chip on the circuit. I'm personally going to use the BCM mode because apparently you can do more with it and it is standard.[citation needed]
  • There are two pins with internal pull-up/down resistors. This surprised me. After some research people say this is a safety mechanism and users should still use their own external resistors, along with a 1k protective resistor.
  • There is a Python library to easily manipulate these pins. Aw yea.

A pull-up resistor is just a resistor that is placed between a pin and an incoming current to protect the microcontroller. This setup runs current into the pin at all times, which means the pin is set to HIGH or "on". Connected between the pin and the resistor is another wire leading out to ground. On this wire is a switch. 

When the switch is pushed, the current goes to ground and the voltage drops to 0. The pin now has no current running through it, and is in a LOW state, or "off". Now you may be thinking ok well I'm pressing a button so shouldn't that mean it's on? That is totally up to you! HIGH and LOW have no meaning until you give it meaning. The pull-down resistor setup is configured so that LOW means off and HIGH means on, but even in that configuration someone can say it's actually the opposite!

Capacitors are used to stabilize voltage so voltage-sensitive circuitry, such as pins, don't happen to misfire. Turns out voltage in circuits "wobble" and are not constant. Capacitors help with that. Inductors are used to transform AC to DC. Combining capacitors and inductors are used for signal processing. Transistors are of course switches, which are used to create AND and OR conditions in a circuit (called gates). These are called passive linear circuit elements. 

Where The Fun Begins

Step 1: Prepare and Plan

So what needs to be done first is finding out if the RPi can power this thing on its own. Turns out, it can't. The LCD requires around 273mA. I found this tracing the power source back to the battery, which was 9V. Along the way was a 33ohm resistor. To find out how much current this is drawing, we plug in these numbers into Ohm's Law:

R = V / I

Since we want to have to find I we'll have to rearrange:

I = V / R

I = 9 / 33

I = 0.272727...

Now can the RPi power it? No. The RPi's 3.3V pins supply up to 15mA. What we'll have to do is hook up a 9V battery to the display and a resistor. The display is supposed to run at 60Hz as well, I think. At this point I hopped onto ##electronics on freenode to get some guidance. The plan of attack was to turn a GPIO pin on and off fast enough to simulate a 60Hz signal.

Next is to hook up the LCD pins to the GPIO pins. Pretty straight forward.

Here is the schematic of what I had (keep reading...):

It is pretty close to completion I'd say.

Step 2: The Code

After figuring out the circuit, we write up some code to manipulate the LCD. Python has a great library for manipulating the Raspberry Pi's GPIO pins. My goal was to be able to show all the numbers on the LCD by simply echoing them into a file. Like this:

echo "23:12" > /dev/clock/LCD

And it'll show up. There are 4 LEDs around the LCD that can be lit up too. I could use those as status indicators for say, if someone has messaged me on IRC, or I have new mail:

echo "on' > /dev/clock/LED1
echo "on' > /dev/clock/LED4

and so and so forth.

I'm thinking of putting the LCD and the RPi back into the case it came in and setting it somewhere useful, like maybe my desk.

In conclusion, I hope others do some interesting things like this. It is awesome manipulating every day objects.

Step 3: Never Put Your Work in a Bag

...In actual conclusion, my led display was thrown out by my girlfriend's mother, along with my jumper cables and the bread board. So this project never got to see the light of an LED.

Lesson learned I guess. Do not trust anyone with your electronics. Yes I am a little frustrated, but in the big scheme of life it is nothing...(fuck!)


  1. I'd make them buy me new ones. Throwing my stuff out on me, how do you tolerate that?


Post a Comment

Popular Posts