Example 33: Light Sensor
Example 33: Light Sensor
Experiment Overview
A photoresistor (LDR) changes resistance based on light intensity — the stronger the light, the lower the resistance. A voltage divider circuit converts resistance changes to voltage changes, which are read by the ADC. Suitable for automatic lighting, light-controlled curtains, and similar applications.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| AO | GPIO34 | Light sensor module analog output |
The stronger the light, the lower the analog output value; the weaker the light, the higher the value.
JSON Configuration Example
{
"peripherals": [
{
"id": "light_01",
"name": "Light Sensor",
"type": 15,
"enabled": false,
"pins": [34],
"params": {}
}
]
}Peripheral Execution Linkage
Scenario 1: Auto Light On at Dusk (Polling Trigger)
Feature: Automatically turn on light when darkness is detected (ADC > 3000)
Web UI Configuration Steps
Step 1: Ensure LED Peripheral is Configured
- Peripheral ID:
led_d1 - Type: GPIO Output
Step 2: Create Rule
- Click Peripheral Config in the left menu → Switch to Peripheral Execution tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Auto Light On at Dusk - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Polling Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Polling Trigger Periodic condition check Target Peripheral Select light_01Light sensor Polling Interval 50005 seconds Condition Expression value > 3000Low light threshold
Step 4: Configure Action
Click Add Action button
Fill in:
- Action Type: Select LOW (common-anode LED)
- Target Peripheral: Select
led_d1
Click Save button
Scenario 2: Auto Light Off at Dawn (Polling Trigger)
Feature: Automatically turn off light when dawn is detected (ADC < 1000)
Configuration Steps:
- Create rule, name:
Auto Light Off at Dawn - Trigger configuration:
- Trigger Type: Select Polling Trigger
- Target Peripheral: Select
light_01 - Polling Interval:
5000 - Condition Expression:
value < 1000(bright light threshold)
- Action configuration:
- Action Type: Select HIGH (common-anode LED off)
- Target Peripheral: Select
led_d1
- Click Save
Light Sensor Value Reference
| Light Condition | ADC Value | Description |
|---|---|---|
| Bright (Sunlight) | <500 | Bright environment |
| Indoor Light | 1000-2000 | Normal indoor |
| Dusk | 2000-3000 | Dim light |
| Dark Night | >3000 | Very dark |
Notes
- Response Speed: Photoresistors respond slowly (~tens of milliseconds); not suitable for fast-changing detection
- Temperature Effect: Temperature changes slightly affect resistance
- Threshold Calibration: Thresholds differ across environments; on-site tuning required
- Non-linearity: The resistance-to-light relationship of photoresistors is non-linear
