Jan 19 2010

doorbell alert with Arduino


My brother and sister-in-law were generous enough to get me an Arduino kit for Christmas. It took me some time to think up a good starting project, but, while I was waiting for a package, it occurred to me that it would be nice to be able to hang out in the basement playing guitar and not have to worry about missing the doorbell when the UPS guy rings. It’s nearly impossible to hear the bell from the basement and I have missed more than one signature-required package just because I couldn’t hear it. While there isn’t an extra doorbell chime in the basement, I do have a computer down there, so I thought this would be a great opportunity to test out my Arduino skills.

Materials (as seen above):

  • Arduino (any version should do)
  • LED (any LED, but have the specs handy)
  • Photoresistor
  • Some resistors
  • Diode (I used a 1N4001; just make sure yours can handle your doorbell’s voltage)
  • A foam earplug
  • Electrical tape (black is probably best)
  • Something to put the circuit on (PCB, breadboard, protoshield, whatever)

(As a bonus, most of the materials, except a resistor, can be found in the Advanced Arduino Starter Kit I got)

The idea behind this project is to have an extender for the current doorbell system. While it would be fairly simple to replace the doorbell altogether (just wire a button to the Arduino), I wouldn’t to add on to the current system without interrupting it. For that, I had to do a little research. Doorbell circuits, as it turns out, use alternating current (AC) coming from the house, at least here in the US, which would be problematic with the Arduino. Whereas direct current (DC) sends a constant signal, AC alternates between positive and negative voltage. Remember sine waves from math class? That’s what AC looks like. If you try to hook that to an Arduino pin, it will read the signal as constantly switching between on and off many times a second. With DC, the Arduino only sees “on” when there is voltage and “off” when there isn’t, so it is much more convenient. It’s fairly simple to convert AC to DC using a diode, but it doesn’t eliminate the on/off problem, at least not the way I did it. A diode only allows positive current to flow, so when you run AC through it, it chops off the bottom half of the sine wave (it’s known as a half-wave rectifier). Unfortunately, that still leaves a bunch of humps on top that the Arduino will read the same as if you hadn’t done anything to the AC signal. You can use a couple more diodes to create a full-wave rectifier, which, while not perfect, would be good enough for our purposes; however, we only really need a half-wave rectifier for the circuit I created. It’s entirely possible to achieve the same results using a full-wave rectifier and a voltage divider, but I think my solution is a little simpler. You can read more about rectifiers on Wikipedia if you want to go that route.

After asking around on the Arduino forums, someone suggested I try an opto-isolator instead. That sounds pretty complicated, but an opto-isolator is really nothing more than an LED and a photoresistor in one package (they make up the “opto” part of the name, short for “optical”). When the LED lights, it triggers the photoresistor. The idea behind an opto-isolator is that you can have two separate circuits interact with one another without interfering with one another (hence “isolator”). In our case, we don’t want to take too much current from the doorbell circuit or it won’t ring very well. We also don’t want to overload the Arduino and we don’t want to have to deal with AC any more than we have to. Unfortunately, an opto-isolator isn’t something I have lying around and RadioShack doesn’t seem to sell them, so I thought I’d test my knowledge and build one myself. Like I said, it’s really nothing more than an LED and a photoresistor, both of which I do have, so I just need something to put them in. I decided to use a foam earplug, which turned out to be more of a pain than I had hoped, but it does a good job of blocking out external light. You’ll probably want to cut a little off the end to shorten it, but it will need to fit both the LED and the photoresistor (not the leads, just the main part). After that, stand it up and squish it down, like this:

We want to poke a big enough hole in the middle to fit our components in. I first tried an awl, but the hole kept closing up when the earplug puffed up, so I needed something bigger. I decided to use the end of a pen, though you could probably use a piece of tubing or a straw. You’ll need to hammer it to push it all the way through, so make sure whatever you use is sturdy.

Clear the gunk out of the hole and wait for the earplug to puff up again. Now that you’ve got a hole, start by shoving the LED into it. It’s kind of a pain to get it in there, but it should go most of the way in. Leave some room on the other side for the photoresistor.

You’re now going to shove the photoresistor into the other side of the earplug. It’s an even bigger pain than the LED was, but you want it to be right up against the LED so that the LED’s light output is the only thing effecting the photoresistor. When you’re done, you should have this goofy looking contraption:
Spider plug!
Just to make sure no extra light gets in anywhere, wrap it with the electrical tape so that it looks even more ghetto.
You should probably just buy a real one.
Is it elegant? Hell no! But it does exactly what I want it to do. What we’re going to do with our DIY opto-isolator is attach the LED side to the doorbell circuit and the photoresistor side to the Arduino. When someone pushes the doorbell, the LED will light. The Arduino, through the photoresistor, will detect the change and send a signal to the computer. You might remember earlier that I said we would be just fine with a half-wave rectifier. If you’ve read through Getting Started With Arduino, then you might remember reading about pulse width modulation (PWM). Using PWM, you can change the brightness of an LED by changing how fast on and off signals are sent to it. The same thing applies to our little circuit. While we are still sending only half of the AC sine wave, the signal switches so fast that the LED appears to remain lit (it may actually stay lit, for all I know, but you’ll want to look that up), so neither we nor the photoresistor can tell the difference. That means that when the photoresistor triggers the Arduino, the Arduino is only seeing one consistent on signal. Pretty neat, huh?

Before we hook anything up, you’ll need to see how much voltage your doorbell’s transformer is putting out. Take the cover off your doorbell’s chime and notice what a simple circuit it is. If you have a simple chime bell like I do, it is nothing more than an electromagnet and some rods. When you send current, it creates a magnet that slams the rod into a piece of metal, creating one chime sound (ding!). When you let go of the button, there is no electricity to create a magnet, so the rod goes back to it’s normal position, overshooting a bit on its way and smacking the other metal plate (dong!). For more on that, read this article. Anyway, the important thing here is to figure out what each of the terminals are connected to. On my chime, there are 3 terminals: front, transformer, and rear. Front is connected to the doorbell (front of the house), transformer connects back to the transformer, and rear would connect to a second doorbell if I had one. Set your multimeter to measure AC voltage and put the positive poker thing on the terminal connected to the doorbell and the negative poker on the terminal connected to the transformer. Have someone push (and hold) the doorbell while you look at your multimeter. My doorbell puts out around 10v, but yours might be different. Use the voltage you get to figure out the size resistor you will need for your LED. Look at your LED’s specifications and see what voltage and current it is rated for. Mine was a Vishay LED that could take a maximum of 3v and somewhere around 30 mA. I decided to play it safe and go with 2.5v and 20 mA. Using Ohm’s Law (voltage = current x resistance), I discovered I needed a 375 ohm resistor, which, as it just so happens, doesn’t exist. You want to play it safe and go higher rather than lower, so the next closest value is a 470 ohm resistor. Using a higher value than necessary will make the LED glow dimmer, but it really makes no difference for my circuit, so 470 is just fine.

Now, how are we actually going to hook it up? The LED needs to be hooked up a specific way or it will break. The longer lead of the LED is the positive lead and will connect to the doorbell side of the chime. Well, not directly. Since the purpose of the resistor is to limit current before it hits the LED, we want to connect the resistor to the terminal and the LED to the resistor. The other side of the LED will connect to the transformer terminal of the chime circuit. But remember that we still haven’t converted the voltage to DC yet, so we will need to add one more thing to our circuit: the diode. Connect the diode before the resistor; that is, it should connect to the terminal, the resistor to the diode, and the LED to the resistor. Like the LED, the diode also needs to be connected a certain way (the resistor does not). On one end of the diode, you should see a colored band (silver on mine) that runs around the body of the diode. That end should connect to the resistor. Think of it like an arrow with the point being the band; current will flow only toward the point and not in the other direction, so we want to point the arrow in the direction we want current to flow.

Now we get to connect it all to the Arduino. You’ll need to use a breadboard or a protoshield for this part (you can use it for the other part, too). Connect one end of the photoresistor to +5v on the Arduino and the other end to a spot on the breadboard. Connect a 10k resistor from ground to the same row on the breadboard, then connect a jumper wire from that row to analog pin 0 on the Arduino. The resistor keeps the Arduino from getting a bunch of wonky signals that would throw of its readings. That’s it! The completed circuit will look something like this (the two loose wires will connect to the doorbell circuit):
Yup, still kinda ugly.
Here’s a schematic of the same circuit:
Not nearly as ugly as the real thing.
Time to program the Arduino. The code is pretty simple; all you need to do is tell the Arduino which pin the photoresistor is connected to and tell it what to do when the photoresistor reacts to the LED (i.e. when the doorbell rings). I decided to have it print a little message (“ding dong!”) over the serial port. The important part of the code is here:

if ((lightLevel >= 500) && (oldLevel < 500)) {
Serial.println("ding dong!"); }

In English, that says that if the photoresistor detects light, print the message to the serial port (and if you watch the serial monitor, you will see the message). I wrote some test code to determine the light levels read by the photoresistor when the LED was on and off. It read about 0 when the LED was off and above 600 (usually closer to 700) when it was on. Since there is plenty of wiggle room in between those extremes, I decided to give myself a little space and use 500 as the sweet spot. The code says that if it reads a value greater than or equal to 500 from the photoresistor AND the value taken before the current reading was less than 500, then it should print the message. That little trick means that it will only print the message the first time the button is pressed instead of printing it over and over again when the button is held down (when the button is held, both values will be above 500). It also means that when the button is released (current value less than 500, old value greater than 500) or not being pressed (both below 500), it won't print the message. You can download the code here. Upload it to your Arduino. You may want to test it with a simple button setup like those illustrated in Getting Started With Arduino before you hook it up to the doorbell. Here's what my final doorbell test looked like:
Probably not the safest way to do it.
It works! If your doorbell is as crappy as mine, you may find that it doesn't always work perfectly. You may need to play with the light level threshold (in the "if" statement) to match your opto-isolator setup or change the delay time. The delay is in there mostly as a debounce precaution, but you may find that you need more or less for your doorbell.

Improvements, had I the money and skill to implement them:

  • Shrink it down and make it permanent. If I were going to keep this installed, I would probably use a Nano and solder everything onto a PCB so it would fit in the chime housing
  • Make it wireless. It isn't that useful if you have to be sitting next to it with a USB cable to use it. Adding an XBee unit or using a bluetooth Arduino would make it much more practical.
  • Add useful alerts. No one wants to sit there with the serial monitor open all day. Having a Growl plugin to notify you when the doorbell rings would be ideal.

If you can think of any other improvements, feel free to share. You could also, as stated earlier, simply replace the whole doorbell circuit with an Arduino. You could hook up a camera or have it send Twitter alerts (or both). I leave the rest up to you.


Jan 3 2009

easy, cheapish media center controller

My friend just got me one of the official PS3 Wireless Keypads. At first, I thought it would be only marginally useful, but as it turns out, it has its own battery and connects via bluetooth rather than through the controller’s connection. So that got me thinking about whether I could connect it to my phone or other bluetooth things. I couldn’t get it working on my phone, but it works almost exactly like I would expect it to on my computer (a Macbook Pro). Why is this useful? My brother was just talking to me about how he wanted to find a small, wireless keyboard he could use to control a media center PC from the couch. The PS3 Wireless Keypad not only gives you that (and smaller than any other keyboard), but it also gives you a touchpad to move the mouse around if necessary. On top of all that, it’s also only $50, which is a lot cheaper than other bluetooth keyboard/mouse combos. Here’s how to set it up (sorry, I don’t have a Windows computer with bluetooth, but I imagine the procedure is similar):

1. If you don’t already have the bluetooth icon in your menu bar, open system preferences and click “Bluetooth.” If it is in the menu bar, just click the icon and select “Open Bluetooth Preferences…” You can see that I’ve already set up my keypad in this screenshot.

BluetoothPane

2. If it isn’t already on, turn on the bluetooth connection by checking the “On” box. Then click the little + sign under the list of devices.

3. On the screen that pops up, click “Continue.” On the next screen, select “Keyboard” and hit “Continue.”

DeviceSelect

4. Hold the blue button (on the left shoulder) on the PS3 keypad and turn it on. Keep holding the button until lights start flashing. It will then show up on the list of devices on the computer’s setup assistant. Select it and click “Continue.” It will give you this screen telling you to input some numbers (your numbers will probably be different):

BluetoothNumbers

5. A new window will pop up telling you it can’t figure out what keyboard your using.

KeyboardUnrecognized

You will notice at this point that when you try to move your mouse to the new window, your trackpad no longer works. Click the touchpad on/off button on the PS3 keypad. Ta-da! It works! Just like on the PS3, the left and right arrows operate as your left and right mouse buttons.

6. It’s going to tell you to push the key to the right of your left shift key on the keypad. I think the keypad isn’t in the special Apple list of keypads or something because it can’t seem to figure it out, but I might just be too impatient to wait.

PushKey

7. Like I said, it doesn’t seem to be able to figure it out, so just push a key on your normal keyboard and it will give you the option to skip this step. Click “Skip.”

Skip

8. It will then have you select the language of your keypad. For me, this was “ANSI” for United States. Select it and click “Done.”

LastStep

9. That window will close and you’ll see the bluetooth setup assistant again. You can just hit “Quit.” Your keypad is all set up!

You’ll notice that not all of the button work quite like they do on the PS3. For instance, hitting the blue button and then the comma key doesn’t give you an apostraphe. All the letters work, though, and so does the touchpad, so even with a couple buttons being off and not having a ctrl, option, apple, or windows key, it could still be pretty useful as a small, relatively cheap media center controller. If I were only going to use it for that and not for PS3, I might saw off some of the extra plastic to make it lie flatter.