SunFounder ESP32 Starter Kit
(continued from previous page)
(G5, half_note),
(G5, quarter_note),
(F5, quarter_note),
(E5, quarter_note),
(D5, half_note),
(C5, quarter_note),
(C5, quarter_note),
(D5, quarter_note),
(E5, half_note),
(D5, quarter_note),
(C5, quarter_note),
(C5, half_note),
]
# Define a function to play a note with the given frequency and duration
def tone(pin,frequency,duration):
pin.freq(frequency)
pin.duty(512)
time.sleep_ms(duration)
pin.duty(0)
# Play the melody
for note in melody:
tone(buzzer, note[0], note[1])
time.sleep_ms(50)
• The tone function sets the frequency of the pin to the value of frequency using the freq method of the pin
object.
• It then sets the duty cycle of the pin to 512 using the duty method of the pin object.
• This will cause the pin to produce a tone with the specified frequency and volume for the duration of duration
in milliseconds using the sleep_ms method of the time module.
• The code then plays a melody by iterating through a sequence called melody and calling the tone function for
each note in the melody with the note’s frequency and duration.
• It also inserts a short pause of 50 milliseconds between each note using the sleep_ms method of the time module.
4. Actuators
3.16 4.1 Small Fan
In this engaging project, we’ll explore how to drive a motor using the L293D.
The L293D is a versatile integrated circuit (IC) commonly used for motor control in electronics and robotics projects.
It can drive two motors in both forward and reverse directions, making it a popular choice for applications requiring
precise motor control.
By the end of this captivating project, you will have gained a thorough understanding of how digital signals and PWM
signals can effectively be utilized to control motors. This invaluable knowledge will prove to be a solid foundation for
your future endeavors in robotics and mechatronics. Buckle up and get ready to dive into the exciting world of motor
control with the L293D!
344 Chapter 3. For MicroPython User