Communication between Arduino Nano and Rasperry Pi via 2.4 GHz radio
Using the nRF24L01 chip and on both ends the RF24 library.
Check that the pins are correctly connected!
For this example on the Arduino CE -> D9 and CSN -> D10,
hence RF24 is initialized with RF24 radio(10,9);
.
And on the Raspberry PI CSN -> 25 (it looks like CE doesn’t matter),
hence radio is initialized with radio = RF24(25,0)
.
The 0
comes from the SPI device name. Check with ls /dev | grep spi
,
e.g. spidev0.0 = 0 and spidev0.1 = 1, etc.
I’m still not quite sure, if the documentation is wrong about the pins or I just don’t get it.
Anyway with the above mentioned setup and the following code I got it finally working! The example is very simple and just send a timestamp from the Arduino to the Raspberry. But I hope you can use it for testing or as a template to build on.
Arduino
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(10,9);
const uint64_t rx_address = 0xF0F0F0F0E1LL;
const uint64_t tx_address = 0xF0F0F0F0D2LL;
void setup() {
Serial.begin(115200);
printf_begin();
radio.begin();
radio.enableDynamicPayloads();
radio.setRetries(5, 15);
radio.openWritingPipe(tx_address);
radio.openReadingPipe(1, rx_address);
radio.startListening();
radio.printDetails();
Serial.println(F("Setup done."));
}
void loop() {
delay(5000);
radio.stopListening();
Serial.print(F("Now sending: "));
unsigned long start_time = micros();
Serial.print(start_time);
Serial.print(F(" ... "));
if (!radio.write( &start_time, sizeof(unsigned long) )){
Serial.println(F("failed."));
} else {
Serial.println(F("ok."));
}
}
Raspberry Pi
from __future__ import print_function
import time
from RF24 import *
import RPi.GPIO as GPIO
tx_address = 0xF0F0F0F0E1
rx_address = 0xF0F0F0F0D2
radio = RF24(25,0);
radio.begin()
radio.enableDynamicPayloads()
radio.setRetries(5, 15)
radio.openWritingPipe(tx_address)
radio.openReadingPipe(1, rx_address)
radio.startListening()
radio.printDetails()
print("Listening...")
while True:
radio.startListening()
if radio.available():
receive_payload = radio.read(4)
print("{}".format(int.from_bytes(receive_payload,
byteorder='little', signed=False)))
time.sleep(0.1)