MicroPython: Python on Microcontrollers

MicroPython: Python on Microcontrollers

Python is a popular programming language with a wide user base and a rich library ecosystem. However, traditional Python is not designed for use on resource-limited platforms such as microcontrollers. This is where MicroPython comes into play.

What are Python and MicroPython?

Python:
Python is a general-purpose high-level programming language. It is known for its clean and understandable syntax, an extensive standard library supported by a large community, and a wide range of application areas.

MicroPython:
MicroPython is an adaptation of the Python language and is specifically optimized to run in resource-limited environments such as microcontrollers and embedded systems. It is designed to bring the power and flexibility of the Python language to microcontroller applications.

Differences Between Python and MicroPython

MicroPython has some key differences with traditional Python. These include memory management, library support, and limitations of some features of the language. However, these differences constitute the strength of MicroPython as a language optimized for microcontroller applications.

Programming the Esp32-Wroom-32 Microcontroller Board

Esp32-Wroom-32 is a microcontroller board known for its Wi-Fi and Bluetooth features. MicroPython is a powerful tool that can be used to program this board. Here are the basic steps to program Esp32-Wroom-32:

1. MicroPython Installation: As a first step, you need to download and install the MicroPython version suitable for Esp32-Wroom-32.
2. Connecting: Connect your Esp32-Wroom-32 card to your computer and specify the appropriate ports.
3. IDE Selection: You must choose an IDE (Integrated Development Environment) to program Esp32-Wroom-32. Popular IDEs such as Thonny, uPyCraft, VSCode can be used.
4. Basic Programming: Start by writing a simple "Hello World" program in MicroPython. You can then develop more complex programs to control sensors, actuators and other external components.

IDEs used
There are several popular IDEs you can use when working with MicroPython. Here are some of them:

1. Thonny: A simple IDE for Python and MicroPython. It is user-friendly when it comes to writing, installing and debugging code.
2. uPyCraft: An IDE designed specifically for MicroPython. It attracts attention with its visual interface and simple use.
3. Visual Studio Code (VSCode): With the MicroPython plugin, VSCode becomes a powerful tool for programming MicroPython.

MicroPython Code Structure

MicroPython carries many features of the traditional Python language, but has some limitations and proprietary syntax. A basic MicroPython code structure includes these elements:


# A simple MicroPython code
led_pin = 5 # GPIO pin number
print("Hello, MicroPython!")

def blink(pin, duration):
     # Blink the LED for the specified time
     # Add relevant code here

blink(led_pin, 5) # Blink the LED for 5 seconds

Simple Code Examples
GPIO Control:

from machine import Pin
import time

led_pin = Pin(5, Pin.OUT) # Set GPIO pin 5 as output

while True:
     led_pin.on() # Turn on the LED
     time.sleep(1) # wait 1 second
     led_pin.off() # Turn off the LED
     time.sleep(1) # wait 1 second

Sensor Reading:
from machine import Pin, ADC
import time

sensor_pin = ADC(0) # Select ADC pin for analog sensor

while True:
     sensor_value = sensor_pin.read() # Read sensor value
     print("Sensor Value:", sensor_value)
     time.sleep(2) # wait 2 seconds


Resources
1. MicroPython Official Site
2. MicroPython Documentation
3. ESP32 MicroPython Installation

In this article, we focused on the MicroPython implementation of Python that can be used on microcontrollers. We focused on the main differences, steps to program the Esp32-Wroom-32 board, IDEs used, MicroPython code structure and simple code examples.