0% found this document useful (0 votes)
6 views22 pages

PP Unit-5

MicroPython is a lightweight implementation of Python 3 designed for microcontrollers and constrained environments, allowing for efficient programming of hardware with limited resources. It includes a smaller subset of standard libraries and is optimized for direct interaction with hardware components, making it suitable for IoT and embedded systems applications. The document covers the installation process, GPIO programming, and sensor interfacing using MicroPython.

Uploaded by

kn0704nk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

PP Unit-5

MicroPython is a lightweight implementation of Python 3 designed for microcontrollers and constrained environments, allowing for efficient programming of hardware with limited resources. It includes a smaller subset of standard libraries and is optimized for direct interaction with hardware components, making it suitable for IoT and embedded systems applications. The document covers the installation process, GPIO programming, and sensor interfacing using MicroPython.

Uploaded by

kn0704nk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Computer Engineering / Information Technology

Python Programming
Unit-5
MicroPython
Contents
• Introduction
• Main difference between MicroPython and Python
• Installation of MicroPython on Hardware
• MicroPython libraries
• GPIO programming on MicroPython Hardware
• Sensor Programming using MicroPython
MicroPython
MicroPython
• MicroPython is a lean and efficient implementation of the Python 3 programming language that
includes a small subset of the Python standard library.
• It is optimized to run on microcontrollers and in constrained environments.
• The MicroPython pyboard is a compact electronic circuit board that runs MicroPython on the bare
metal, giving you a low-level Python operating system that can be used to control all kinds of
electronic projects.
• MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision
integers, closures, list comprehension, generators, exception handling and more.
• Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.
• MicroPython aims to be as compatible with normal Python as possible to allow you to transfer
code with ease from the desktop to a microcontroller or embedded system.
MicroPython v/s Python
MicroPython v/s Python
• Python: It is a general-purpose programming language designed for a wide range of applications,
from web development to data analysis.
• It runs on most operating systems and is known for its simplicity and readability.

• MicroPython: It is a lean and efficient implementation of Python 3 that is optimized to run on


microcontrollers and in constrained environments.
• It is specifically designed to run on hardware with limited resources (like memory and processing
power), such as microcontrollers.
MicroPython v/s Python
1) Purpose and Usage:
Python:
• A general-purpose programming language.
• Used for web development, data analysis, artificial intelligence, automation, scripting, and more.
• Runs on operating systems like Windows, macOS, Linux, and more.

MicroPython:
• A specialized version of Python optimized for microcontrollers and embedded systems.
• Ideal for programming hardware with limited resources like memory and processing power.
• Runs directly on microcontrollers, enabling direct interaction with hardware.
MicroPython v/s Python
2) Resource Requirements:
Python:
• Requires more system resources (RAM, CPU, and storage) as it runs on operating systems.
• Includes extensive standard libraries and features.

MicroPython:
• Designed to run on microcontrollers with limited resources (e.g., 256KB RAM, 2MB Flash
storage).
• Stripped-down version of Python with essential libraries for embedded programming.
MicroPython v/s Python
3) Standard Libraries:
Python:
• Comes with a comprehensive set of standard libraries for diverse tasks (e.g., file I/O, networking,
GUI development).
• Supports numerous third-party libraries for advanced functionalities.

MicroPython:
• Includes a smaller subset of Python's standard libraries, focused on hardware and embedded
systems (e.g., machine, ujson, urequests).
• Custom libraries are available for specific sensors and modules.
MicroPython v/s Python
4) Development Environment:
Python:
• Can be developed using various IDEs and text editors like PyCharm, Visual Studio Code, or even
Notepad++.
• Supports debugging tools and extensive testing frameworks.

MicroPython:
• Often developed using lightweight editors and tools that support serial communication with
microcontrollers, such as Thonny, uPyCraft, or even a simple terminal with REPL.
• Limited debugging capabilities compared to full Python, but sufficient for embedded development.
MicroPython v/s Python
5) Performance:
Python:
• Slower in execution compared to compiled languages (like C or C++) but sufficient for most
applications due to the high-level nature and readability.

MicroPython:
• Optimized for performance on microcontrollers, but still slower than C/C++ on the same hardware.
• Efficient enough for real-time and low-level hardware interaction in most cases.
MicroPython v/s Python
6) Target Hardware:
Python:
• Runs on standard computers, servers, and sometimes on mobile devices.
• Not intended for direct interaction with hardware components like sensors or actuators without
additional interfaces or platforms (e.g., Raspberry Pi with GPIO libraries).

MicroPython:
• Runs on microcontrollers (e.g., ESP8266, ESP32, STM32) and directly controls hardware
components like LEDs, sensors, motors, etc.
• Ideal for IoT (Internet of Things) and embedded systems applications.
Installation of MicroPython on Hardware
Installation of MicroPython on Hardware
• Download the MicroPython Firmware:
Visit the official MicroPython website and download the appropriate firmware for your
microcontroller (e.g., ESP32, ESP8266, etc.).

• Flash the Firmware:


Use a tool like esptool.py to flash the downloaded firmware onto your microcontroller.
Example command: esptool.py --port /dev/ttyUSB0 erase_flash followed by esptool.py --port
/dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20220117-v1.18.bin.

• Connect to the Microcontroller:


After flashing, use a serial tool like PuTTY or screen to connect to the device and access the
MicroPython REPL (Read-Eval-Print Loop).
MicroPython Libraries
MicroPython Libraries
• MicroPython supports a wide range of libraries, both built-in and external, that are similar to
standard Python libraries but tailored for microcontrollers.

• Examples include:
1. ‘machine’ for accessing hardware-specific functions (e.g., GPIO, ADC, PWM).
2. ‘network’ for handling Wi-Fi or network connectivity.
3. ‘urequests’ for making HTTP requests.
4. ‘time’ for handling time-related tasks.
GPIO Programming on MicroPython
Hardware
GPIO Programming on MicroPython
Hardware
• GPIO (General-Purpose Input/Output) pins on a microcontroller can be controlled using
MicroPython.
• Basic usage:
from machine import Pin
led = Pin(2, Pin.OUT) # Set GPIO 2 as output
led.on() # Turn the LED on
led.off() # Turn the LED off

• You can configure GPIO pins as inputs, outputs, or even with pull-up/down resistors for more
complex tasks.
Sensor Programming Using MicroPython
Sensor Programming Using MicroPython
• MicroPython allows you to interface with various sensors (e.g., temperature, humidity, pressure)
using libraries like machine and sensor-specific drivers.

• Example using a DHT22 sensor:


from machine import Pin
import dht
sensor = dht.DHT22(Pin(4)) # Connect DHT22 data pin to GPIO 4
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
print('Temperature:', temperature)
print('Humidity:', humidity)

You might also like