Imagine that your cyber:bot is navigating a course, and there’s a dark or shady area at the end, such as a cardboard box garage. Your robot’s final task in the course is to stop inside that dark area. Or, perhaps you want the robot to search for and stop under bright light. There’s a simple phototransistor circuit you can use that lets the micro:bit module know it detected bright or dim light by comparing the ambient light to some specified level.
Ambient means ‘existing or present on all sides’ according to Merriam Webster’s dictionary. For the light level in a room, think about ambient light as the overall level of brightness.
(1) phototransistor
(2) jumper wires
(1) resistor, 2 kΩ (red-black-red)
(1) cardboard box "garage" that fits the cyber:bot
(1) Optional - a flashlight or desk lamp, incandescent works best
After some testing, and depending on the light conditions, you might end up replacing the 2 kΩ resistor with one of these resistors, so keep them handy:
(1) resistor, 220 Ω (red-red-brown)
(1) resistor, 470 Ω (yellow-violet-brown)
(1) resistor, 1 kΩ (brown-black-red)
(1) resistor, 4.7 kΩ (yellow-violet-red)
(1) resistor, 10 kΩ (brown-black-orange)
The drawing below will help you tell apart the phototransistor and infrared LED, since they look similar.
The schematic and wiring diagram below show the schematic and wiring diagram of a circuit very similar to the ones in streetlights that turn on automatically at night. The circuit outputs a voltage that varies depending on how much light shines on the phototransistor. The cyber:bot will monitor the voltage level with one of its analog to digital pins.
In the circuit you just built, a wire connects A/D2 to the row where the phototransistor’s emitter and resistor meet. The voltage at this part of the circuit will change as the light level sensed by the phototransistor changes. The phototransistor_voltage script measures the voltage at A/D2— one of the micro:bit module ’s three analog to digital input channels—and scrolls that value on the micro:bit module’s display. You will use this sketch to take and write down voltage readings of the ambient light, shade cast by your hand, and optionally of bright light from a flashlight if you have one handy.
Example script: phototransistor_voltage
# phototransistor_voltage from cyberbot import * while True: ad2 = pin2.read_analog() volts = ad2 * (3.3/1024) out = "volts = " + str(volts) + "V" display.scroll(out, 80) sleep(500)
Measurements will vary with ambient light levels, but in general, brighter measurements will tend toward 3.3, and darker ones will tend toward 0 V. If you point the phototransistor at the sun or a bright flashlight, the measurements should be close to 3.3 V. As you block the light and cast darker shade, the measurements should decrease.
Once you have a resistor value in place that works well for differentiating ambient light and shade in your area, you are ready for the next step.
The script halt_under_shadow will make the cyber:bot go forward until the phototransistor detects a shadow that’s dark enough to make the voltage applied to D/A2 drop below 0.1 V, or a different threshold you choose.
#halt_under_shadow from cyberbot import * bot(18).servo_speed(75) bot(19).servo_speed(-75) while True: ad2 = pin2.read_analog() volts = ad2 * (3.3/1024) if volts < 0.1: # Update voltage threshold value here bot(18).servo_speed(None) bot(19).servo_speed(None)
The micro:bit module’s A/D0, A/D1, and A/D2 sockets are connected to its Nordic chip's pins that are configured for analog to digital conversion. Analog to digital conversion, called "A to D" and abbreviated A/D, is how microcontrollers measure voltage. The microcontroller splits a voltage range into many numbered, equal divisions, with each number representing a voltage level. More divisions provide a higher resolution, with more precise voltage measurements.
Each of the micro:bit module’s analog inputs has a 10-bit resolution, meaning that it uses 10 binary digits to describe its voltage measurement. With 10 binary digits, you can count from 0 to 1023; that’s a total of 1024 voltage levels if you include zero.
By default, the cyber:bot’s read_analog function is configured to use the 0…1023 values to describe where a voltage measurement falls in a 3.3 V scale. If you split 3.3 V into 1024 different levels, each level is 3.3/1024ths of a volt apart. 3.3/1024ths of a volt is approximately 0.00322266 V, or about 3.22 thousandths of a volt. So, to convert a value returned by read_analog to a voltmeter-style value, all the volts function has to do is multiply by 3.3 and divide by 1024.
Example: The read_analog function returns 742; how many volts is that?
Answer:
V = 742 x 3.3 V / 1024
= 2.39121094 V
= 2.39 V
The previous scripts have been storing the read_analog function values as the variable ad2. This variable is stored as an integer since it returns the values between 0 and 1023. It is then multiplied by 3.3 and divided by 1024 to get the voltage value (just like we converted 742), and this is being stored as the float variable volts.
ad2 = pin2.read_analog()
volts = ad2 * (3.3/1024)
The script phototransistor_voltage displays the value returned by volts with the display.scroll function.
The script halt_under_shadow script uses that value to bring the cyber:bot to a stop when it detects a shadow (if volts < 0.1).
Binary vs. Analog and Digital
A binary sensor can transmit two different states, typically to indicate the presence or absence of something. For example, a whisker sends a high signal if it is not pressed, or a low signal if it is pressed.
An analog sensor sends a continuous range of values that correspond to a continuous range of measurements. The phototransistor circuits in this activity are examples of analog sensors. They provide continuous ranges of values that correspond to continuous ranges of light levels.
A digital value is a number expressed by digits. Computers and microcontrollers store analog measurements as digital values. The process of measuring an analog sensor and storing that measurement as a digital value is called analog to digital conversion. The measurement is called a digitized measurement. Analog to digital conversion documents will also call them quantized measurements.
A resistor “resists” the flow of current. Voltage in a circuit with a resistor can be likened to water pressure. For a given amount of electric current, more voltage (pressure) is lost across a larger resistor than a smaller resistor that has the same amount of current passing through it. If you instead keep the resistance constant and vary the current, you can measure a larger voltage (pressure drop) across the same resistor with more current, or less voltage with less current.
The micro:bit module’s analog inputs are invisible to the phototransistor circuit. So, a circuit plugged into the A/D2 socket on the cyber:bot board is monitored by the micro:bit, but the micro:bit has no affect on the circuit.
Take a look at the circuit below. With 3.3 volts (3.3 V) at the top and GND (0 V) at the bottom of the circuit, 3.3 V of electrical pressure (voltage) makes the supply of electrons in the cyber:bot’s batteries want to flow through it.
The reason the voltage at A/D2 (VAD2) changes with light is because the phototransistor lets more current pass when more light shines on it, or less current pass with less light. That current, which is labeled I in this circuit, also has to pass through the resistor. When more current passes through a resistor, the voltage across it will be higher. When less current passes, the voltage will be lower. Since one end of the resistor is tied to GND = 0 V, the voltage at the VAD2 end goes up with more current and down with less current.
If you replace the 2 kΩ resistor with a 1 kΩ resistor, VAD2 will see smaller values for the same currents. It will take twice as much current to get VAD2 to the same voltage level, which means the shadow will have to be twice as dark to reach the 0.1 V level, which is the default voltage in the script halt_under_shadow that makes the cyber:bot stop.
So, a smaller resistor in series with the phototransistor makes the circuit less sensitive to light. If you instead replace the 2 kΩ resistor with a 10 kΩ resistor, VAD2 will be 5 times larger with the same current, and it’ll only take 1/5th the light to generate 1/5th the current to get VAD2 past the 0.1 V level. So, a larger resistor makes the circuit more sensitive to light.
Connected in Series — When two or more elements are connected end-to-end, they are connected in series. The phototransistor and resistor in this circuit are connected in series.
Two properties affect the voltage at VAD2: current and resistance, and Ohm’s Law explains how it works. Ohm’s Law states that the voltage (V) across a resistor is equal to the current (I) passing through it multiplied by its resistance (R). So, if you know two of these values, you can use the Ohm’s Law equation to calculate the third:
V = I x R
Voltage (V) is measured in units of volts, which are abbreviated with an upper-case V. Current (I) is measured in amperes, or amps, which are abbreviated A. Resistance (R) is measured in ohms which is abbreviated with the Greek letter omega (Ω). The current levels you are likely to see through this circuit are in milliamps (mA). The lower-case m indicates that it’s a measurement of thousandths of amps. Similarly, the lower-case k in kΩ indicates that the measurement is in thousands of ohms.
In some textbooks, you will see E = I × R instead. E stands for electric potential, which is another way to say “volts."
Let’s use Ohm’s Law to calculate VAD2 with the phototransistor, letting two different amounts of current flow through the circuit:
The examples below show the conditions and their solutions. When you try these calculations, remember that milli (m) is thousandths and kilo (k) is thousands when you substitute the numbers into Ohm’s Law.
Example 1: I = 0.92 mA and R = 2 kΩ
Example 2: I = 0.25 mA and R = 2 kΩ
Let’s say that the ambient light in your room is twice as bright as the light that resulted in VA3 = 3.1 V for bright light and 0.5 V for shade. Another situation that could cause higher current is if the ambient light is a stronger source of infrared. In either case, the phototransistor could allow twice as much current to flow through the circuit, which could lead to measurement difficulties.
Question: What could you do to bring the circuit’s voltage response back down to 3.1 V for bright light and 0.5 V for dim?
Answer: Cut the resistor value in half; make it 1 kΩ instead of 2 kΩ.
Does it bring VA3 back to 3.1 V for bright light and 0.5 V for dim light with twice the current? (It should; if it didn’t for you, check your calculations.)