Add a capacitive sensor to your Arduino and make your Arduino work as a keyboard.
Materials
- Arduino Uno or Leonardo (use a Leonardo if you want to use it as a keyboard on your laptop)
- Breadboard
- Jumpercables
- 1㏁ resistor
- Crocodile clip
- Something conductive (metal/aluminium foil/conductive paint/copper tape/…)
Hardware
Use the materials and circuit diagram to set up the hardware. The pins 2 and 12 are chosen random, but they should match the pins in your code. The yellow cable labeled ‘TOUCH’ can be connected with a crocodile clip to whatever conductive material you want to use.
Code
Start up the Arduino IDE and open the Library manager:
Search for the CapacitiveSensor library, hit install and restart the Arduino IDE.
Use the following code to make the built-in LED flash for a moment. The example code also has lines for two other capacitive sensors on pins 4 and 5.
#include <CapacitiveSensor.h>
// Set the Send Pin & Receive Pin.
CapacitiveSensor cs_12_2 = CapacitiveSensor(12, 2);
//CapacitiveSensor cs_12_4 = CapacitiveSensor(12,4);
//CapacitiveSensor cs_12_5 = CapacitiveSensor(12,5);
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// Set the sensitivity of the sensor.
long touch1 = cs_12_2.capacitiveSensor(1000);
// long touch2 = cs_12_4.capacitiveSensor(1000);
// long touch3 = cs_12_5.capacitiveSensor(1000);
// When we touched the sensor, the built-in LED is turn on
if (touch1 > 1000) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a moment (1/10 second)
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}