Open 4.4Sound detection.py and click .
'''
* Filename : Sound detection
* Thonny : Thonny 4.1.4
* Auther : http//www.keyestudio.com
'''
from machine import ADC,Pin
import time
# configure ADC, range of 0-3.3V
# define io26,io27,io28,io29 to ADC channel 0,1,2,3
MicroPhone = machine.ADC(27) #MicroPhone = ADC(1)
led = Pin(11, Pin.OUT)
while True:
Micro_value = MicroPhone.read_u16()
print(Micro_value)
time.sleep(0.1)
if Micro_value > 10000:
led.on()
else:
led.off()
Conceive:
Set a threshold of analog value to determine the thunder volume. When thunder is detected, the analog value exceeds the threshold, and the white LED lights up as an alarm. The LED will go off when the value is lower than the threshold.
Code structure:
Initialization. Set the pins of the sound sensor and the white LED module.
Loop.
Print the analog value of sound volume detected by the sound sensor.
Determine whether the analog value is greater than 10000 (Herein, we set 10000 as the standard threshold of thunder volume, which is adjustable according to needs).
analog value > 10000, the white LED lights up.
analog value ≤ 10000, the white LED goes off.