Passive Buzzer with Raspberry PI Pico and MicroPython

With Passive Buzzer you may give your Raspberry PI Pico an inexpensive technique to emit sounds and modulated them in accordance with your wants. It's so versatile that you may additionally allow you to RPI Pico singing!

On this tutorial, I’m going to indicate you the best way to join and use a passive buzzer with Raspberry PI Pico utilizing MicroPython.

The passive buzzer is an inexpensive and easy DC powered component capable of generate sound at completely different frequencies if triggered with low voltage present.

passive buzzer

A passive buzzer differs from an lively buzzer as a result of lively one has an inner oscillator capable of generate a “normal” sound when merely activated with a steady set off. Passive buzzer, however, wants a sq. wave set off (PWM).

Raspberry PI Pico has lots of ports capable of generate PWM (pulse-width modulation), as you possibly can see from Raspberry PI Pico pinout.

Please think about that Raspberry PI Pico is a microcontroller, completely different from Raspberry PI laptop boards (like Raspberry PI Zero W, RPI 4 mannequin B, and so forth). For these boards, you'll confer with my Use Passive Buzzer with Raspberry PI and Python tutorial.

What We Want

As traditional, I counsel including from now to your favorite e-commerce procuring cart all wanted {hardware}, in order that on the finish it is possible for you to to guage total prices and resolve if persevering with with the challenge or eradicating them from the procuring cart. So, {hardware} will likely be solely:

raspberry pi pico microcontroller

Relating to passive buzzer, you may also consider a budget Elegoo starter kit, which incorporates numerous sensors helpful to your electronics tasks.

Verify {hardware} costs with the next hyperlinks:

Amazon raspberry pi boards box
amazon raspberry pi pico box
Amazon passive buzzer
Amazon Breadboard box
Amazon Dupont Wiring box

Wiring Diagram

Following image exhibits the wiring diagram adopted:

raspberry pi pico passive buzzer wiring

Word that passive buzzer have a “+” signal on high which establish the optimistic aspect, to be linked to PWM pin.

Please discover beneath some extra photos from my wiring:

raspberry pi pico passive buzzer details 01
raspberry pi pico passive buzzer details 02
raspberry pi pico passive buzzer details 03

Step-by-Step Process

Put together cabling in accordance with the earlier paragraph. Join RPI Pico to Thonny (you possibly can confer with my tutorial concerning the First steps with Raspberry PI Pico).

Get and Perceive my Code

Now obtain following file to make use of your passive buzzer:

You possibly can each load it in your Raspberry PI Pico storage or run it out of your laptop.

I’ll clarify all code strains within the following paragraphs. It's actually easy as passive buzzer juste requires a PWM set off to run.

First strains import required libraries:

from machine import Pin, PWM
from time import sleep

Then the PIN used to set off optimistic aspect on buzzer is related to a variable for higher code understanding and administration. Keep in mind to make use of your GPIO quantity if you happen to use a distinct pin.

The second line initializes the PWM port with a PWM object:


In my code, I additionally outlined a operate to make easy utilizing the passive buzzer. It takes in enter 4 parameters:

  • buzzerPinObject: a PWM PIN object initialized as in earlier line
  • frequency: the frequency that needs to be carried out from our buzzer
  • sound_duration: how lengthy the sound should run
  • silence_duration: if required, how lengthy the sound ought to cease after the passive buzzer has run. If no silence is required, this may be set to 0 (zero)

I activate the passive buzzer with an obligation cycle completely different from zero, whereas I deactivate it with an obligation cycle to 0. Sound period and silence period are then carried out with “sleep” statements which merely waits sustaining earlier settings. This operate is kind of straightforward to learn:

def buzzer(buzzerPinObject,frequency,sound_duration,silence_duration):


Following strains use our operate by enjoying some notes:

 #C (DO)

 #D (RE)

buzzer(BuzzerObj,659,0.5,0.1) #E (MI)
buzzer(BuzzerObj,698,0.5,0.1) #F (FA)

buzzer(BuzzerObj,784,0.5,0.1) #G (SOL)
buzzer(BuzzerObj,880,0.5,0.1) #A (LA)
buzzer(BuzzerObj,987,0.5,0.1) #B (SI)

You possibly can attempt, for instance, some musical notes being approssimatively:

A (LA)440
B (SI)494
C (DO)523
D (RE)587
E (MI)659
F (FA)698
G (SOL)784

Final line releases the PWM pin by de-initilizing it:


Working the Script

Run this script in your Thonny IDE (F5) and you'll begin listening your passive buzzer emitting notes sound.

Bonus Instance: Raspberry PI Pico Singing Beethoven

A humorous means to make use of my script is performing advanced sounds with the notes from earlier desk (and/or from its reference hyperlink on the backside of desk). I’ve ready 2 scripts which can carry out two of most well-known symphony from Beethowen: Für Elise and Ode to Pleasure!

In a different way from my “base” script, as a substitute of passing immediately frequencies to the buzzer() operate, I added a translation desk which converts notes in numbers, the place numbers correspond to frequencies.

For instance, I outline the DO#5 as:


I exploit the “d” as a substitute of “#” char as a result of the ashtag in python means a remark.

This notice is then run on this new model of script with the next line:


Now, let’s make our Pico singing. Under are the 2 downloadable scripts:

Dowload them, use into your Raspberry PI Pico.

Following YouTube video present end result.

Fur Ode To Pleasure:

Fur Elise:

Take pleasure in!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *