Arduino 101: Using a Capacitive Touch sensor to control an LED

Words
904
Reading
5 min
Listen
Play
9y

What Will I Learn?

  • You will learn what a touch sensor is and how it works.
  • You will learn how to use a touch sensor to control an LED.
  • You will learn to integrate the touch sensor with the arduino uno board and LED.

Requirements

Hardware

  • Arduino Uno

image.png

Image source

  • Capacitive Touch Sensor switch module

image.png

Image source

  • LED

image.png

Image source

  • Resistor (220 ohms)

image.png

Image source

  • Breadboard

image.png

Image source

  • Jumper wires

image.png

Image source

  • USB type A to B cable

image.png

Image source

  • Computer

image.png

Image source

Software

  • Arduino Software/Arduino IDE

Knowledge

  • Basic electronics and programming knowledge

Difficulty

  • Basic

Project description

  • This is a simple project for arduino beginners who want to know the basic operation of a capacitive touch sensor switch module and how it can be implemented with an arduino board. The touch sensor is connected to the arduino uno board as the input and LED is the output. Once the touch sensor pad is touched by your hand it will sense the electrical capacitance of your hand which will trigger the switch thus turning the LED ON and when your hand is removed the LED will turn OFF.

Component description

Arduino Uno - a type of arduino board that is used regularly by specialist and hobbyist because of its robust design. It is equipped with a microcontroller board that is based on ATMega328P with 14 advanced I/O pins (6 are PWM outputs), 6 analog inputs, 16 Mhz quartz crystal, a power jack, a USB port, an ICSP header and a reset button.

Capacitive touch sensor switch module - This type of device uses your body as part of the circuit. Once the touch sensor pad is touched by your hand or any part of your body, the capacitance of the circuit is changed and the sensor will detect this. The detected change in the capacitance results in the output changing the states of the device.
This module consists of three pins, VCC for the voltage source, GND for the ground and SIG for the signal. The sensor pad is located in the center of the module and it also has a built in LED.

image.png

Image source

Tutorial Contents

Using a Capacitive Touch Sensor to control an LED

Step 1: Gather the Parts

  • You can purchased the components in the local electronics shop or you can order it up online.

image.png

Step 2: Construct the circuit

Connect the Sources

  • Connect the 5V pin of the arduino uno board into the top/bottom slot of the breadboard. The connections here are horizontally connected making it an ideal spot for the sources.
  • Connect the GND pin of the arduino uno board into the top/bottom slot of the breadboard but make sure not to short them out to avoid damaging the board.

image.png

Connect the LED

  • Connect the anode(+) of the LED to a 220 ohm resistor that is connected to pin number 2 of the arduino uno board. This resistor will protect the LED from over supply of current.
  • Connect the cathode(-) of the LED to the common ground.

image.png

Connect the Sensor

  • Connect the VCC pin of the touch sensor to the voltage source terminal(+) in the breadboard.
  • Connect the GND pin of the touch sensor to the common ground terminal(-) in the breadboard.
  • Connect the SIG pin of the touch sensor to pin number 4 of the arduino uno board.

image.png

Step 3: Programming

  • Connect the arduino uno board to the computer using the usb type a to b cable.

image.png

Image source

  • Once connection is established, open the arduino IDE and go to Tools > Board: > then select Arduino/Genuino Uno.

image.png

  • Copy the codes into you own sketch in the arduino IDE.
#define touchpin 4 // sets the capactitive touch sensor @pin 4
int ledPin = 2; // sets the LED @pin 2
void setup() {
  pinMode(touchpin, INPUT); //sets the touch sensor as input
  pinMode(ledPin, OUTPUT);  //sets the led as output
}
void loop() {
  int touchValue = digitalRead(touchpin); //reads the touch sensor signal
  if (touchValue == HIGH){      //if sensor is HIGH
    digitalWrite(ledPin, HIGH);   //LED will turn ON
  }
  else{       //otherwise
    digitalWrite(ledPin,LOW); //LED is turned OFF
  } 
  delay(300);   //delay of 300milliseconds
}

This is a very simple code for controlling the LED using the touch sensor. In the first code block, it sets the pins of the touch sensor and the LED. Then is sets the touch sensor pin as the INPUT and the led pin as the OUTPUT. In the loop command, it sets the touchValue to read the signal of the touch sensor pin, then if the value is HIGH, the LED will also be HIGH, thus turning it ON. Otherwise, if the sensor value is LOW, the LED would be also LOW and turned OFF.

  • After typing the code in your own sketch, click the Verify button to save and compile the sketch. This will also check for any errors in the program.

image.png

  • Once the program is saved and no errors where found, click the Upload button to start installing the program into the arduino uno board.
    image.png

Step 4: Testing

  • After the the programming is finished, unplug the arduino uno board from the computer, then connect it with a battery pack to power up the arduino.
  • Now, after the arduino uno boots up and is ready, press the touch pad of the capacitive touch sensor using your finger. This should trigger the switch and will turn the LED ON.
  • Remove your finger from the touch pad, this should turn the LED OFF.
  • If nothing is working, redo the previous steps and make sure to follow the steps carefully and check your wirings.

Curriculum

Here are my other arduino tutorials that you might find interesting:



Posted on Utopian.io - Rewarding Open Source Contributors