EasyManuals Logo

SunFounder ESP32 User Manual

Default Icon
771 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #424 background imageLoading...
Page #424 background image
SunFounder ESP32 Starter Kit
(continued from previous page)
sensor = ADC(Pin(35))
sensor.atten(ADC.ATTN_11DB)
# Initialize buzzer
buzzer = PWM(Pin(13), freq=440, duty=0)
light_low=4095
light_high=0
# Map the interval of input values to output values
def interval_mapping(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# Create a tone using the specified pin, frequency, and duration
def tone(pin,frequency,duration):
pin.freq(frequency)
pin.duty(512)
time.sleep_ms(duration)
pin.duty(0)
# Calibrate the photoresistor's maximum and minimum values in 5 seconds.
timer_init_start = time.ticks_ms()
led.value(1) # turn on the LED
while time.ticks_diff(time.ticks_ms(), timer_init_start)<5000:
light_value = sensor.read()
if light_value > light_high:
light_high = light_value
if light_value < light_low:
light_low = light_value
led.value(0) # turn off the LED
# Play the tones based on the light values
while True:
light_value = sensor.read()
pitch = int(interval_mapping(light_value,light_low,light_high,50,6000))
if pitch > 50 :
tone(buzzer,pitch,20)
time.sleep_ms(10)
Upon starting the program, the LED turns on, providing us with a five-second window to calibrate the photoresistor’s
detection range.
Calibration is a crucial step as it accounts for various lighting conditions that we may encounter while using the device,
such as varying light intensities during different times of the day. Additionally, the calibration process takes into account
the distance between our hands and the photoresistor, which determines the playable range of the instrument.
Once the calibration period is over, the LED turns off, indicating that we can now play the instrument by waving our
hands over the photoresistor. This setup enables us to create music by adjusting the height of our hands, providing an
interactive and enjoyable experience.
418 Chapter 3. For MicroPython User

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the SunFounder ESP32 and is the answer not in the manual?

SunFounder ESP32 Specifications

General IconGeneral
BrandSunFounder
ModelESP32
CategoryController
LanguageEnglish