Lab 6: Capacitive Sensing with Piezo

Yuetian Wang
1 min readOct 12, 2021

--

Yuetian Wang. Professor Kimiko Ryokai. INFO C262, Fall 2021

Part 1

Descript

I use honeydew as the capacitive sensor to blink the LED next to pin 13

Video

Part 2

Descript

I tried to use aluminum foil to blink the LED in pin 13 and the piezo Speaker. And I found that piezo speaker will make different tones when I touch different parts of the aluminum foil.

Code

#include “CapacitiveSensor.h”

/*
* Makes the built in LED next to pin 13 blink when the capacitive sensor
* reaches the threshold value
* Noura Howell 2018
*/

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

// You may need to adjust this value to work for your particular sensor.
// Use the capacitive sensing test sketch to get a sense of the range of values
// coming from your capacitive sensor.
int threshold = 100;
int ledPin=13;
int speakerPin = 10;
int toneVal;
int noteDuration=10;
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 — just as an example
pinMode(ledPin, OUTPUT);
pinMode(speakerPin,OUTPUT);
Serial.begin(9600);
}

void loop()
{
long capVal = cs_4_2.capacitiveSensor(30);

Serial.println(capVal);
toneVal = capVal*6;

if ( capVal > threshold ) {
digitalWrite(ledPin, HIGH);
tone(speakerPin, toneVal, noteDuration);

} else {
digitalWrite(LED_BUILTIN, LOW);
noTone(8);
}
delay(10);
}

Video

Component

1-Arduino

1-Breadboard

1-LED Light

1- 220 ohm resistor

1-Piezo Speaker

1- 10k ohm resistor

1-aluminum foil

1–10M ohm resistor

Wires

--

--