Example Sketch: PhototransistorVoltage
The PhototransistorVoltage sketch makes the Serial Monitor display the voltage measured at A3—one of the Arduino’s five analog input channels that are accessible through the BOE Shield. In the circuit you just built, a wire connects A3 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 Serial Monitor screencapture below shows some example voltage measurements.
- Reconnect programming cable and battery pack power to your board.
- Put the BOE Shield’s power switch in position 1.
- Enter, save, and upload the PhototransistorVoltage sketch to your Arduino.
- Slowly move the flashlight or lamp over the phototransistor, and watch the value of A3 in the Serial Monitor. Brighter light should cause larger voltage values and dimmer light should cause smaller voltages.
- If the ambient light is brighter than just fluorescent lights, and you have a bright flashlight, you may need to replace the 2 kΩ resistor with a smaller value. Try 1 kΩ, 470 Ω, or even 220 Ω for really bright lights.
- If the ambient light is low, and you are using a fluorescent desk lamp bulb or an LED flashlight for your bright light, you may need to change the 2 kΩ resistor to 4.7 kΩ, or even 10 kΩ.
- Record values for ambient light (your normal room light levels), and then bright light, like when you shine a flashlight right on the phototransistor. Make a note of them—you’ll need them for the sketch after this one.
/* * Robotics with the BOE Shield - PhototransistorVoltage * Display voltage of phototransistor circuit output connected to A3 in * the serial monitor. */ void setup() // Built-in initialization block { Serial.begin(9600); // Set data rate to 9600 bps } void loop() // Main loop auto-repeats { Serial.print("A3 = "); // Display "A3 = " Serial.print(volts(A3)); // Display measured A3 volts Serial.println(" volts"); // Display " volts" & newline delay(1000); // Delay for 1 second } float volts(int adPin) // Measures volts at adPin { // Returns floating point voltage return float(analogRead(adPin)) * 5.0 / 1024.0; }