Writing the Code

About: Learn electronics and Arduino with Tinkercad Circuits!

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Setting Up the Circuit

Lesson Overview:

Now we'll program our cap touch sensor!

Step 1: Introduction

In this lesson you will learn how to turn on the lamp using the touch sensor. Our Touchy Feely Lamp circuit is in the Workplane, so you can refer to it. However, since we don't have a conductive material or the CapacitiveSensor library available in the simulator -- you will have to program your physical Arduino Uno using IDE on your computer.

Let's get started!

  1. Continue to the next step.

Step 2: Preparing the CapacitiveSensor Library

The most recent version of the CapacitiveSensor library can be found here:


  1. Download the file to your computer and unzip it.

  2. Open your Arduino sketch folder (it will be in your “Documents” folder by default). In the folder, create a new directory named “libraries”.

  3. Place the CapacitiveSensor folder you unzipped in this folder and restart the Arduino software.

  4. Click the File>Examples menu in the Arduino software, and you’ll see a new entry for “CapacitiveSensor”. The library you added included an example project. Open the CapacitiveSensorSketch example and compile it. If you don’t get any errors, you’ll know you installed it correctly!

  5. Continue to the next step.

Step 3: Include the Library

Now that the library has been downloaded, you can include it in the program and create an instance of the library.

When you use this library, you tell the instance what pins it will be using to send and receive information. In this case, pin 4 sends a signal the conductive sensor material through the resistor, and pin 2 is the senses it.

  1. Copy the code below into Arduino IDE.

    CapacitiveSensor capSense = CapacitiveSensor(4,2);

  2. Continue to the next step.

Step 4: Write the Header

In the header of the code, set up a variable for the sensing threshold at which the lamp will turn on. You’ll change this number after you test the sensor’s functionality. You can think of the value 1000 as a place holder for now.

The last step in the header is to assign the LED pin:

  1. Copy the code below into Arduino IDE.

    int threshold = 1000; 
    const int ledPin = 12;

  2. Continue to the next step.

Step 5: Write the Setup() Function

The setup() function in this program is also very short. You will initialize a serial connection so you can watch the sensor values while using your project. Also set ledPin as an OUTPUT.

  1. Copy the code below into Arduino IDE.

    void setup(){ 
    	pinMode(ledPin, OUTPUT); 

  2. Continue to the next step.

Step 6: Sensing Touch

In the loop() function, create a variable to hold the sensor’s value. The function CapacitiveSensor()returns the sensor's value in arbitrary units. It takes an argument identifying the number of samples you want to read.

You'll also print the sensor value to the serial monitor.

  1. Copy the code below into Arduino IDE.

    void loop(){ 
    	long sensorValue = capSense.capacitiveSensor(30); 

  2. If you read only a few samples, you might see a lot of variation in the sensor. If you take too many samples, you could really slow the program down. 30 samples is a good starting value.

  3. Continue to the next step.

Step 7: Lamp Control

With an if()...else statement, check to see if the sensor value is higher than the threshold. If it is, turn the LED on. If it is not, turn it off.

Then add a small delay() before ending the loop().

  1. Copy the code below into Arduino IDE.

    	if(sensorValue > threshold){ 
    		digitalWrite(ledPin, HIGH); 
    	} else{ 
    		digitalWrite(ledPin, LOW); 

  2. Continue to the next step.

Step 8: Use It!

After programming the Arduino, you’ll want to find out what the sensor values are when the foil is touched.

Once you figure out a good threshold value for the sensor, the light will turn on when you touch the switch!

  1. Upload the sketch to your Arduino Uno. Make sure your foil or conductive object are attached to the circuit!
  2. Open the serial monitor and note the value coming from the sensor when you’re not touching it. This should look like a string of numbers, with one number per line.
  3. Press gently on your touch switch. The number in the Serial Monitor should increase. Try pressing more firmly and see if it changes.
  4. Once you know the range of values you’re getting from the sensor, go back to the sketch and change the threshold variable to a number that is greater than the sensor’s value when not touched, but less than its value when pressed.
  5. Upload the sketch with the new value. The light should come on reliably when you touch the wire, and turn off when it’s left alone!
  6. Continue to the next step.

Step 9: Think About It...

You probably noticed that the values from the sensor changed depending on how much of your finger was touching the sensor. The sensor changes its capacitance when your skin collects electrical charge. What would happen if you are wearing gloves while you try to interact with the sensor?

  1. Continue to the next step.

Step 10: Review

Congratulations on creating your first capacitive touch sensor! Capacitive sensing works by measuring the ability of your fingers to store electrical charge. In an Arduino sketch, the library CapacitiveSensor makes this possible.

Capacitive touch sensing is an interesting concept for electronic products because the switches don't have any moving parts, they are very thin, and (as you will see in the next lesson) they can be hidden under other materials.

  1. Can you think of any electronic devices in your classroom that use capacitive touch? Some laptop touch pads use this technique!
  2. Continue to the next lesson.

In the next lesson you will learn to actually build the lamp!

Next Lesson:Building a Lamp



    • Remix Contest

      Remix Contest
    • Faux-Real Contest

      Faux-Real Contest
    • Paper Contest

      Paper Contest