Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Visible Light Navigation for the cyber:bot

Visible Light Navigation for the cyber:bot

Measuring Phototransistor Voltage

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.

Hardware Setup

  • Set the cyber:bot board’s power (PWR) switch to Position 0.
  • Make sure the battery holder is loaded with 5 AA batteries.
  • Make sure the battery holder’s barrel plug is firmly plugged into the cyber:bot board’s barrel jack. 
  • Connect your micro:bit module to your computer with a USB cable.

Software Setup

  • In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
  • Make sure the cyberbot.py module is added to the Project Files.
    (See Add modules to your micro:bit).

Example script: phototransistor_voltage

  • Set the project’s name to phototransistor_voltage, enter the script below, and then click Save.
    (See Save & Edit Scripts and Flash Scripts with Python Editor.)
  • Click Send to micro:bit.  
    (See Flash Scripts with Python Editor.)
  • Set the cyber:bot board’s PWR switch to 1.
# 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.

  • Look at the display, and record the value you see for the ambient light in the room.
  • Use your hand to cast shade on the phototransistor, and watch the display change.  Brighter light should cause larger voltage values and dimmer light should cause smaller voltages.  Record the value for the room’s ambient light.
  • If you have a flashlight, shine it at the phototransistor and watch the display change again, and record the value.
  • If the ambient light is bright, you may need to replace the 2 kΩ resistor with a smaller value.  Try 1 kΩ, 470 Ω, or even 220 Ω, and be sure to record your values.
  • If the ambient light is low, you may need to change the 2 kΩ resistor to 4.7 kΩ, or even 10 kΩ, and be sure to record your values.

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.

Setting PWR switch to 0 vs. Unplugging Battery Holder   

Taking a break, building a circuit, or writing a script?    

  • Just set the cyber:bot board’s PWR switch to 0 until it’s time to test the next script.

If you are done for the day, always follow these steps.

  • Set PWR to 0.
  • Unplug the battery holder’s barrel plug from the cyber:bot board’s barrel jack.

Printer-friendly version
Simple Light to Voltage Sensor
Prev
Park in the Dark
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress