Build a reliable touch switch module that works standalone or with a microcontroller.
BUILD TIME: 30 MINUTES
DIFFICULTY RATING: BEGINNER
Touch switches are great devices, requiring minimal mechanical exertion to activate them. We’ve created a reliable and versatile touch switch module for use with microcontrollers.
THE BROAD OVERVIEW
Back in Issue 6, we published a few ideas around touch switches. While these circuits work as described, they’re fairly experimental and were designed primarily to demonstrate principles rather than be put into practice. So in order to generate some sort of useful output, we need to do more.
To start off, we’ve taken the piezo-based circuit from our group of methods described. The main reason for this is reliability in all temperatures and humidities, because it’s based around the highly predictable and repeatable piezoelectric effect.
Piezo buzzers are readily available, so serve as cost-effective “noise makers” in many projects and applications. Many people, however, don’t realise they work in reverse, which is the basis for this project.
HOW IT WORKS
The major components in this circuit are our piezoelectric module, an op-amp, and a 555 timer.
When pushed (even very lightly), the piezo module creates a small electrical current. This is then fed into an LM358 op-amp, which amplifies the tiny current many times over. But the output from the op-amp only lasts as long as you touch the piezo module, which could be a mere fraction of a second. This is where the incredibly versatile 555 timer comes into play. We feed our op-amp output into the trigger pin of the 555, which extends that brief pulse into a few seconds of constant 5V output. This 5V output is fed to an LED (purely for visual indication of activation), and an output pin. This output pin provides 5V, which can be fed to a microcontroller (which we’ll demonstrate further), or drive a 5V relay or transistor directly to control without loads.
While we have created a PCB for this project, to make implementation easier, it's easy to make up a working prototype using a breadboard too. Simply follow the diagram below.
The PCB is very compact, measuring a tiny 53 x 21mm. It is designed as a module, but we have still included a few mounting holes just to make life easier for some applications. As usual, our Bantam Tools’ Mini Desktop PCB Mill made fairly light work of this PCB, and gave us a great rapid prototype in just a few hours.
You will likely have a commercial PCB, which is solder masked, with plated through-holes, and all the common features that make construction even easier. Nevertheless, check over your PCB for broken tracks, corrosion, or other manufacturing defects. Give it a good clean with some isopropyl alcohol or circuit board cleaner, to help remove any grease or residue that may exist; this will help reduce the incidence of dry joints and other problems. Use the lowest temperature possible for the solder you’re using, to help avoid overheating components. It also helps to avoid overheating the tracks on the PCB, which can be quickly damaged by excessive heat.
As with all our PCB projects, we recommend following a standard construction pattern, which is hardware, passive components, semiconductors, then anything ultra sensitive (such as the op-amp IC). That means you should first install the pin headers and IC sockets if you’re using them. Then move on to the resistors and capacitors (be sure to take care with C1, which is polarised), then the LED (make sure it’s the correct way around) and piezo device. Finally, insert the op-amp IC and 555 timer - be sure to insert them as per the overlay (note: they both face the same direction, which does make things a little simpler).
Take a good look over the finished board to check for any solder bridges, poor tracks, and to double-check the orientation of any polarised components. If you’re confident in your assembly, carefully apply 5V power, taking care of the polarity (as there’s no reverse polarity protection on the PCB). If you touch the piezo, you should see the LED illuminate briefly.
If not, check back over your circuit carefully for bridged tracks or poor solder joints. The LM358 op-amp and 555 timer ICs are easily damaged. If you’re using IC sockets, remove the ICs from the PCB while you perform your checks. Recheck the orientation of the ICs, LED, and electrolytic capacitor. Use a multimeter to check that you have 5V across pin X and Y on the op-amp and 555 timer, as this will quickly indicate any potential issues with a short circuit or dry joint too.
If you only need a momentary output, this module can be used without any additional circuitry. All you need to do is change R4 and C1 values to adjust the latching time from the 555 timer, and you have a flexible momentary output. This can be easily used to switch a 5V relay or suitable transistor, to control all sorts of different loads you may require.
Here are some example values. The latching time is approximate, so it’s not suitable for precision timing, but for applications such as “turn the light on for approximately one minute”, it’s generally accurate enough.
The formula to calculate the delay is:
T = 1.1 x R x C, where T is measured in seconds, R is resistance in ohms (not kOhms or MOhms), and C is capacitance in Farads.
With the values specified in the circuit diagram we can calculate T as:
T = 1.1 x 4.7 x 103 x 22.2 x 10-65
T = 0.11374 seconds
To make a 10-second delay, increase the resistor value to 470kΩ. Of course, component tolerance and such, means that you’ll get “approximately 10 seconds”, not millisecond-precision. Fvor many applications this would be suitable anyway to, say, trigger an electronic door latch; so it can provide a good momentary output if that’s all you need.
INTEGRATING A MICROCONTROLLER
For a variety of reasons, this module really comes into its own when using it with a microcontroller. It’s 5V-compatible, so is suitable for use with many microcontrollers out there. We’ll use an Arduino Uno for our examples.
We have provided two sketches to provide precisely timed actions from the push of a piezo (button). There are momentary (push on / auto off after delay) and latching (push on / push off) sketches. We are simply providing a relay output, but you can easily expand on this code to provide additional functionality, such as sending a Twitter message, or other IoT functionality with appropriate hardware.
|1 x Piezo Transducer||AB3440||S6140|
|1 x LM358||ZL3358||Z2540|
|1 x 555||ZL3555||Z2755|
|1 x 1N4004 Diode*||ZD1004||Z0109|
|1 x Red LED||ZD0100||Z0700|
|1 x 220Ω Resistor*||RR0556||R7542|
|2 x 1k Resistors*||RR0572||R7558|
|1 x 4.7k Resistor*||RR0588||R7574|
|1 x 10k Resistor*||RR2798||R7582|
|1 x 10nF Capacitor||RC5348||R2846|
|1 x 22μF Electrolytic Capacitor||RE6092||R5084|
|1 x 28 Pin Header||HM3211||P5430|
* Total number of parts shown, may be sold in packs.
For all variations, we connect the output pin from the PCB to pin 5 of the Arduino, and the relay module to pin 5. These pins may need to be different if you’re using a different Arduino board, or different microcontroller altogether - in which case you’ll need to modify the code accordingly. The output from the module is TTL -compatible, so can be used with any digital pin.
This code provides a more precise timing than simply modifying the resistor and capacitor values for the 555 timer. This is especially true when you’re talking minutes or hours, rather than a few seconds.
In order to activate the momentary output, we simply wait for a detection of a push from our output, trigger our relay for five seconds, and then reset.
while (digitalRead(piezoInput) != HIGH);
Serial.print(“digitalRead(piezoInput) = “);
Serial.println(“Sensor press detected - relay on for 5 seconds”);
This code implements a straight forward latching output. Touch once to activate the relay, touch again to deactivate. This is useful for turning on a light, pump, or similar, where you want to manually turn it back off again.
Serial.println(“Waiting for press on sensor. This will enable relay until another press turns it ‘off’”);
while (digitalRead(piezoInput) == LOW)
Serial.println(“Waiting for another press on sensor. This will disable the relay and end reset.”);
while (digitalRead(piezoInput) == LOW)
As you can see, the code is very easy to read and expand on.
With both of our sketches, the default state of the relay is deactivated. Your relay module probably has normally closed and normally open contacts. This gives you an easy method to change the on/off functionality. Of course, if power is lost to the Arduino, the relay will return to default state too.
WHERE TO FROM HERE?
As mentioned briefly, adding some further IoT functionality (Twitter alerts, Slack messages, or simple data logging) are all fairly easy to implement using the microcontroller of your choice. We haven’t provided a 3D printable mount for this project, mainly because its application is varied, and this module will find its way into all sorts of ideas. The sky is the limit. Go forth and conquer!