ESP32 PWM
(Pulse Width Modulation) 🔗

Introduction to PWM

PWM (Pulse Width Modulation):

  • A technique to produce analog-like signals using digital means.
  • Varies the width of pulses in a sequence to represent a range of analog values.

Applications:

  • Motor speed control
  • LED brightness control
  • Signal generation, and more!

A 5Hz square wave:

A 10Hz square wave:

A PWM wave:

A PWM wave:

Function Explanations for PWM

from machine import Pin, PWM

pwm = PWM(Pin(5), freq=1000, duty=512)

pwm.freq(100)
pwm.duty(512)
  • Pin(NUMBER): Define a pin.
  • PWM(pin, freq=1000, duty=512): Create a PWM object for the specified pin.
  • pwm.duty(VALUE): Set the duty cycle. (0 - 1023)
  • pwm.freq(VALUE): Set the frequency. (HZ)

Wokwi Logic Analyzer

Wokwi Logic Analyzer

PulseView -- HomePage

PulseView -- Download

PulseView

PulseView

PulseView

PulseView

PulseView

PulseView

PulseView

PulseView

Hardware Connections - LED

LED:

  • Anode connected to Pin 5.
  • Cathode to GND via 220Ω resistor.

PWM - LED 🔗

Dynamic LED Brightness Control

Changing LED Brightness Over Time:

led_pin = Pin(5)
led_pwm = PWM(led_pin)
# Increasing brightness
for duty in range(0, 1024, 64):
    led_pwm.duty(duty)
    time.sleep(0.5)
# Decreasing brightness
for duty in range(1023, -1, -64):
    led_pwm.duty(duty)
    time.sleep(0.5)

Hardware Connections - Motor

Motor 規格:

  • 重量:9g

  • 尺寸:23*12.2*29mm

  • 工作電壓:4.8V

  • 轉矩:1.8kg-cm,當工作電壓為4.8V時

  • 運轉速度:0.1秒 ∕ 60度 ,當工作電壓為4.8V時

  • 脈衝寬度範圍:500~2400µs

  • 死頻帶寬度(dead band width):10µs

Hardware Connections - Motor

Motor 規格:

  • 伺服馬達有三條線:

    • 電源 (Power)
    • 接地 (Ground)
    • 訊號線 (Signal)
  • 訊號線用於傳送PWM脈波來控旋轉角度。

  • PWM脈波的頻率是50Hz。

  • 脈衝的持續時間:

    • 0.5ms 代表 0 度
    • 2.4ms 代表 180 度

Dynamic Motor Speed Control 

# PWM frequency for the servo
pwm_freq = 50

# The width (duration) of each pulse for the given frequency
pwm_width = 1 / pwm_freq

# Define the range of angles the servo can rotate to
min_angle, max_angle = 0, 180

# Define the pulse widths (in seconds) corresponding to the minimum and maximum angles for the servo
min_width, max_width = float('500e-06'), float('2400e-06')

# Calculate the duty cycle values for the minimum and maximum pulse widths
min_duty = min_width / pwm_width
max_duty = max_width / pwm_width

# Determine the conversion factor to map an angle to its corresponding duty cycle
angle_conversion_factor = (max_duty - min_duty) / (max_angle - min_angle)

Dynamic Motor Speed Control 

def rotate(servo, degree, delay=0.4):
    """
    Rotate the servo to the desired angle.
    
    Parameters:
    - servo: The servo object.
    - degree: The desired angle to rotate the servo to (0 to 180 degrees).
    - delay: Time (in seconds) to wait after rotating the servo. Default is 0.4 seconds.
    """
    
    # Calculate the duty cycle for the desired angle
    dc = angle_conversion_factor * degree + min_duty
    
    # Uncomment the next line if using MicroPython with the `duty_u16` method
    #servo.duty_u16(int(65535 * dc))
    
    #Set the servo's duty cycle to move it to the desired angle
    servo.duty(int(1023 * dc))
    
    # Wait for the specified delay to let the servo move and settle
    sleep(delay)

Dynamic Motor Speed Control 

Hardware Connections - Speaker

Speaker:

  • Positive Pin to Pin 5.
  • Negative Pin to GND.

Hardware Connections - Speaker

Speaker Ambulance Ring

Playing Tones with a Speaker

Frequency & Pitch: The frequency set determines the pitch of the sound produced. Higher frequencies result in higher pitched sounds.

Duty & Volume: The duty cycle impacts the volume of the sound. A 50% duty cycle (512 out of 1024) typically produces clear sound without distortion.

Solfège is a method used to teach pitch and sight singing. In this system, each note is given a name, making it easier to remember and sing.

Solfège Standard Note Name Frequency (Hz)
Do C 262
Re D 294
Mi E 330
Fa F 349
Sol G 392
La A 440
Si B 494
Do C 523

Playing Tones with a Speaker

Playing Tones with a Speaker

from machine import Pin, PWM
import time

# Dictionary to map standard note names to frequencies
standard_to_freq = {
    'C': 262,
    'D': 294,
    'E': 330,
    'F': 349,
    'G': 392,
    'A': 440,
    'B': 494,
    'C_high': 523
}

# Define the speaker pin and PWM object
speaker_pin = Pin(7)
speaker_pwm = PWM(speaker_pin)

# Let's play "Twinkle Twinkle Little Star" using standard note names
song = ['C', 'C', 'G', 'G', 'A', 'A', 'G', 'F', 'F', 'E', 'E', 'D', 'D', 'C']
for note in song:
    frequency = standard_to_freq[note]
    speaker_pwm.freq(frequency)
    speaker_pwm.duty(512)
    time.sleep(0.5)
    speaker_pwm.duty(0)  # Turning off between notes
    time.sleep(0.1)

Playing Tones with a Speaker

from machine import Pin, PWM
import time

# Dictionary to map standard note names to frequencies
standard_to_freq = {
    'C': 262,
    'D': 294,
    'E': 330,
    'F': 349,
    'G': 392,
    'A': 440,
    'B': 494,
    'C_high': 523
}

# Define the speaker pin and PWM object
speaker_pin = Pin(5)
speaker_pwm = PWM(speaker_pin)

# Let's play "Twinkle Twinkle Little Star" using standard note names
song = ['C', 'C', 'G', 'G', 'A', 'A', 'G', 'F', 'F', 'E', 'E', 'D', 'D', 'C']
for note in song:
    frequency = standard_to_freq[note]
    speaker_pwm.freq(frequency)
    speaker_pwm.duty(512)
    time.sleep(0.5)
    speaker_pwm.duty(0)  # Turning off between notes
    time.sleep(0.1)

Playing Tones with a Speaker

Conclusion

PWM offers versatile control over devices like LEDs, motors, and speakers. By understanding frequency and duty cycle, you can fine-tune device behaviors, ranging from LED brightness to musical notes and motor speed.

ESP32 PWM

By wschen

ESP32 PWM

This presentation introduces the concept of Pulse Width Modulation (PWM) and its applications in controlling LED brightness, motor speed, and speaker volume. Learn how to use PWM to create dynamic and precise control over these devices. Explore powerful tools like Wokwi Logic Analyzer and PulseView for analyzing and debugging PWM signals.

  • 173