PP Unit-5
PP Unit-5
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:
• 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.).
• 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.