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
- G – for the ground pin.
- V – for the supply voltage.
- S – for the signal pin.
Bill Of Materials
- ESP32 development board.
- Gorillacell ESP32 shield.
- 3-pin female-female dupont jumper wires.
- Gorillacell buzzer moduel.
Hardware Instruction
- Provide power to the buzzer module through the G-pin and V-pin.
- Attach the buzzer S-pin to ESP32 GPIO 23.
Software Instruction
- Copy the source code and paste it to your Thonny IDE and click the RUN button.
- 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()
2. Example # 2, play some popular melodies:
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
-
Purchase your Gorillacell ESP32 development kit at: https://gorillacell.kr
-
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
- 049 - MicroPython TechNotes: MP3 Player
- 048 - MicroPython TechNotes: Analog Touch Sensor
- 047 - MicroPython TechNotes: E108 GPS
- 046 - MicroPython TechNotes: RF433 Transceivers
- 045 - MicroPython TechNotes: Infrared Transmitter
- 044 - MicroPython TechNotes: Infrared Receiver
- 043 - MicroPython TechNotes: ESP12E WiFi | External WiFi module
- 042 - MicroPython TechNotes: JDY-32 | Bluetooth Low Energy BLE
- 041 - MicroPython TechNotes: Bluetooth HC-06
- 040 - MicroPython TechNotes: Relay
- 039 - MicroPython TechNotes: Electromagnet
- 037 - MicroPython TechNotes: Servo Motor
- 036 - MicroPython TechNotes: Stepper Motor
- 035 - MicroPython TechNotes: Dual Motor Driver
- 034 - MicroPython TechNotes: DC Motors | Gear Motor and Fan Motor
- 033 - MicroPython TechNotes: TCS34725 RGB Color Sensor
- 032 - MicroPython TechNotes: BMP280 Sensor
- 031 - MicroPython TechNotes: TOF Distance Sensor
- 030 - MicroPython TechNotes: DS3231 RTC
- 029 - MicroPython TechNotes: HC-SR04 Ultrasonic Sensor
- 028 - MicroPython TechNotes: DHT11 Temperature and Humidity Sensor
- 027 - MicroPython TechNotes: Rotary Encoder
- 026 - MicroPython TechNotes: Light Dependent Resistor (LDR)
- 025 - MicroPython TechNotes: Joystick
- 024 - MicroPython TechNotes: Slider Switch
- 023 - MicroPython TechNotes: Continuous Rotation Potentiometer
- 022 - MicroPython TechNotes: Potentiometer | Reading an Analog Input
- 021 - MicroPython TechNotes: Color Touch Sensor
- 020 - MicroPython TechNotes: Touch Sensor
- 019 - MicroPython TechNotes: Switch Module
- 018 - MicroPython TechNotes: Button | Reading an Input
- 017 - MicroPython TechNotes: LASER Module
- 016 - MicroPython TechNotes: RGB LED Matrix
- 015 - MicroPython TechNotes: Neopixel 16
- 014 - MicroPython TechNotes: 8x8 Dot Matrix Display (I2C)
- 013 - MicroPython TechNotes: 8x16 Dot Matrix Display (SPI)
- 012 - MicroPython TechNotes: 8x8 Dot Matrix Display (SPI)
- 011 - MicroPython TechNotes: 1.3 OLED Display
- 010 - MicroPython TechNotes: 0.96 OLED Display
- 009 - MicroPython TechNotes: 7 Segment Display
- 008 - MicroPython TechNotes: 16x2 LCD
- 007 - MicroPython TechNotes: RGB LED
- 006 - MicroPython TechNotes: Traffic Light LED Module
- 005 - MicroPython TechNotes: Gorilla Cell LED | MicroPython Hello World
- 004 - MicroPython TechNotes: Gorilla Cell I/O Devices
- 003 - MicroPython TechNotes: Gorillacell ESP32 Shield
- 002 - MicroPython TechNotes: Introduction for Gorillacell ESP32 Dev Kit
- 001 - MicroPython TechNotes: Get Started with MicroPython
- 000 - MicroPython TechNotes: Unboxing Gorillacell ESP32 Development Kit
No comments yet!