Looking to place your advertisement?

Advertise your brand and services to thousands of IT enthusiasts!

Using touch sensor with Arduino

  • avatar
  • 401 Views
  • 4 mins read

Touch sensor (also known as touch button) is widely used to control devices. It detects touch, force or pressure on its surface changing the logic state of the circuit. Actually, it works in very similar way as to buttons. Checkout the wiring and Arduino code implementation to track the current state and the state changes.

Components

arduino-nano

1x Arduino Nano (or another Arduino module)

Available on: Aliexpress · Banggood · Ebay

mini-breadboard

1x Mini-breadboard

Available on: Aliexpress · Banggood · Ebay

touch-sensor

1x Touch sensor

Available on: Aliexpress · Banggood · Ebay

dupont

Dupont wires

Available on: Aliexpress · Banggood · Ebay

Wiring schema

This example demonstrates the use of a touch sensor with Arduino Nano. By default, the signal value is LOW and it switches to HIGH when the touch sensor is pressed.

arduino touch sensor

Note: the button pin can be connected to Arduino Nano D4 or any other digital input pin.

Arduino code

We've defined a struct (called touch) to represent the current state of the touch sensor and the state in the previous loop iteration. The serial monitor will output a message when the sensor changes the state, i.e., is pressed, in real time.

#define BUTTON_PIN 4

struct touch {
byte wasPressed = LOW;
byte isPressed = LOW;
};

touch touch;

void setup()
{
pinMode(BUTTON_PIN, INPUT_PULLUP);

Serial.begin(115200);
}

void loop()
{
touch.isPressed = isTouchPressed(BUTTON_PIN);

if (touch.wasPressed != touch.isPressed) {
Serial.println("Touch pressed");
}

touch.wasPressed = touch.isPressed;
}

bool isTouchPressed(int pin)
{
return digitalRead(pin) == HIGH;
}

Note: any other digital PIN may be used.

Credits

Official GitHub: https://github.com/hibit-dev/touch

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

0 Comments

Leave a Reply

Your email address will not be published.

Replying to message:

Hey visitor! Register your account and get access to featured articles and more - it's free.