Micropython Lesson 5
Generating Tones
Generate a tone on the onboard Buzzer from square waves
Components Required
Buy
Introduction
In this example, you are learning to generate a tone using the onboard buzzer on the Magicbit.
Learning Outcomes
From this example, you’ll get an understanding about,
- generating square waves
Theory
Piezo buzzers are commonly used in embedded systems to give audible tones. It works well with a square wave as input.
Methodology
For this example, we use the piezo buzzer wired to pin 25 of the Magicbit.
Code
from machine import Pin,PWM
import time
BUZZER=Pin(25, Pin.OUT)
while True:
BUZZER.on()
time.sleep_ms(300)
BUZZER.off()
time.sleep_ms(300)
Explanation
There is no function for MicroPython to generate a square wave to activate the buzzer. Therefore, we should create a square wave to replicate the tone function used in Arduino.
Activity
Note:: Create a program that plays one frequency when one push button on the board is pressed, and another frequency when the other push-button when pressed.