Light and Shadow

In this project we want to control the dimming of an LED using a light sensor.

Sensor: Light sensor or LDR (Light dependent resistor).

Actuator: LED

Description of interaction: The incoming analog values of the LDR should be mapped to the brightness of the LED. For example: If you cover the LDR with your hand more shadow is created and the analog values of the LDR rise up. Rising analog values should then increase the brightness of the LED light.

What the code should do: Measure the incoming values with value= anologRead(analog pin_lightsensor) and read them with Serial.Print(value) in the Serial Monitor. Define the LDR value range and map it to 0-255 standard brightness values of an LED.

Step by Step:

  • Correctly connect an LDR and an LED to your breadboard an Arduino.
  • Open the example code File>Examples>01.Basics>AnalogReadSerial. This code reads an analog value on pin A0 and prints it to the Serial Monitor. You should see values changing when covering the LDR with your hand.
  • Map these values to the brightness values of an LED (0-255): Use map() function see Arduino reference. Insert the map() function just right before delay(1).
  • The calculation is done, but we still need to tell the microcontroller how bright the LED should light up: analogWrite(LED, mapped value).