Oct. 36, 2023
See image for 21.1
Create a system where distance measured by an ultrasonic sensor is identified with notes and LEDs.
Component | Amount |
---|---|
ESP32-S3-WROOM | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
LED Bar Graph | 1 |
Ultrasonic Sensor | 1 |
Passive Buzzer | 1 |
NPN Transistor | 1 |
Resistor 1K Ω | 1 |
Resistor 220 Ω | 10 |
Wires M-M | 16 |
# supporting scripts
from hcsr04 import SR04
from machine import Pin,PWM
import time
# ultrasonic sensor initializations
SR=SR04(16,15)
time.sleep_ms(2000)
# buzzer initializations
buzz = PWM(Pin(14),2000)
buzz.deinit()
def LEDs(n): # change LED lighting
pins=[21,47,48,38,39,40,41,42,2,1]
for i in range(len(pins)):
led = Pin(pins[i],Pin.OUT)
if n < i:
led.value(1)
else:
led.value(0)
time.sleep_ms(20)
noten = [262,294,330,349,392,440,494,523] # do, re, mi, ...
try:
buzz.init()
buzz.duty(50)
buzz.freq(262)
while True:
dis = SR.distance()
print(dis)
time.sleep_ms(500)
if dis < 5:
buzz.freq(262)
LEDs(1)
elif dis >= 5 and dis < 10:
buzz.freq(294)
LEDs(2)
elif dis >= 10 and dis < 15:
buzz.freq(330)
LEDs(3)
elif dis >= 15 and dis < 20:
buzz.freq(349)
LEDs(4)
elif dis >= 20 and dis < 25:
buzz.freq(392)
LEDs(5)
elif dis >= 25 and dis < 30:
buzz.freq(440)
LEDs(6)
elif dis >= 30 and dis < 35:
buzz.freq(494)
LEDs(7)
elif dis >= 35:
buzz.freq(523)
LEDs(8)
except Exception as e:
print("Error:", e)
buzz.deinit()
Create a system where an infrared remote controls several different things including a buzzer, a motor, a neopixel, and an LCD1602.
Component | Amount |
---|---|
ESP32-S3-WROOM | 1 |
GPIO Extension Board | 1 |
Project Board | 2 |
9V Battery | 1 |
LCD Module | 1 |
LED Neopixel | 1 |
DC Motor | 1 |
L293D Module | 1 |
Infrared Receiver | 1 |
Infrared Remote | 1 |
Passive Buzzer | 2 |
NPN Transistor | 2 |
Resistor 1K Ω | 2 |
Resistor 10K Ω | 1 |
Wires M-F | 7 |
Wires M-M | 15 |
Fan component for motor made by: Jonah Watts
# supporting scripts
from irrecvdata import irGetCMD
from machine import I2C, Pin, PWM
from I2C_LCD import I2cLcd
from notes import *
import neopixel
import time
# LCD1602 initializations
i2c = I2C(scl=Pin(18), sda=Pin(8), freq=400000)
devices = i2c.scan()
if len(devices) == 0:
print("No i2c device !")
else:
for device in devices:
print("I2C addr: "+hex(device))
lcd = I2cLcd(i2c, device, 2, 16)
# buzzer initializations
myWay = [ # from "My Way" by Frank Sinatra
[G(4),C(2)],[],[1,0],[1,A_(2)],[],[1,0],
[1,F(3)],[],[1,0],[1,B(2)],[],[0,0],
[E(4),G(3)],[],[1,0],[1,A_(2)],[],[0,0],
[F(4),E(3)],[],[0,0,],[G(4),A_(2)],[],[0,0],
#17
[G(4),F(2)],[],[1,0],[1,A_(2)],[],[1,0],
[1,D(3)],[A_(4),1],[D(5),0],[G(5),A_(2)],[B(5),1],[D(6),0],
[G(6),F(3)],[],[1,0],[1,A_(2)],[],[1,0],
[1,D(3)],[],[1,0],[1,A_(2)],[],[0,0],
#18
[F(4),F(2)],[],[1,0],[1,A(2)],[],[1,0],
[1,C(3)],[],[1,0],[1,A(2)],[],[1,0],
[1,F(3)],[],[1,0],[1,A(2)],[],[0,0],
[0,C(3)],[],[0,0]
]
marioNotes = [ # from "Super Mario Bros: Main Theme"
[0,C(3)],[],[],[],[],[0,0],
[G(5),0],[],[0,0],[F_(5),G(3)],[],[0,0],
[F(5),0],[],[0,0],[D_(5),0],[],[],
[1,C(4)],[],[0,1],[E(5),1],[],[0,0],
[0,F(3)],[],[0,1],[G_(4),1],[],[0,0],
[A(4),0],[],[0,0],[C(5),C(4)],[],[0,0],
[0,C(4)],[],[0,1],[A(4),1],[],[0,0],
[C(5),F(3)],[],[0,1],[D(5),1],[],[0,0],
[0,C(3)],[],[],[],[],[0,0],
[G(5),0],[],[0,0],[F_(5),G(3)],[],[0,0],
[F(5),0],[],[0,0],[D(5),0],[],[1,0],
[1,G(3)],[],[0,0],[E(5),C(4)],[],[0,0],
[],[],[],[C(6),G(4)],[],[1,1],
[],[],[0,0],[C(6),G(4)],[],[0,0],
[C(6),G(4)],[],[],[],[],[0,0],
[0,G(3)],[],[],[],[],[0,0]
]
songs = [myWay,marioNotes]
current = 0
dut = 30
pB1 = PWM(Pin(1),2000) # for treble
pB2 = PWM(Pin(2),1000) # for bass
pB1.duty(dut)
pB2.duty(dut)
pB1.deinit() # turn off initial sound
pB2.deinit() # "
incre = 0
def Play(song, inc): # play song in array format
print(inc,song[inc])
if len(song[inc]) == 2:
if not song[inc][0] == 0 and not song[inc][0] == 1:
pB1.init()
pB1.freq(song[inc][0])
elif not song[inc][0] == 1:
pB1.deinit()
if not song[inc][1] == 0 and not song[inc][1] == 1:
pB2.init()
pB2.freq(song[inc][1])
elif not song[inc][1] == 1:
pB2.deinit()
return len(song)
# infrared remote initializations
infrar = irGetCMD(10)
# neoPixel initializations
led = Pin(16, Pin.OUT)
np = neopixel.NeoPixel(led, 8)
colours = [[255,255,255], # white
[255,0,0], # red
[255,30,0], # orange
[255,100,0], # yellow
[0,255,0], # green
[0,0,255], # blue
[20,0,40], # indigo
[90,10,100], # violet
[0,0,0]] # off
pixCol = 0
pixPos = 0
def neoDisplay(): # change neopixel menu display
if pixCol == 0:
col = "W"
elif pixCol == 1:
col = "R"
elif pixCol == 2:
col = "O"
elif pixCol == 3:
col = "Y"
elif pixCol == 4:
col = "G"
elif pixCol == 5:
col = "B"
elif pixCol == 6:
col = "I"
elif pixCol == 7:
col = "V"
elif pixCol == 8:
col = "-"
string = "|pos:" + str(pixPos + 1) + " |col:" + col + " "
return string
# DC motor initializations
in1Pin=Pin(13, Pin.OUT)
in2Pin=Pin(14, Pin.OUT)
enablePin=Pin(12, Pin.OUT)
pwm=PWM(enablePin,10000)
def driveMotor(dir,spd):
if dir:
in1Pin.value(1)
in2Pin.value(0)
else :
in1Pin.value(0)
in2Pin.value(1)
pwm.duty(spd)
menu = True
motor = False
music = False
light = False
try:
while True:
if menu: # main menu -------------------------------------
lcd.move_to(0, 0)
lcd.putstr("> Menu <")
lcd.move_to(0, 1)
lcd.putstr("1-BUZ 2-DC 3-LED")
while menu:
reding = infrar.ir_read()
if reding:
if reding == "0xff30cf":
print("1-BUZ")
menu = False
music = True
elif reding == "0xff18e7":
print("2-DC")
menu = False
motor = True
elif reding == "0xff7a85":
print("3-LED")
menu = False
light = True
elif music: # music menu -----------------------------------
exMusic = False
while music:
lcd.move_to(0, 0)
lcd.putstr("> Buzzer Music <")
lcd.move_to(0, 1)
lcd.putstr(" ~MyWay Mario ")
while True:
reding = infrar.ir_read()
if reding == "0xffe01f" and current: # <<
lcd.move_to(0, 1)
lcd.putstr(" ~MyWay Mario ")
current = 0
elif reding == "0xffa857": # play
break
elif reding == "0xff906f" and not current: # >>
lcd.move_to(0, 1)
lcd.putstr(" MyWay ~Mario ")
current = 1
elif reding == "0xff30cf": # 1
current = 0
break
elif reding == "0xff18e7": # 2
current = 1
break
elif reding == "0xffc23d": # back
exMusic = True
break
if exMusic:
menu = True
music = False
break
m = Play(songs[current], incre)
incre += 1
time.sleep_ms(15)
lcd.move_to(0, 0)
lcd.putstr("Now Playing: ")
lcd.move_to(0, 1)
if current:
lcd.putstr("~ Mario Theme ")
speed = 4
else:
lcd.putstr("~ My Way ")
speed = 9
while True:
if incre >= m:
break
for i in range(speed):
if i == (speed-1):
Play(songs[current], incre)
incre += 1
time.sleep_ms(15)
elif light: # light menu -----------------------------------------
lcd.move_to(0, 0)
lcd.putstr("> LED Control <")
lcd.move_to(0, 1)
lcd.putstr("|pos:1 |col:W ")
while light:
reding = infrar.ir_read()
if reding:
print(reding)
if reding == "0xff6897": # 0 - off
print("OFF")
for i in range(8):
np[i] = colours[8]
np.write()
time.sleep_ms(30)
elif reding == "0xffe01f": # << - back
if pixPos > 0:
pixPos -= 1
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xffa857": # |> - enter
np[pixPos] = colours[pixCol]
np.write()
time.sleep_ms(30)
elif reding == "0xff906f": # >> - forward
if pixPos < 7:
pixPos += 1
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xffb04f": # C - colour
if pixCol < 8:
pixCol += 1
else:
pixCol = 0
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff30cf": # 1
pixPos = 0
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff18e7": # 2
pixPos = 1
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff7a85": # 3
pixPos = 2
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff10ef": # 4
pixPos = 3
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff38c7": # 5
pixPos = 4
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff5aa5": # 6
pixPos = 5
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff42bd": # 7
pixPos = 6
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff4ab5": # 8
pixPos = 7
lcd.move_to(0, 1)
lcd.putstr(neoDisplay())
elif reding == "0xff52ad": # 9 - all
for i in range(8):
np[i] = colours[pixCol]
np.write()
time.sleep_ms(30)
elif reding == "0xffc23d": # back
menu = True
light = False
elif motor: # motor menu ----------------------------------------
while motor:
lcd.move_to(0, 0)
lcd.putstr(">DC Motor Ctrl <")
lcd.move_to(0, 1)
lcd.putstr(" << >> ")
reding = infrar.ir_read()
if reding:
if reding == "0xffe01f": # <<
print("turn left")
driveMotor(0,1000)
for i in range(5,0,-1):
lcd.move_to(0, 0)
lcd.putstr("turning left ")
lcd.move_to(0, 1)
lcd.putstr("for: " + str(i) + " seconds ")
time.sleep_ms(1000)
driveMotor(0,0)
elif reding == "0xff906f": # >>
print("turn right")
driveMotor(0,1000)
for i in range(5,0,-1):
lcd.move_to(0, 0)
lcd.putstr("turning right ")
lcd.move_to(0, 1)
lcd.putstr("for: " + str(i) + " seconds ")
time.sleep_ms(1000)
driveMotor(0,0)
elif reding == "0xffc23d": # back
menu = True
motor = False
except Exception as e:
print("Error:", e)
pB1.deinit()
pB2.deinit()
pwm.deinit()
Additional file: notes.py
Follow this youtube link for the video: https://youtu.be/PBbuSIWp3Nw