Introduction: USB Powered Clapper Switch - Extremely Little HW Required!

About: Hi there! My name is Patrick, and I am an electronics engineering technician who works full time as a lab tech, and part time as an electronics engineer/salesman. I own an ebay store, and two websites, which …
Hi all!
Thanks for having a look at my instructable!  If you have by chance seen my original clap-clap on/clap clap off circuit instructable.  This clapper circuit is very different than my original, in that it requires absolutely no amplification of the audio signal being sampled by the on-board electret microphone! As well, the circuitry is powered exclusively by the USB port on my PC!

For all of you who find this instructable interesting, I would apprecaite your vote in the USB contest.  As well, please rate this instructable.  If you found it to be good, I want to know!  If you found it needed work, I want to know!  Thank you for your consideration!
I'll be creating a kit for this instructable within the next few weeks, but without the relay. It will be available here:
http://www.electroniclessons.com
Or here:
http://ww.engineeringshock.com

Here are some of the key features:
* By simply turning your computer on, you can clap-clap on anything powered by AC that is within the limit of the 5v relay you are using (A desk lamp for example - Seen in the below video)

* EXTREMELY LITTLE HARDWARE REQUIRED!  Less than $10

* No amplifier circuitry required.  The audio is sampled through the use of an ADC (Analog to Digital Converter), located on the MCU.

* Powered SAFELY by the USB port on your PC or laptop.

THIS VIDEO TALKS ABOUT THE PROJECT, GIVES YOU A DEMONSTRATION, AND TALKS ABOUT THE SCHEMATIC DIAGRAM! 

Step 1: How It Works!

There are many different variations of the clap circuit.  You can have circuits that activate when it hears a loud noise (clap), and deactivates when it hears another.  Typical clap circuits, such as the one I created in my second instructable requires two loud noises (claps) within a very short period of time to activate the circuit, then anoother two claps do deactivate the circuit.  This is all done through simple programming. 

The electronic hardware is very limited, which is great, as most of us don't have a ton of money to spend on our side projects.  I'm very proud of this fact.  I've never seen any version of the clapper that used an ADC to sample for noise.  All of my previous clap circuits, and all of the other clap circuit variations that I've seen require either passive or active amplification.  It took a bit of time to get the programming right.... I had to make many changes to several timing registers before I was content with the result.

The CIRCUIT SCHEMATIC on the following page includes an in depth analysis of the simple circuitry involved.  You should have no problem following along.  For now, let's talk about the basic theory behind the circuit.  As well, if you've watched the video, you'll have seen a schematic breakdown from myself personally.

Step-By-Step:
1)  As soon as the devive is plugged into the USB port of the computer, it becomes active.  By active, I mean that is is constantly scanning for loud noises, such as a clap.

2)  As soon as the device detects a loud enough noise, it starts a countdown sequence.  In this very short time, the device is constantly scanning for a second loud noise.  The duration of the countdown sequence is under 600 milliseconds. 

3) If within the period of the countdown sequence a second loud noise is detected, the microprocessor activates a relay, which is connected to an AC power source, and a load that requires AC (A lamp, fan, LCD,etc).  If the countdown sequence elapses without another noise being detected, then the program starts over, and scans for an initial loud noise.  This is to ensure that two claps are required for activation, not just one.

4)  If the device detected two claps, your relay is now activated, and your AC device is powered.  The program now goes into an extremely similar area of the program that basically mimics the first part of the program.  It waits for another set of claps, only this time the relay will deactivate if two claps are detected. 

5)  If two more claps are detected within the countdown sequence, the relay is deactivated, and the AC device turns off.  The program then starts over.

It is a very simple programming algorithm.  I decided to use the PIC10F222 Microprocessor (MCU), which is EXTREMELY cheap - Less than $1 if purchased in bulk).  The problem with the PIC10 series, is that it is an archiac MCU, and it comes without the BTG (Bit toggle)  and COMPARE commands.  This made programming the MCU a pretty large pain in the butt.  It requires a lot more code, and a TON more patience.  I've provided the code in the SOFTWARE step, so have a look at it.  I've done my best to comment the code, so try to follow along with it!

NO AMPLIFIER???
That's right!  No amplifier!  The coupled signal coming from the microphone is so small that it typically requires amplification in order to be compatible with the PIC.  What I've done here is, through programming, taken readings from the ADC, and looked for signals in the 20-40 millivolt range (0.02-0.04 volts).  If the noise is loud enough, the ADC is able to pick up that small signal, and turn it into a hexadecimal value, which is then compared against a pre-loaded value.  If the signal is stronger than 20mv, then it is accepted by the programming as a "CLAP".

THE ADC:
The PIC10F222 has an internal ADC capability that allows for the user to sample an analog signal between 0v and 5v.  This analog value is then turned into a HEX value based on a binary number.  This is an 8-bit HEX value. 
If we have 5v at our analog input, and we take a sample, the value in the ADC register will be "1111 1111, which equals FF in HEX"
If we have 0v at our analog input, and we take a sample, the value in the ADC register will be "0000 0000, which equals 00 in HEX"
In the programming, I run a routine that samples the voltage at the ADC input, and if the value is 0000 0001 (01H) or greater, it is recognized by the program as a "Clap".

COMPONENT LIST (Using USB as the power supply):
* Electret/Condenser microphone
* PIC10F222 MCU 8-pin DIP IC
* USB cable
* Terminal Block
* 5v relay
* NPN small signal transistor, such as 2N2222, 2N4401, or S9013
* Dollar Store power bar
* Resistors: 470R / 100k / 2x 10k
* Red LED
* N4004 diode
* Capacitors: 2x 0.1uf / 10uf
* Prototyping PCB (5cm / 7cm)

TOOLS REQUIRED:
1) Soldering Iron
2) Glue Gun
3) Safety Goggles
4) Patience
5) Some soldering experience

Step 2: The Schematic Diagram With Hardware Discussion!

Here is the fun part!  The hardware discussion is always my favorite part of any instructable.  I hope you enjoy it, and that you don't have too many problems following along.  Let's break it down step-by-step!

THE POWER SUPPLY:
I've added two different variations of the power supply.  One includes a USB power supply, and one includes an LM7805 5v regulator IC.  Since the PIC10F222 and the 5v relay require 5v, either of these solutions will work.  Since this project is based around the USB, we will concentrate more on using the USB port on your computer as a power supply.  Now, there is a lot of debate about how much current your USB port can supply.  I've used mine for applications that require 400mA.  As I understand it, the absolute MAXIMUM that the USB port can supply is 500mA.  Some people suggest that USB ports can support only 100mA, but I believe that this is only the case for some USB port configurations.  Regardless, this device consumes less than 100mA when the relay is turned on, and I have had no problems at all.  To be more specific, my device consumes around 10mA when idle, and about 50mA when the relay is turned on.     

Getting down to it... If you cut open a standard USB cable, you'll see a bunch of wires.   You are only interested in the red wire (5VDC positive), and black (DC ground - Negative).  These are your power supply wires that will apply 5VDC to the clapper.  There are two capacitors in parallel with the red and black wire.  The 10uf capacitor is for ripple, and the 0.1uf capcaitor is a decoupling capacitor.  DO NOT EXCEED 10uf.  In fact, the 5v on the USB port is already extremely stable, so you should not need to use this capacitor.  I placed it there for performance.  A "Just in case" kind of thing.  The 0.1uf decoupling capacitor is to filter out any high frequency signals that may be on the line for one reason or another.  When initially plugged in, the capacitor looks like a dead short on the line for a few milliseconds.  The USB port can support this, but if you use a large value capacitor, it will take longer to charge, and that can potentially hurt your USB port.  

In the case of using an LM7805 5v regulator, you need 7VDC minimum at the input pin, which is pin#1 to regulate the output to 5v.  The middle pin, pin#2 is ground.  The third pin, pin#3 is the 5v regulated output.  I always place a 100uf capacitor at the input, and a 0.1uf capacitor at the output.  The 100uf smooths potential ripple at the input, and the 0.1uf capacitor acts to filter out any unwanted high frequency spikes on the line.  In this case, I assume that the user is going to use a wall wart, so I've placed decoupling capacitors at the input and the output of the LM7805.  If you are using a battery as the input DC voltage, you're not going to need either of these capacitors.  They are normally used if a wall wart is being used as the input supply.

THE ELECTRET MICROPHONE:
The electret microphone is a tiny little microphone that has two leads; a positive and a negative.  The negative side is usually denoted by a black wire, or a little black ring around the lead.  In this circuit, we have the negative lead connected directly to the DC ground line.  We have a 10k resistor in series with the microphone which is 100% required.  Not only to protect the microphone, but so that we have an area where we can couple a signal from.  If we had the microphone connected directly to the 5v line, we wouldn't be able to couple any signal from the sensor to our MCU.  There is a 0.1uf capacitor connected between the positive pin of the microphone to the ADC pin on the PIC10F222.  When the microphone picks up a noise, the signal is coupled through the capacitor to the ADC input of the MCU.  This signal is EXTREMELY small.  The capacitor acts to rid the signal of the DC component.  The signal coming being coupled is an AC audio signal.  This capacitor is CRUCIAL!  Don't leave home without it =)  There is a 100k Ohm resistor in parallel with the ADC input and the ground line.  This may not be necesary, but I've added this capacitor as a high resistance bleeder that will take care of any possible signal on the lines that aren't directly related to the audio signals coming from the microphone.  It may not be necessary, but I always use one. 

THE PIC!0F222 Microcontroller (MCU):
The brain!  This baby does everything for us.  It samples the ADC input, takes care of processing our inputted information, and sets our output conditions.  It acts to sample the voltage at the ADC input, process it, and under the right conditions, activate/deactivate the relay.that controls our AC load.  It requires a 5VDC power supply (Pin#2), and DC ground (Pin#7).  The allocated ADC input is Pin#5 (GPIO 0), and the pin that acts as the output that activates our relay is pin#4 (GPIO,1).  GPIO is the name that is used to describe an I/O port (Input or Output port).  Programming determines whether a GPIO acts as an input or output.  Look for this in the programming!

THE RELAY DRIVER CIRCUIT:
We obviously can't drive AC with our piddly little 5V USB power supply, so we are going to have to use a relay to switch our 120VAC power supply on and off.  This simple 5v SPDT (single pole double throw) relay requires 5v along the coil to activate the circuit.  If you don't know much about relays, I suggest reading more about them online.  However, I'll go into a little bit of detail here.  The output of the PIC cannot support enough current to drive the relay.  We need to use a transistor to drive the relay by sinking power directly from our 5v power supply line.  The PIC drives the transistor, which drives the relay.  The PIC sends a positive signal (5v) to the base of the NPN transistor to turn the transistor on.  There is a 10k protective resistor between the PIC and the base of the transistor.  When power is applied to the base of the transistor, power at the collector sinks through to the emitter, which is connected to ground.  The collector is connected to one of the coil pins on the relay.  The other coil pin on the relay is connected to our 5v power supply.  When the transistor is turned on, power is connected to the coil through the transistor, and it creates a magnetic field that acts to connect the COMMON contact of the relay to the NO (Normally Open) pin.  By default, the COMMON pin is connected to the NC (Normally Closed) pin.  There is a protective diode that MUST be connected between the 5v supply and the collector of the transistor.  The Cathode most be connected to the 5v source, and the anode must be connected to the collector of the transistor.  If you reverse this, you're going to blow your supply when the relay turns on.  You're also going to potentially harm your small signal NPN transistor.  DO NOT REVERSE!  When the relay is deactivated, the magnetic field along the coil collapses and a large voltage spike is created.  This spike is abolished by the diode.  IT MUST BE PLACED in order to protect your transistor, and your power supply, which is your USB port. 

THE AC CONNECTION:
First and foremost, BE CAREFUL when playing with AC.  It can kill you if you aren't careful.  Be very cautious and thoughtful when working with AC.  What I've done here, is I've taken a dollar store power bar, and I've carefully cut the surrounding insulation that holds the neutral wire (white), the black wire (hot) and the Earth ground wire (Green).  Make absolutely sure that you do not cut the insulation on the coloured wires.  When all three wires are exposed, and you're certain that you haven't nicked the insulation on the coloured wires, cut the BLACK wire and strip back about a quarter of a centimeter of the black insulation on both of the severed ends.  MAKE SURE THAT THE AC PLUG IS NOT PLUGGED IN TO THE WALL!  When you have these wires exposed, connect one of them to the common pin of the relay, and the other to the normally open pin of the relay; preferrably through a terminal block.  Make sure that none of the wire is exposed, so that you don't hurt yourself when you plug the device into the AC output.  When the relay is activated, it will re-connect the severed black wire.  If you have an AC device plugged into your power bar, and the relay is turned on, then the AC power will be applied to the AC device that you have plugged in.  

IF YOU'RE UNCERTAIN ABOUT SOMETHING, OR HAVE ANY QUESTIONS, FEEL FREE TO MAIL ME THROUGH INSTRUCTABLES AND I'LL GET BACK TO YOU =)


Step 3: The Software!

I used the MPLAB ICD2 PIC programmer puck to program my PIC10F222.  If can be found here:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010046

A copy of the code can be downloaded here:
http://www.electroniclessons.com/COMMENTED%20CLAP.txt
I've done my best to comment the code so that you can follow along.  I've used standard PICASM code, that follows along with the instruction set that can be found in the data sheet,  I hope you find it interesting!

The PIC10F222 Data sheet can be found here:
http://ww1.microchip.com/downloads/en/DeviceDoc/41270E.pdf

Step 4: Safety and Considerations!

SAFETY:
When playing with AC, don't fool around.  You don't want to go blowing your breaker, or worse, receive one heck of a deadly shock.  When you are testing your device, make sure that someone is with you.  If you are young and inexperienced, talk with someone who knows more about electrical safety.

CONSIDERATIONS:
Thank you to all of you who made it this far.  I hope you found this instructable educational and fun.  I'll soon be coming out with a DIY kit for this that can be found at:
http://www.electroniclessons.com
or
http://www.engineeringshock.com

We sell all sorts of fun hobby electronic stuff, so check us out.  Thank you all very much for taking the time to read this.
Kindest regards,
Patrick Mitchell
Engineeringshock.com

USB Contest

Participated in the
USB Contest

Celestron Space Challenge

Participated in the
Celestron Space Challenge