In our daily life, many of our electronic devices will alarm when encountering a fault, and the sound is often more likely to attract people's attention than the indicator light. In this section, we will learn how to drive an active buzzer with Walnut Pie.
Buzzers are mainly divided into active buzzers and passive buzzers. Active buzzers make sounds through high and low level control (fixed frequency), and passive buzzers are controlled by PWN waves (can make sounds of different frequencies). In this section, we will explain the use of active buzzers.
The usage of active buzzer is similar to LED. You only need to give the buzzer a high level to make a sound.
Connection method of active buzzer and Walnut Pi: GND--GND, VCC -- 3.3V, IO -- PI15 (You can also change to the GPIO you want to use)
The name of the Walnut Pie PI15 in the Python library is board.KEY :
In CircuitPython, you can directly use the digitalio (digital IO) module to program IO input to achieve high and low level output. The specific introduction is as follows:
value refers to the delay time, in seconds. A value of 1 means 1 second, and a value of 0.1 means 0.1 second, that is, 100ms.
The active buzzer uses the digitalio object just like the LED. In the output mode, we can use the code to change the input high and low levels every 0.5 seconds to make the buzzer beep.
Here we use Thonny to remotely run the above Python code on Walnut Pie. For more information about how to run Python code on Walnut Pie, please refer to: Running Python Code
When running the code, you can hear the active buzzer making periodic "beeping" sounds.