Tag Archives: micropython

Raspberry Pi Pico and MPU6050 with MicroPython

Raspberry Pi Pico and MPU6050 with MicroPython

The MPU6050 is an inertial unit that combines an accelerometer and a gyroscope. It is used to measure acceleration, inclination and angular velocity. In this tutorial, we will try to explain how to use an MPU6050 with a Raspberry Pi Pico using MicroPython.

The accelerometer makes it possible to know the acceleration and/or the gravitational field according to 3 axes: x, y and z. If you take a closer look, you can see a drawing of the x and the y axes on the module, while the z axis is perpendicular to the plane of the module. When the module is stationary, flat on a table, it will measure an acceleration of 1 g along the z axis, due to the force of gravity acting downwards. If the measured acceleration is zero along the 3 axes, the MPU-6050 module is in free fall!

The gyroscope measures the angular speed along the 3 axes. When the module is immobile, the 3 components of the angular velocity are, in principle, zero (a calibration is however necessary for the result to be exact).

Connections

The MPU-6050 module has 8 connectors, but only 4 of them are necessary for its operation (2 for power supply, and 2 for data transmission by I2C):

  • VCC pin (MPU-6050) => 3.3V output pin (Raspberry Pi Pico).
  • GND pin (MPU-6050) => GND pin (Raspberry Pi Pico).
  • SCL pin (MPU-6050) => GP9 pin (Raspberry Pi Pico).
  • SDA pin (MPU-6050) => GP8 pin (Raspberry Pi Pico).

Installing imu.py and vector3d.py libraries

You can find the necessary libraries on this Github repository. Also, you need to copy the “imu.py” and “vector3d.py” files to the flash memory of the Raspberry Pi Pico.

First script

This script displays the measurements from the accelerometer and the gyroscope. From the data of the accelerometer, the script tries to determine if one of the 3 axes (x, y or z) approaches the vertical. Based on the data from the gyroscope, the script indicates whether the MPU-6050 module is spinning.

Run this script, and orient the MPU-6050 module in different ways to see how the displayed values vary.

'''
Using the MPU6050 inertial unit (accelerometer + gyrometer) with a Raspberry Pi Pico.
For more info:
bekyelectronics.com/raspberry-pi-pico-and-mpu-6050-micropython/
'''

from imu import MPU6050  # https://github.com/micropython-IMU/micropython-mpu9x50
import time
from machine import Pin, I2C

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
imu = MPU6050(i2c)

# Temperature display
print("Temperature: ", round(imu.temperature,2), "°C")

while True:
    # reading values
    acceleration = imu.accel
    gyroscope = imu.gyro
    
    print ("Acceleration x: ", round(acceleration.x,2), " y:", round(acceleration.y,2),
           "z: ", round(acceleration.z,2))

    print ("gyroscope x: ", round(gyroscope.x,2), " y:", round(gyroscope.y,2),
           "z: ", round(gyroscope.z,2))

# data interpretation (accelerometer)

    if abs(acceleration.x) > 0.8:
        if (acceleration.x > 0):
            print("The x axis points upwards")
        else:
            print("The x axis points downwards")

    if abs(acceleration.y) > 0.8:
        if (acceleration.y > 0):
            print("The y axis points upwards")
        else:
            print("The y axis points downwards")

    if abs(acceleration.z) > 0.8:
        if (acceleration.z > 0):
            print("The z axis points upwards")
        else:
            print("The z axis points downwards")

# data interpretation (gyroscope)

    if abs(gyroscope.x) > 20:
        print("Rotation around the x axis")

    if abs(gyroscope.y) > 20:
        print("Rotation around the y axis")

    if abs(gyroscope.z) > 20:
        print("Rotation around the z axis")
    
    time.sleep(0.2)

Second script

In this second example, the Raspberry Pi Pico’s built-in LED lights up when you shake the MPU6050 module.

'''
Using the MPU6050 inertial unit (accelerometer + gyroscope) with a Raspberry Pi Pico.
The Pico LED lights up when the MPU6050 is shaken.
For more info:
bekyelectronics.com/raspberry-pi-pico-and-mpu-6050-micropython/
'''

from imu import MPU6050  # https://github.com/micropython-IMU/micropython-mpu9x50
import time
from machine import Pin, I2C

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
imu = MPU6050(i2c)

# LED initially off
Pin(25, Pin.OUT).value(0)

while True:
    # acceleration reading
    acceleration = imu.accel.magnitude
    print (acceleration)
    
    # value at rest is 1
    if abs(acceleration - 1) > 0.1:
        print("It is moving!")   
        Pin(25, Pin.OUT).value(1) # turn on the LED
    else: 
        Pin(25, Pin.OUT).value(0) # turn off the LED
        
    time.sleep(0.2)

That’s it, this is how you can use the MCU6050 unit with your Paspberry Pi Pico. You can check other tutorials from this link. If you want to establish wireless communication between two of your Picos using NRF24L01, click here.

Raspberry Pi Pico with nRF24L01 using MicroPython

Raspberry Pi Pico with nRF24L01 using MicroPython

In this tuturial, we will establish a wireless communication between two Raspberry Pi Pico equipped with an nRF24L01 module each. We will use the MicroPython languge to program our Raspberry Pi Pico.

nRF24L01 modules are designed to transmit the information at a 2.4 GHz radio frequency. Each module can transmit and receive signals. The range can be a few tens of meters.

Connections

You need to have a pair of nRF24L01 modules. Depending on their brand, you may find:

  • Green modules with 10 clearly marked pins
  • Black modules with 8 pins that have no identification.

Wire each of your Raspberry Pi Pico to an nRF24L01 module as follows:

  • VCC (nRF24L01) => 3.3V output (Raspberry Pi Pico).
  • GND (nRF24L01) => GND (Raspberry Pi Pico).
  • SCK (nRF24L01) => GP6 (Raspberry Pi Pico).
  • MOSI (nRF24L01) => GP7 (Raspberry Pi Pico).
  • MISO (nRF24L01) => GP4 (Raspberry Pi Pico).
  • IRQ (nRF24L01) => not connected
  • CE (nRF24L01) => GP12 (Raspberry Pi Pico).
  • CSN (nRF24L01) => GP5 (Raspberry Pi Pico).

We highly recommend that you add a 10 µF capacitor between the GND and VCC pins of the nRF24L01 module. This is a solution to consider if you notice erratic operation.

Installing the nrf24l01.py library

After downloading the nrf24l01.py library, it is important to install the nrf24l01.py file in the flash memory of the Raspberry Pi Pico (using, for example, Thonny).

This library is accompanied by a very well done program called “nrf24l01test.py”. To use it with the Raspberry Pi Pico, however, you will need to change the pin numbers at the start of the program.
Based on this example, you’ll find two separate scripts below. These scripts was designed to be used simultaneously on two different Raspberry Pi Picos. The first script is for the transmitter, and the other one is for the receiver.

The transmitter’s script

Each second, this Raspberry Pi Pico increments an integer number and sends it to the nRF24L01 module. It then checks whether it receives a response from the receiver.

"""
nRF24L01 transmitter
Raspberry Pi Pico and nRF24L01 module
Once per second, a numerical value is sent, and we
checks if we receive a response.
For more info:
www.bekyelectronics.com/raspberry-pico-nrf25l01-micropython/
"""
import ustruct as struct
import utime
from machine import Pin, SPI
from nrf24l01 import NRF24L01

# pin definition for the Raspberry Pi Pico:
myPins = {"spi": 0, "miso": 4, "mosi": 7, "sck": 6, "csn": 5, "ce": 12}

# Addresses (little endian)
pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")

print("NRF24L01 transmitter")

csn = Pin(myPins["csn"], mode=Pin.OUT, value=1)
ce = Pin(myPins["ce"], mode=Pin.OUT, value=0)
nrf = NRF24L01(SPI(myPins["spi"]), csn, ce, payload_size=8)

nrf.open_tx_pipe(pipes[0])
nrf.open_rx_pipe(1, pipes[1])
nrf.start_listening()

counter = 0  # Increase the value by 1 with each emission

while True:
    # Stop listening, time to send a message
    nrf.stop_listening()
    
    counter = counter + 1 # preparing the message to send
    print("sending:",  counter)
    
    try:
        nrf.send(struct.pack("i",  counter)) # sending the message
    except OSError:
        pass

    # Listen if the other Pico answers us
    nrf.start_listening()

    # Wait for 250ms max
    start_time = utime.ticks_ms()
    timeout = False
    while not nrf.any() and not timeout:
        if utime.ticks_diff(utime.ticks_ms(), start_time) > 250:
            timeout = True

    if timeout:  # no response received
        print("failure, no response")

    else:  # a response has been received
        (response,) = struct.unpack("i", nrf.recv())
        print ("response recue:", response)

    # Wait a second before sending the next message
    utime.sleep_ms(1000)

The receiver’s script

If this Raspberry Pi Pico receives a message from the transmitter, it calculates the modulo of the number received and returns the result to the transmitter. It therefore returns the number “0” when it receives an even number, and the number “1” when it receives an odd number.

"""
nRF24L01 receiver
Raspberry Pi Pico and nRF24L01 module.
If an integer is received, it is acknowledged by flipping its modulo.
For more info:
www.bekyelectronics.com/raspberry-pico-nrf25l01-micropython/
"""
import ustruct as struct
import utime
from machine import Pin, SPI
from nrf24l01 import NRF24L01
from micropython import const

# delay between receiving a message and waiting for the next message
POLL_DELAY = const(15)
# Delay between receiving a message and sending the response
# (so that the other pico has time to listen)
SEND_DELAY = const(10)

# Pico pin definition:
myPins = {"spi": 0, "miso": 4, "mosi": 7, "sck": 6, "csn": 5, "ce": 12}

# Addresses
pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")

csn = Pin(myPins["csn"], mode=Pin.OUT, value=1)
ce = Pin(myPins["ce"], mode=Pin.OUT, value=0)
nrf = NRF24L01(SPI(myPins["spi"]), csn, ce, payload_size=8)

nrf.open_tx_pipe(pipes[1])
nrf.open_rx_pipe(1, pipes[0])
nrf.start_listening()

print("nRF24L01 receiver; waiting for the first post...")

while True:
    if nrf.any(): # we received something
        while nrf.any():
            buf = nrf.recv()
            counter = struct.unpack("i", buf)
            print("message received:", counter[0])
            utime.sleep_ms(POLL_DELAY) # delay before next listening
            
        response = counter[0]%2 # preparing the response

        utime.sleep_ms(SEND_DELAY) # Give the other Pico a brief time to listen
        nrf.stop_listening()
        try:
            nrf.send(struct.pack("i", response))
        except OSError:
            pass
        print("reply sent:", response)
        nrf.start_listening()

That’s it, this is how you establish wireless communication between your Picos using NRF24L01. I hope this tutorial was helpful for you. You can check other tutorials from this link.