ENERGEN
Published

Toggle LED with Touch Sensor

How to use Touch Sensor with Arduino

BeginnerFull instructions provided261
Toggle LED with Touch Sensor

Things used in this project

Story

Read more

Schematics

Touch sensor with Arduino

Code

Toggle LED with TTP223 Capacitive Touch Sensor project

Arduino
const int SENSOR_PIN = 2; // the Arduino's input pin that connects to the sensor's SIGNAL pin 

// Variables will change:
int lastState = LOW;      // the previous state from the input pin
int currentState;         // the current reading from the input pin
int ledState = LOW;             // the current LED state

void setup() {
  pinMode(SENSOR_PIN, INPUT); // initialize the Arduino's pin as aninput
 
  pinMode(LED_BUILTIN, OUTPUT);  // initialize digital pin LED_BUILTIN as an output.
}

void loop() {
  // read the state of the the input pin:
  currentState = digitalRead(SENSOR_PIN);

  if(lastState == LOW && currentState == HIGH){
    // toggle LED state
    if(ledState == LOW)
      ledState = HIGH;
    else if(ledState == HIGH)
      ledState = LOW;

    // control LED
    digitalWrite(LED_BUILTIN, ledState);
  }

  // save the the last state
  lastState = currentState;
}

Credits

ENERGEN

ENERGEN

17 projects • 9 followers
Youtube creator

Comments