Tag Archives: pico

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.

Top 9 uses of a Raspberry Pi

Top 9 uses of a Raspberry Pi

Recently the Raspberry Pi celebrated its 10th birthday. But people still asking what they can use a raspberry pi for? After reading this post, you will discover lot of uses of a Raspberry Pi. This post is a summary of ExplainingComputer Youtube channel video

If you don’t know what a raspberry pi is, you can check out this post. That will help you understand everything about these super tiny computers.

Small desktop computer

People used the very first Raspberry Pi 1B for word processing as well as for some light web browsing. Fast forward to today and modern Raspberry Pi 4 or Pi 400 is a very capable low-cost desktop computer. Many different desktop operating systems are now available for the Pi. As well as several cases that turn it into a rather stylish small computing device. Office applications work very fluidly on a modern pie and the browsing experience is also pretty decent. With a little patience, it’s even possible to edit video and to run sophisticated 3d and compositing packages including blender.

Media player

Many people use the Raspberry Pi as a  media player by installing a media center operating system such as OSMC or libra. Both of which boots directly into a player called Kodi. This is very easy to configure and provides a clear straightforward interface that works well on a Raspberry Pi. It plays content from local drives accessed from a home network or streamed over the internet. Or By using a wide variety of add-ons that include Youtube. 

NAS or server

Users who want to store and exchange files use the Raspberry Pi as a NAS or network-attached-storage device. Popular NAS software includes open media vault otherwise known as OMV. More broadly, a Raspberry Pi will transform into a small server by installing the Next Could. Next Could can run groupware and which allows files exchanges over the internet. You also can set up a Raspberry Pi as a plex server to share media files. Without mentioning using it to host a website.

Robotics & automation

The GPIO pins on a Raspberry Pi can control motors servos and other electrical and electronic devices. This makes the Pi a great controller for robotics and automation with many hobbyists. Robots operated remotely over Wi-Fi, Bluetooth or another wireless link are a perfect example. Similarly, the Raspberry Pi can automate smart homes. That allows remote or programmed control of everything from light bulbs to watering systems. The limit is the maker’s imagination.

Surveillance & IoT

Raspberry Pi is a connecter of all manner of sensors to a network and more broadly to the internet. That allows it to function as an IoT or internet of things device. A Raspberry Pi is great for streaming video from a camera. Which can allow you to monitor activity in your garden or the latest withdrawals from your fridge. You can even make it a remote weather station with the temperature, humidity and air quality sensors.

Printer controller

The Raspberry pi can control a printer. This includes connecting a conventional inkjet or laser to a Pi. That allows accessing a printer without an ethernet or Wi-Fi connection over a network. Alternatively, installing Octopi allows a raspberry pi to control and monitor a 3d printer. Soon enough, I’m sure that developers will include our favorite single-board computer in the 4d printing process.

Retro gaming

A Raspberry Pi is powerful enough to emulate many classic microcomputers and games consoles. That makes it excellent for retro gaming. Popular emulation software includes DOSBox as well as Retropie, which brings together emulation station retro arch and similar projects. That enable a Raspberry Pi to run all manner of arcade home consoles and classic PC games.

Smart speaker

A Raspberry Pi even provides a potential platform for machine learning and artificial intelligence. You can use it as the brains of a smart speaker. Engineers have demonstrated that a Raspberry Pi can run amazon’s Alexa. Also, as a Google assistant accessed via a smart speaker built with a google AIY kit.

Learning platform

A very important thing you can do with raspberry pi is to use it as a  learning platform. Lots of people use a Raspberry Pi to learn about coding, networking, IoT. It remains an exceptionally good piece of educational hardware. Even though any computer could be a learning platform. But the Raspberry Pi is a computer that you can experiment with without messing up anything. You can just re-flash the micro SD card start again if you mess up a Raspberry Pi’s operating system. It’s probably less convenient if you reinstall the operating system on any other computer, but. So the Raspberry Pi is very useful as a learning platform.

That’s the end of this post. If you know other uses of a Raspberry Pi, feel free to share them in the comments below.