Blink built-in LED on the ESP32 using MicroPython:
In this program, we first import the machine
module which provides access to the hardware on the ESP32. We then create a Pin
object for the LED, using pin 2 and specifying that it is an output pin.
We then enter an infinite loop where we turn the LED on, wait for half a second using the time.sleep()
function, turn the LED off, and wait for another half a second before repeating the process. This creates the effect of the LED blinking on and off every half a second.
To run this program on your ESP32, you can copy and paste the code into a file on your computer and then transfer the file to the ESP32 using a tool like ampy or WebREPL. Alternatively, you can use a tool like Thonny or Mu to write and run the code directly on the ESP32 using a USB cable.
To write this code to an ESP32 MCU, you can use one of several methods, depending on your preferred development environment and hardware setup. Here are two common methods:
Method 1: Using a text editor and ampy
- Connect your ESP32 to your computer using a USB cable.
- Open a text editor and copy the code into a new file.
- Save the file with a
.py
extension, such asled_blink.py
. - Install ampy by running
pip install adafruit-ampy
in a command prompt or terminal window. - Use ampy to upload the file to the ESP32 by running
ampy --port COMX put
led_blink
.py
, replacingCOMX
with the name of the serial port that the ESP32 is connected to (e.g.COM3
on Windows or/dev/ttyUSB0
on Linux). This will copy theled_blink
.py
file to the root directory of the ESP32's file system. - Reset the ESP32 and the code should run automatically.
Method 2: Using a MicroPython IDE
- Connect your ESP32 to your computer using a USB cable.
- Open a MicroPython IDE such as Thonny or Mu.
- Connect to the ESP32 by selecting the appropriate serial port and baud rate in the IDE's settings.
- Copy the code into the IDE's editor.
- Use the IDE's upload or save function to write the code to the ESP32's file system. This will copy the code to the root directory of the ESP32's file system.
- Reset the ESP32 and the code should run automatically.
In either method, you may need to modify the code to use the correct I2C pin numbers for your particular OLED display. Be sure to consult the documentation or specifications for your display to determine the correct pin numbers.
For this time, I use Mu editor https://codewith.mu/en/download
What is MicroPython ?
MicroPython is a version of the Python programming language that has been designed specifically to work on microcontrollers and embedded systems. Microcontrollers are small computers that are used to control devices like sensors, motors, and lights.
MicroPython is very similar to regular Python, so if you know Python, you can easily learn MicroPython. However, MicroPython has been optimized to work on small devices, which means it has some differences compared to regular Python.
One of the biggest differences between Python and MicroPython is that MicroPython has been designed to work under constrained conditions. This means that it has been designed to work on devices that have limited processing power and memory. Because of this, MicroPython does not come with the entire pack of standard libraries that are included with regular Python.
Instead, MicroPython includes a small subset of the Python standard libraries that are most useful for working with microcontrollers and embedded systems. These libraries include modules that allow you to easily control and interact with the GPIOs (General Purpose Input/Output pins) on your device, use Wi-Fi and other communication protocols, and more. This makes it easier to write programs for your microcontroller without having to worry about memory constraints and other limitations.
What is Mu Editor?
Mu Editor is a software program that you can use to write programs in MicroPython and upload them to your ESP32 or ESP8266 board. Mu Editor is designed to be easy to use, even for people who are new to programming. It has a simple and intuitive interface, and it even includes a menu that makes it easy to install the MicroPython firmware on your board.
By using Mu Editor, you can write programs in Python that control the functions of your ESP32 or ESP8266 board. This could include things like turning lights on and off, controlling motors, or gathering data from sensors. Once you have written your program in Mu Editor, you can upload it to your board and see your project in action!
Installing Mu Editor
Mu Editor is a simple Python editor for beginners. As mentioned on the Mu Editor website, “Less is More: Mu has only the most essential features, so users are not intimidated by a baffling interface.”[Mu Editor Webpage]
Installing Mu Editor is very straightforward. Follow the next steps:
1) Go to the following website and download Mu Editor for your operating system: https://codewith.mu/en/download.
2) Run the installer you’ve just downloaded—it’s probably in your Downloads folder.
3) Finally, follow the on-screen instructions to install it.
If you have trouble with the installation, look at the official instructions on the Mu Editor website: Mu Editor Installation Guides.
And that’s it. It couldn’t be simpler.
Mu Editor Overview
Open Mu Editor. The following window will open.
You can see that the interface is very simple and intuitive to use. Let’s take a quick look at the buttons.
- Mode: choose which mode you want to use. Select ESP MicroPython
- New: creates a new file.
- Load: chooses a file to load into Mu.
- Save: saves the file to your computer.
- Run: runs the current script in your ESP32 or ESP8266 boards without actually uploading code to the board.
- File: opens a window with the files saved on your board filesystem and a window with the files saved on your computer’s current directory.
- REPL: allows you to talk to the ESP32 or ESP8266 by writing commands that the board will run right away. REPL means Read, Evaluate, Print, Loop. The ESP32/ESP8266 waits for instructions by presenting >>> in the REPL. You should type your commands and hit Enter. The ESP will get the commands and send a response.
- Plotter: provides a graphical interface to display and plot values.
- Zoom-in: increases code font size.
- Zoom-out: decreases code font size.
- Theme: changes between different themes.
- Check: checks your code for errors.
- Tidy: tidies up your code when it comes to spaces and indentation.
- Help: opens Mu Editor website help page.
- Quit: closes Mu Editor.
How to flash MicroPython Software ?
When you buy an ESP32 or ESP8266 board, it doesn't come with MicroPython pre-installed. To start using MicroPython on your board, you need to first flash, upload, or burn the MicroPython firmware onto it.
There are different ways to install MicroPython firmware on your board, and one popular method is to use the Mu Editor software. Mu Editor comes with a tool that makes it easy to install the firmware on your board quickly and easily. You can follow the steps in the guide to use Mu Editor to install the firmware on your board.
However, if you have trouble using Mu Editor, there is another method that you can use to install the firmware. This method involves using a tool called esptool.py, which is a command-line utility for flashing firmware onto ESP32 and ESP8266 boards. If you encounter any issues with Mu Editor, you can follow the tutorial to use esptool.py to flash the MicroPython firmware onto your board instead.
Once you have successfully installed the MicroPython firmware on your board, you can start using Mu Editor (or any other Python editor that supports MicroPython) to write and upload your programs to the board.
How to download MicroPython ?
To use MicroPython on your ESP32 or ESP8266 board, you need to download the MicroPython firmware first. Here's how you can do it:
Go to the MicroPython Downloads page: https://micropython.org/download/.
Choose the type of board you are using (ESP32 or ESP8266).
On the board-specific download page, you'll see links to download .bin files. Download the latest stable release (not the Nightly builds, which are only recommended for advanced programmers).
The downloaded file will likely go to your Downloads folder.
Once you have downloaded the MicroPython firmware, you can proceed with flashing it onto your board.
Flashing MicroPython Firmware using Mu Editor
To flash the MicroPython firmware onto your ESP32 or ESP8266 board, you can use the Mu Editor software or the esptool.py command-line utility. Here are the general steps:
Connect your board to your computer using a USB cable.
Open the Mu Editor software (if using).
Select the correct port for your board under the Port menu in the Mu Editor.
Choose your board type (ESP32 or ESP8266) in the Mu Editor.
Click the "Flash" button in the Mu Editor.
Browse for the MicroPython firmware .bin file you downloaded previously.
Click on "Erase & write firmware" to start burning the MicroPython firmware onto your board.
If using an ESP32 board, you'll need to hold the onboard BOOT/FLASH button for about two seconds after clicking the "Erase & write firmware" button to put your board in flashing mode.
Wait for the burning process to complete. This may take a few seconds.
Once the process is completed, you should be able to start using MicroPython on your ESP32 or ESP8266 board.
10.
Now successfully flashed MicroPython firmware on your boards using Mu Editor.
Running First Script
Great! Now that you have the MicroPython firmware installed on your ESP32/ESP8266 board and have connected it to your computer, you can start writing and executing code on the board.
Here are the steps to upload a simple script that blinks the onboard LED of your ESP32/ESP8266:
Open the Mu Editor software.
In the "Files" section, click the "New" button to create a new file.
In the new file, type the following code:
Save the file with a name like "blink.py" to the board's file system. You can do this by clicking the "Save" button in the Mu Editor and choosing the board's file system as the save location.
Once the file is saved, click the "Repl" button in the Mu Editor.
In the REPL prompt, type the following command to run the script:
import blink
The onboard LED on your ESP32/ESP8266 should start blinking on and off in 1-second intervals.
Now you have successfully written and executed your first MicroPython script on your ESP32/ESP8266 board.
No comments:
Post a Comment