What Next? 307
Experiment 35: Checking the Real World
Now we need a program to tell the chip what to do. Using the Programming
Editor, start a new document. The code should look like this:
main:
readadc 2,b0
debug b0
goto main
The command “readadc 2,b0” means “read the analog input on Logic Pin 2,
convert from analog to digital, and store the result in b0.”
The command “debug b0” tells the chip to go into program debugging mode,
in which it uses its USB cable to tell the Programming Editor the values of all
the variables while the program is running. The variables are displayed in a
debugging window.
Download the program, and as the program starts to execute, the debugging
window should open. Start adjusting the trimmer while looking at the value of
b0, and you’ll see b0 change its value.
You can make a table and draw a graph showing the relationship between the
resistance between Logic Pin 2 and ground, and the value of b0. Just pull the
trimmer off the breadboard, measure its resistance with a meter, then increase
its resistance by, say, 200Ω, put it back into the breadboard, and look at the
value of b0 again.
This is laborious, but calibrating equipment is always laborious—and in any
case, I decided to do it for you. The graph is shown in Figure 5-146. You can
also see the raw data numbers in the following table. I was pleased to find that
the PICAXE gives a very precise, linear response to the input voltage. In other
words, the graph is a straight line.
Figure 5-146. When an ADC input pin is hooked up to a 2K po-
tentiometer, which is connected across the same voltage that
powers the chip, you should find that the resistance between
the input pin and the negative side of the power supply gener-
ates the series of digital values shown on the graph. Note that
the potentiometer must have a 2K value, and the power supply
is assumed to be precisely 5 volts.
2000
1500
1000
500
(^102030405060708090100110120130140150160170180190200210220230240250)
Equivalent Value Created by PICAXE Analog-Decimal Converter
Resistance in Ohms Between Pin and Negative Ground