Make 1 LED dim depending on the light measured by the light sensor.
Arduino Uno
Breadboard
1x LED
1x 220 Ω resistor
1x 10k Ω resistor
1x CDS sensor
Wires
To use a light sensor it is required to use on of the analog pins (A0, A1, A2, A3, A4 or A5). The value from analog pin is a value between 0 and 1023, where 0 is 0 Volts and 1023 is 5 Volts.
0 volts
2.5 Volts
5 Volts
⟶
⟶
⟶
0
512
1023
⟶
⟶
⟶
0
128
255
Most of the times we might need to use the value read in a different scale, so we can use it for example to turn on a LED gradually depending on the light sensor value.
Make sure than when you selected a pin for the LED, is on of the pins with a ~ symbol, which allow to send not just HIGH and LOW values, but in between values, from 0 to 255, where 0 is LOW and 255 is HIGH.
The code will be very simple.
Prepare pin A0 as input to be able to read values from the ligth sensor.
Prepare pin 9 as ouput to be able to change the
The value of pin A0 will be saved on a variable sensorValue, using the function analogRead.
Afterwards will map a value from the sensor to a value between 0 and 255, so we can control the intensity of the LED. The result of that mapping will be saved on the variable bright.
Use the function analogWrite to change the brightness of the LED connect to pin 9.
Use the serial monitor to view the values sent by the sensor.