Make a capacitive sensor with CapBoard

Reading Time: 2 minutes

In this example we present how to realize a capacitive sensor using CapBoard and any conductive object.

The object to be used as sensor must be connected to one of the nine CapBoard ports. In the following code we have set the “SensorPin” variable with the value of the port used (D2-D8, D10, D11), while the “Emitter Pin” variable must always remain set to 12.

For more information about how this type of sensor works we recommend to visit the Arduino page dedicated to capacitive sensors.

/* Capacitive Sensor Test for CapBoard Turns on and off a LED
connected to the port defined by the variable LedPin according
to the state of a capacitive sensor connected to the port 
defined by the variable SensorPin. 

To use this sketch you need to install the CapacitiveSensor library. 
Follow this link to download the library:
http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense 

This example code is in the public domain. 

Artis Lab - 2016 
www.artislab.it 
info@artislab.it */ 

#include <CapacitiveSensor.h>

boolean isTriggered1=false; 
int Threshold=300; //Change this value to calibrate the sensor 
//Use the serial monitor to find the value provided by your sensor 
int Samples=30; 
int LedPin=13; 
int SensorPin=2; 
int Emitter=12; 
CapacitiveSensor CapSensor = CapacitiveSensor(Emitter,SensorPin); 

void setup(){ 
     pinMode(LedPin,OUTPUT); 
     //uncomment the next line if you want to enable the serial
     //communication with the laptop 
     Serial.begin(9600); 
} 

void loop(){ 
     long total1=CapSensor.capacitiveSensor(Samples); 
     //uncomment the next line if you want to send the 
     //sensor data to the laptop over the serial communication, 
     //comment this line if you want to send the sensor state 
     //over the serial with Serial.write!!! 
     Serial.println(total1); 
     if(total1>Threshold) { 
           if(!isTriggered1) { 
               digitalWrite(LedPin,HIGH); 
               //Uncomment the following line to send data over the serial 
               //Serial.write(1); //Sensor State 
               //Serial.write(206); //Sensor ID 
               isTriggered1=true; 
               //Uncomment the following line to print the state of the sensor 
               //Don't use it togheter with Serial.write!!! 
               //Serial.println("CapSens-On"); 
           }
     } else { 
           if(isTriggered1) { 
               digitalWrite(LedPin,LOW); 
               //Uncomment the following line to send data over the serial 
               //Serial.write(0); //Sensor State 
               //Serial.write(206); //Sensor ID 
               isTriggered1=false; 
               //Uncomment the following line to print the state of the sensor 
               //Don't use it togheter with Serial.write!!! 
               //Serial.println("CapSens-Off"); 
           } 
     } 
} 

If you have any problems, curiosities and suggestions please feel free to contact us.

Security Code:
security code
Please enter the security code:

Submit

Leave a Reply

Your email address will not be published. Required fields are marked *