PWM Control in Lighting Systems: Techniques and Tips

Pulse Width Modulation (PWM) Control / Visits:19

When we think about PWM (Pulse Width Modulation) in lighting systems, the first thing that comes to mind is dimming LEDs or controlling the brightness of a room. But what if I told you that the same PWM signal that smoothly fades your desk lamp can also make a micro servo motor rotate with surgical precision? This intersection—where lighting control meets micro servo motor actuation—is where some of the most innovative embedded system designs are born.

In this post, we’re going to dive deep into the techniques and practical tips for using PWM to control lighting systems while simultaneously driving micro servo motors. Whether you are building a robotic arm with integrated status lights, a smart lamp that physically orients its beam, or an automated stage light that pans and tilts, understanding how PWM bridges these two worlds is essential.

Why Micro Servo Motors and Lighting Are a Perfect Pair

Micro servo motors, like the ubiquitous SG90 or MG90S, are small, lightweight, and incredibly responsive. They typically operate on a 50 Hz PWM signal (20 ms period), where the pulse width dictates the angular position—usually between 1 ms (0°) and 2 ms (180°). Lighting systems, on the other hand, often use higher-frequency PWM (anywhere from 100 Hz to several kHz) to avoid visible flicker.

The challenge? You cannot simply feed a lighting-dedicated PWM signal into a micro servo motor and expect it to behave. The timing parameters are fundamentally different. But when you design a system that uses PWM cleverly, you can achieve synchronized behavior—for example, a servo that rotates a color filter wheel while the LED brightness changes in lockstep.

The Core Difference: Frequency vs. Duty Cycle Sensitivity

  • Lighting PWM: Frequency must be high enough to avoid flicker (typically >200 Hz for human eyes, >1 kHz for cameras). Duty cycle controls intensity.
  • Micro Servo PWM: Frequency is fixed at 50 Hz (20 ms period). Duty cycle is narrow (5% to 10%) and controls angular position, not power.

This means that if you try to drive a micro servo motor with a 1 kHz PWM signal, the servo will either vibrate, overheat, or simply not respond. The internal control circuit expects a specific timing window.

PWM Generation Techniques for Dual-Purpose Systems

1. Using Two Independent PWM Channels

The most straightforward technique is to use separate PWM channels from your microcontroller. Most modern MCUs (like the ESP32, STM32, or Arduino-based ATmega328) have multiple timers and PWM outputs.

Example setup: - Channel A: 1 kHz, variable duty cycle → LED driver - Channel B: 50 Hz, 1–2 ms pulse → Micro servo motor

Tip: On platforms like Arduino, avoid using analogWrite() for the servo channel because it defaults to ~490 Hz (or 980 Hz on some pins). Instead, use the Servo.h library which handles the 50 Hz timing in software, or configure a hardware timer explicitly.

cpp

include <Servo.h>

Servo myServo; int ledPin = 9; // PWM-capable pin

void setup() { myServo.attach(10); // 50 Hz handled by library pinMode(ledPin, OUTPUT); }

void loop() { // Servo to 90°, LED at 50% brightness myServo.write(90); analogWrite(ledPin, 128); // 1 kHz on most boards delay(1000); }

Caveat: The Servo.h library on Arduino disables analogWrite() on certain pins due to timer conflicts. Always check the datasheet.

2. Time-Division Multiplexing (TDM) on a Single Pin

If you are limited on GPIO pins or timer channels, you can time-division multiplex the same PWM pin between the lighting load and the servo. This requires external hardware—typically a transistor switch or an analog multiplexer.

How it works: - During the first 20 ms window, you output a 50 Hz servo-compatible pulse. - During the next 20 ms window, you output a high-frequency burst for the LED. - Repeat.

Hardware tip: Use a fast-switching MOSFET (e.g., 2N7002) to toggle between the LED and the servo signal path. The microcontroller must synchronize the switching with the PWM period.

Software tip: This is best implemented using timer interrupts. On an ESP32, you can use the LEDC PWM driver to change frequency on-the-fly, but be aware that changing frequency mid-operation can cause glitches.

3. Using a Dedicated PWM Controller IC

For complex systems with multiple servos and LEDs, consider an I2C or SPI-based PWM driver like the PCA9685. This chip generates 16 independent PWM channels, each with its own frequency and duty cycle.

Why this is a game-changer: - You can set one channel to 50 Hz for servos and another channel to 1.6 kHz for LEDs. - The microcontroller only sends commands via I2C, freeing up CPU cycles. - The PCA9685 has a built-in oscillator, so timing is precise.

Wiring note: The PCA9685 outputs are open-drain, so you need pull-up resistors for servo control lines. For LEDs, a current-limiting resistor is sufficient.

4. Frequency Synthesis via Software PWM

If your microcontroller lacks hardware PWM, you can bit-bang the signal. This is common in low-cost 8-bit MCUs like the ATTiny85.

Technique: - Use a timer interrupt that fires every 1 µs. - Maintain a counter for the servo pulse (1–2 ms) and a separate counter for the LED PWM. - The ISR toggles the pins based on the counters.

Warning: Software PWM consumes significant CPU time. For a micro servo motor, the 20 ms period must be maintained with jitter under 10 µs, which is challenging on slower MCUs.

Synchronizing Servo Movement with Lighting Effects

One of the most visually impressive applications is to have the micro servo motor physically move a light source (or a reflector) while the LED brightness changes in a coordinated pattern. This creates dynamic lighting effects that feel alive.

Technique: Phase-Aligned PWM

Phase-aligned PWM means that the rising edge of the servo pulse and the LED pulse occur at the same time. This is critical when you want the servo to reach a position exactly when the LED hits peak brightness.

Implementation on STM32: - Use a single timer with two compare channels. - Channel 1: Servo output (50 Hz, compare value for 1.5 ms). - Channel 2: LED output (same timer, but with a higher prescaler for higher frequency). - By using the same timer base, the phases are inherently aligned.

Code snippet (HAL-based): c // Timer2 configured for 50 Hz (20 ms period) // CH1: Servo pulse, CH2: LED PWM HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1500); // 1.5 ms pulse __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 500); // 50% duty at 1 kHz

Smooth Motion with Fading

To make the servo move smoothly while the LED fades, use a motion profile like a sine or cubic bezier. Update both the servo angle and the LED brightness in small increments at a high refresh rate (e.g., 50 Hz for servo, 200 Hz for LED).

Tip: Do not update the servo position faster than 50 Hz—it will cause jitter. Instead, interpolate the position over multiple cycles.

Thermal Considerations for Micro Servo Motors in Lighting Systems

When a micro servo motor is used near high-power LEDs, heat becomes a real issue. Servo motors are not designed for continuous operation under load, and the plastic gears can soften or warp.

PWM Frequency and Motor Heating

  • Standard 50 Hz: The servo receives a pulse every 20 ms. The motor is powered only for 1–2 ms per cycle. This low duty cycle keeps the motor cool.
  • Higher frequencies: If you accidentally feed a 200 Hz signal, the servo will try to respond 200 times per second. The internal H-bridge will switch rapidly, causing I²R losses and overheating.

Practical tip: Never exceed 60 Hz for a micro servo motor. Some high-end digital servos accept up to 333 Hz, but standard analog servos (like the SG90) will fail.

Heat Dissipation from LEDs

If your lighting system uses high-power LEDs (e.g., 10W or more), the heat sink will radiate heat onto the nearby servo. Mount the servo at least 2 cm away from the LED heat sink, or use a thermal barrier.

Active cooling: A small 5V fan controlled by a second PWM channel can blow air over both the LED and the servo. This fan can be driven by the same MCU.

Wiring and Noise Reduction Techniques

PWM signals are digital square waves, but they can pick up noise from inductive loads (like the servo motor itself). Lighting systems often have long wires, which act as antennas.

Grounding Strategy

  • Use a star ground topology: all grounds (MCU, servo, LED driver, power supply) meet at a single point.
  • Do not daisy-chain ground wires from the servo to the LED driver—this creates ground loops.

Signal Isolation

For high-power lighting (AC mains dimming), use an optocoupler between the MCU PWM output and the triac driver. For servos, this is usually unnecessary, but if the servo is far from the MCU (over 1 meter), use a twisted pair cable and a 100Ω series resistor at the source.

Decoupling Capacitors

Place a 100 µF electrolytic capacitor near the servo power pins. The servo draws high inrush current (up to 500 mA) when starting. Without this capacitor, the voltage drop can cause the MCU to reset or the LED to flicker.

Tip: For a system with multiple servos, use one 470 µF capacitor per servo.

Advanced Tip: Using PWM to Read Servo Position

Did you know that a micro servo motor can also output a PWM signal? Some servos (like the Feetech FS90R) have a feedback wire that outputs a pulse proportional to the current position. You can read this pulse using an input capture pin on your MCU.

Application in lighting: - Mount a servo that rotates a gobo wheel. - The feedback PWM tells the MCU exactly which gobo is in front of the LED. - The MCU then adjusts the LED color temperature accordingly.

Reading technique: - Use a timer in input capture mode. - Measure the pulse width (typically 1–2 ms). - Map it to an angle, then to a lighting preset.

cpp // Pseudo-code for reading servo feedback int readServoFeedback(int pin) { int pulseWidth = pulseIn(pin, HIGH, 25000); // timeout 25 ms return map(pulseWidth, 1000, 2000, 0, 180); }

Common Pitfalls and How to Avoid Them

Pitfall 1: Using delay() in the Loop

When you use delay(), the PWM signal to the servo stops. The servo will lose its position and drift. Always use non-blocking timing (millis() or timer interrupts).

Fix: cpp unsigned long previousMillis = 0; int servoPos = 90; int ledBrightness = 128;

void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= 20) { // 50 Hz previousMillis = currentMillis; myServo.write(servoPos); analogWrite(ledPin, ledBrightness); // Update positions if needed } }

Pitfall 2: Ignoring Servo Stall Current

When a servo reaches its mechanical limit (end stop), it draws maximum current. If the LED is also at full brightness, the total current may exceed the power supply rating.

Solution: Use a current-limiting resistor or a polyfuse on the servo power line. Alternatively, sense the current with a shunt resistor and reduce the LED brightness if the servo is stalling.

Pitfall 3: Crosstalk Between PWM Channels

On some microcontrollers, two PWM channels on the same timer share the same frequency. You cannot have one channel at 50 Hz and another at 1 kHz on the same timer.

Workaround: Use different timers. For example, on an Arduino Uno, Timer1 can be used for servo (50 Hz) and Timer2 for LED (1 kHz). The Servo.h library uses Timer1, so you must use Timer2 for analogWrite() on pins 3 and 11.

Real-World Project Ideas

Smart Desk Lamp with Physical Beam Control

Use two micro servo motors to pan and tilt a high-power LED module. The PWM for the LED brightness is synchronized with the servo position to create a spotlight effect. Add a rotary encoder for manual control.

Key specs: - Servo: MG90S (metal gears for durability) - LED: 10W COB with 12V driver - PWM frequency: 50 Hz for servos, 500 Hz for LED (to avoid beat frequency with camera shutter)

Automated Plant Growth Light

A servo rotates a color filter wheel (red/blue/full spectrum) while the LED intensity ramps up and down to simulate sunrise and sunset. The PWM for the servo is updated every 20 ms, while the LED PWM is updated every 5 ms for smooth fading.

Tip: Use a real-time clock (RTC) module to store the lighting schedule. The servo position can be stored as an angle in EEPROM.

Interactive Art Installation

A grid of micro servo motors each holds a small LED. The servos tilt the LEDs toward a moving target (tracked by a camera). The PWM for each servo and LED is calculated by a central ESP32 using Wi-Fi to receive tracking data.

Challenge: With 16 servos and 16 LEDs, you need 32 PWM channels. Use four PCA9685 boards daisy-chained on I2C.

Final Thoughts on PWM Mastery

Mastering PWM control for both lighting and micro servo motors is about understanding timing at a granular level. The 20 ms heartbeat of a servo and the microsecond-level precision of a dimming LED are not enemies—they are two rhythms that can dance together when you orchestrate them correctly.

Remember these three rules: 1. Frequency is king: Servos demand 50 Hz. LEDs demand higher. Never compromise. 2. Power is separate: Always use dedicated regulators for servo and LED power. The MCU should only handle signals. 3. Test with an oscilloscope: Your eyes cannot see 1 µs jitter, but your servo can feel it. A $30 oscilloscope will save you hours of debugging.

Whether you are building a prototype for a maker fair or a production-grade lighting fixture, the techniques above will give you a solid foundation. The next time you see a lighting effect that moves, you’ll know exactly how it was done—one PWM pulse at a time.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/pulse-width-modulation-pwm-control/pwm-lighting-systems-guide.htm

Source: Micro Servo Motor

The copyright of this article belongs to the author. Reproduction is not allowed without permission.

About Us

Lucas Bennett avatar
Lucas Bennett
Welcome to my blog!

Tags