Skip to content

Light-Controlled Servo Motor and Dynamic LED ​

Project Overview ​

This project uses an Digicomp ESP32-S3 Dev Board to automate a hardware system comprising an LDR Light Sensor Module, an onboard LED, and a 9g micro servo motor (DXW90 / SG90).

The system performs two main real-time functions based on ambient light levels:

  • Dynamic LED Dimming: The onboard LED brightness scales proportionally with ambient darkness using Pulse Width Modulation (PWM).
  • Automated Servo Movement: When ambient darkness crosses a set threshold, the 9g servo motor automatically rotates to a 90° position. When ambient light returns, it returns to the home position 0°.

Hardware Components ​

  • ESP32-S3 Dev Board
  • LDR Light Sensor Module (4-pin with potentiometer)
  • 9g Micro Servo Motor (DXW90 / SG90)
  • Onboard LED (GPIO 21)
  • Jumper wires
  • USB-C Cable
  • Computer/Laptop running Command Prompt (cmd)

Pin Connections ​

ComponentPin LabelESP32-S3 Dev Board PinNotes / Function
LDR Sensor ModuleVCC (+)3.3VSensor Power
LDR Sensor ModuleGND (-)GNDSensor Ground
LDR Sensor ModuleAOGPIO 4Analog Light Intensity (ADC1)
LDR Sensor ModuleDOGPIO 5Digital Output Threshold
9g Servo MotorSignal (Yellow/Orange)GPIO 650Hz PWM Signal
9g Servo MotorVCC (Red)5V / VBUS5V Power Supply
9g Servo MotorGround (Brown/Black)GNDPower Ground
Onboard LEDBuilt-inGPIO 21Controlled via 16-bit PWM

Connection Summary ​

  • LDR Module AO → ESP32-S3 GPIO 4
  • LDR Module DO → ESP32-S3 GPIO 5
  • LDR Module VCC → ESP32-S3 3.3V
  • LDR Module GND → ESP32-S3 GND
  • Servo Signal → ESP32-S3 GPIO 6
  • Servo VCC → ESP32-S3 5V / VBUS
  • Servo GND → ESP32-S3 GND

Software & Environment Setup ​

  • MicroPython Firmware: Flashed onto the ESP32-S3
  • Command Line Tool: mpremote (invoked via python -m mpremote)
  • Python Modules Used:
  • machine (Pin, ADC, PWM)
  • time (sleep, delays)

How the Project Works ​

The system operates in three main functional phases:

Light Sensing (ADC & Digital Reads) ​

The LDR module's Analog Output (AO) is connected to GPIO 4. The ESP32-S3 uses a 12-bit Analog-to-Digital Converter (ADC) with 11dB attenuation (0V to 3.3V range) to read values from 0 (Brightest) to 4095 (Darkest).

Dynamic LED Brightness Scaling ​

The onboard LED on GPIO 21 is driven using PWM at 500 Hz. The 12-bit ADC reading from the LDR module (0 to 4095) is mapped to a 16-bit PWM duty cycle (0 to 65535) using the following formula:

Duty Cycle = (Raw ADC / 4095) × 65535

As ambient light decreases, the LDR resistance rises, producing a higher ADC value. This increases the PWM duty cycle, making the LED shine brighter in darker conditions.

Servo Motor Position Control ​

The 9g micro servo on GPIO 6 is driven using a 50 Hz PWM frequency (20 ms period). The pulse width is mapped between 0.5 ms (Duty ~1638) for 0° and 2.5 ms (Duty ~8192) for 180°.

When ambient darkness crosses the defined threshold (DARK_THRESHOLD = 2000), the servo angle switches from 0° to 90°.

Complete MicroPython Program (main.py) ​

python
import machine
import time

# =====================================================================
# 1. HARDWARE CONFIGURATION
# =====================================================================

LDR_AO_PIN = 4  # Analog Output (ADC1_CH3)
LDR_DO_PIN = 5  # Digital Output (Threshold trigger)
LED_PIN = 21     # Onboard LED (PWM)
SERVO_PIN = 6   # 9g Servo Signal Wire (PWM)

# Setup Analog Read for LDR
ldr_adc = machine.ADC(machine.Pin(LDR_AO_PIN))
ldr_adc.atten(machine.ADC.ATTN_11DB)  # Full 0V - 3.3V range (0 - 4095)

# Setup Digital Read for LDR Threshold Pin
ldr_do = machine.Pin(LDR_DO_PIN, machine.Pin.IN)

# Setup Onboard LED PWM (500 Hz for smooth, flicker-free dimming)
led_pwm = machine.PWM(machine.Pin(LED_PIN))
led_pwm.freq(500)

# Setup 9g Servo PWM (Standard 50 Hz control frequency)
servo_pwm = machine.PWM(machine.Pin(SERVO_PIN))
servo_pwm.freq(50)

# =====================================================================
# 2. HELPER FUNCTIONS
# =====================================================================

def set_servo_angle(angle):
    """
    Maps an angle (0-180 deg) to standard 50Hz PWM duty cycle.
    0 deg   ~ 0.5ms pulse (duty_u16 ~ 1638)
    180 deg ~ 2.5ms pulse (duty_u16 ~ 8192)
    """
    angle = max(0, min(180, angle))
    duty = int(1638 + (angle / 180.0) * (8192 - 1638))
    servo_pwm.duty_u16(duty)

def clear_outputs():
    """Turns off LED and disables PWM channels safely."""
    led_pwm.duty_u16(0)
    set_servo_angle(0)
    time.sleep(0.2)
    led_pwm.deinit()
    servo_pwm.deinit()

# =====================================================================
# 3. MAIN APPLICATION LOOP
# =====================================================================

print("--------------------------------------------------")
print("ESP32-S3 Light-Controlled Servo & LED System")
print("--------------------------------------------------")
print("Reading sensors... Press Ctrl+C in REPL to stop.\n")

DARK_THRESHOLD = 2000

try:
    while True:
        # Read LDR Analog Value (0 = Bright Light, 4095 = Dark)
        raw_ldr = ldr_adc.read()

        # Calculate darkness percentage (0.0 to 1.0)
        darkness_ratio = raw_ldr / 4095.0
        darkness_pct = darkness_ratio * 100.0

        # Read Digital Output Pin state
        digital_dark = ldr_do.value()

        # --- A. DYNAMIC LED BRIGHTNESS CONTROL ---
        led_duty = int(darkness_ratio * 65535)
        led_pwm.duty_u16(led_duty)

        # --- B. SERVO MOTOR CONTROL ---
        if raw_ldr > DARK_THRESHOLD:
            set_servo_angle(90)
            servo_status = "ROTATED (90°)"
        else:
            set_servo_angle(0)
            servo_status = "HOME (0°)"

        # Display telemetry in terminal
        print(f"LDR Raw: {raw_ldr:4d} | Darkness: {darkness_pct:5.1f}% | LED Duty: {led_duty:5d} | Servo: {servo_status}")

        time.sleep(0.1)

except KeyboardInterrupt:
    print("\nStopping system...")
    clear_outputs()
    print("System stopped cleanly.")

Working Flow Chart ​

text
       ESP32-S3 Boots Up & Initializes MicroPython
                           │
    Configure Pins: ADC (GPIO4), DO (GPIO5), PWM (GPIO2 & 6)
                           │
                 Read LDR Sensor Output
                           │
       ┌───────────────────┴───────────────────┐
       ▼                                       ▼
Calculate LED Duty Cycle                Compare Raw ADC
 (Darker = Brighter)                 with Threshold (2000)
       │                                       │
Apply PWM to GPIO 21                     ┌──────┴──────┐
       │                                ▼             ▼
       │                          Raw > 2000    Raw <= 2000
       │                                │             │
       │                            Rotate to     Rotate to
       │                               90°           0°
       └───────────────────┬──────────────────┘
                           │
              Print Telemetry to Terminal
                           │
               Delay 0.1s & Repeat Loop

Deployment Instructions (via cmd & mpremote) ​

  1. Upload main.py to ESP32-S3:
cmd
python -m mpremote connect COM8 cp main.py :main.py
  1. Soft Reset Board:
cmd
python -m mpremote connect COM8 soft-reset
  1. Monitor Real-Time Output:
cmd
python -m mpremote connect COM8 repl

Verification Matrix ​

Test ScenarioActionExpected Hardware ResponseExpected Output Stream
Ambient LightNormal room lightingOnboard LED dim or OFF; Servo at 0°`LDR Raw: <2000
Darkness TriggerCover LDR sensor completelyOnboard LED ramps to full brightness; Servo rotates to 90°`LDR Raw: >2000
Partial DarkPartially shade sensorLED glow increases proportionallyLED Duty value increases smoothly
ExitPress Ctrl + C in REPLServo homes to 0°, LED turns OFF cleanlySystem stopped cleanly.

Conclusion ​

The Light-Controlled Servo Motor and Dynamic LED System demonstrates real-time closed-loop control using an ESP32-S3, LDR Light Sensor, onboard LED, and a 9g servo motor. MicroPython's hardware abstraction layer allows seamless management of multi-channel PWM output and ADC inputs concurrently.

Contributors

The avatar of contributor named as Poojith Nirmal Poojith Nirmal
The avatar of contributor named as Karthik Bhat Karthik Bhat

Changelog

Made in India