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

rcTime and Voltage Decay

When light levels are low, the rcTime function might take time measurements too large for int or even word variables to store.  The next step up in storage capacity is a long variable, which can store values from -2,147,483,648 to 2,147,483,647.  So, the function definition long rcTime(int pin) is set up to make the function return a long value when it’s done.  It also needs to know which pin to measure.

long rcTime(int pin)           

A charge transfer measurement takes seven steps: (1) Set the I/O pin high to charge the capacitor.  (2) Wait long enough for the capacitor to charge.  (3) Change the I/O pin to input.  (4) Check the time.  (5) Wait for the voltage to decay and pass below the Arduino’s 2.1 V threshold.  (6) Check the time again.  (7) Subtract the step-3 time from the step-6 time.  That’s the amount of time the decay took.

{                               
  pinMode(pin, OUTPUT);         // Step 1, part 1
  digitalWrite(pin, HIGH);      // Step 1, part 2
  delay(1);                     // Step 2
  pinMode(pin, INPUT);          // Step 3  part 1
  digitalWrite(pin, LOW);       // Step 3, part 2
  long time  = micros();        // Step 4  
  while(digitalRead(pin));      // Step 5
  time = micros() - time;       // Step 6 & 7
  return time;                  
}  

In this sketch, Step 1 has two sub-steps.  First, pinMode(pin, OUPUT) sets the I/O pin to an output, then digitalWrite(pin, HIGH) makes it supply 5 V to the circuit.  Step 3 also has two sub-steps, because the I/O pin is sending a high signal.  When the sketch changes the I/O pin’s direction from output-high to input, it adds 10 kΩ of resistance to the circuit, which must be removed.  Adding digitalWrite(pin, LOW) after pinMode(pin, INPUT) removes that resistance and allows the capacitor to drain its charge normally through the phototransistor.

Optional Advanced Topic: Voltage Decay Graphs

The graph below shows the BOE Shield-Bot’s left and right QT circuit voltage responses while the BothLightSensors sketch is running.  The device that measures and graphs these voltage responses over time is called an oscilloscope.  The two lines that graph the two voltage signals are called traces.  The voltage scale for the upper trace is along the left, and the voltage scale for the lower trace is along the right.  The time scale for both traces is along the bottom.  Labels above each trace show when each command in BothLightSensors executes, so that you can see how the voltage signals respond.

The upper trace in the graph plots the capacitor’s voltage in the pin 8 QT circuit; that’s the left light sensor.  In response to digitalWrite(8, HIGH), the voltage quickly rises from 0 V to almost 5 V at about the 1 ms mark.  The signal stays at around 5 V for the duration of delay(1).  Then, at the 2 ms mark, the rcTime call causes the decay to start.  The rcTime function measures the time it takes the voltage to decay to about 2.1 V and stores it in the tLeft variable.  In the plot, it looks like that decay took about 1 ms, so the tLeft variable should store a value close to 1000.

The lower trace in the graph plots the pin 6 QT circuit’s capacitor voltage—the right light sensor.  This measurement starts after the left sensor measurement is done.  The voltage varies in a manner similar to the upper trace, except the decay time takes about 2 ms.  We would expect to see tRight store a value in the 2000 neighborhood.  This larger value corresponds to a slower decay, which in turn corresponds to a lower light level.

 


Printer-friendly version
Test the Phototransistor Circuit
Prev
Activity 3: Light Measurements for Roaming
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