I have been trying to connect a Pi Pico and a Arduino Nano using Nrf24l01 modules. For some reason I couldn't get the circuits to talk to each other. in both circuits the Nrf24l01 module initializes properly. additionally I was succesful in connecting two arduino boards (one UNO and one Nano) using the same Nrf24l01 modules. I looked online but couldn't find anyone who had tried doing this before.
for the Nano I am using the Nrf24l01 RadioHead library (the common lib didn't work for me) Here is the code for the Nano:
// nrf24_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
// reliability, so you should only use RH_NRF24 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf24_server.
// Tested on Uno with Sparkfun NRF25L01 module
// Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module
#include <SPI.h>
#include <RH_NRF24.h>
//uint8_t address = 0xf0;//f0f0f0e1;
//byte address[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xe1 };
uint8_t tx_address[] = { 0xe1, 0xf0, 0xf0, 0xf0, 0xf0 };
uint8_t rx_address = 0xd2;
// Singleton instance of the radio driver
RH_NRF24 nrf24;
void setup()
{
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(120))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate250kbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
//setting tx pipe address
nrf24.setNetworkAddress(tx_address, 5);
//setting rx pipe address
nrf24.setThisAddress(rx_address);
}
void loop()
{
Serial.println("Sending to nrf24_server");
// Send a message to nrf24_server
uint8_t data[] = "A";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
/*
// Now wait for a reply
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.waitAvailableTimeout(500))
{
// Should be a reply message for us now
if (nrf24.recv(buf, &len))
{
Serial.print("got reply: ");
Serial.println((char*)buf);
}
else
{
Serial.println("recv failed");
}
}
else
{
Serial.println("No reply, is nrf24_server running?");
}*/
delay(400);
}
For the Pico I used the micropython nrf24l01.py library. Here is the main.py code:
from nrf24l01 import NRF24L01
from machine import SPI, Pin
from time import sleep
import struct
csn = Pin(14, mode=Pin.OUT, value=1) # Chip Select Not
ce = Pin(17, mode=Pin.OUT, value=0) # Chip Enable
led = Pin(25, Pin.OUT) # Onboard LED
spi = SPI(0, baudrate=10_000_000, polarity=0, phase=0, sck=Pin(6), mosi=Pin(7), miso=Pin(4))
payload_size = 4
#send_pipe = b"\xd2\xf0\xf0\xf0\xf0"
#receive_pipe = b"\xe1\xf0\xf0\xf0\xf0"
#tx pipe address
tx_pipe = b"\xd2\xf0\xf0\xf0\xf0"
#rx pipe address
rx_pipe = b"\xe1\xf0\xf0\xf0\xf0"
def setup():
print("Initialising the nRF24L0+ Module")
nrf = NRF24L01(spi, csn, ce, channel=46, payload_size=payload_size)
nrf.open_tx_pipe(tx_pipe)
nrf.open_rx_pipe(1, rx_pipe)
nrf.start_listening()
return nrf
def flash_led(times:int=None):
''' Flashed the built in LED the number of times defined in the times parameter '''
for _ in range(times):
led.value(1)
sleep(0.01)
led.value(0)
sleep(0.01)
# main code loop
flash_led(10)
nrf = setup()
print("nRF24L01 Listening")
nrf.start_listening()
msg_string = ""
while True:
msg = ""
# Check for Messages
if nrf.any():
print("got somthing!")
package = nrf.recv()
message = struct.unpack("s",package)
msg = message[0].decode()
flash_led(1)
# Check for the new line character
if (msg == "\n") and (len(msg_string) <= 20):
print("full message",msg_string, msg)
msg_string = ""
else:
if len(msg_string) <= 20:
msg_string = msg_string + msg
else:
msg_string = ""
I would be very thankfull if someone could help me figure this out.
Thanks!