Experiment 35: Checking the Real World
Often we want a microcontroller to measure something and respond in an appropriate way. For instance, it can measure a low temperature and sound an alarm, as I suggested in the example that I gave earlier.
The PICAXE has three analog-to-digital converters (ADCs) built in, accessible via logic pins 1, 2, and 4, as shown in Figure 5-139. The best way to use them is by applying a potential somewhere between 0 and 5 volts. In this experiment, I’ll show you how to calibrate the response of the chip.
You will need:
Trimmer potentiometer, 2K. Quantity: 1.
PICAXE 08M chip and associated USB cable and socket. Quantity: 1 of each.
Procedure
Take the same trimmer potentiometer that you used in Experiment 32 and wire its center terminal to Logic Pin 2 of the PICAXE (which is hardware pin 5). The other two terminals of the 2K trimmer go to positive and to negative, respectively. So depending how you set the trimmer, the pin of the PICAXE is directly connected to positive (at one end of the scale), or directly connected to negative (at the other end of the scale), or somewhere in between. See Figure 5-144 for the revised schematic, and Figure 5-145 for a photograph of the breadboarded circuit.
Figure 5-144. This schematic, drawn in a layout suitable for breadboarding, shows how a 2K potentiometer can be used to apply a varying voltage to one of the pins of the PICAXE that is capable of converting an analog signal to a digital value.
Figure 5-145. The trimmer potentiometer added to the previously breadboarded circuit.
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 potentiometer, 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 generates 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.
This table shows measurements made with PICAXE 08M controller.
Resistance (in ohms) between the ADC pin and the negative supply
Equivalent digital value
2000
255
1900
243
1800
230
1700
218
1600
205
1500
192
1400
179
1300
166
1200
154
1100
141
1000
128
900
115
800
102
700
90
600
77
500
64
400
51
300
38
200
26
100
13
0
0
Now we can modify the program to make it do something with the information that it’s taking in:
main:
readadc 2,b0
let w1 = 5 * b0
high 1
pause w1
low 1
pause w1
goto main
Notice what’s happening here. First we get a value in b0, and then on the next line, we do some arithmetic with it. The asterisk means “multiply.” So the statement says, “Take whatever value is in b0, multiply by 5, and transfer it to another variable, w1.” We have to use a w variable, because when we multiply the value of b0 by 5, we may get a number that is bigger than 255—too big to fit into a byte variable.
Finally, we take variable w1 and use it with a “pause” statement instead of a fixed number value. We’re saying to the PICAXE, “pause for whatever number of microseconds you get by checking the value of w1.”
So the software checks a variable resistance, turns it into a number, and applies that number to adjust the flashing speed of the LED.
Think back to the need of the cart powered by stepper motors. It was supposed to check two photoresistors, and adjust the speed of each motor accordingly. Well, this PICAXE program is a step in that direction. It can measure voltage on a pin and change the output frequency on another pin. If you had two PICAXE chips, you could wire each of them to a photoresistor and a motor. Then you could adjust the behavior of your cart by editing the second line in the program, where it converts the value of b0 to the value of w1 which will be used in the “pause” command to determine the number of pulses per second. Instead of multiplying by 5 you could multiply by 7 or whatever number gives you the result you need. This leads to an important conclusion: a big advantage of a programmable chip is that you can make adjustments in software.
Because the PICAXE 08M actually has more than one ADC input, and has three pins that can be used for output,