038 - MicroPython TechNotes: Buzzer

Introduction

In this article, we will talk about the BUZZER module with ESP32 using MicroPython.

A BUZZER module is basically an electrical device that converts electrical signal into an audible audio signal or a sound that we can hear. Typical uses of Buzzer are in alarms, user audio feedback, or to provide some kind of melodies.

Pinout

  1. G – for the ground pin.
  2. V – for the supply voltage.
  3. S – for the signal pin.

Bill Of Materials

  1. ESP32 development board.
  2. Gorillacell ESP32 shield.
  3. 3-pin female-female dupont jumper wires.
  4. Gorillacell buzzer moduel.

Hardware Instruction

  1. Provide power to the buzzer module through the G-pin and V-pin.
  2. Attach the buzzer S-pin to ESP32 GPIO 23.

Software Instruction

  1. Copy the source code and paste it to your Thonny IDE and click the RUN button.
  2. Modify it according to your liking and most of all, enjoy.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Example # 1, exploring the basics of controlling the buzzer through PWM:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin
 5from machine import PWM
 6from time import sleep_ms
 7
 8buzzer = PWM(Pin(23, Pin.OUT))
 9buzzer.init(freq=0, duty=0)
10
11# ******************************************************************
12# The following should be explored using the REPL:
13# ******************************************************************
14# # 1. To set a pwm frequency which denotes what musical note to play
15# buzzer.freq(1047) # Play C note at 6th octave.
16# 
17# # 2. Set the volume by changing the pwm duty value
18# buzzer.duty(512)
19# 
20# # 3. Let the sound ring for a certain duration
21# sleep_ms(1000)
22# 
23# # 4. Turn off the pulse by setting the duty value to 0
24# buzzer.duty(0)
25# 
26# # 5. Or play a different musical note, lets say C note at 2nd octave
27# buzzer.init(freq=69, duty=512)
28# sleep_ms(1000)
29# buzzer.duty(0)
30# 
31# # 6. or play a C note at 8th octave
32# buzzer.init(freq=4186, duty=512)
33# sleep_ms(1000)
34# buzzer.duty(0)
35# 
36# # 7. And to disable the pwm driver.
37# buzzer.deinit()
  1# More details can be found in TechToTinker.blogspot.com 
  2# George Bantique | tech.to.tinker@gmail.com
  3
  4from machine import Pin
  5from machine import PWM
  6from time import sleep_ms
  7
  8
  9class GORILLACELL_BUZZER: 
 10    def __init__(self, sig_pin):
 11        self.pwm = PWM(Pin(sig_pin, Pin.OUT))
 12        
 13    def play(self, melodies, wait, duty):
 14        for note in melodies:
 15            self.pwm.freq(note)
 16            self.pwm.duty(duty)
 17            sleep_ms(wait)
 18        # Disable the pulse, setting the duty to 0
 19        self.pwm.duty(0)
 20        # Disconnect the pwm driver
 21        #self.pwm.deinit() # remove to play the next melodies
 22
 23# Notes and its equivalent frequency
 24B0  = 31
 25C1  = 33
 26CS1 = 35
 27D1  = 37
 28DS1 = 39
 29E1  = 41
 30F1  = 44
 31FS1 = 46
 32G1  = 49
 33GS1 = 52
 34A1  = 55
 35AS1 = 58
 36B1  = 62
 37C2  = 65
 38CS2 = 69
 39D2  = 73
 40DS2 = 78
 41E2  = 82
 42F2  = 87
 43FS2 = 93
 44G2  = 98
 45GS2 = 104
 46A2  = 110
 47AS2 = 117
 48B2  = 123
 49C3  = 131
 50CS3 = 139
 51D3  = 147
 52DS3 = 156
 53E3  = 165
 54F3  = 175
 55FS3 = 185
 56G3  = 196
 57GS3 = 208
 58A3  = 220
 59AS3 = 233
 60B3  = 247
 61C4  = 262
 62CS4 = 277
 63D4  = 294
 64DS4 = 311
 65E4  = 330
 66F4  = 349
 67FS4 = 370
 68G4  = 392
 69GS4 = 415
 70A4  = 440
 71AS4 = 466
 72B4  = 494
 73C5  = 523
 74CS5 = 554
 75D5  = 587
 76DS5 = 622
 77E5  = 659
 78F5  = 698
 79FS5 = 740
 80G5  = 784
 81GS5 = 831
 82A5  = 880
 83AS5 = 932
 84B5  = 988
 85C6  = 1047
 86CS6 = 1109
 87D6  = 1175
 88DS6 = 1245
 89E6  = 1319
 90F6  = 1397
 91FS6 = 1480
 92G6  = 1568
 93GS6 = 1661
 94A6  = 1760
 95AS6 = 1865
 96B6  = 1976
 97C7  = 2093
 98CS7 = 2217
 99D7  = 2349
100DS7 = 2489
101E7  = 2637
102F7  = 2794
103FS7 = 2960
104G7  = 3136
105GS7 = 3322
106A7  = 3520
107AS7 = 3729
108B7  = 3951
109C8  = 4186
110CS8 = 4435
111D8  = 4699
112DS8 = 4978
113
114# This is the list of notes for mario theme
115# 0 denotes rest notes
116mario = [
117     E7, E7,  0, E7,  0, C7, E7,  0,
118     G7,  0,  0,  0, G6,  0,  0,  0,
119     C7,  0,  0, G6,  0,  0, E6,  0,
120      0, A6,  0, B6,  0,AS6, A6,  0,
121     G6, E7,  0, G7, A7,  0, F7, G7,
122      0, E7,  0, C7, D7, B6,  0,  0,
123     C7,  0,  0, G6,  0,  0, E6,  0,
124      0, A6,  0, B6,  0,AS6, A6,  0,
125     G6, E7,  0, G7, A7,  0, F7, G7,
126      0, E7,  0, C7, D7, B6,  0,  0,
127    ]
128
129# This is the list of notes for jingle bells
130jingle = [
131    E7, E7, E7, 0,
132    E7, E7, E7, 0,
133    E7, G7, C7, D7, E7, 0,
134    F7, F7, F7, F7, F7, E7, E7, E7, E7, D7, D7, E7, D7, 0, G7, 0,
135    E7, E7, E7, 0,
136    E7, E7, E7, 0,
137    E7, G7, C7, D7, E7, 0,
138    F7, F7, F7, F7, F7, E7, E7, E7, G7, G7, F7, D7, C7, 0 
139    ]
140
141# This is the list of notes for Twinkle, Twinkle Little Star
142twinkle = [
143    C6, C6, G6, G6, A6, A6, G6, 0,
144    F6, F6, E6, E6, D6, D6, C6, 0,
145    G6, G6, F6, F6, E6, E6, D6, 0,
146    G6, G6, F6, F6, E6, E6, D6, 0,
147    C6, C6, G6, G6, A6, A6, G6, 0,
148    F6, F6, E6, E6, D6, D6, C6, 0,
149    ]
150
151# Instantiate a buzzer object which is attached on GPIO 23
152buzzer = GORILLACELL_BUZZER(23)
153
154print("Playing mario.")
155buzzer.play(mario, 150, 512)
156sleep_ms(1000)
157
158print("Playing jingle bells.")
159buzzer.play(jingle, 250, 512)
160sleep_ms(1000)
161
162print("Playing twinkle, twinkle little star.")
163buzzer.play(twinkle, 600, 512)

References And Credits

  1. Purchase your Gorillacell ESP32 development kit at: https://gorillacell.kr

  2. MicroPython PWM references: https://docs.micropython.org/en/v1.15/esp32/tutorial/pwm.html https://docs.micropython.org/en/latest/library/machine.PWM.html



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.