Capacitance Switch controls a Relay on Arduino

The sketch was updated Nov 2015. It can be downloaded from my GitHub page here. For a sketch that will control up to five relays from five switches see my post here.

This sketch uses a small capacitance switch to control a relay. The output from the switch is normally LOW, but when touched the output goes HIGH. The relay used is a single relay made by Ywrobot, it has a single changeover switch output and needs 5 volts to operate. The relay is operated when the control input is HIGH and when this happens the onboard LED turns on and the switch output changes.


The connection diagram shows a 4 capacitance switch, but a single switch would work equally well. The Capacitance switch is powered from the digital pins.

When the sketch is first run the relay is ‘off’, when the switch is touched the relay is switched on and the LED lights, the Arduino LED on pin 13 remains alight as long as the switch is touched. The relay remains switched ON until the capacitance switched is touched for a second time and the cycle can be completed.

The WHILE loop acts as a de-bounce, waiting until the switch is released.

/* Simple sketch to show the operation of a capacitance switch

Chris Rouse Nov 2015

The switch used here is mounted on a small board
with three connections, G (Gnd) V (Vcc) ans Out ( the switch output)
The switch output goes LOW when pressed

Connect Gnd to Arduino pin 5 - supplies 0v
Connect Vcc to Arduino pin 6 - supplies 5 volts
Connect Out to Arduino pin 7

The relay used is a Ywrobot board with 1 relay
The relay is switched on by a HIGH signal

Connect the Relay board to Gnd and 5 volts
and the control pin to Arduino pin 8
*/
boolean latch = false;

# define relay 8 // this is the pin used to switch the relay
# define Gnd 5 // this is the pin used to act as Gnd for the switch
# define Vcc 6 // this is the pin used to act as +5v for the switch
# define capSwitch 7 // this is the capacitance switch output
# define LED 13 // onboard LED

void setup() {
Serial.begin(9600); // used for debugging
pinMode(relay, OUTPUT);
digitalWrite(relay,LOW); // turn OFF the relay
pinMode(capSwitch, INPUT);
pinMode(Vcc, OUTPUT);
digitalWrite(Vcc,HIGH);// this pin will supply 5 volts
pinMode(Gnd, OUTPUT);
digitalWrite(Gnd,LOW); // this pin will supply the GND to the board
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // turn LED off
}

void loop() {

// turn switch ON on first touch
if ((digitalRead(capSwitch) == HIGH) && (latch == false)) {
digitalWrite(relay, HIGH); // relay ON
Serial.println("Relay ON");
digitalWrite(LED, HIGH); // turn LED ON
latch = true;
// wait here until the cap switch is no longer touched
while ((digitalRead(capSwitch) == HIGH)){
digitalWrite(LED, LOW); // turn LED OFF
delay(500); // try to stop debounce
}
}

// turn relay OFF on second touch
if ((digitalRead(capSwitch) == HIGH) && (latch == true)) {
digitalWrite(relay, LOW); // relay OFF
Serial.println("Relay OFF");
digitalWrite(LED, HIGH); // turn LED ON
latch = false;
// wait here until the cap switch is no longer touched
while ((digitalRead(capSwitch) == HIGH)){
digitalWrite(LED, LOW); // turn LED OFF
delay(500); // try to stop debounce
}
}
}

Advertisements

8 thoughts on “Capacitance Switch controls a Relay on Arduino”

      1. many many thanks, and one more thing i want to request you that if possible, please give me a 5 relay diagram including program.

        Like

      2. I have added a circuit and sketch for a 5 relay setup here. The wiring setup is also shown. Up to 5 switches and relays can be used and the relays turned off or on in any order. I hope this is useful.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s