If you are interested in adding the ‘magic’ of touch to control your electronics project, a capacitive touch sensor module might be the way to go. Our touch sensor module makes it very easy to integrate capacitive touch sensing into you project. In this tutorial we a going to use a touch sensor module to toggle the status of an led.
to follow along in this tutorial, you will need to have the following
- capacitive touch sensor
- Arduino uno rev3 (or any Arduino board)
- Warefab tft touchscreen
- Jumper wires
- An led
- 220 Ohm resistor
Once you have all the required components its time to wire our circuit.
– Connect signal pin from the touch sensor to digital pin 7 on the Arduino
– Connect the positive lead of the LED to digital pin 8 on the Arduino
– Lastly, supply power to the circuit.
Connecting the components
Arduino code to use in our project
#define sensor_pin 7 //sensor conencted to pin 7
#define led_pin 8 //sensor connected to
uint8_t state = false; //boolean to save button state
pinMode(led_pin, OUTPUT); //set led pin as output
pinMode(sensor_pin, INPUT); //set sensor_pin as input
if (digitalRead(sensor_pin) == LOW) //check if the touch sensor has been touched
state = !state; //toggle the value of the boolean state
//as long as the sensor is activated stay in the loop
digitalWrite(led_pin, state); //set led on/off when a touch event occurs
How the Code works
We set our signal pin as digital input pin on the Arduino board
Also we set our LED positive pin as digital output pin on the Arduino board
in our loop method we check if the sensor has been touched, if a touch event occurs, the ‘state’ variable toggles, which we have used to control our LED to either turn it ON or OFF.
You may note we have a ‘while’loop in our code, this is to make sure only one touch event occurs only when we touch and release.
if (digitalRead(sensor_pin) == LOW)
state = !state;
Run the Code
this project can be used as a simple smart switch in a project.
with the inclusion of the display you can get the status of multiple switches in a customised interface