Introduction: Arduino Clap-On Lights - Introduction to Relays
Everyone knows about clap-on lights, or "clappers". I've always wanted one and my personal philosophy is that the best way to get what you want is to make it yourself. Making clap-on lights is easy with Arduino. What's more, it gives you the chance to make a simple project with a relay. Knowing how to incorporate relays into your projects opens up a whole new world of what you can do with your Arduino.
Without going into too much detail, relays are electromechanical devices that allow you to use a relatively low amount of power (like the 5V from an Arduino pin) to control the switch of relatively high currents or voltages (lamps, motors, heaters, AC circuits, etc.). There are a number of different types of relays, but at their heart they all perform this function. The relay I used for this project is from Ebay(check here), I didn't know much about it besides it said that it could be used with Arduino (5V control) and it could handle up to 250VAC. Shop around, I got mine for under a dollar. My favorite thing about this project was how easy it was to use the relay.
Obviously, you need to be careful when working with high voltages from wall outlets, but as long as your are careful these are really fun to work with. I was able to test my clap-on light code with an LED on my breadboard, and then when it was time to add a real bedside lamp I simply switched out the relay for the LED and it worked like a charm. More on that later, let's start by getting the parts necessary..
Step 1: Gather Components
The parts you will need for this are as follows:
- Piezo unit
- 1M ohm resistor
- 5V Low-level Trigger Relay 250VAC
- Lamp (that you can cut the power cord on)
- jumper wires
Step 2: Wire It Together
I feel like the photos do more justice than I'll be able to do with words.
Here are some things to keep in mind:
- When attaching the Piezo:
- Sometimes polarity matters. The piezo I used did not have any markings so I was free to choose the + and - terminals.
- The "-" terminal of your piezo needs a resistor between it and ground. Your resistor value will change how sensitive the piezo is to noise. This "-" terminal is also the terminal you will connect to the analog pin of the Arduino (see schematic)
- When preparing the lamp:
- The live-wire in the lamp's power cord is the only one you need to cut. Leave the ground uncut.
- If you need help determining the live-wire, it's the one wired to the skinny leg of the plug (see sketch)
- When attaching the relay:
- Relay To Arduino:
- VCC on relay --> 5V on Arduino (the "+" on breadboard)
- GND on relay --> GND on Arduino ("-" on breadboard)
- IN on relay --> PIN 3 on Arduino (any digital pin is fine, just account for it in your code)
- Relay To Arduino:
- Relay To Lamp:
- NO (normally open) - this side of switch is normally open. When the Arduino digitalPin connected to the relay is switched to HIGH the switch is closed and the light turns on. I used this for the lamp.
- COM (common) - this port is used regardless of choosing to have the switch normally open or closed.
- NC (normally closed) - connecting to this side of the switch will keep the switch normally closed (mind-blowing, right?). I'm not sure what situation you would want this, but to open the switch requires setting the digitalPin to HIGH on Arduino.
Step 3: Write the Code
I've attached the code I wrote for this. Basically, your piezo is always listening for claps. When it hears sounds of the proper threshold it will update a counter. Every two claps will cause the state of the light to switch and it will reset the clap counter. I also included some code to flash the onboard LED on the Arduino each time a clap is registered. Other than that, the code is pretty self-explanatory and I tried to comment it out as best I could.
Step 4: Test
Once you have everything wired you should be good to go. Plug it in and see how it works. I personally like to test my code using an LED and the Serial monitor with the Arduino attached to my computer. Then, once I know the code is good I switch to plugging the Arduino directly into the wall and adding the relay in place of the LED. This is probably unnecessary.
Anyway, if you've made it this far give yourself a round of applause...
No, seriously, you've got to clap to make sure it works :)
Participated in the
Lamps and Lighting Contest 2016