Class LDR

All Implemented Interfaces:
DeviceEventConsumer<AnalogInputEvent>, Closeable, AutoCloseable, Runnable, Consumer<AnalogInputEvent>

public class LDR
extends AnalogInputDevice

Generic Photoresistor / Light-Dependent-Resistor (LDR).
Wiring:

 vRef     vLDR       GND
   |        |         |
   +---R1---+---LDR---+
 

When there is a lot of light the LDR has low resistance (vLdr ~ 0V). When it is dark the resistance increases (vLdr ~ vRef).

vLDR = vRef * (rLDR / (rLDR + R1))

Given R1 = 10,000ohm and vRef = 5v:
When dark, if rLDR == 100,000 Ohm: vLDR = 5 * (100,000 / (100,000 + 10,000)) = 4.54V
When light, if rLDR == 100 Ohm: vLDR = 5 * (100 / (100 + 10,000)) = 0.0495V

rLDR = R1 / (vRef / vLDR - 1)

Given R1 = 10,000ohm and vRef = 5v:
When dark, if vLDR=4V: rLDR = 10,000 / (5 / 4 - 1) = 40,000 Ohm
When light, if vLDR=1V: rLDR = 10,000 / (5 / 1 - 1) = 2,500 Ohm

  • Constructor Details

    • LDR

      public LDR​(int gpio, float r1) throws RuntimeIOException
      Parameters:
      gpio - GPIO to which the LDR is connected.
      r1 - Resistor between the LDR and ground.
      Throws:
      RuntimeIOException - If an I/O error occurred.
    • LDR

      public LDR​(AnalogInputDeviceFactoryInterface deviceFactory, int gpio, float r1) throws RuntimeIOException
      Parameters:
      deviceFactory - Device factory to use to construct the device.
      gpio - GPIO to which the LDR is connected.
      r1 - Resistor between the LDR and ground.
      Throws:
      RuntimeIOException - If an I/O error occurred.
  • Method Details

    • getLdrResistance

      public float getLdrResistance() throws RuntimeIOException
      Read the resistance across the LDR.
      Returns:
      Resistance (Ohms)
      Throws:
      RuntimeIOException - If an I/O error occurred.