Oct. 4, 2023
Create a system, where pressing one button plays “Row, row, row your boat” and another button plays “Mary had a little lamb” by sending out the correct frequencies on a buzzer.
Component | Amount |
---|---|
ESP32-S3-WROOM | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
Passive Buzzer | 1 |
NPN Transistor | 1 |
Wire M-M | 8 |
Big Push Button | 2 |
Resistor - 1K Ω | 1 |
Resistor - 10K Ω | 4 |
from machine import Pin, PWM
import time
button_a=Pin(21,Pin.IN,Pin.PULL_UP)
button_b=Pin(47,Pin.IN,Pin.PULL_UP)
passiveBuzzer=PWM(Pin(3),2000)
interval = 450 # set metronome interval
passiveBuzzer.deinit()
def C(oct): # freq for C
if oct == 0:
return 16
if oct == 1:
return 33
if oct == 2:
return 65
if oct == 3:
return 131
if oct == 4:
return 262
if oct == 5:
return 523
if oct == 6:
return 1047
if oct == 7:
return 2093
if oct == 8:
return 4186
def D(oct): # freq for D
if oct == 3:
return 147
if oct == 4:
return 294
def E(oct): # freq for E
if oct == 3:
return 165
if oct == 4:
return 330
def F(oct): # freq for F
if oct == 3:
return 175
if oct == 4:
return 349
def G(oct): # freq for G
if oct == 3:
return 196
if oct == 4:
return 392
def A(oct): # freq for A
if oct == 3:
return 220
if oct == 4:
return 440
def B(oct): # freq for B
if oct == 3:
return 247
if oct == 4:
return 494
def hold(n,t): # play frequency for given length
passiveBuzzer.init()
passiveBuzzer.freq(n)
time.sleep_ms(int(t*interval)-20)
passiveBuzzer.deinit()
time.sleep_ms(20)
def rowBoat(): # "Row, Row, Row your Boat" - song
hold(C(4),1)
hold(C(4),1)
hold(C(4),0.75)
hold(D(4),0.25)
hold(E(4),1)
hold(E(4),0.75)
hold(D(4),0.25)
hold(E(4),0.75)
hold(F(4),0.25)
hold(G(4),2)
hold(C(5),0.33)
hold(C(5),0.33)
hold(C(5),0.33)
hold(G(4),0.33)
hold(G(4),0.33)
hold(G(4),0.33)
hold(E(4),0.33)
hold(E(4),0.33)
hold(E(4),0.33)
hold(C(4),0.33)
hold(C(4),0.33)
hold(C(4),0.33)
hold(G(4),0.75)
hold(F(4),0.25)
hold(E(4),0.75)
hold(D(4),0.25)
hold(C(4),2)
def littleLamb(): # Mary had a little Lamb" - song
hold(E(4),1)
hold(D(4),1)
hold(C(4),1)
hold(D(4),1)
hold(E(4),1)
hold(E(4),1)
hold(E(4),2)
hold(D(4),1)
hold(D(4),1)
hold(D(4),2)
hold(E(4),1)
hold(G(4),1)
hold(G(4),2)
hold(E(4),1)
hold(D(4),1)
hold(C(4),1)
hold(D(4),1)
hold(E(4),1)
hold(E(4),1)
hold(E(4),1)
hold(E(4),1)
hold(D(4),1)
hold(D(4),1)
hold(E(4),1)
hold(D(4),1)
hold(C(4),2)
try:
while True: # listen for respective button pressing
if not button_a.value():
rowBoat()
if not button_b.value():
littleLamb()
except:
passiveBuzzer.deinit()
Create a system that communicates via UART to another system to activate their buzzer and LED and also to receive input from that system to activiate the local buzzer and LED. This should be done by pressing buttons on each respective board. In this case I worked with Jonah, who’s lab 3.2 project is visible here.
Component | Amount |
---|---|
ESP32-S3-WROOM | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
Green LED | 1 |
NPN Transistor | 1 |
Active Buzzer | 1 |
Push Button | 1 |
Wire M-M | 10 |
Resistor - 220 Ω | 1 |
Resistor - 1K Ω | 1 |
Resistor - 10k Ω | 2 |
Jonah’s Project | 1 |
from machine import Pin, PWM, UART
import math
import time
PI = 3.14
led = Pin(1, Pin.OUT)
button = Pin(21, Pin.IN,Pin.PULL_UP)
passiveBuzzer=PWM(Pin(3),2000)
passiveBuzzer.deinit()
usart_flag=0
# Initialize UART class
myUsart = UART(1, baudrate=115200, bits=8, parity=0, rx=5, tx=2, timeout=10)
def alert(): # buzzer sound control
for x in range(0,136):
sinVal=math.sin(x*10*PI/180)
toneVal=2000+int(sinVal*500)
passiveBuzzer.freq(toneVal)
time.sleep_ms(10)
while True:
if myUsart.any(): # if we receive signal through the rx
usart_buffer=myUsart.read()
passiveBuzzer.init()
led.value(1) # then light up LED
alert() # and play sound on buzzer
usart_flag=1
if usart_flag==1: # stop process after initiating it once
passiveBuzzer.deinit()
led.value(0)
usart_flag=0
if not button.value(): # if button pressed, send signal over the tx
myUsart.write("1\r\n")
time.sleep_ms(200)
See Jonah’s Source Code for receiving/sending counterpart (in lab 3).