IAN LANG ELECTRONICS

A touchless switch, as the name implies, is a switch that is activated when you wave your hand (or any other part of your anatomy and I do not wish to know what part it is if you do thank you) or an object over the sensor. In this study we'll be going over a design using an IR sensor module,  the Sharp GP2Y0A21YK, and which is intended for a display in a toy shop to make Hornby trains move around a track when somebody puts their hand over the sensor. Touchless switches are also used to help blind people and as proximity sensors in industrial machinery.

First things first. Here's how to wire up your UNO:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

So, in addition to that sensor, you'll need a relay capable of handling the power you're going to chuck through it, a transistor (2N222A will do it), a snubber diode (1N004) and a 2k2 resistor (1/4 W will do).

 

I know what you are thinking. You are thinking "Lang, you do not usually bother with such complications. Why, sometimes you are barely legal. Why the transistor  and diode, are you just trying to get us to buy more bits in some sort of shady deal with an electronics company, you charlatan?"

 

Actually, although I might be a charlatan, in this case they are necessary. The fact is that the Arduino can't kick out enough current to satisfy the coil of the relay. Every relay has what is known as a pull-in current, and it is the minimum needed to make the coil active and thus magnetise and attract the contact away. The transistor is in the ground path of the coil, and when it conducts the ground path is achieved through the collector (which faces the relay) and the emitter (which faces GND). It only conducts when the base has a current, and this is supplied via the 2k2 resistor from pin 6 of the Arduino. The 2k2 is there to make sure an overcurrent is not present at the base of the transistor, otherwise it'll blow up. The diode, which fits over the coil terminals of the relay, is a snubber. The coil is inductive and sets up a back emf when deactivating. The back emf finds less resistance through the diode than the transistor and goes that way instead, if it went into the transistor it might blow it up. Eventually the power in the back emf dissipates through the coil losses of the relay and all remains well.

 

If you like, put an LED at the controlled terminals of the relay (the ones that are not attached to the coil but to the contacts). You don't need a visual indicator because you'll hear the relay clicking but it's nice to have one anyway. Then upload the following code:

 

 

void setup(){

  pinMode(6,OUTPUT);

}

 

void loop(){

  if (analogRead(A0)>615){digitalWrite(6,1);}

  else{   digitalWrite(6,0);}

}

 

Yup. That's it. Now, wave your hand a foot or so  from the sensor. Absolutely nothing will happen. Put your hand a few inches directly above the sensor, and move your hand down slowly. A few inches from the sensor the relay will click, and if you have put an LED on it, the LED will light. As you keep moving your hand down, it will go off again. If you have a piece of clear glass or plastic (not coloured) place it on the sensor. Now do the same trick again. The sensor will sense your hand through the plastic or glass. This means we can shove the circuit in a display case and keep it safe from damage by the grubby-fingered little oiks infesting the toy shop. Nor do they need to touch the glass of the display case. But I bet they do.

 

Now, this is all very well but it's only active when somebody is physically holding their hand in front of the sensor at the right distance. Suppose it takes a train 40 seconds to go round the track? Well, by changing the code we can have a timed event happen and then the relay goes off again:

 

long timethen;

boolean stopinput=false;

void setup(){

pinMode(6,OUTPUT);}

 

void loop(){

if (analogRead(A0)>615 && stopinput==false){

timethen=millis();stopinput=true;digitalWrite(6,1);}

if (millis()-timethen>=40000&&stopinput==true){

stopinput=false;digitalWrite(6,0);delay(500);}

}

 

 

If you upload the code to the board, you'll find that if you wave your hand about over the sensor the relay will stay on for 40 seconds and further inputs are ignored. This is exactly what we need for the toy shop. Let's canter through the code and see how it works.

 

Firstly we set up our variables:

 

long timethen;

boolean stopinput=false;

 

timethen is a variable of long type because it's going to be used to compare with the millis ( ) function built in to the Arduino. If we made it an int, it would overflow very quickly and the whole sketch would act in unpredictable ways. The boolean stopinput is to be used as an argument to decide whether or not to accept further input based on the condition of the relay.

 

In setup, we merely set pin 6 (attached to the base of the transistor) as an output pin:#

 

void setup(){

pinMode(6,OUTPUT);}

 

This is necessary to bias the transistor in such a way as to ensure full conduction.

Loop's first action is to poll for a reading from the sensor. There is a boolean argument:

 

if (analogRead(A0)>615 && stopinput==false){

 

In the first instance we know stopinput is false because we set it so at decalaration. So half the conditional is satisfied. The other half can only be so when a hand or other object is correctly positioned to produce a digital reading of 615 from the the Sharp GP2Y0A21YK. As each digital reading step on the Arduino is 4.89 mV, this equals just over 3V.

Now, the the Sharp GP2Y0A21YK produces a greater voltage the nearer an object is in front of it, but only up to a point. If the object moves inwards to about 3 or 4 inches away from the sensor, the voltage drops dramatically, and it's this effect we are exploting for touchless tecnology. If you want the sensor to activate when the hand or whatever is further away than it is currently, lower the value from 615.

If the conditional is satisfied, the following code is executed:

 

timethen=millis();stopinput=true;digitalWrite(6,1);}

 

So firstly the current run-time according to millis( ) is recorded to the variable timethen, the variable stopinput is changed to false, locking this code out of future cycles, and pin 6 is set high, biasing the transistor base on which causes it to conduct, making a path to ground for the relay coil, which energises and the relay switches over.

The next line of the loop runs:

 

if (millis()-timethen>=40000&&stopinput==true){

 

Now if stopinput equals false, this conditional can't be satisfied. If stopinput equals true, the other conditional can't be satisfied, and so either one or the other of the conditionals is running but not both at the same time. In the other half of this conditional, millis-time then has to equal or be greater than 40,000. This means that millis must run on for forty seconds after activation as it measures in millisseconds   MILLISecond- get it? Subtle isn't it?

 

Anyway, if both halves of the argument are true, this happens:

 

stopinput=false;digitalWrite(6,0);delay(500);}

 

And so stopinput is set to false, locking this conditional out and unlocking the other one, pin 6 is set low so the transistor no longer conducts and the relay coil has no path to ground, and a half-second delay is set up before any more input can be polled.

 

So, that's a momentary and a time delay switch. There remains only the toggle.

 

boolean oncondition=false;

void setup(){

  pinMode (6,OUTPUT);}

 

 

void loop(){

  if (analogRead(A0)>615&&oncondition==false){

digitalWrite(6,1);oncondition=true;delay(4000);}

  if (analogRead(A0)>615&&oncondition==true){

    digitalWrite(6,0);oncondition=false;delay(4000);}

}

 

Upload this to your board and then pass your hand across the sensor. You'll find that the relay stays on until you pass your hand across again, but not before four seconds have passed. if you keep your hand there for longer than four seconds, you'll find that the relay toggles. This is one way to eliminate cycling. There are better, see how creative you can get.

The code works in exactly the same way as above, locking out one or the other of the conditionals by the use of the variable oncondition. This design is for a bedroom lamp and needs a much meatier relay than usual as it carries 120W at the controlled terminals- or about 2A of current. It doesn't click, it thumps.

 

And that's it. Three different kinds of switches, all touchless, all from the same circuit, and all as easy as falling off a log. I prefer this to capacitive touch switches because you don't need to find the touchplate, which is a boon if you're partially or fully blind. The downside is that an object in the way can have the effect of blocking the switch, and in the case of the toggle, lead to cycling.

 

Ian Lang, December 2011

 

 

 

 

 

 

 

 

A Touchless Switch

Go Back