Test the Light Sensor Eyes

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!

  • Put the cyber:bot board's power switch in position 1.
  • Enter, save, and flash the script left_light_sensor.
# 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)   
  • 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!

  • Save a copy of the script 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.
  • Save your changes and flash the script to your micro:bit module.
  • 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.