Tag Archives: raspbian

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.

Twister: the brilliant OS for Raspberry Pi

Twister: the brilliant OS for Raspberry Pi

Since 2012, Raspberry Pi OS, formerly Raspbian, has been the go-to operating system for Raspberry Pi enthusiasts. Raspberry Pi OS is not the only operating system and a recent addition is Twister OS, which is based on Raspberry Pi OS, but it offers a larger selection of pre-installed apps and a choice of themes, some of which provide a Windows/macOS experience for the humble Pi.

In this tutorial, we will install Twister OS on a Raspberry Pi 4, then change the theme to suit Windows 10 and macOS. We’ll also take a look at the additional features of this excellent operating system, such as streaming media (Netflix, Amazon Prime, Hulu, Disney+) and games.

You can also use this OS with the Raspberry Pi 3B+. But for best performance, we recommend using a Raspberry Pi 4.

How to install Twister OS on the raspberry pi?

  • Download the latest version of Twister OS from this link which at the time of writing this article is v2.1.2.
  • Download balenaEtcher from here and install it.
  • Insert a micro SD card into your computer to install the OS on it.
  • Open balenaEtcher, select the Twister OS image, select the microSD card then click Flash to write the image.
  • Now, you can boot your Raspberry Pi from the microSD card.

On the first boot, you will see the Twister OS in all its glory. Stunning wallpaper and aesthetics that use the XFCE window manager to great effect. Twister OS is based on the latest version of Raspberry Pi OS (formerly Raspbian) and features all the applications found in a typical Raspberry Pi OS installation. Additional applications and themes provided by Twister OS are maintained and fixed by the Twister OS team.

Screenshot of the twister OS

Updating Twister OS

  • Check your internet connectivity.
  • From the main menu, select System >> Twister OS Patcher.
  • Follow the instructions to update and restart your Raspberry Pi.

Add a Windows or MacOS theme to your Twister OS

Twister OS is a chameleon, it can change its appearance in just a few clicks. If you want your OS to look like a Windows or Apple device, you can. The themes offered are of superb quality and really give the impression that we are using this OS.

To change the theme of your Twister OS just follow these steps:

  • On your desktop, Double-click on ThemeTwister.
  • Click Next to enter the theme selection screen.
  • Click the button below each theme to select it. A terminal window will appear and ask you to press Enter to restart.

Play with your Twister OS

Twister OS comes with a lot of gaming options. One can play classic PC games using DOSBOX x86Box, installable through PiKiss. Steam support is built into Twister OS, but don’t expect to rip demons in Doom Eternal, prefer simple 2D indie games.

Retro game players can play their game collections using RetroPie.

  • Install all the ROMs you have in /home/pi/RetroPie/roms selecting the correct system for each of the ROMs.
  • Connect a USB/Bluetooth gamepad
  • Double-click the RetroPie desktop icon and follow the on-screen instructions to set up your controller.
  • Choose the system for which you installed ROMs, and press the B button on your gamepad to select it.
  • Browse the game list and press B to play.

You can install the listed games in the Games Menu using PiKISS. Try AM2R: Return of Samus, an original game based on the Metroid series.

Play media files

There are several options for playing the videos and audio files. VLC and Parole media players allow playback through the desktop.

Kodi comes pre-installed on Twister OS and works exactly as you expect. Now enjoy loading up your media and streaming services on the big screen.

If you use streaming services like Amazon Prime Video, Netflix, Hulu, you can use the Chromium (Media Edition) version in the Internet menu to watch your favorite shows.

Control your Android device

The scrcpy app provides a way to interact with your Android device via USB debugging and it works remarkably well. It gives you the ability to control the device using a mouse and keyboard.

To set this up, follow these steps:

  • On your Android device, go to Settings >> About Phone and tap on Build Number until you enable the Developer Mode.
  • In Settings, find USB Debugging and select the first option.
  • Use the slider to enable USB debugging.
  • In Twister OS, click on the My Android icon. This will trigger a query on your Android device. Allow the request and your Android device will appear on the screen.

Using the keyboard and mouse, you can interact with the device now.

for other uses of the Raspberry Pi, check this link.