Example script: left_light_sensor
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!
- Set the cyber:bot board's PWR switch to 0.
- Use the micro:bit Python Editor to open a previous cyber:bot project, like halt_under_shadow or cyberbot-template-with-blink.
- Set the project's name to left_light_sensor, update the script so that it matches the one below, and then click Save.
- Click Send to micro:bit.
# 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)
- Set the cyber:bot board's PWR switch to position 1.
- Point your cyber:bot towards the room's light source, then shade the left phototransistor with your hand.
- Watch the micro:bit module's display. It should be showing numbers between 0 and 77,000, with brighter light making for smaller numbers. If not, check wiring and rerun the program.
Your Turn
The other phototransistor circuit on the right side of the robot also needs testing!
- Change the project name from left_light_sensor as right_light_sensor.
- Change the both of the bot(8) commands to bot(6).
- Change the variable qt_left to qt_right in both places.
- Click the Save button to save your changes.
- Click Send to micro:bit.
- Set the cyber:bot board's PWR switch to position 1.
- Make sure the micro:bit module’s display is changing its numbers as you point the right phototransistor towards the light source and then shade it with your hand.
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.