book collections email follower instructable user

Step 2: Wiring Up and Testing the Phototransistor

Picture of Wiring Up and Testing the Phototransistor

Normally, you would need a resistor in series with the phototransistor across power, but I knew that the Arduino had the ability to enable an internal pull-up resistor on any of the pins. I suspected that I could take advantage of that to hook up the phototransistor to the Arduino without any additional components. It turned out my hunch was correct!

I used wires to connect the phototransistor to the GND and A5 pins on the Arduino. I then created a sketch that set the A5 pin as an INPUT_PULLUP. This is normally done for switches, but in this case it provides power to the phototransistor!

#define SENSOR A5
void setup() {
  Serial.begin(9600);
  pinMode(SENSOR, INPUT_PULLUP);
}
void loop() {
  // Read analog value continuously and print it
  Serial.println(analogRead(SENSOR));
}

This sketch prints values to the serial port corresponding to the ambient brightness. By using the handy "Serial Plotter" from the "Tools" menu of the Arduino IDE, I can get a moving plot of ambient light! As I cover and uncover the phototransistor with my hands, the plot moves up and down. Nice!

This sketch is a nice way to check whether the phototransistor is wired up with the right polarity: the phototransistor will be more sensitive when hooked up one direction versus the other.