ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 1676
Скачиваний: 0
Chapter 7: Digital to Analog the Easy Way using PWM· Page 139
Why did I learn it?
Using a simple RC circuit with a buffer is an inexpensive and easy way to get 8-bit DAC. It's also really easy to program, and the accuracy is much closer to that of a D/A converter integrated circuit than it is to the accuracy of the resistive ladder network with 10% resistors.
There are many instances in which analog quantities will change the resistance of a device. The voltage divider can be an effective way to track these changes in resistance. The measured voltage at the output of a voltage divider can give us information, not only about the resistance value, but about the analog quantity that causes the resistance value to change.
How can I apply this?
The optics industry has many applications where the levels of light are controlled and sensed. In the communications industry, fiber optic cables are used to send your phone conversation across long distances. In the robotics industry, sensing light is an important aspect of controlling the movement of the mechanical parts of the robot.
The application you're probably most familiar with is the remote control on your television set. When you point the remote control at your television and press a button to change the channel, that information is sent using light, which is received by a sensor on the front of your TV.
Many industrial processes need to use feedback to sense the level of light and adjust it to keep it in a particular range. In photography, many cameras depend on analog measurements taken by a light meter. These measurements can in turn be used to control the aperture of the shutter when taking a photograph. The aperture is how far the shutter opens to let light into the camera. In the next chapter, we'll build a light meter.
Chapter 8: Light Meter Gizmo with R/C Time Constants Page 141
Chapter #8: Light Meter Gizmo with R/C Time Constants
In this chapter, we'll be using concepts we learned in previous activities to develop a light meter gizmo. Here is the list of gizmo specifications:
•The gizmo should beep every half-second.
•When the light level in the room gets brighter, the gizmo should beep at a higher pitch.
•When the light level in the room gets dimmer, the gizmo should beep at a lower pitch.
This experiment introduces a way of using a BASIC Stamp I/O pin to measure variable resistance or capacitance. A number of sensors aside from the photoresistor vary in either resistance or capacitance. A simple and straightforward way of measuring these quantities can make interfacing with these sensors immensely easier.
In this experiment, we'll begin by building our gizmo, next we'll look more closely at the measurement technique to see how it can be used to monitor variable resistance or capacitance. Figure 8-1 shows a generic RC circuit that can be used to take RC time constant measurements.
Vdd |
|||||||||||||
C |
|||||||||||||
Figure 8-1 |
|||||||||||||
P15 |
RC Circuit |
||||||||||||
Ω |
|||||||||||||
220 |
For measuring |
||||||||||||
values of R or C |
|||||||||||||
R |
|||||||||||||
Vss
Page 142 · Basic Analog and Digital
In Chapter #7, the amount of time it takes a capacitor to charge in an RC circuit with a step input was introduced. The same principle applies here. A step will be applied to the circuit, and the amount of time it takes the capacitor to charge will be measured. Here's how it works.
The BASIC Stamp must be programmed to set P15 (the I/O pin we'll be using) high for a few milliseconds to let the voltage across the capacitor approach 0 volts. The reason the voltage across the capacitor approaches 0 volts is because Vdd is 5 volts at the positive terminal of the capacitor, and P15 is set to 5 volts at negative terminal. So, the voltage across the capacitor is 5-5 volts = 0 volts.
As soon as the voltage across the capacitor is close enough to 0 volts, P15 is set to function as an input. Immediately, the BASIC Stamp starts counting in 2 microsecond increments. As soon as P15 becomes an input, it doesn't have an output voltage of 5 volts any more. P15 behaves like an open circuit as far as the RC circuit is concerned.
All of the sudden, the circuit is different. It's a resistor and a capacitor in series. The capacitor starts with no charge, but the circuit has 5 volts across it (Vdd – Vss), so the capacitor starts to charge up. As the capacitor starts charging, the voltage at P15 starts falling. The voltage at P15 will fall until it crosses the BASIC Stamp's I/O pin threshold voltage. It takes a certain amount of time for the voltage at P15 to decay from 5 volts, to the 1.4 volt threshold voltage of P15.
The BASIC Stamp can be used to measure the amount of time it takes the output to drop from 5 to 1.4 volts, and this measurement can be used to determine an unknown capacitance or resistance.
The equation for the voltage at P15 is given by:
− t
VP15 = Vdd × e R×C
This equation is widely used in many fields, and it's called the exponential decay equation. In our case, it's an equation of exponentially decaying voltage. Figure 8-2 shows the decays of three RC circuits that differ only in resistance. Each of the voltage outputs takes a different amount of time to decay from 5 volts to 1.4 volts.
Chapter 8: Light Meter Gizmo with R/C Time Constants Page 143
Output |
5 |
V1 |
→ R1 = 5 kΩ |
|||||
Voltage |
||||||||
4 |
V2 |
→ R2 = 10 kΩ |
||||||
(Volts) |
||||||||
V3 |
→ R3 = 15 kΩ |
|||||||
3 |
||||||||
Vt |
= 1.4 V |
|||||||
2 |
V1 |
V2 |
V3 |
Vt |
||||
1 |
||||||||
0 |
||||||||
-0.02 |
-0.01 |
0 |
0.01 |
0.02 |
0.03 |
0.04 |
0.05 |
|
Input |
5 |
|||||||
Voltage |
4 |
|||||||
(Volts) |
||||||||
3 |
||||||||
2 |
||||||||
1 |
||||||||
0 |
||||||||
-0.02 |
-0.01 |
0 |
0.01 |
0.02 |
0.03 |
0.04 |
0.05 |
|
Time, (Seconds)
Figure 8-2
RC circuit
Responds
To a negative step input for three different resistance values. Note how each output takes a different amount of time to decay from 5 volts to 1.4 volts.
The difference in time it takes the three different circuit outputs to decay can be used to determine a resistance value if the capacitance value stays the same. Likewise, if the resistance is fixed but the capacitance varies, the capacitance can be determined by measuring the time between the step input and the threshold voltage.
A PBASIC command called RCTIME can be used to make the BASIC Stamp determine the amount of time it took the RC output to decay from 5 volts to 1.4 volts, which is the
value of t in the exponential decay equation. The value of t that gets measured is t1.4 Volts. The value of t1.4 Volts is the amount of time it takes for the voltage at I/O pin P15 to fall from 5 volts to 1.4 volts.
It turns out that the amount of proportional to R multiplied by C. represented by the Greek letter tau.
time it takes for the voltage to decay is directly R ´ C is called the RC time constant, and it is often
At the instant the voltage at P15 reaches 1.4 volts, the input value at pin P15 goes from 1 to 0, and the BASIC Stamp stops counting 2 us intervals.
Page 144 · Basic Analog and Digital
Parts Required
This experiment requires the following parts:
(1) 0.1 µF poly capacitor
(1) 1 µF electrolytic capacitors
(1) 10 µF electrolytic capacitor
(1) Piezoelectric speaker
(1) 220 resistor
(1) Photoresistor
Build It
Build the circuit shown in Figure 8-3 Note that the resistor in the circuit on the left uses the variable photoresistor. The circuit on the right is the circuit we used to play music in Chapter #5.
Vdd |
||
1 µF |
||
P15 |
P7 |
Figure 8-3 |
220 Ω |
Ambient Light Meter |
|
Circuit. |
||
Vss |
Vss |
||||
You will have to calibrate both your circuit and program due to varying light conditions. Do not point the photoresistor directly at the sun. We'll use the slot in the center of the breadboard as our light collector. Point the photoresistor at the slot as shown in Figure 8- 4.
Chapter 8: Light Meter Gizmo with R/C Time Constants Page 145
Vdd Vin Vss
X3
P15
P14
P13
P12
P11
P10
P9
P8
P7
P6
P5
P4
P3
P2
P1
P0
X2
Figure 8-4
Placement of the Photoresistor
Face the photoresistor into the slot in the middle of the breadboard. This way the photoresistor will detect the light reflected off the white breadboard. This will make the photoresistor less subject to wide resistance swings that can happen when the photoresistor is faced directly at bright light sources.
The PBASIC instruction set has a command called RCTIME, which measures the amount of time it takes a capacitor to discharge under the conditions just described. The format of the RCTIME command is:
RCTIME Pin, State, Result
As usual, Pin refers to a BASIC Stamp I/O pin. Referring to Figure 8-1, we are using pin P15. Now, what about State? The operator state is set to 1 when we are using the RC circuit shown in Figure 8-1. If we used an RC circuit instead, the state would be set to 0. The Result is a number of 2 microsecond time increments that the BASIC Stamp counted during discharge. How does this work?
First I/O pin 15 must be set high, to charge the capacitor to 5 volts. When the BASIC Stamp gets to the RCTIME command, it switches I/O pin 15 to input. The voltage starts to drop as shown in Figure 8-2. The BASIC Stamp counts the number of 2 microsecond increments between switching to input and the time the voltage at P15 crosses the 1.4 volt logic threshold. The number of 2 microsecond increments then gets saved as the result variable.
We'll use this segment of code in Program Listing 8.1 to measure the RC time. We will first wait 100 ms with pin P15 high. This gives the capacitor enough time to charge up.
HIGH 15
PAUSE 100
Page 146 · Basic Analog and Digital
Then RCTIME takes care of the time measurement and data storage.
RCTIME 15, 1, light
Program It
Enter Program 8.1 in your BASIC Stamp Editor, and download to the BASIC Stamp.
'Basic Analog and Digital - PL8_1R0.bs2
'Program Listing 8.1 Revision 0.
'{$STAMP BS2}
'{$PBASIC 2.5}
light |
VAR |
Word |
dischargeTime |
VAR |
Word |
sound |
VAR |
Word |
a |
CON |
40 |
DO |
||
HIGH 15 |
||
PAUSE 100 |
||
RCTIME 15, 1, |
light |
|
dischargeTime |
= light |
* 2 |
sound = (65535 - light) / a |
||
FREQOUT 7, 500, sound |
||
DEBUG HOME |
||
DEBUG "Discharge time |
= ", DEC5 dischargeTime, " u-seconds." |
|
DEBUG CR, CR, |
"Sound Frequency = ", DEC5 sound, " Hz." |
|
PAUSE 500 |
||
LOOP |
||
The Output
The piezoelectric speaker should beep every half second or so. If you put your hand over the circuit to shade it, the tone of the beep should get lower. If you face your breadboard directly at a light in the room, the pitch should get higher. Figure 8-5 a sample output for a well-lit room.
Chapter 8: Light Meter Gizmo with R/C Time Constants Page 147
Figure 8-5
Sample Debug Terminal Output in a Well-lit Area with no Direct Sun
Figure 8-6 shows a sample output with a book casting a shadow over the light meter. The tone emitted by the speaker is audibly lower
Figure 8-6
Sample Debug
Terminal Output in
Shade