Using Python to Control ESP32 LED and Buzzer with Thonny IDE and MicroPython
If you have an ESP32 microcontroller and want to have some fun experimenting with LEDs and buzzers, then this tutorial is for you! With the help of Thonny IDE and MicroPython, you can easily program your ESP32 to control the LED and buzzer modules.
Getting Started
Before you begin, make sure you have the following hardware components:
- ESP32 development board
- LED module
- Buzzer module
Installing Thonny IDE
First, you’ll need to download and install Thonny IDE on your computer. Thonny IDE is a great tool for programming microcontrollers with Python as it provides a user-friendly interface and supports MicroPython.
Programming the ESP32
Connect your ESP32 board to your computer using a USB cable. Open Thonny IDE and create a new MicroPython project. You can then start writing Python code to control the LED and buzzer modules.
Controlling the LED
To turn on the LED connected to your ESP32, you can use the following code snippet:
import machine
led = machine.Pin(13, machine.Pin.OUT)
led.on()
This code initializes pin 13 as an output pin and turns on the LED connected to it.
Controlling the Buzzer
To make the buzzer emit a sound, you can use the following code:
import machine
import time
buzzer = machine.Pin(14, machine.Pin.OUT)
while True:
buzzer.on()
time.sleep(0.5)
buzzer.off()
time.sleep(0.5)
This code initializes pin 14 as an output pin and repeatedly turns the buzzer on and off, creating a buzzing sound.
Conclusion
With Thonny IDE and MicroPython, you can easily program your ESP32 to control LEDs and buzzers. Experiment with different patterns and frequencies to create interesting light and sound effects. Have fun exploring the possibilities of Python programming on your ESP32!
請問 python code 寫完, download 到板上.
可以下電後再上電. 重覆執行嗎?