element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Nico teWinkel's Blog Pi Pico Sound
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ntewinkel
  • Date Created: 7 Jun 2022 10:50 PM Date Created
  • Views 6493 views
  • Likes 11 likes
  • Comments 1 comment
  • raspberry pi pico
  • piezo
  • sound
  • pi pico
  • speaker
Related
Recommended

Pi Pico Sound

ntewinkel
ntewinkel
7 Jun 2022

I got a little side tracked while adding an extra feature to a project I've been working on.

The plan was to "quickly add" a little buzzer to sound when the timer hits zero.

However, being the interpreted language it is, Python kicked my butt again while I tried to add it to the main project, and all I got was an annoying never-ending nasty beep. I did not like that. The cat did not like that. The cat gave me a dirty look and left.

I know better than to keep hacking at it like that, so I did what I would recommend any good, not-stubborn, programmer should do in such cases, and that is to spin off the feature into a separate app.

Isolating the code to a test app was a good idea - it very quickly became very clear to me that I was calling the "sleep" function the wrong way. The examples on the line in the webs said "sleep(..)", when what the Pi Pico's microPython really wants is  "time.sleep(..)". Face palm

But hey! If it wasn't for that bit of annoyance I would not have thought of creating a separate little example, and the world would be one blog post the poorer for it.

So this one is REALLY easy, btw. Assuming you don't make the same typos I made. Rolling eyes

All you need it a Raspberry Pi Pico, and a little piezo speaker. Other speaker type things will probably work too. Maybe even an old set of headphones with the plug cut off if you happen to be hoarding one of those.

element14's stores have them:
The Raspberry Pi Pico: https://canada.newark.com/raspberry-pi/raspberry-pi-pico/raspberry-pi-board-arm-cortex/dp/22AJ1097
Piezo speakers: they have a whole line up of them at various price points, but here's one that's just a dollar: https://canada.newark.com/multicomp-pro/abt-414-rc/audio-transducer-2-4khz-85db/dp/93T7245

I also used a breadboard and some header pins to bring it all together, but those details are up to whatever you prefer. You could just solder it on directly as well.

Then simply connect the Piezo speaker between GP6 and the GND pin that's right beside it. Super easy!
The piezo speaker probably has a + marked on, so make sure you put that pin to GP6.
Mine has TWO plusses marked on it, so I can only assume this is important. I'm too scared to try it the other way around. Don't do it. They wouldn't be wasting ink like that if it wasn't life threatening, would they?

Ignore the extra wires in the picture - that's the other project still in progress. Focus on the piezo speaker.

Pi Pico with Piezo Sound

The sound is actually pretty decent considering the speaker and how little effort went into the coding.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

The test code I used just plays a few notes, but if you want to get fancy with it, there are some good examples online that include little melodies.
You would probably have to update the notes array to be a 2-dimensional array that holds the frequency as well as the duration of each note.

#
#  Play a note on a buzzer
#

from machine import Pin, PWM
import time


BUZZER_PIN = 6 # Piezo buzzer + is connected to GP6, - is connected to the GND right beside GP6
buzzer = PWM(Pin(BUZZER_PIN, Pin.OUT))

def playNote(frequency, duration, pause) :
    global buzzer
    buzzer.duty_u16(5000)  # adjust loudness: smaller number is quieter.
    buzzer.freq(frequency)
    time.sleep(duration)
    buzzer.duty_u16(0) # loudness set to 0 = sound off
    time.sleep(pause)
    

#notes = [440, 494, 523, 587, 659, 698, 784]
notes = [440, 523, 659, 784]

for note in notes :
    playNote(note, 0.1, 0.01)

Enjoy!
-Nico

  • Sign in to reply
Parents
  • DAB
    DAB over 3 years ago

    Good job, Nico.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • DAB
    DAB over 3 years ago

    Good job, Nico.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube