Pico Inventor's Kit Experiment 6 - Setting the Tone with a Piezo Buzzer

Experiment 6 from the Inventors Kit for Raspberry Pi Pico, in which we explore setting the tone with a piezo buzzer. Included in this resource are code downloads, a description of the experiment and also a video walk-through. This resource provides additional help and is not meant to replace the documentation that ships with the kit.

The Raspberry Pi Pico is a compact microcontroller based on the RP2040 processor - Raspberry Pi’s first in-house designed silicon. The RP2040 on the Pico uses 2 ARM processor cores, and also has 4 programmable IO controllers (PIO). These are like mini processors, and can help the Pico complete more complex tasks than it would otherwise be able to do. The Pico has 28 general purpose IO pins (GPIO), and 3 of these can also be used as Analogue inputs. The Pico can be programmed in several languages. The experiments in these resources are written in the Thonny Editor using MicroPython.

Pico Inventor's Kit Exp' 6 - Setting the Tone with a Piezo Buzzer

The Piezo Element Buzzer has to be driven with a signal that alternates in voltage (from 0V to 5V) at the desired frequency of the tone that wants to be produced. This experiment will explain how the Pico can do this. A potentiometer will be used as an input to control and alter the frequency of this tone.

  • To learn how to control the tone of a piezo buzzer using a PWM output.
  • To use a potentiometer to vary the frequency of the tone.

Video Walk-through:


Exp 6 - The Code

All experiments are coded in MicroPython in Thonny. To open in Thonny, copy and past the code below directly into the Thonny editor.

Kitronik Inventor's Kit Exp 6

Control the tone of a piezo buzzer with a pot.

We read the value of the analogue input A0, then rescale it to a different range.
The input is 0 - 65536, the output is scaled from 120Hz to 5kHz.
import machine
import math
import time

buzzer = machine.PWM(machine.Pin(15)) # Setup GP15 as the pin controlling the buzzer with a PWM output
buzzer.freq(1000) # set the frequency of the PWM signal driving the buzzer to 1 kHz
pot = machine.ADC(26) # Setup the analogue (A0) on GP26 with a human-readable name
prevFrequency = 0 buzzer.duty_u16(32767) # Set a 50% duty cycle for the buzzer to produce a consistent tone # Convert a value proportionally from one range to another def scale(value, fromMin, fromMax, toMin, toMax): return toMin + ((value - fromMin) * ((toMax - toMin) / (fromMax - fromMin))) while True: potValue = pot.read_u16() # This variable reads the voltage that the potentiometer is adjusted to # Convert analogue input to frequency between 120Hz and 5kHz frequency = scale(potValue, 0, 65535, 120, 5000) # Only change the frequency if the new value is definitely different from the previous one # This keeps the buzzer sounding a constant pitch when the potentiometer isn't moving if ((frequency < (prevFrequency - 50)) or (frequency > (prevFrequency + 50))): prevFrequency = frequency buzzer.freq(math.trunc(frequency)) # Change the frequency of the buzzer to the value set by the potentiometer time.sleep_ms(25)

Inventors Kit Extra Resources:

Each of the ten experiments has been designed to ease you into coding and physical computing for the for the Raspberry Pi Pico. The experiments have been chosen to cover the key concepts of physical computing and they also increase in difficulty as you progress. This resource has been put together to provide additional information for this experiment and has not been designed to replace the booklet. The majority of the information you will need to perform and understand this experiment is contained in the booklet.

If you are new to coding and physical computing, even the least complex examples can be quite challenging. With this in mind, we created walk-through videos for each of the experiments. Our presenter talks you through the circuit in a way that backs up the information given in the booklet but in a style that some might find easier to absorb. As well as having a video walk-through, each page also contains the code. Although it is always good to tackle the code yourself, it can be handy for testing your circuit quickly. The code has been heavily commented as an extra learning resource. To get the most out of the experiment, once you've tested your circuit have a go at coding the experiment from scratch.

Follow the links in the table below:

Exp No. Experiment Name.
Digital Inputs & Outputs.
Light Sensor & Analog Inputs.
Dimming an LED using a potentiometer.
Using a transistor to drive a motor.
Control a servo with a potentiometer.
Setting the tone with a piezo buzzer.
Using a seven segment display.
Exploring wind power.
Capacitor charge circuit.
Controlling ZIP LEDs.

Leave a comment

All comments are moderated before being published