This project implements a closed-loop PID control system to adjust the brightness of an LED based on ambient light levels. The Wio Terminal's built-in light sensor continuously monitors the environment, and the PID algorithm dynamically adjusts the LED's PWM output to maintain a target brightness level.
π‘ ObjectiveMaintain total light level at setpoint by compensating ambient light with LED brightness using closed-loop PID control.
π§ Hardware Required- LED connected to D1 pin on Wio Terminal
- Resistor
- Breadboard and jumper wires
PID (Proportional-Integral-Derivative) is a control loop feedback mechanism widely used in industrial control systems. It calculates an "error" value as the difference between a desired setpoint and a measured process variable, then applies correction based on three tπ‘ Objective
Maintain ambient light at setpoint by compensating with LED brightness using feedback from the Wio Terminal's light sensor.erms:
1. Proportional (P) - Reacts to Current Error
Proportional = Kp Γ (Target - Current)- What it does: Immediate response to the current error
- Effect: Larger errors get larger corrections
- Too high: Causes oscillation and instability
- Too low: Slow response, never reaches target
2. Integral (I) - Corrects Past Errors
Integral = Ki Γ β(All Past Errors)- What it does: Eliminates steady-state error that persists over time
- Effect: Accumulates small errors that the P term misses
- Too high: Causes overshoot and windup
- Too low: System never quite reaches the target
3. Derivative (D) - Predicts Future Errors
Derivative = Kd Γ (Current Error - Previous Error)- What it does: Damps the system and reduces overshoot
- Effect: Slows down the response when approaching the target
- Too high: Makes system sluggish and slow to respond
- Too low: Allows oscillation and overshoot
In Our Lighting System:
Error = Target Lux - Current Lux- P Term: If it's too dark, immediately increase LED brightness
- I Term: If it stays slightly dark, gradually increase more
- D Term: If we're approaching the target, slow down the changes
Tuning Approach:
1. Start with P only (set Ki=0, Kd=0)
2. Increase Kp until the system oscillates, then reduce by 50%
3. Add Ki to eliminate steady-state error
4. Add Kd to reduce overshoot and oscillation
π§ Software Logic1. Read Light Sensor: Continuously measure ambient light using the Wio Terminal's built-in sensor.
Sensor Specifications:
- Sensor Type: Analog light sensor
- Range: 0 to 1023 (10-bit ADC)
- 0: Complete darkness
- 1023: Maximum brightness
- Location: Built into Wio Terminal
2. Compute Error: Calculate the difference between the target light level and the current reading.
3. Apply PID Algorithm:
- Proportional: Reacts to current error.
- Integral: Accounts for accumulated error over time.
- Derivative: Predicts future error based on rate of change.
4. Adjust LED Brightness: Use PWM to control the LED's brightness based on PID output.
5. Visual Feedback:
- Real-time light level bar with color coding
- Historical data graph
- PID parameters display
- Current values monitoring
- Real-time PID control
- Interactive display with light bar and historical graph
- Button controls:
- A: Decrease target (-25)
- B: Increase target (+25)
- C: Cycle through PID tuning presets
- Serial monitoring for data analysis
When starting the system:
- Cover the light sensor β LED lights up brightly
- Shine light on the sensor β LED dims down
- When reaching target β LED operates at appropriate level
Desired Outcome:
- The system tries to maintain ambient light
- LED compensates when ambient light is below the set point
- LED dims when ambient light is above the set point
- Start with moderate P gain (Kp=2.0-3.0)- Add small integral term (Ki=0.1-0.2) for accuracy- Use derivative (Kd=0.5-1.0) to reduce oscillation- Press Button C to test different preset combinations
π₯οΈ Display Layout- Top: PID parameters and real-time values
- Middle: Color-coded light level bar
- Bottom: Historical data graph with setpoint line
ββββββββββββββββββββββββββββββββββββββββ
β BRIGHTNESS CONTROL β β Title
β βββββββββββββββββββββββββββββββββββββββ£
β PID PARAMETERS: CURRENT VALUES: β
β Kp: 3.0 Target: 500 β β Parameters
β Ki: 0.20 Light: 350 β β Current Values
β Kd: 0.8 Output: 180 β
β β
β A: -Target B: +Target C: Tune PID β β Button Instructions
ββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββ
β Light Level: ββββββββββββ 450 β β Light Bar
β β
β Light Level Over Time β
β ββββββββββββββββββββββββββββββ β
β β ___---___ β β β Graph
β β ___/ \___ β β
β β____-- \--_____ β β
β ββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββ οΈ Important Note:
The LED should be positioned to NOT directly illuminate the light sensor,
otherwise it creates a feedback loop that destabilizes the system.
π Future Enhancements- Data Logging: Save performance data to SD card for analysis
- Wireless Connectivity: Bluetooth/Wi-Fi for remote monitoring and control
- PID Auto-Tuning: Implement self-adjusting PID parameters
- Smart Features: Time-based schedules and weather adaptation
- Mobile Integration: Smartphone app for system management












Comments