LED + photoresistor - mako

Go to content
Arduino > Tutorials
LED and photoresistor
A photoresistor is a intresting object. It is a electronic component whose it's resistance is inversley proportional to the amount of the light that hits it. This implies that the electric current that passes through this component is proportional to the intensity of a light source. In this way, a sort of potentiometer is created which can be activated by light rather than by mechanical forces or electrical signals.
In this tutorial a twilight switch is created, the classic application for photoresistors. Furthermore there is an implementation compared to the normal switch, the led not only turns on and off but the more light received by the photoresistor, the more light emitted by the led.
int ledPin = 3;
int PWMValue;
int sensorValue = 0;

void setup() {

void loop() {
sensorValue = analogRead(A0);
PWMValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, PWMValue);
Sketch analysis
int PWMValue: sets PWMValue as integer
int sensorValue : sets sensorValue as integer
sensorValue = analogRead(A0): reads the input on analog pin A0 and assign it to the sensorValue variable.
PWMValue = map(sensorValue, 0, 1023, 0, 255): maps sensor value to proportional PWM values. The sensor values range goes from 0 to 1023, the led values range goes from 0 to 255.
analogWrite(ledPin, PWMValue): writes PWMValue to digital pin 3, the intensity of the light will be proportional to the value read by PWMValue.

There are no reviews yet.
Back to content