Ad Code

Responsive Advertisement

Thonny IDE- Micropython Blinking LED on ESP32

To familiarize you with the process of creating and executing code on your ESP32/ESP8266 boards, we'll upload a new script that simply blinks your ESP32 or ESP8266's on-board LED.

When you launch Thonny IDE for the first time, the Editor displays an untitled file. Copy the following script into that file:

from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT)
while True:
  led.value(not led.value())
  sleep(0.5)
 

Running the Script

To execute the script on your board, simply click the Run icon in Thonny IDE.

The on-board LED will start blinking.

To stop the program, press the STOP button or CTRL+C.

Run the Code Automatically

Simply running the file through Thonny does not permanently copy it to the ESP32 or ESP8266 filesystem. This implies that if you unplug it from your computer and power it on, nothing will happen because the board's filesystem has no MicroPython files.

The Thonny IDE "Run" function is useful for testing the code, but if you wish to permanently upload it to your board, you must first create and save a file to the board's filesystem. Let's see how to accomplish it.

Uploading Code to the ESP32 Board

If you have not already done so, click the Stop button to terminate the previous program's execution.

1) After copying the code, save the file by clicking the Save button or selecting File > Save as.

2) Next, select MicroPython device.

3) Name the file main.py; otherwise, it will not start automatically on the ESP32 or ESP8266.


Notice that the pop-up window displays the files that are currently saved on the board's memory. There should be a file named boot.py. That file is generated by default when you burn MicroPython firmware.

4) Finally, click OK to begin.

5) Now, hit the on-board RST/EN button to restart the board and start the code.

6) Now, your board should blink the blue on-board LED every 500 milliseconds.

After saving the file, you can remove and reapply power to the board, or power it using a power supply other than your computer.

When you power up the board, the LED will begin to blink automatically. This implies the main.py file was successfully uploaded to the board and is now running automatically.

Congratulations! You've just created and uploaded your first MicroPython script to your ESP32 board.

Post a Comment

0 Comments

Ad Code

Responsive Advertisement