It's easy to use the STEMMA Piezo Driver Amp with Python or CircuitPython, and the pwmio module. This module allows you to easily write Python code to control pulse width modulation (PWM).
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
First wire up a driver to your board exactly as follows along with a piezo element. The following is the driver wired to a Feather RP2040 using the STEMMA JST-PH connector:
- Board 3.3V to driver VIN (red wire)
- Board GND to driver GND (black wire)
- Board pin 5 to driver SIG (white wire)
- Driver VO+ to piezo positive (red wire)
- Driver VO- to piezo negative (black wire)
The following is the adapter wired to a Feather RP2040 using a solderless breadboard:
- Board 3.3V to driver VIN (red wire)
- Board GND to driver GND (black wire)
- Board pin 5 to driver SIG (white wire)
- Driver VO+ to piezo positive (red wire)
- Driver VO- to piezo negative (black wire)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported. pwmio will also need to be implemented for your board.
Here's the Raspberry Pi wired using the STEMMA JST-PH connector:
- Pi 3.3V to driver VIN (red wire)
- Pi GND to driver GND (black wire)
- Pi GPIO 5 to driver SIG (white wire)
- Driver VO+ to piezo positive (red wire)
- Driver VO- to piezo negative (black wire)
Here's the Raspberry Pi using a solderless breadboard:
- Pi 3.3V to driver VIN (red wire)
- Pi GND to driver GND (black wire)
- Pi GPIO 5 to driver SIG (white wire)
- Driver VO+ to piezo positive (red wire)
- Driver VO- to piezo negative (black wire)
Python Setup
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
pwmio
is a built-in module and does not need to be installed separately using pip
. Make sure to check that pwmio
is supported on your platform though if you aren't using something more common like a Raspberry Pi.
CircuitPython Usage
To use with CircuitPython, you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the code.py file in a zip file. Extract the contents of the zip file, and copy the code.py file to your CIRCUITPY drive. The example uses built-in modules, so no additionally libraries need to be copied to the CIRCUITPY/lib folder.
Python Usage
Copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board import pwmio piezo = pwmio.PWMOut(board.D5, duty_cycle=0, frequency=440, variable_frequency=True) while True: for f in (262, 294, 330, 349, 392, 440, 494, 523): piezo.frequency = f piezo.duty_cycle = 65535 // 2 # On 50% time.sleep(0.25) # On for 1/4 second piezo.duty_cycle = 0 # Off time.sleep(0.05) # Pause between notes time.sleep(0.5)
When you run the example code, you'll hear an ascending C major scale through the piezo element on a loop. Use the gain DIP switch to adjust the gain for the piezo element.
Python Note
If you run this example on a Raspberry Pi, you'll see this printed to the serial console when the code starts running:
Variable Frequency is not supported, continuing without it...
After that you'll hear the tones being played through the piezo element.
Text editor powered by tinymce.