Micropython Lesson 5
Generate a tone on the onboard Buzzer from square waves
In this example, you are learning to generate a tone using the onboard buzzer on the Magicbit.
From this example, you’ll get an understanding about,
- generating square waves
Piezo buzzers are commonly used in embedded systems to give audible tones. It works well with a square wave as input.
For this example, we use the piezo buzzer wired to pin 25 of the Magicbit.
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)
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.
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.