Maker.io main logo

How to Use a Phototransistor with an Arduino

2022-11-30 | By Maker.io Staff

A phototransistor is an active semiconductor component that can detect various light levels with fast response times. The response time of the phototransistor is 1 - 10 microseconds (µs). The response curve of a phototransistor has a broad wavelength range, from the near-ultraviolet, visible, and near-infrared parts of the electromagnetic spectrum. Therefore, the phototransistor can be used in various light detection circuits and devices.

How to Use a Phototransistor with an Arduino A typical phototransistor

The phototransistor’s wavelength spectrum is shown below.

How to Use a Phototransistor with an Arduino Phototransistor wavelength spectrum

A noteworthy characteristic of the phototransistor wavelength chart above is the nonlinear shape curve. This nonlinear output response allows the phototransistor to respond to various light levels expediently.

There are lots of applications that could use phototransistors, for example, a sunrise alarm clock. Upon detecting the sun’s light rays, the phototransistor may be used to provide an audible or visual alarm alerting the user that it is morning. But how do we wire a phototransistor to an Arduino to create a Light Activated LED fader effect?

Wiring a Phototransistor to an Arduino Uno

Building a light-activated LED fader effect requires wiring a phototransistor to an Arduino. The two primary leads of the phototransistor are the collector (C) and the emitter (E). The phototransistor C-lead will be wired to the Arduino Uno’s +5V pin, while the phototransistor's E-lead will be wired in series to the Arduino Uno’s ground (GND) through a 10KΩ fixed resistor. Where the two components are attached to each other, a jumper wire will be used to provide a continuously varying voltage signal to the Arduino Uno’s analog-to-digital converter (ADC) on pin A0. The ADC pin reads the various voltage levels produced by the phototransistor. A visual indicator consisting of a series current-limiting resistor wired to a blue LED is attached to digital pin D9 of the Arduino Uno. See below for the light-activated LED fader wiring diagram.

How to Use a Phototransistor with an Arduino The Light Activated LED fader electrical circuit wiring diagram

The electrical wiring diagram labeled the key termination points, the blue LED, and the phototransistor leads. You can use the schematic diagram below as you’re working on this:

How to Use a Phototransistor with an Arduino Scheme-It Light Activated LED fader circuit schematic diagram

Your circuit should look similar to the below design when you’re done:

How to Use a Phototransistor with an Arduino Functional Light Activated LED fader device

Light Activated LED fader code

With the wiring complete, upload the Light Activate LED fader code to the Arduino Uno. Open the Arduino Integrated Development Environment (IDE) and use the code shown below. The blue LED’s intensity will align appropriately, depending on the ambient light level. Experiment with various light levels using external light sources with the movement of your hand.

Copy Code
// Setup serial port baud rate and define INPUT and OUTPUT pins
void setup()
{Serial.begin(9600)}
 pinMode(blueSensorPin, INPUT);
 pinMode(blueLEDPin, OUTPUT);
}
// Read phototransistor light levels, display raw and converted analog values.
// Produce varying intensity LED levels
void loop()
{
 blueSensorValue = analogRead(blueSensorPin);
 delay(5);
 Serial.print("Raw Sensor value \t Blue:");
 Serial.println("blueSensorValue");
 //delay(1000);
 blueValue = blueSensorValue/4;
 
 Serial.print("Mapped Sensor Value \t Blue:");
 Serial.println(blueValue);
 //delay(1000);
 analogWrite(blueLEDPin, blueValue);
}
 The relevant Arduino Uno code

The following video clips show the light-activated LED fader circuit in operation.

Blue LEDs response to a hand moving over the phototransistor:

Phototransistor’s response to an external light source:

Happy visual lighting effects experimenting!

Recommended Reading

SparkFun Simple Sketches - Photo Interrupter

How to Measure Voltage in a Circuit

Grove Beginner Kit for Arduino – All-in-one Arduino Compatible Board with 10 Modules and 12 Projects

制造商零件编号 1540052NA3090
WL-TTRW THT PHOTOTRANSISTOR ROUN
Würth Elektronik
¥3.54
Details
制造商零件编号 A000073
ARDUINO UNO SMD R3 ATMEGA328
Arduino
¥182.01
Details
制造商零件编号 CF14JT10K0
RES 10K OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
¥0.81
Details
制造商零件编号 CFS1/4CT52R221J
RES 220 OHM 5% 1/4W AXIAL
KOA Speer Electronics, Inc.
¥0.81
Details
制造商零件编号 151033BS03000
LED BLUE CLEAR 3MM ROUND T/H
Würth Elektronik
¥2.19
Details
制造商零件编号 BB-32621
BREADBOARD TERM STRIP 3.20X2.08"
Bud Industries
¥40.44
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum