Package com.diozero.devices
Class LDR
- java.lang.Object
-
- com.diozero.api.GpioDevice
-
- com.diozero.api.GpioInputDevice<AnalogInputEvent>
-
- com.diozero.api.AnalogInputDevice
-
- com.diozero.devices.LDR
-
- All Implemented Interfaces:
DeviceInterface
,DeviceEventConsumer<AnalogInputEvent>
,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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.diozero.api.AnalogInputDevice
AnalogInputDevice.Builder
-
-
Field Summary
-
Fields inherited from class com.diozero.api.GpioDevice
pinInfo
-
-
Constructor Summary
Constructors Constructor Description LDR(int gpio, float r1)
LDR(AnalogInputDeviceFactoryInterface deviceFactory, int gpio, float r1)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description float
getLdrResistance()
Read the resistance across the LDR.-
Methods inherited from class com.diozero.api.AnalogInputDevice
accept, addListener, addListener, close, convertToScaledValue, disableDeviceListener, enableDeviceListener, getRange, getScaledValue, getUnscaledValue, run
-
Methods inherited from class com.diozero.api.GpioInputDevice
addListener, hasListeners, removeAllListeners, removeListener
-
Methods inherited from class com.diozero.api.GpioDevice
getGpio
-
-
-
-
Constructor Detail
-
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 Detail
-
getLdrResistance
public float getLdrResistance() throws RuntimeIOException
Read the resistance across the LDR.- Returns:
- Resistance (Ohms)
- Throws:
RuntimeIOException
- If an I/O error occurred.
-
-