Editing Piezo Speaker

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
= Description =
 
= Description =
 
A Piezo-Speaker has two connectors and can generate sounds if you apply a frequency. One is + and one is -. If you connect it to the ESP + is connected to the Pin and - to GND.
 
A Piezo-Speaker has two connectors and can generate sounds if you apply a frequency. One is + and one is -. If you connect it to the ESP + is connected to the Pin and - to GND.
With the speaker in the LMUBox the long Pin is +.
 
  
 
[[file:Piezo_Speaker_close_up.jpg|x200px]]
 
[[file:Piezo_Speaker_close_up.jpg|x200px]]
Line 11: Line 10:
  
 
= How to control it in MicroPython =
 
= How to control it in MicroPython =
 
== Basic code to generate a 500 Hz signal ==
 
<syntaxhighlight lang="python" line='line'>
 
from machine import Pin
 
from time import sleep, sleep_us, sleep_ms
 
 
# assume our speaker is connected to Pin 27, we use it as output
 
myLED = Pin(27, Pin.OUT)
 
 
#generate a 500Hz on/off signal
 
while True:
 
      # this switcheson for 1ms = 1000 us
 
      myLED.on()
 
      sleep_us(1000)
 
      # this switches off for 1 ms = 1000 us
 
      myLED.off()
 
      sleep_us(1000)
 
</syntaxhighlight>
 
 
== Code to play a melody - decoding notes ==
 
 
Example code shows how you can play a melody with the speaker. If you want to add higher notes see https://en.wikipedia.org/wiki/Piano_key_frequencies for the needed frequency. The example code was taken and edited from https://micropython-on-esp8266-workshop.readthedocs.io/en/latest/basics.html#beepers.
 
Example code shows how you can play a melody with the speaker. If you want to add higher notes see https://en.wikipedia.org/wiki/Piano_key_frequencies for the needed frequency. The example code was taken and edited from https://micropython-on-esp8266-workshop.readthedocs.io/en/latest/basics.html#beepers.
 
There are some issues with restarting PWM outputs - see [[Known Issues]].
 
  
 
<syntaxhighlight lang="python" line='line'>
 
<syntaxhighlight lang="python" line='line'>
Line 68: Line 45:
 
[[file:Piezo_Speaker.jpg|300px]]
 
[[file:Piezo_Speaker.jpg|300px]]
 
[[file:Piezo_Speaker_close_up.jpg|300px]]
 
[[file:Piezo_Speaker_close_up.jpg|300px]]
 
= Related Tutorial Videos =
 
<youtube>HPQMvL1SP_c</youtube>
 

Please note that all contributions to Sketching with Hardware at LMU Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see My wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)