The A/D circuit for Park in the Dark works over a limited light range. You might get that circuit all nice and calibrated in one room, then take it to a brighter room and find that all the voltage measurements will stay above 0.1 V. Or, maybe you’ll take it into a darker room, and the voltages will end up never making it past 0.1 V.
Let's try a different kind of phototransistor circuit that the cyber:bot can use to measure a much wider range of light levels. This circuit and script can return values ranging from 0 to over 75,000. Just note: with this circuit, smaller values indicate bright light, and large values indicate low light, the opposite of our previous circuit. This new circuit uses a new component: a capacitor.
A capacitor is a device that stores an electrical charge. It is a fundamental building block of many circuits. Batteries are also devices that store charge, and for these activities it will be convenient to think of capacitors as tiny batteries that can be charged, discharged, and recharged.
How much charge a capacitor can store is measured in farads (F). A farad is a very large value that’s not practical for use with these cyber:bot circuits. The capacitors in your kit store fractions of millionths of farads. A millionth of a farad is called a microfarad, and it is abbreviated μF. This one stores one hundredth of one millionth of a farad: 0.01 μF.
Your cyber:bot kit's electronic component pack comes with two different capacitors. One is marked 103 and the other is marked 104. These marks are a measurement in picofarads. In this labeling system, the value is the number 10 followed by the specified number of zeros added.
For example, 103 is the number 10 with 3 zeros added capacitor’s case is a measurement in picofarads or (pF). In this labeling system, 103 is the number 10 with three zeros added, so the capacitor is 10,000 pF, which is 0.01 μF.
(10,000) × (1 × 10-12) F = (10 × 103) × (1 × 10-12) F
10 × 10-9 F = 0.01 × 10-6 F
= 0.01 μF.
The capacitor labeled 103 with the value of 0.01 µF is the value we will use next.
The circuits shown here can respond independently to the light level reaching each phototransistor. Each one naturally adapts to ambient light levels without having to swap out resistors. The phototransistors will be pointing upward at about 45°, one forward-left and the other forward-right. This way, a script monitoring the values of both phototransistor circuits can determine which side of the cyber:bot is exposed to brighter light. Then, this information can be used for navigation decisions.
(2) phototransistors
(2) capacitors, 0.01 μF (marked 103)
(2) resistors, 220 Ω (red-red-brown)
(2) jumper wires
The roaming examples in this chapter will depend on the phototransistors being pointed upward and outward to detect differences in light levels from different directions. Adjust the phototransistors to point upward at a 45° from the breadboard, and outward about 90° apart, as shown below.
Think of each capacitor in this circuit as a tiny rechargeable battery, and think of each phototransistor as a light-controlled current valve. Each capacitor can be charged to 3.3 V and then allowed to drain through its phototransistor. The rate that the capacitor discharges depends how much current the phototransistor (current valve) allows to pass, which in turn depends on the brightness of the light shining on the phototransistor’s base. Again, brighter light results in more current passing, shadows result in less current.
This kind of phototransistor/capacitor circuit is called a charge transfer circuit. The cyber:bot will determine the rate at which each capacitor loses its charge through its phototransistor by measuring how long it takes the capacitor’s voltage to decay, that is, to drop below a certain voltage value. The decay time corresponds to how wide open that current valve is, which is controlled by the brightness of the light reaching the phototransistor’s base. More light means faster decay, less light means slower decay.
QT Circuit — A common abbreviation for charge transfer is QT. The letter Q refers to electrical charge (an accumulation of electrons), and T is for transfer.
Connected in Parallel — The phototransistor and capacitor shown in the figure above are connected in parallel; each of their leads are connected to common terminals (also called nodes). The phototransistor and the capacitor each have one lead connected to GND, and they also each have one lead connected to the same 1 kΩ resistor lead.
The script left_light_sensor charges the capacitor in the P8 circuit, measures the time it takes for the capacitor to discharge, and displays that value on the micro:bit module’s LED matrix. Remember, with this circuit and script, lower numbers mean brighter light now!
# left_light_sensor from cyberbot import * while True: bot(8).write_digital(1) qt_left = bot(8).rc_time(1) display.scroll(str(qt_left)) sleep(500)
The other phototransistor circuit on the right side of the robot also needs testing!
These steps are important!
Your circuits and code must pass these tests before continuing. The rest of the examples in this chapter rely on both light sensors working correctly.
The charge transfer measurement is done with these two lines of code:
bot(8).write_digital(1) qt_left = bot(8).rc_time(1)
The first line, bot(8).write_digital(1), does two things. First, it charges the capacitor by setting Propeller I/O pin P8 to output high (3.3 volts). Then, it waits one millisecond for the capacitor to fully charge - more than enough time for this tiny one.
The next line, qt_left = bot(8).rc_time(1), is doing a lot more. The rc_time function:
Here is an example of a charge transfer measured with an oscilloscope, a device that displays voltage as it changes over time. The vertical axis on the left is volts, and time increases by milliseconds along the bottom.
The red line that’s graphing voltage is called a trace. The trace plots the capacitor’s voltage in the P8 QT circuit; that’s the left light sensor. In response to bot(8).write_digital(1), the voltage quickly rises from 0 V to almost 3.3 V at about the 1 ms mark, and stays there for 1 millisecond. Then, at the 2 ms mark, the rc_time call allows the voltage in the capacitor to start draining, or "decay." The rc_time function measures the time it takes the voltage to decay to the Propeller I/O pin's logic threshold of about 1.6 V. This measured rc decay time gets stored in the qt_left variable. In the plot, it looks like that decay took about 1 ms, so the qt_left variable would store a value close to 1000.
Keep in mind that these are not the voltage measurements from Activity 1, they are time measurements. When the voltage decay takes a short time the number will be small, and that means bright light. When the voltage decay takes a long time, the number will be larger, and that means dimmer light, shade, or for really large numbers, darkness.
Keep in mind that these are not the voltage measurements from Activity 1, they are time measurements. When the voltage decay takes a short time the number will be small, and that means bright light. When the voltage decay takes a long time, the number will be larger, and that means dimmer light, shade, or for really large numbers, darkness.