RoboCircuits
Published © CC BY-NC-SA

Touch Switches for Home

Smart Home

IntermediateFull instructions provided2 hours2,288
Touch Switches for Home

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Grove - 2-Channel SPDT Relay
Seeed Studio Grove - 2-Channel SPDT Relay
×2
Capacitive Touch Sensor Breakout - MPR121
Adafruit Capacitive Touch Sensor Breakout - MPR121
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Touch Switches by RoboCircuits

Code

Touch Switches by RoboCircuits

Arduino
#include <EEPROM.h>

void ICACHE_RAM_ATTR button1();
void ICACHE_RAM_ATTR button2();

boolean buttonState1 = false, buttonState2 = false,

void button1()
{
  delayMicroseconds(100000);
  if(digitalRead(in1)==HIGH)                                     
  {
    buttonState1 =! buttonState1;
    digitalWrite(relay1,buttonState1);
    delayMicroseconds(200000);
    EEPROM.write(101,buttonState1);
    EEPROM.commit();
  }
}

void button2()
{
  delayMicroseconds(100000);
  if(digitalRead(in2)==HIGH)                                 
  {
    buttonState2 =! buttonState2;
    digitalWrite(relay2,buttonState2);
    delayMicroseconds(200000);
    EEPROM.write(102,buttonState2);
    EEPROM.commit();
  }
}

void setup()
{
  EEPROM.begin(512);
  Serial.begin(115200);
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode( in1, INPUT_PULLUP );
  pinMode( in2, INPUT_PULLUP );
  pinMode(LED_BUILTIN, OUTPUT);
  buttonState1 = EEPROM.read(101);
  buttonState2 = EEPROM.read(102);
  digitalWrite(relay1,buttonState1);
  digitalWrite(relay2,buttonState2);
  
  attachInterrupt(digitalPinToInterrupt(in1), button1, RISING);
  attachInterrupt(digitalPinToInterrupt(in2), button2, RISING);
  delay(100);
}

Credits

RoboCircuits
37 projects • 221 followers
YouTuber, Explorer, Creator, Programmer, Arduino Lover and Engineer
Contact

Comments

Please log in or sign up to comment.