4. Buzzer Driver Experiment
About 2 min
Routine Code
from machine import Timer, PWM
import time
from fpioa_manager import fm
from maix import GPIO
# Create Timer 0 Channel 0 and set to PWM mode
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
# Configure PWM: frequency 500, initial duty cycle 100, pin 9
ch = PWM(tim, freq=500, duty=100, pin=9)
# CanMV board pin configuration
fm.register(11, fm.fpioa.GPIOHS11, force=True)
fm.register(12, fm.fpioa.GPIOHS12, force=True)
fm.register(13, fm.fpioa.GPIOHS13, force=True)
fm.register(16, fm.fpioa.GPIOHS16, force=True)
# Configure pins as input
key1 = GPIO(GPIO.GPIOHS11, GPIO.IN)
key2 = GPIO(GPIO.GPIOHS12, GPIO.IN)
key3 = GPIO(GPIO.GPIOHS13, GPIO.IN)
key4 = GPIO(GPIO.GPIOHS16, GPIO.IN)
# Define four different sound frequencies
freq1 = 500
freq2 = 1000
freq3 = 1500
freq4 = 2000
def play_sound(frequency):
ch.freq(frequency)
ch.duty(10) # Adjust duty cycle to control volume
while True:
# Check if button 1 is pressed
if not key1.value():
play_sound(freq1)
time.sleep_ms(100) # Brief delay to avoid rapid polling
# Check if button 2 is pressed
elif not key2.value():
play_sound(freq2)
time.sleep_ms(100) # Brief delay to avoid rapid polling
# Check if button 3 is pressed
elif not key3.value():
play_sound(freq3)
time.sleep_ms(100) # Brief delay to avoid rapid polling
# Check if button 4 is pressed
elif not key4.value():
play_sound(freq4)
time.sleep_ms(100) # Brief delay to avoid rapid pollingExperiment Preparation
- Connect the K210 to your computer via USB.
- Open CanMV IDE, connect to the K210, and run the routine code above.
Experiment Results
- After running, press any of the four buttons on top of the K210. The K210 plays the corresponding tone continuously. Each button produces a different sound. Press the reset button to stop the program.
Routine Code Explanation
- Import required modules.
TimerandPWMfrom themachinemodule create a timer and configure pulse-width modulation (PWM), commonly used to control motor speed, LED brightness, and audio signals by varying duty cycle.
- Import required modules.
from machine import Timer, PWM
import time
from fpioa_manager import fm
from maix import GPIO- Create a timer and configure PWM. The
Timerclass creates timer objecttimusing TIMER0 CHANNEL0 in PWM mode. ThePWMobjectchis created with frequency 500 Hz, initial duty cycle 100%, and output on pin 9. This PWM signal can drive devices such as a buzzer.
- Create a timer and configure PWM. The
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
ch = PWM(tim, freq=500, duty=100, pin=9)- Board pin configuration. Same as the button experiment; not described in detail here.
fm.register(11, fm.fpioa.GPIOHS11, force=True)
fm.register(12, fm.fpioa.GPIOHS12, force=True)
fm.register(13, fm.fpioa.GPIOHS13, force=True)
fm.register(16, fm.fpioa.GPIOHS16, force=True)
key1 = GPIO(GPIO.GPIOHS11, GPIO.IN)
key2 = GPIO(GPIO.GPIOHS12, GPIO.IN)
key3 = GPIO(GPIO.GPIOHS13, GPIO.IN)
key4 = GPIO(GPIO.GPIOHS16, GPIO.IN)- Define four variables
freq1,freq2,freq3, andfreq4representing four sound frequencies in Hz. These change the PWM frequency to produce different tones on the device connected to pin 9.
- Define four variables
freq1 = 500
freq2 = 1000
freq3 = 1500
freq4 = 2000- Define
play_sound(frequency)to set the PWM frequency viach.freq(frequency)and adjust volume withch.duty(10)(10% duty cycle for moderate volume).
- Define
def play_sound(frequency):
ch.freq(frequency)
ch.duty(10)- Enter an infinite loop that continuously checks whether each button is pressed. When a button is pressed (low level), call
play_soundwith the corresponding frequency.
- Enter an infinite loop that continuously checks whether each button is pressed. When a button is pressed (low level), call
while True:
# Check if button 1 is pressed
if not key1.value():
play_sound(freq1)
time.sleep_ms(100)
# Check if button 2 is pressed
elif not key2.value():
play_sound(freq2)
time.sleep_ms(100)
# Check if button 3 is pressed
elif not key3.value():
play_sound(freq3)
time.sleep_ms(100)
# Check if button 4 is pressed
elif not key4.value():
play_sound(freq4)
time.sleep_ms(100)