1. Home
  2. ESP-8266
  3. Sensors
  4. Light Sensor (LDR)

Light Sensor (LDR)

The most simple light sensor is a light dependent resistor (LDR). It costs almost nothing, and serves its purpose. The following drawing shows how to connect an LDR to the analog input port from the ESP. You need the pulldown resistor (10kΩ in our case) in order to get a valid signal. Values are between 0 (total darkness) and 1023 (bright).

Required Module

In order to use the LDR sensor, you must compile the ADR module into your firmware.

The following code snippet shows how to use a timer to repeatedly read the light value every 5 seconds and print it to the console.

-- continuously poll light intensity --
tmr.alarm(4,5000,tmr.ALARM_AUTO, function() 
    local light_value = adc.read(0)
    print("Light value: " .. light_value)         
end)
Updated on February 9, 2017

Was this article helpful?

Related Articles