A capacitive touch sensor you can run in your browser
Category: Engineering
A touch pad can detect a finger. The touch pad measures how long a small electrical charge takes to build up. The circuit is simple. The circuit uses two microcontroller pins, a large resistor, and a conductive pad. The important part is the connection between the charge curve and the firmware. The firmware reads that curve.
This LabWired lab makes that connection visible. An Arduino Nano runs compiled CapacitiveSensor firmware against simulated electronics. Press the touch pad. Watch the oscilloscope. Read the number that the firmware sends over Serial.
Try the touch pad
The frame below is an interactive lab. Press and hold the touch pad with your mouse or your finger. The D13 LED should light. Release the touch pad. Watch the D13 LED turn off again. If the lab is paused, press Run first.
The oscilloscope tool shows the two probe channels. Open Serial to see the firmware total= readings.
The circuit
The Nano D4 pin sends the charge signal. A 1 MΩ resistor connects D4 to the touch-pad node. D2 also connects to that node. D2 is the receive pin.
| Connection or part | Value and role |
|---|---|
| D4 → R1 → D2 / touch pad | 1 MΩ resistor between the send and receive pins |
| Touch pad → ground | 20 pF pad capacitance |
| Switched finger branch | An additional 100 pF connects while you hold the touch pad |
| D13 → resistor → LED → ground | 330 Ω series resistor for the indicator LED |
| Scope CH1 | D2 / touch-pad voltage |
| Scope CH2 | D4 / send-pin voltage |
The model includes the microcontroller pin direction and drive state. The firmware can discharge the receive node. The firmware can release the receive node as an input. The firmware can then measure the response of the node to the send pin. When you hold the touch pad, the electrical model changes. The same firmware observes this model.
What the firmware measures
The sketch uses the open-source CapacitiveSensor library pinned to commit aa0184827c. The sketch adapts the library example. The sketch uses one sensor and a D13 indicator.
The central call is:
CapacitiveSensor cs_4_2(4, 2);
long total = cs_4_2.capacitiveSensor(30);
The library changes the pin states. The library counts the work for the receive pin to cross its input threshold. The sketch prints the result. The sketch compares the result with an LED threshold.
Observed runs in this lab showed released readings around 5. The same runs showed held readings around 3,042. The library produces these counts. The counts measure time. These counts depend on the circuit. These counts depend on the library settings. These counts depend on the simulated execution. The useful observation is the clear change between the two states.
Read the waveform
CH2 shows the send pin as that pin changes state. CH1 shows the touch-pad voltage through R1. With the additional finger capacitance connected, the touch pad takes longer to charge. That longer transition gives the firmware a larger count.
You may see a wider gap after every 30 measurements. The sketch prints the result to Serial between batches. A comparison run removed those prints. That gap became shorter. The recorded samples stayed continuous. The oscilloscope shows the time that the firmware uses between measurements.
Try 500 µs/div to inspect a wider time window. A change of timebase changes the oscilloscope display. A change of trigger changes the oscilloscope display. The simulation continues to run. You can compare the charge curve with the Serial readings. You do not need to restart the sketch.
Change the firmware decision
The sketch starts with:
const long THRESHOLD = 1524;
Open the full lab to try a source change. Sign in to compile your edits. Find this constant in Source. Change 1524 to 4000. Build the edited sketch. Run the firmware from the build.
The circuit stays the same. The readings stay the same. After this change, the LED should stay off while you hold the touch pad. A reading near 3,042 is below 4,000. The count should increase when you press the touch pad. The count should decrease when you release the touch pad. Serial should still show the count.
Restore 1524. Build the sketch again. Run the firmware to get the original LED response.
This experiment separates the measurement from the decision. The firmware makes that decision from the result. The simulated electronics still respond to the touch pad. Your compiled firmware decides whether that response is large enough to light the LED.
What this demonstrates
The compiled Arduino firmware runs on a simulated ATmega328P. The GPIO activity of the firmware drives the analog circuit model. The result voltage feeds back into the firmware through D2. The LED output comes from that firmware. The Serial output comes from that firmware. This example shows the firmware and the circuit in use together. This example does not validate a physical touch sensor. This example does not validate every Arduino library. Real touch-pad geometry can change the readings. The wire installation can change the readings. Real noise can change the readings. The connection of a person to ground can change the readings.