Skip to content
Parallax Learn (Stage)

Parallax Learn (Stage)

This is the staging site. Please see https://learn.parallax.com for the official site.

  • 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 Tutorial Series
          • Cybersecurity: Radio Data tutorialCybersecurity Tutorial Series
          • cyber:bot Tutorial Series
          • Boe-Bot Tutorial SeriesBoe-Bot Tutorial Series
          • Arduino Shield-Bot Tutorial Series
          • ActivityBot with C TutorialsActivityBot with C Tutorials
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot with BlocklyProp Tutorial Series
          • Scribbler 3 Tutorial SeriesScribbler 3 Tutorial Series
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education Tutorial Series
          • Propeller C-Language BasicsPropeller C Basics and Projects
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit C Tutorial Series
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit BlocklyProp Tutorials
          • Badge WX Tutorial SeriesBadge WX Tutorial Series
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics and Projects
          • 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
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Robotics with the Board of Education Shield for Arduino

Robotics with the Board of Education Shield for Arduino

Test the Phototransistor Circuit

The sketch LeftLightSensor charges the capacitor in the pin 8 QT circuit, measures the voltage decay time, and displays it in the Serial Monitor.  Remember, with this circuit and sketch, lower numbers mean brighter light.

We’ll be using this light-sensing technique for the rest of the chapter, so you can  take the BOE Shield-Bot from one room to another without having to worry about finding the right resistors for different ambient light levels.

  • If there is direct sunlight shining in through the windows, close the blinds.
  • Enter and upload LeftLightSensor, and open the Serial Monitor.
  • Make a note of the value displayed in the Serial Monitor.
  • If the Serial Monitor does not display values or seems to get stuck after just one or two, it may mean that there’s an error in your circuit.  If you see these symptoms, check your wiring and try again. 
  • Use your hand or a book to cast a shadow over the pin 8 phototransistor circuit.
  • Check the measurement in the Serial Monitor again.  The value should be larger than the first one.  Make a note of it too.
  • If there’s no output to the Serial Monitor, or if it is just stuck at one value regardless of light level, there could be a wiring error.  Double-check your circuit (and your code too, if you hand-entered it.)
  • Move the object casting the shadow closer to the top of the phototransistor to make the shadow darker.  Make a note of the measurement.
  • Experiment with progressively darker shadows, even cupping your hand over the phototransistor.  (When it’s really dark you may have to wait a few seconds for the measurement to finish.)
/*
 * Robotics with the BOE Shield - LeftLightSensor
 * Measures and displays microsecond decay time for left light sensor.
 */

void setup()                                 // Built-in initialization block
{
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone

  Serial.begin(9600);                        // Set data rate to 9600 bps
}  
 
void loop()                                  // Main loop auto-repeats
{
  long tLeft = rcTime(8);                    // Left rcTime -> tLeft
 
  Serial.print("tLeft = ");                  // Display tLeft label
  Serial.print(tLeft);                       // Display tLeft value
  Serial.println(" us");                     // Display tLeft units + newline

  delay(1000);                               // 1 second delay
}
                                             // rcTime function at pin  
long rcTime(int pin)                         // ..returns decay time
{                                            
  pinMode(pin, OUTPUT);                      // Charge capacitor
  digitalWrite(pin, HIGH);                   // ..by setting pin ouput-high
  delay(1);                                  // ..for 5 ms
  pinMode(pin, INPUT);                       // Set pin to input
  digitalWrite(pin, LOW);                    // ..with no pullup
  long time  = micros();                     // Mark the time
  while(digitalRead(pin));                   // Wait for voltage < threshold
  time = micros() - time;                    // Calculate decay time
  return time;                               // Return decay time
}  

Your Turn: Test the Other Phototransistor Circuit

Before moving on to navigation, you’ll need to run the same test on the right (pin 6) light sensor circuit. Both circuits have to be working well before you can move on to using them for navigation—there’s that subsystem testing again!

  • In the rcTime call, change the pin parameter from 8 to 6.
  • Change all instances of tLeft to tRight.
  • Run the sketch, and verify that the pin 6 light sensor circuit is working.

It would also be nice to have a third sketch that tests both phototransistor circuits. 

  • Re-save the sketch as BothLightSensors, and update the comments.
  • Replace the loop function with the one below.   
  • Try rotating your BOE Shield-Bot until one side is pointing toward the brightest light source in the room and the other is pointing away from it. What is the largest difference you can get between tLeft and tRight in the Serial Monitor?
void loop()                                  // Main loop auto-repeats
{
  long tLeft = rcTime(8);                    // Left rcTime -> tLeft
  Serial.print("tLeft = ");                  // Display tLeft label
  Serial.print(tLeft);                       // Display tLeft value
  Serial.print(" us      ");                 // Display tLeft units

  long tRight = rcTime(6);                   // Left rcTime -> tRight
  Serial.print("tRight = ");                 // Display tRight label
  Serial.print(tRight);                      // Display tRight value
  Serial.println(" us");                     // Display tRight units + newline

  delay(1000);                               // 1 second delay
}

Printer-friendly version
Building the Photosensitive Eyes
Prev
rcTime and Voltage Decay
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn (Stage) • Built with GeneratePress