PWM (Pulse Width Modulation):
Applications:
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)LED:
Pin 5
.GND
via 220Ω resistor.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)
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
Motor 規格:
伺服馬達有三條線:
訊號線用於傳送PWM脈波來控旋轉角度。
PWM脈波的頻率是50Hz。
脈衝的持續時間:
# 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)
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)
Speaker:
Pin 5
.GND
.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 |
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)
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)