Open an editor (nano or IDLE) and paste in the following code. As with all the program
examples in this book, you can also download the program from the Code section of
import RPi.GPIO as GPIO
a_pin = 18
b_pin = 23
while not GPIO.input(b_pin):
count = count + 1
When you run the program, you should see some output like this:
$ sudo python pot_step.py
The reading will vary between about 10 and about 170 as you rotate the knob of the
To explain how this program works, I first need to explain how the step response technique
can be used to measure the resistance of the variable resistor.
This way of doing things is called step response because it works by seeing how the circuit
responds from the step change when an output is switched from low to high.
You can think of a capacitor as a tank of electricity, and as it fills with charge, the voltage
across it increases. You can’t measure that voltage directly, because the Raspberry Pi
doesn’t have an ADC converter. However, you can time how long it takes for the capacitor
to fill with charge to the extent that it gets above the 1.65V or so that constitutes
a high digital input. The speed at which the capacitor fills with charge depends on the
value of the variable resistor (Rt). The lower the resistance, the faster the capacitor fills
with charge and the voltage rises.