Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
KevsRobots Learning Platform
54% Percent Complete
By Kevin McAleer, 4 Minutes
In MicroPython, a module is a file that contains Python code, including functions, classes, and variables. A library is a collection of modules that provide additional functionality for your projects.
module
library
A module is a file containing Python code that defines functions, classes, and variables. Modules help in organizing code and making it reusable. You can import a module into your MicroPython program to use its functions and classes.
Here’s an example of a simple module named my_module.py:
my_module.py
# my_module.py def greet(name): return f"Hello, {name}!" class Calculator: def add(self, x, y): return x + y PI = 3.14159
You can use this module in your main program by importing it:
# main.py import my_module print(my_module.greet("Alice")) calc = my_module.Calculator() print(calc.add(5, 3)) print(my_module.PI)
In this example, my_module.py contains a function, a class, and a variable. By importing my_module in main.py, you can access and use these elements.
my_module
main.py
A library is a collection of modules that provide additional functionality to your MicroPython projects. Libraries extend the capabilities of your code by providing pre-written functions and classes.
MicroPython includes several built-in libraries you can use. For example, the math library offers mathematical functions:
math
import math print(math.sqrt(16)) # Outputs: 4.0 print(math.pi) # Outputs: 3.141592653589793
You can also install external libraries to add more features to your projects. This is often done using a package manager like upip.
upip
For instance, to install the urequests library for making HTTP requests, you can use:
urequests
import upip upip.install('micropython-urequests')
Then, you can use the installed library in your program:
import urequests response = urequests.get('http://example.com') print(response.text)
Creating your own modules helps you organize your code and make it reusable across different projects. Here’s how you can create and use your own module:
Create a new file named my_robot.py with the following content:
my_robot.py
# my_robot.py class Robot: def __init__(self, name): self.name = name def greet(self): return f"Hello, I am {self.name}"
In your main program, import and use the my_robot module:
my_robot
# main.py import my_robot r1 = my_robot.Robot("Robo") print(r1.greet())
In this example, you create a module my_robot.py that defines a Robot class. In main.py, you import and use the Robot class from the my_robot module.
Robot
MicroPython includes the mip module, which can install packages from micropython-lib and third-party sites like GitHub and GitLab. The mip tool is similar to Python’s pip but uses micropython-lib as its default index. It also automatically fetches compiled .mpy files when downloading from micropython-lib.
mip
You can use mip from the REPL to install packages:
import mip # Install the latest version of "pkgname" (and dependencies) mip.install("pkgname") # Install a specific version of "pkgname" mip.install("pkgname", version="x.y") # Install the source version (i.e., .py rather than .mpy files) mip.install("pkgname", mpy=False)
Modules and libraries in MicroPython enhance your projects by providing reusable and organized code. Whether you are using built-in libraries, installing external ones, or creating your own modules, they help you build more efficient and maintainable programs.
< Previous Next >