Source Code for the Leaphy MicroPython Library
- Maker Nano RP2040
- Pico W
- Nano RP2040 Connect
REPL (Read-Eval-Print Loop) mode allows you to execute Python code one line at a time directly on your microcontroller. This mode is useful for testing and debugging as you can see immediate results of your commands.
For Linux Users: If you're using Linux, you can easily access REPL mode using tio, a terminal tool that interacts with your microcontroller via USB.
First, install tio by running the following command in your terminal:
sudo apt install tio
Connect to your microcontroller by launching tio on the appropriate USB port.
tio /dev/ttyACM0
This will bring you into REPL mode, indicated by the prompt:
>>>
If you prefer a graphical interface, you can use Thonny, a Python IDE designed for beginners, but also suitable for microcontroller programming.
Download and install Thonny from thonny.org
Open Thonny and ensure your microcontroller is connected. At the bottom of the Thonny window, you should see the REPL prompt:
>>>
Install the Package: To begin, you need to connect your microcontroller to Wi-Fi. This is done in REPL mode, follow these steps:
Connect to the wifi:
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("wifi name", "wifi password")
You can now type the following command in the REPL to install the package:
import mip
mip.install("github:leaphy-robotics/leaphy-micropython")
Follow these steps to make a buzzer beep using the leaphymicropython library:
- Start by importing the set_buzzer function into your script:
from leaphymicropython.actuators.buzzer import set_buzzer
Call the set_buzzer function with the appropriate parameters:
set_buzzer(pin_number, pwm_value, frequency)
The pin number to which the buzzer is connected.
The duty cycle for the buzzer, determining the volume of the beep, this value is between 0 - 255
The frequency of the beep, which controls the pitch of the sound (e.g., 1000 for a standard tone).
Follow these steps to read magnetic heading values using the QMC5883L sensor with the leaphymicropython library:
Start by importing the required libraries and setting up the I2C connection:
import time
from math import degrees, atan2
from machine import I2C
from leaphymicropython.sensors.compass import QMC5883L
Create an I2C object and initialize the QMC5883L sensor:
i2c = I2C(0)
qmc = QMC5883L(i2c)
print(qmc.magnetic)
Define the functions needed to convert magnetic vector data into degrees and to get the heading:
def vector_2_degrees(x, y):
"""
Converts a vector to degrees.
"""
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle
def get_heading(sensor):
"""
Calculates the heading from the sensor data.
"""
mag_x, mag_y, _ = sensor.magnetic
return vector_2_degrees(mag_x, mag_y)
Use a loop to continuously read and print the heading in degrees:
while True:
print(f"heading: {get_heading(qmc):.2f} degrees")
time.sleep(0.2)
Converts the X and Y magnetic vector components into an angle in degrees, ensuring the angle is positive.
Retrieves the magnetic X and Y components from the sensor and calculates the heading in degrees.
Follow these steps to control a DC motor using the leaphymicropython library, based on the distance measured by an ultrasonic sensor using the leaphymicropython library:
Start by importing the required libraries:
from time import sleep
from leaphymicropython.actuators.dcmotor import DCMotor
from leaphymicropython.sensors.sonar import read_distance
while True:
distance = read_distance()
motor = DCMotor()
if distance < 10:
motor.left(255, 1)
sleep(1)
else:
motor.forward(255)
Reads the distance from the ultrasonic sensor and returns the value in centimeters.
Initializes the DC motor object, allowing you to control its movements.
Turns the motor to the left at the specified speed (0-255) for the given duration in seconds.
Moves the motor forward at the specified speed (0-255). The motor will continue to move forward until another command is issued.
If the distance measured by the ultrasonic sensor is less than 10 cm, the motor will turn left at full speed for 1 second. If the distance is greater than or equal to 10 cm, the motor will move forward at full speed.
Follow these steps to read temperature and humidity values using the DHT22 sensor with the leaphymicropython library:
Start by importing the DHT22 module:
from leaphymicropython.sensors.dht22 import DHT22
Create a DHT22 sensor object on the correct pin:
sensor = DHT22(1)
Use a loop to continuously read and print the temperature and humidity values:
while True:
print(sensor.read_temperature())
print(sensor.read_humidity())
Initializes the DHT22 sensor object on the specified pin.
Reads the current temperature from the sensor and returns it in degrees Celsius.
Reads the current humidity from the sensor and returns it as a percentage.
Follow these steps to read values from a line sensor using the leaphymicropython library:
Start by importing the read_line_sensor function:
from time import sleep
from leaphymicropython.sensors.linesensor import read_line_sensor
Use a loop to continuously read and print the value from the line sensor:
while True:
print(read_line_sensor(1))
sleep(1)
Reads the value from the line sensor connected to the specified pin.
Follow these steps to make the RGB LED blink using the leaphymicropython library:
Start by importing the required libraries:
from time import sleep
from leaphymicropython.actuators.rgbled import RGBLed
Create an RGBLed object by specifying the pins connected to the red, green, and blue channels:
led = RGBLed(1, 2, 4)
Use a loop to continuously set the LED color and toggle it:
while True:
led.set_color(255, 0, 0) # Set color to red
sleep(1)
led.set_color(0, 0, 0) # Turn off the LED
sleep(1)
Initializes the RGB LED object by specifying the GPIO pins connected to the red, green, and blue components.
Red_pin: The GPIO pin number connected to the red LED channel.
Green_pin: The GPIO pin number connected to the green LED channel.
blue_pin: The GPIO pin number connected to the blue LED channel.
Sets the color of the RGB LED by specifying the intensity of the red, green, and blue channels. Each parameter should be a value between 0 and 255.
Red: Intensity of the red channel (0-255).
Green: Intensity of the green channel (0-255).
Blue: Intensity of the blue channel (0-255).
Follow these steps to control a servo motor using the leaphymicropython library:
Start by importing the set_servo_angle function:
from leaphymicropython.actuators.servo import set_servo_angle
Use the set_servo_angle function to rotate the servo to the desired angle:
set_servo_angle(1, 90)
This function rotates the servo motor to a specified angle.
pin_number: The GPIO pin number to which the servo is connected.
angle: The angle to which the servo should rotate (0-180 degrees).
Follow these steps to read distance using a sonar sensor with the leaphymicropython library:
Start by importing the read_distance function:
from time import sleep
from leaphymicropython.sensors.sonar import read_distance
Use a loop to continuously read and print the distance in centimeters:
while True:
print(read_distance(1))
sleep(1)
This function reads the distance from a sonar sensor connected to the specified pin and returns the distance in centimeters.
Pin_number: The GPIO pin number to which the sonar sensor's trigger pin is connected.