> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/wokwi/wokwi-docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Photoresistor (LDR) Sensor

> Photoresistor (LDR) sensor module for measuring light intensity

Photoresistor (LDR) sensor module

## Pin names

<ParamField path="VCC" type="Power">
  Positive power supply
</ParamField>

<ParamField path="GND" type="Ground">
  Ground
</ParamField>

<ParamField path="DO" type="Digital Output">
  Digital output
</ParamField>

<ParamField path="AO" type="Analog Output">
  Analog output
</ParamField>

## Attributes

<ParamField path="lux" type="string" default="500">
  Initial light value (lux)
</ParamField>

<ParamField path="threshold" type="string" default="2.5">
  Digital output threshold voltage
</ParamField>

<ParamField path="rl10" type="string" default="50">
  LDR resistance @ 10lux (in kilo-ohms)
</ParamField>

<ParamField path="gamma" type="string" default="0.7">
  Slope of the log(R) / log(lux) graph
</ParamField>

## Operation

The photoresistor sensor module includes a LDR (light-dependant resistor) in series with a 10K resistor. The AO pin is connected between the LDR and the 10K resistor.

The voltage on the AO pin depends on the illumination - that is the amount of light that falls on the sensor. You can read this voltage by connecting the AO pin of the photoresistor sensor to an analog input pin and then using the `analogRead()` function.

There are two parameters that control the sensitivity of the LDR: rl10 and gamma. rl10 is the resistance of the LDR at illumination level of 10 lux. The gamma value determines the slope of the log(R) / log(lux) graph. You can usually find these two values in the datasheet of the LDR.

The following table shows the relationship between the illumination level (lux), resistance (R), and the voltage level
on the AO pin when gamma = 0.7 and rl10 = 50 (the default values):

| Condition            | Illumination (lux) | LDR Resistance | Voltage\* | analogRead() value |
| -------------------- | ------------------ | -------------- | --------- | ------------------ |
| Full moon            | 0.1                | 1.25MΩ         | 4.96      | 1016               |
| Deep twilight        | 1                  | 250kΩ          | 4.81      | 985                |
| Twilight             | 10                 | 50kΩ           | 4.17      | 853                |
| Computer monitor\*\* | 50                 | 16.2kΩ         | 3.09      | 633                |
| Stairway lighting    | 100                | 9.98kΩ         | 2.50      | 511                |
| Office lighting      | 400                | 3.78kΩ         | 1.37      | 281                |
| Overcast day         | 1,000              | 1.99kΩ         | 0.83      | 170                |
| Full daylight        | 10,000             | 397Ω           | 0.19      | 39                 |
| Direct sunlight      | 100,000            | 79Ω            | 0.04      | 8                  |

\* When VCC = 5V\
\*\* Measured one meter away from the monitor

The following code to convert the return value of `analogRead()` into a illumination value (in lux):

```cpp theme={null}
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;

// Convert the analog value into lux value:
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
```

The `lux` variable will contain the illumination level in lux. The value of `lux` may be infinite (`inf`) when the sensor is in a very bright environment. You can use `isfinite(lux)` to check if the value is finite before using it, like in [this example](https://wokwi.com/projects/361196415746754561).

## Digital output

The digital output ("DO") pin goes high when it's dark, and low when there's light. On the physical sensor, you tweak the small on-board potentiometer to set the threshold. In the simulator, use the "threshold" attribute to set the threshold voltage. The default threshold is 2.5 volts, or about 100 lux.

The bottom LED ("DO LED") is connected to the digital output, and lights whenever the DO pin goes low. In other words, it lights when the sensor is illuminated.

## Automation controls

The photoresistor sensor can be controlled using [Automation Scenarios](/wokwi-ci/automation-scenarios). The names of the controls match the names of the attributes defined above:

<ParamField path="lux" type="float">
  Set the lux value (lux)
</ParamField>

The following example sets the illumination to 100 lux:

```yaml theme={null}
  - set-control:
      part-id: photoresistor1
      control: lux
      value: 100
```

## Simulator examples

* [Photoresistor Digital Example](https://wokwi.com/projects/305193592908939842)
* [Photoresistor Analog Example](https://wokwi.com/projects/305193627138654786)
