0

I have a Raspberry Pi 3 and an Arduino Mega 2560 both attached to their own nRF24L01 chip. This is my first time using the nRF24L01 chip, so I copied and modified some demonstration code that I saw on the Internet whereby I want the Arduino to transmit a Hello World to the Raspberry Pi 3. I show my demo code below.

Code on my transmitting Arduino:

// Arduino-nrf.ino
#include <SPI.h>
#include <RF24.h>

RF24 radio(9,10); 

void setup(){
  Serial.begin(115200);
  Serial.println(0);      //start

  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_1MBPS);
  radio.setChannel(0x76);
  radio.openWritingPipe(0xF0F0F0F0E1LL);
  radio.enableDynamicPayloads();
  radio.powerUp();

}

void loop() {
  const char text[] = "Hello!";
  Serial.println("looping");
  if(radio.write(&text,sizeof(text))){
    Serial.println("Write success!");
  } else {
    Serial.println("Write fail!");
  }
}

I used the https://github.com/TMRh20/RF24/archive/master.zip for my RF24.h. My wiring for the nrf24l01 - Arduino is as follows:

VCC to 3.3
GND to GND
CSN to 8
CE to 7
SCK to 52
MOSI to 51
MISO to 50

Here is the code that I run on my receiving RPi:

// Pi-nrf.py
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1],[0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0,17)

radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)

radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openReadingPipe(1,pipes[1])
radio.printDetails()
radio.startListening()

while True:
  while not radio.available(0):
    time.sleep(1/100)

  receivedMessage = []
  radio.read(receivedMessage, radio.getDynamicPayloadSize())
  if len(receivedMessage) > 0:
    # print("Received: {}".format(receivedMessage))
    string = ""

    for n in receivedMessage:
      if (n >= 32 and n <=126):
        string += chr(n)
    if len(string) > 0 :
      print("Our received message decodes to: {}".format(string))

For my nrf24l01 to RPI3 connection, they are:

VCC to 3.3
GND to GND
CSN to GPIO8
CE to GPIO17
MOSI to GPIO10
MISO to GPIO09
SCK to GPIO11

I start up the RPi code first. Then when I run the Arduino code, I quickly look at the Serial Monitor and I see that it keeps saying looping Write Fail!, but on the RPi end, I see this output:

Our received message decodes to: x
Our received message decodes to: x
Our received message decodes to: xy
Our received message decodes to: xy
Our received message decodes to: xy
Our received message decodes to: x
Our received message decodes to: xy
Our received message decodes to: x
Our received message decodes to: x
Our received message decodes to: y
Our received message decodes to: y
Our received message decodes to: x
Our received message decodes to: y
Our received message decodes to: x
Our received message decodes to: y
Our received message decodes to: ~s~
Our received message decodes to: ~s~
Our received message decodes to: ~s~
Our received message decodes to: s
Our received message decodes to: ~s~
Our received message decodes to: {
Our received message decodes to: {{

Anyone know why the RPi is printing out gobbly goop instead of the phrase Hello!? Or what I can do to successfully transmit the phrase Hello!?

  • @jsotola yes, I tried to pay around with data rate but I have an issue where I can't go any lower than 1MBPS. I've asked that problem here: raspberrypi.stackexchange.com/questions/94293/… – John Feb 16 at 20:00
  • The output is still rather random and unintelligible regardless of whether I sent one character or multiple characters.. For example, sending "A" could result in "666666" or "266666" or "{" or anything else. likewise for "AAAAA" – John Feb 16 at 20:02
  • I suspect the issue might be with the Arduino Mega itself... so I'm going to buy some more Arduinos shortly to test. – John Feb 16 at 20:02
  • thanks I just tried changing the channels on both the rpi and the arduino, but exactly the same issues. I also tried the newly arduino uno that I purchased and also the same issue. Maybe I should try a basic example of getting two arduinos to talk to each other first before trying to get an arduino talking to an rpi. – John Feb 16 at 23:10
  • that is a very good idea – jsotola Feb 17 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.