NRF24 communication between Arduino Mega and STM32

So I'm working on a remote RC car project. I'm using an Arduino Mega as the "main" controller. It's used to control the motors, LEDs and read the ultrasonic sensors to make sure that the car doesn't bump into something. The remote controller uses an STM32F103C microcontroller because I wanted to have a fast MCU for the remote to reduce latency so that the car responds to the remote controller ASAP. The car and the remote communicate using a NRF24+ module. I found an NRF24 library for the STM32 (GitHub - jaretburkett/RF24-STM: RF24 for STM32duino. nRF24L01+ supporf for STM32F103 Arduino). On my Arduino Mega, I'm using TMRh20's NRF24 library.

The issue is that the transmitter is transmitting successfully, but the receiver isn't receiving anything. On my Arduino Mega, I also tried to check if the isChipConnected function returns true, and it does. I also tried this connection tester sketch:

#include <nRF24L01.h>
#include <RF24.h>
#include "printf.h"

const int pinCE = 7;
const int pinCSN = 8;

RF24 wirelessSPI(pinCE, pinCSN);
 
const uint64_t pAddress = 0xB00B1E5000LL;            

void setup(){
  Serial.begin(115200);
  printf_begin();
  wirelessSPI.begin();
  wirelessSPI.setDataRate(RF24_2MBPS);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.stopListening();
  wirelessSPI.printDetails();

}

void loop(){
}

And it shows correct info. I also tried this sketch on my STM32 (I just changed the library so it uses the modified one - github link above), and it prints the same correct info. I also tried using TMRh20's library on the STM32, but the printDetains() function doesn't work, but I tried the isChipConnected function and it returns true and transmission also seems to work, but the Arduino Mega is not receiving anything.
I also tried to swap the two sketches, so the STM32 was the receiver and the Arduino Mega was the transmitter, but nothing changed. The isChipConnected function was returning true on both and the transmitter was "transmitting successfully", but the receiver wasn't getting anything.

Here's my transmitter sketch:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "printf.h"

const int pinCE = 7;
const int pinCSN = 8;

RF24 wirelessSPI(pinCE, pinCSN);

const uint64_t pAddress = 0x68;

void setup(){
  Serial.begin(115200);
  printf_begin();
  wirelessSPI.begin();
  wirelessSPI.setPALevel(RF24_PA_LOW);
  wirelessSPI.setDataRate(RF24_2MBPS);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.stopListening();
  wirelessSPI.printDetails();

}

void loop(){
  if(!wirelessSPI.isChipConnected()){
    Serial.println("Error");
    return;
  }
  String text = "Ping!";
  char dataToSend[24];
  text.toCharArray(dataToSend, sizeof(dataToSend));
  wirelessSPI.write(&dataToSend, sizeof(dataToSend));
  Serial.print("Transmitted: ");
  Serial.println(dataToSend);
  delay(500);
}

And my receiver code:

#include <SPI.h>
#include "nRF24L01-STM.h"
#include "RF24-STM.h"

const int pinCE = PB1; 
const int pinCSN = PA4;

RF24 wirelessSPI(pinCE, pinCSN);
 
const uint64_t pAddress = 0x68;

void setup(){
  Serial.begin(115200);
  wirelessSPI.begin();
  wirelessSPI.setPALevel(RF24_PA_LOW);
  wirelessSPI.setDataRate(RF24_2MBPS);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.startListening();
}

void loop(){
  if(!wirelessSPI.isChipConnected()){
    Serial.println("Error");
    return;
  }
  if(wirelessSPI.available()){
    char data[24] = "";
    wirelessSPI.read(&data, sizeof(data));
    Serial.println("Received: " + String(data));
  }
}

CommandActor:
The issue is that the transmitter is transmitting successfully, but the receiver isn't receiving anything.

That statement is not logical. Unless the message is received properly you cannot know whether the problem lies with the Tx or the Rx.

Maybe you have a second regular Arduino that you could use as a partner to the Mega and then you really would know that the Mega was transmitting properly

I don't have an STM32 but (with appropriate allowance for the different library) I would expect the examples in this Simple nRF24L01+ Tutorial to work. They certainly work with a Mega and Uno. I presume you are aware that the SPI pins on a Mega are in a different place compared to an Uno.

...R

Hi CommandActor, i had the same problem trying to set my Arduino UNO and the STM32 Nucleo Bords. I check your Post and help me to set my STM32 Code properly. I will Post the STMcubeMx Code since i just copy your Rx Arduino Code, hope it will help you with your Porject.

PS:I am new in this forum so if i did some mistakes by posting the code incorrectly or giving too less information about it pls let me know so i can get better,thanks. :slight_smile:

TX Board:STM Nucle-L476RG
Librarys: STM32-Tutorials/Tutorial 24 - NRF24L01 Radio Transceiver at master · MYaqoobEmbedded/STM32-Tutorials · GitHub

Code: Tx with STMCubeMX

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "MY_NRF24.h"

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart2;


/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// Set TxAddress:
uint64_t TxpipeAddres = 0x68;

//Set the Text massage:
 char myTxData[60] = "Hallo World";
/* USER CODE END 0 */

  /* USER CODE BEGIN 2 */
  //Set the radio.begin() on the STM32 Board, You need to give the CE,CSN Ports and the SPI
  NRF24_begin(GPIOB,GPIO_PIN_9,GPIO_PIN_8,hspi1);
  // Set UART
  nrf24_DebugUART_Init(huart2);
  // Set the Print Radio Settings
  printRadioSettings();
  //Transmit - NO Acknowledgment //
  NRF24_setPALevel(RF_PWR_HIGH);
  NRF24_setAutoAck(false);
  NRF24_setChannel(52);
  NRF24_setPayloadSize(32);
  NRF24_setDataRate(RF24_2MBPS);
  NRF24_openWritingPipe(TxpipeAddres);
  NRF24_stopListening();


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
 // If you send the massage , print it on the Serial output
 if(NRF24_write(myTxData,32))
 {
 HAL_UART_Transmit(&huart2, (uint8_t*)"Transmitted successfully\r\n", strlen("Transmitted successfully\r\n"), 10);
 }

 HAL_Delay(1000);

  }
  /* USER CODE END 3 */
}