Arduino: How to Use the Touch Sensor

Click to see full image

Note: It is highly recommended that you read the older arduino posts if you are a beginner so you can understand the concepts more easily.

Parts NeededIMG_0509

  • arduino uno board
  • touch sensor
  • computer with the arduino software(arduino IDE)
  • male to female wires that are compatible with arduino
  • a wire that connects the arduino board to the computer(it should come with the arduino board if you order a kit)

The Code

//LED turns on if sensor detects something
const int LED=13; //the led is on pin 13
const int SENSOR=2; //the sensor is on pin 2

int val=0; //val is used to store the input of the sensor

void setup() {
pinMode (LED,OUTPUT);//led is an output
pinMode (SENSOR,INPUT);//sensor is an input
}

void loop() {
val=digitalRead(SENSOR);//read and store the value of sensor

if (val==(LOW)) {
digitalWrite(LED,HIGH);//if sensor is on, turn on led
}

else{
digitalWrite(LED,LOW);//if not, keep led off
}
}
//note: if sensor is low, it detects something

 

Understanding the Code

The first part of the code says const int LED = 13; and const int SENSOR = 2;. This sets up the variables, and the values do not change through the code. LED is now pin 13, and SENSOR is now pin 2.

The next part of the code is int val = 0; this gives the variable val a value; 0. However, since there is no const(constant) before the int(integer), the value of val can change throughout the code. Val is used to store the input of the sensor. This means that we will use val to store the value of the sensor.

In void setup, we are setting the variables as inputs or outputs. The LED is an output, and the SENSOR is an input.

In void loop, the code inside will run for an infinite time. Val = digitalRead(SENSOR). digitalRead(SENSOR) programs the board to read and store the value of SENSOR. Val now equals that value. Now, we can use val to represent the value of the sensor.

The last part of the code is an if/else statement. If val = LOW(the sensor is touched), turn on the led. else, if val is not equal to LOW, keep the led off.

Setting Up the BoardIMG_0512[1]

On the touch sensor, there should be three pins labeled G,V, and S. These stand for ground, voltage, and signal. Connect the ground pin on the touch sensor to one of the ground pins on the power section of the arduino board using a male-female wire. Connect the voltage pin to the 5v pin on the power section of the arduino board. 5v stands for five volts. Connect the signal pin to whatever value you gave the variable IMG_0519[1]SENSOR; in this case, connect the signal pin to pin 2 on the arduino board. You have now connected the touch sensor to the arduino board.

 

 

IMG_0513[1]

Connect the led by gently inserting the longer side of the LED into the pin that is the same value you gave the variable, LED; in this case, the value is 13. Connect the shorter side into the pin right next to it, which is another ground pin. Remember: if  you gave the LED a different value, you need to connect it with wires. This is because the shorter side of any LED must always go into ground. You should avoid stretching the LED to make the shorter side fit into ground, because this might damage the LED.

Running the Program 

To run this program, you need to click on the check button(verify). This checks the program for any mistakes or errors. Once there are no mistakes in your program, click on the button next to it(upload). This uploads the program onto the arduino board. This program will stay on the board until you upload another program.

Once you have uploaded the program, the LED should turn on when you put your finger on the touch sensor. If the program does not work, check your program again to make sure it matches mine.

Leave a comment