knock sensor/How a knock becomes HIGH/04. What the capacitor does
How a knock becomes HIGH · 04 of 10

What the capacitor does

C1, 100 nF from SIGNAL to GND, charges the instant the spring touches and drains through the LED and the pull-down when it lets go. Worked out from the parts, that stretches each touch by a millisecond or less: enough to fill in a flicker of the contact, not enough to turn a knock into one long pulse.

Where it sits

C1 is the brown part at the right of the row of three, wired from SIGNAL to GND beside the pull-down. While the spring touches the rod, SIGNAL is joined straight to VCC, and C1 charges to VCC at once: nothing slows it but the contact itself. The interesting part is what happens when the spring lets go.

One touch, and what follows it

What the capacitor does
Your board
Touch
0.6 ms
HIGH for certain until
+0.07 ms
LOW for certain after
+1.1 ms
Calculated, not measured. While the spring touches, SIGNAL is at 3.3 V. When it lets go, the capacitor holds it up, but the LED drains it quickly down to about 2 V, in 0.19 ms, and then the 10 kΩ pull-down drains the rest with a 1 ms time constant. The ESP32-S3 is certain to read LOW again 1.1 ms after the touch ends. So the capacitor stretches each touch by a millisecond or less and fills in any gap shorter than that; it does not turn a knock into one long pulse.

The shaded strip is one touch, 0.6 ms long. When it ends, C1 is the only thing holding SIGNAL up, and two paths drain it. While SIGNAL is above about 2.0 V, the LED conducts, and C1 empties into the LED's 1 kΩ and the pull-down together, which is quick: about 0.1 ms is the time constant. Below 2.0 V the LED stops, and only the 10 kΩ pull-down is left: 100 nF times 10 kΩ is 1 ms, and the fall slows down.

Pick your board. The dashed lines are its own thresholds, from its datasheet: above the upper one it is certain to read HIGH, below the lower one certain to read LOW. Between them it could read either. On an Uno SIGNAL is back below the LOW line about half a millisecond after the touch; on the 3.3 V boards, whose LOW line is lower, about a millisecond.

What that means for a sketch

Each touch reaches your pin as a pulse a little longer than the touch, by a fraction of a millisecond. A contact that flickers open for less than that, as metal touching metal often does, is smoothed over and reads as one pulse.

But the spring's separate touches come a couple of milliseconds apart in the figure in the first article, and SIGNAL has long since fallen by the next one. So the capacitor does not make a knock into one pulse, and it does not make the pulses long. They are still short enough that a loop with a delay(100) in it misses most of them, and still several per knock. Both are for the sketch to handle, and the reading chapter does.

All of this is worked out, not measured: the LED is modelled as switching off sharply at 2.0 V, which a real one does not quite do.

When it does not work

Does the capacitor mean I do not need a hold-off in the sketch?

No. It holds SIGNAL up for a millisecond or less after each touch, and the spring's touches come further apart than that, so one knock still arrives as several pulses. Count the first and ignore the rest for about 100 ms, as the sketches in this book do.

Could a bigger capacitor make one knock one pulse?

It would stretch each touch, and a large enough one would join them up. It would also make SIGNAL fall slowly through the region where a pin reads neither HIGH nor LOW for certain, and it is a soldering job on a finished board. A hold-off in the sketch does the same job with nothing to change.

Are these numbers measured?

No. They are worked out from the parts' values, with the LED treated as a part that conducts above about 2.0 V and not at all below. A real LED tails off gradually, and a real X7R capacitor is within about 10 % of its value, so read them as how long, roughly, rather than exactly.

Where this goes next

Why the block's VCC pin goes to 3V3 beside an ESP32 and 5V beside an Uno.

VCC sets the voltage

Edit this page — content/books/knock-sensor/what-the-capacitor-does.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Knock Sensor

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum