American library books Β» Other Β» Make: Electronics by Charles Platt (read me a book .TXT) πŸ“•

Read book online Β«Make: Electronics by Charles Platt (read me a book .TXT) πŸ“•Β».   Author   -   Charles Platt



1 ... 113 114 115 116 117 118 119 120 121 122
Go to page:
switch or button attached to any of its input-capable pins. A 10K resistor must be used to pull down the state of the pin when the contact of the switch or button is open. Otherwise, you may get unpredictable results.

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,

1 ... 113 114 115 116 117 118 119 120 121 122
Go to page:

Free e-book: Β«Make: Electronics by Charles Platt (read me a book .TXT) πŸ“•Β»   -   read online now on website american library books (americanlibrarybooks.com)

Comments (0)

There are no comments yet. You can be the first!
Add a comment