How it works

This capture from an oscilloscope shows whats happening on the digital pin (yellow). The blue line indicates when the Pi starts counting and when the counting is complete, about 4.5ms later.

This is because the capacitor acts like a bucket and the resistor is like a thin pipe. To fill a bucket up with a very thin pipe takes enough time that you can figure out how wide the pipe is by timing how long it takes to fill the bucket up halfway

In this case, our 'bucket' is a 1uF ceramic capacitor. You can change the capacitor nearly any way you want but the timing values will also change. 1uF seems to be an OK place to start for most sensors. If you want more range, use a bigger cap - but it will take longer to measure. For faster reads, go with a smaller capacitor
This guide was first published on Jul 29, 2012. It was last updated on Jul 29, 2012. This page (How it works) was last updated on May 04, 2015.