Oct. 11, 2023
Create a system, where touching a respective wire makes a corresponding light turn on and create the corresponding sound from the “Do Re Mi” sounds.
Component | Amount |
---|---|
ESP32-WROVER | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
Passive Buzzer | 1 |
NPN Transistor | 1 |
LED Bar Graph | 1 |
Resistor - 1K Ω | 1 |
Resistor - 220 Ω | 7 |
Wire M-M | 18 |
# note: this file is not compatible with the ESP32-S3
from machine import PWM, Pin, TouchPad
import time
lp = [2,0,5,18,19,21,22] # LED pins
tp = [15,4,13,12,14,33,32] # touch pins
bf = [262,294,330,349,392,440,494] # buzzer frequencies
pb = PWM(Pin(23),2000) # initialize passive buzzer
pb.deinit() # set sound off
for i in range(7): # turn off all neo LEDs
l = Pin(lp[i], Pin.OUT)
l.value(0)
while True:
for i in range(7): # check all 7 open wires
touch = TouchPad(Pin(tp[i],Pin.IN,Pin.PULL_UP))
if touch.read() < 100: # if a wire gets touched
led = Pin(lp[i], Pin.OUT)
led.value(1) # turn on LED
pb.init()
pb.freq(bf[i]) # turn on correct sound
time.sleep_ms(20)
while touch.read() < 100:
time.sleep_ms(20) # hold until wire is let go
led.value(0) # turn everything off
pb.deinit()
time.sleep_ms(100)
Create a system that acts as an alarm. It should have an LED grow brighter in the dark and when light is detected, the sound alarm and neopixel should go crazy. Until a button is pressed.
Component | Amount |
---|---|
ESP32-WROVER | 1 |
GPIO Extension Board | 1 |
Project Board | 1 |
Blue LED | 1 |
NPN Transistor | 1 |
Passive Buzzer | 1 |
NeoPixel LED | 1 |
Big Push Button | 1 |
Resistor - 220 Ω | 1 |
Resistor - 1K Ω | 1 |
Resistor - 10k Ω | 3 |
Wire M-F | 3 |
Wire M-M | 9 |
from machine import Pin,PWM,ADC
import neopixel
import time
import math
# initialize necessary components
neo = neopixel.NeoPixel(Pin(15, Pin.OUT), 8)
btn = Pin(4,Pin.IN,Pin.PULL_UP)
buz = PWM(Pin(23,Pin.OUT),2000)
buz.deinit()
led = PWM(Pin(25,Pin.OUT),1000)
adc = ADC(Pin(36))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_10BIT)
# function for buzzer sound
PI = 3.14
def alert():
for x in range(0,36):
sinVal=math.sin(x*10*PI/180)
toneVal=2000+int(sinVal*500)
buz.freq(toneVal)
time.sleep_ms(10)
val = 10
colours = [[val,0,0],
[0,0,val],
[val,val,val],
[0,0,0]]
# support function
def even(n):
if n % 2 == 0:
return True
else:
return False
# function for neopixel LEDs
def alarmLED():
eo = True
for x in range(3):
for i in range(8):
if even(i) and eo:
neo[i] = colours[x]
neo.write()
elif not even(i):
neo[i] = colours[x]
neo.write()
if eo:
eo = False
else:
eo = True
time.sleep_ms(200)
for j in range(8):
neo[j] = colours[3]
neo.write()
time.sleep_ms(200)
triggered = False
try:
while True:
adcValue = adc.read()
led.duty(adcValue) # change blue LED based on photoresistor value
if adcValue >= 500 and triggered: # if in darkness, enable alarm again
triggered = False
if adcValue < 500 and not triggered: # if leaving darkness, cue alarm
triggered = True
buz.init()
while adcValue < 500: # keep alarm going until it is dark again or until...
alarmLED()
alert()
adcValue = adc.read()
led.duty(adcValue)
if not btn.value(): # ...a button is pressed
adcValue = 501
time.sleep_ms(20)
buz.deinit()
time.sleep_ms(100)
except:
buz.deinit()
led.deinit()