Using up to 5 Capacitance switches to control up to 5 Relays.


The code is available on my GitHub page here.

This project allows up to 5 Capacitance Switches to control up to 5 relays. When a switch is first touched the associated relay closes. When that switch is touched again the relay opens. All relays can be on at the same time and can be switched off in any order.

When the Capacitance switch is touched the output goes LOW, the switch outputs are scanned and as soon as one goes LOW the associated relay is switched on. The sketch then waits for the switch to be released before scanning the switches again. A second touch on the switch will switch the relay off.

There is some attempt to ignore two switches being touched at the same time and the lowest number switch will be accepted. The switch pads are small and close together and my fingers are large so trying to press switch 2 often turns on both switch 1 and switch 2. I suspect the best action for large fingers is to use one of those conductive pens used on smart phones and tablets, but I have not tried that yet.

All five INPUTS are pulled HIGH in the sketch, so they can be left open if only one or two switches are required. In my setup shown above I only have a 4 switch board, so the fifth input is left unconnected.

circuit for 5 cap switch

Up to 5 Relays operated by up to 5 capacitance switches

Chris Rouse Nov. 2015

The Capcitance switches are mounted on a small board
connections, G (Gnd) V (Vcc) and Out ( the switch outputs)
The switch outputs go LOW when pressed

Connect Gnd to Arduino Gnd
Connect Vcc to Arduino 5 volts
Connect Outputs to Arduino pins 7 to 12

The relays are switched on by a LOW signal

Connect the Relay board to Gnd and 5 volts
and the control pins to Arduino pins 2 to 6

int relay;
int capSwitch;
boolean relay1 = false;
boolean relay2 = false;
boolean relay3 = false;
boolean relay4 = false;
boolean relay5 = false;
boolean pressButton;

# define LED 13 // onboard LED

void setup() {
Serial.begin(9600); // used for debugging
// setup relay switching outputs
for (relay = 1; relay<6; relay++){
pinMode(relay, OUTPUT);
digitalWrite(relay,LOW); // turn OFF the relay
//setup capacitance switch inputs
for(capSwitch = 7; capSwitch< 11; capSwitch++){
pinMode(capSwitch, INPUT_PULLUP); // pull any unused inputs HIGH
// setup onboard LED
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // turn LED off
Serial.println("This sketch will control upto 5 relays from a bank of upto 5 capacitanc e switches");
Serial.println("At the start all relays should be switched off");
Serial.println("Touching a switch will latch the relay ON, touching that switch again will turn the relay OFF");

void loop() {
// turn switch ON on first touch
// OFF on second press
delay(200); // helps stop false triggering
pressButton = false;
relay = 0;
for(capSwitch =7; capSwitch < 11; capSwitch++){
if(pressButton == false){
if ((digitalRead(capSwitch) == HIGH)) {
digitalWrite(LED, HIGH); // turn LED ON to show button press
relay = capSwitch-5; // the pin the relay is connected to
pressButton = true;
// now toggle relay state
case 2:
if (relay1){digitalWrite(relay, LOW);relay1 = false; Serial.println("Relay 1 OFF");} // relay ON
else{digitalWrite(relay, HIGH);relay1 = true; Serial.println("Relay 1 ON");} // relay OFF

case 3:
if (relay2 == true){digitalWrite(relay, LOW);relay2 = false; Serial.println("Relay 2 OFF");} // relay ON
else{digitalWrite(relay, HIGH);relay2 = true; Serial.println("Relay 2 ON");} // relay OFF
case 4:
if (relay3 == true){digitalWrite(relay, LOW);relay3 = false; Serial.println("Relay 3 OFF");} // relay ON
else{digitalWrite(relay, HIGH);relay3 = true; Serial.println("Relay 3 ON");} // relay OFF
case 5:
if (relay4 == true){digitalWrite(relay, LOW);relay4 = false; Serial.println("Relay 4 OFF");} // relay ON
else{digitalWrite(relay, HIGH);relay4 = true; Serial.println("Relay 4 ON");} // relay OFF
case 6:
if (relay5 == true){digitalWrite(relay, LOW);relay5 = false; Serial.println("Relay 5 OFF");} // relay ON
else{digitalWrite(relay, HIGH);relay5 = true; Serial.println("Relay 5 ON");} // relay OFF

// wait here until the cap switch is no longer touched
while (digitalRead(capSwitch) == HIGH){
// wait for switch to be released
digitalWrite(LED, LOW); // turn LED OFF as button now released


4 thoughts on “Using up to 5 Capacitance switches to control up to 5 Relays.”

  1. I am having a problem with interference from a switching power supply. I am using the capacitance switches to control a 2812 LED strip, which is powered by a 40A 5V switching supply. My arduino and switches work great by themselves, but even hooking the ground lead of the 5v supply to the circuit (the arduino uses a separate voltage source) renders it non-functional, with long delays (high capacitance) on the switches. Using the 5V 40A aupply to power the auduino plus the 2812 LED strip doesn’t help.

    The ripple on a scope (looking at the 40A power supply) is less than 50 mV. So far, capacitors haven’t helped.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: