🔬

Understanding PWM and Signal Characterization

PWM and Signal Analysis Lab

Objective

  • Understand how Pulse Width Modulation (PWM) controls LED brightness.
  • Learn how varying duty cycle and effective frequency affect the output signal.
  • Use the Red Pitaya oscilloscope to visualize the PWM signals.
  • Measure the period and calculate the duty cycle of the signal using cursors on the oscilloscope.

Theory

What is PWM?

Pulse Width Modulation (PWM) is a method of reducing the average power delivered by an electrical signal by effectively chopping it into discrete parts. In PWM, the output switches between HIGH (full voltage) and LOW (0V) rapidly. The key parameter is the duty cycle—the percentage of one period in which the signal is HIGH.
  • Duty Cycle:
    • If the duty cycle is 50%, the signal is HIGH for half the period and LOW for the other half. For example, at a 10 Hz frequency (period = 100 ms), a 50% duty cycle means the signal is HIGH for 50 ms and LOW for 50 ms.
  • Frequency:
    • Frequency is the number of PWM cycles per second (Hz). Higher frequencies mean that the LED is switched on and off faster than the human eye can detect, resulting in smooth brightness transitions. Lower frequencies might lead to visible flicker.

PWM in LED Dimming

When controlling an LED:
  • Digital On/Off: A simple 50/50 square wave results in full brightness when ON and complete darkness when OFF.
  • Variable Duty Cycle: Changing the duty cycle changes the average power delivered to the LED. A higher duty cycle means the LED is ON longer per cycle, increasing brightness. Conversely, a lower duty cycle reduces brightness.
  • Effective Frequency: Even if the hardware PWM frequency is fixed, modifying the rate at which PWM values are updated (the effective frequency) can change how smooth or flickery the LED appears.

Materials Required

  • Arduino Uno or Arduino Uno R4 board
  • LED
  • Current-limiting resistor (1 kΩ recommended)
  • Breadboard and jumper wires
  • Red Pitaya STEMlab 125-14 (or another oscilloscope) for signal visualization

Circuit Wiring Instructions

  1. LED Connection:
      • Connect Arduino digital pin 9 (PWM-capable) to one end of a 1 kΩ resistor.
      • Connect the other end of the resistor to the LED’s anode (long lead).
      • Connect the LED’s cathode (short lead) to Arduino GND.
        • notion image
  1. Oscilloscope Connection (Optional):
      • Attach the probe tip of the Red Pitaya (or another oscilloscope) to pin 9 (or to the junction between the resistor and LED).
        • notion image
      • Connect the oscilloscope ground clip to Arduino GND.
      notion image

Segment Instructions and Code for Each PWM State

State 0: 10 Hz Digital On/Off Toggling (50/50 Duty Cycle)

Explanation:
In this state, the LED toggles between ON and OFF at 10 Hz (period = 100 ms; 50 ms HIGH, 50 ms LOW). This demonstrates a fixed 50/50 duty cycle digital square wave.
Code:
// State 0: 10Hz Digital On/Off (50/50 Duty Cycle) void state0() { digitalWrite(9, HIGH); delay(50); // LED ON for 50ms digitalWrite(9, LOW); delay(50); // LED OFF for 50ms }
Red Pitaya Analysis:
  • Set the oscilloscope time base to show a 100 ms period.
  • Use cursors to mark the start and end of a cycle.
    • notion image
  • Calculate the period (should be ~100 ms) and note that the duty cycle is 50% (50 ms HIGH / 100 ms total).
    • notion image

State 1: 10 Hz PWM with a 10/90 Duty Cycle

Explanation:
Here, the LED is driven with PWM at 10 Hz. The duty cycle is set to approximately 10% (PWM value ~26) and 90% (PWM value ~230) alternating every 50 ms.
Code:
cpp Copy // State 1: 10Hz PWM with 10/90 Duty Cycle void state1() { static bool toggle = false; if (toggle) analogWrite(9, 26); // Dim: 10% brightness else analogWrite(9, 230); // Bright: 90% brightness toggle = !toggle; delay(50); // 50ms per state (10Hz cycle) }
Red Pitaya Analysis:
  • Observe the PWM waveform on the oscilloscope.
  • Use cursors to measure the high and low intervals within one cycle:
    • High interval should be ~10 ms (10% of 100 ms).
    • Low interval should be ~90 ms.
  • Calculate the duty cycle: (High Time / Total Period) * 100 ≈ 10% when dim and 90% when bright.
    • notion image

State 2: 10 Hz PWM with a 90/10 Duty Cycle

Explanation:
This state reverses the order: the LED toggles between ~90% brightness (PWM value ~230) first, then ~10% brightness (PWM value ~26) at a 10 Hz rate.
Code:
cpp Copy // State 2: 10Hz PWM with 90/10 Duty Cycle void state2() { static bool toggle = false; if (toggle) analogWrite(9, 230); // Bright: 90% brightness else analogWrite(9, 26); // Dim: 10% brightness toggle = !toggle; delay(50); // 10Hz cycle (100 ms period) }
Red Pitaya Analysis:
  • Similar to State 1, use the oscilloscope to measure the intervals.
  • Confirm that the only difference is the order of brightness levels compared to State 1.
    • notion image

State 3: PWM with Fixed 70/30 Duty Cycle & Varying Frequency

Explanation:
In this state, the duty cycle remains fixed at about 70% (ON) and 30% (OFF) while the effective PWM frequency changes over the 10-second interval:
  • First 3.33 sec: 20 Hz (Period = 50 ms, ON ~35 ms)
  • Next 3.33 sec: ~30 Hz (Period ≈ 33 ms, ON ~23 ms)
  • Last 3.33 sec: 40 Hz (Period = 25 ms, ON ~18 ms)
Code:
// State 3: PWM with Fixed 70/30 Duty Cycle and Varying Frequency void state3() { unsigned long now = millis(); unsigned long stateElapsed = now - stateStartTime; // stateStartTime is global unsigned long period, onTime; if (stateElapsed < stateDuration / 3) { period = 50; // 20Hz: period = 50ms onTime = 35; // 70% of 50ms ≈ 35ms } else if (stateElapsed < 2 * stateDuration / 3) { period = 33; // ~30Hz: period ≈ 33ms onTime = 23; // 70% of 33ms ≈ 23ms } else { period = 25; // 40Hz: period = 25ms onTime = 18; // 70% of 25ms ≈ 18ms } // Calculate current phase within the PWM cycle unsigned long phase = (now - stateStartTime) % period; if (phase < onTime) digitalWrite(9, HIGH); else digitalWrite(9, LOW); delay(1); // Short delay for processing }
Red Pitaya Analysis:
  • Observe the waveform’s period decrease over the 10-second period.
  • Use cursors to measure the period and on-time in each sub-interval:
    • First interval: period ~50 ms, on-time ~35 ms.
    • Second interval: period ~33 ms, on-time ~23 ms.
    • Third interval: period ~25 ms, on-time ~18 ms.
  • Discuss how increasing frequency reduces visible flicker.
    • notion image

State 4: 20 Hz PWM with Sweeping (Triangular) Duty Cycle

Explanation:
In this state, the LED brightness is swept smoothly via a triangular ramp. The duty cycle changes continuously at a fixed 20 Hz rate (Period = 50 ms), sweeping from 0 up to 100% and back to 0 over the 10-second interval.
Code:
// State 4: 20Hz PWM with a Sweeping (Triangular) Duty Cycle void state4() { const unsigned long period = 50; // 20Hz period = 50ms float frac = (float)(millis() - stateStartTime) / (float)stateDuration; unsigned long onTime; if (frac <= 0.5) { onTime = (unsigned long)(frac * 2.0 * period); // Ramp up from 0 to 50ms } else { onTime = (unsigned long)((1.0 - (frac - 0.5) * 2.0) * period); // Ramp down from 50ms to 0 } unsigned long phase = (millis() - stateStartTime) % period; if (phase < onTime) digitalWrite(9, HIGH); else digitalWrite(9, LOW); delay(1); }
Red Pitaya Analysis:
  • Use the oscilloscope to visualize the smooth triangular modulation of the PWM duty cycle.
  • Use cursors to measure the rising and falling portions of the cycle.
  • Notice how the LED brightness gradually increases and decreases.
    • notion image

Discussion Questions & Answers

  1. How does changing the duty cycle affect the average brightness of the LED?
    1. Answer: Increasing the duty cycle increases the proportion of time the LED is ON, resulting in a higher average voltage and greater brightness. A lower duty cycle reduces the average power, making the LED dimmer.
  1. What is the difference between a digital on/off signal and a PWM signal?
    1. Answer: A digital on/off signal (as in State 0) is a fixed square wave with a 50/50 duty cycle, meaning the LED is fully ON or OFF. A PWM signal varies the duty cycle, effectively controlling the average voltage and brightness of the LED. PWM can simulate varying brightness levels even though the output is still a rapidly switching digital signal.
  1. How does varying the effective PWM frequency (as in State 3) impact the perceived flicker of the LED?
    1. Answer: At lower effective frequencies, the on/off transitions are slower and may be visibly noticeable as flicker. As the effective frequency increases, the transitions occur faster than the human eye can resolve, resulting in smoother brightness without noticeable flicker.
  1. Which state produced the smoothest dimming effect, and why?
    1. Answer: State 4, with the continuous triangular sweep at a fixed 20 Hz rate, produces the smoothest dimming effect. This is because the duty cycle changes gradually over the PWM period, providing a continuous transition in brightness without abrupt jumps.
  1. How might PWM principles be applied to other systems such as motor control or audio signal generation?
    1. Answer: In motor control, PWM adjusts the motor speed by varying the average voltage delivered, ensuring efficient and precise control. In audio signal generation, PWM can be filtered to create analog-like waveforms for sound output. These principles allow for energy-efficient, precise control in a wide range of applications.