nRF24 Single Variable Transmission

Hello everyone, first time posting here. I am having trouble with the nRF24L01 modules I just got my arduino uno. I am just trying to send 1 int value from module to another. I have my code below, I have no clue where the error could possibly be. I am simply performing an analog read of a potentiometer on my transmitting uno, then trying to send its value to the my second uno so I can rotate a stepper motor.

I have messed with the code multiple times now and I cannot figure out why the receiver is not picking up the value from the transmitter. Or why the transmitter is not sending the value. I also have checked a million times and I know all my pins are connected correctly, and I am using the 3.3V pins on each of my uno's to power the nRF, not the 5V pin. Furthermore, I know the potentiometer is reading the correct value on the transmitter, I have checked the serial port each time to make sure the value changes correctly.

Transmitter Code:

`Preformatted text`/*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

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


int pot=A0;
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openWritingPipe(address);
}

void loop() {
  radio.startListening();
  int potValue = analogRead(pot);
  int angle = map(potValue, 0, 1023, 0, 360);
  radio.write(&angle, sizeof(angle));
  Serial.println(angle);
  radio.stopListening();
  delay(5);
  }

Receiver Code:

/*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

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

RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

int CCS = 10000;
int CCF = 5000;
int CWS = 10000;
int CWF = 5000;
int blue = 6;   //former pin 6
int yellow = 5; //pin 5
int pink = 4;   //pin 4
int orange = 3; //pin 3

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openReadingPipe(0, address);

  pinMode(blue, OUTPUT);          //make pin 12 an output
  pinMode(yellow, OUTPUT);      //make pin 11 an output
  pinMode(pink, OUTPUT);         //make pin 10 an output
  pinMode(orange , OUTPUT);   //make pin 9 an output
}

void loop() {
  if ( radio.available()) {
    while (radio.available()) {
      radio.startListening();
      int angle = 0;
      radio.read(&angle, sizeof(angle));
      radio.stopListening();  
      Serial.println(angle);

      //Create our loop to alter the angle of the motor
      if (angle > 0 && angle < 10) { //no rotation between 0 and 10, note I copy paste this code mutliple times between each speed/direction change, I know I could have a very long if statement condition here,
        //copy pasting it helps my brain more
        digitalWrite(orange, LOW); //~3     9
        digitalWrite(pink, LOW);   //4     ~10
        digitalWrite(yellow, LOW); //~5    ~11
        digitalWrite(blue, LOW);   //~6     12
        delayMicroseconds(CCS);
        digitalWrite(orange, LOW);
        digitalWrite(pink, LOW);
        digitalWrite(yellow, LOW);
        digitalWrite(blue, LOW);
        delayMicroseconds(CCS);
        digitalWrite(orange, LOW);
        digitalWrite(pink, LOW);
        digitalWrite(yellow, LOW);
        digitalWrite(blue, LOW);
        delayMicroseconds(CCS);
        digitalWrite(orange, LOW);
        digitalWrite(pink, LOW);
        digitalWrite(yellow, LOW);
        digitalWrite(blue, LOW);
        delayMicroseconds(CCS);
      }
}

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Can you communicate using some examples that come with the library that you use to verify that you do not have other problems (e.g. bad connections, bad reception). I know that I had endless problems due to interference.

How are you powering the nRFs?

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Thank you for the quick reply, and I do not have time to set up the examples right now, I need to get some sleep. However, I will try them in the morning to make sure they are transmitting and receiving correctly.

Also hopefully I edited the original post correctly, and sorry for posting in the wrong location.

Looks a lot better :wink:

Here is tested example code to send and receive one int number. It is adapted from the examples in Robin2's simple rf24 demo tutorial. Read and follow the tutorial and you have a good chance to get the radios to work. Especially pay attention to how to properly power the radio modules.

Sender:

// SimpleTx - the master or the transmitter

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int dataToSend = 123;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

void setup()
{
   Serial.begin(9600);

   Serial.println("SimpleTx Starting");

   radio.begin();
   //RF24_1MBPS, RF24_2MBPS, RF24_250KBPS
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   //radio.setChannel(10);
   //RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
   radio.setPALevel(RF24_PA_LOW);
   radio.openWritingPipe(slaveAddress);
}
//====================

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      dataToSend++;
      send();
      prevMillis = millis();
   }
}

//====================

void send()
{
   bool rslt;
   rslt = radio.write( &dataToSend, sizeof(dataToSend) );
   // Always use sizeof() as it gives the size as the number of bytes.
   // For example if dataToSend was an int sizeof() would correctly return 2

   Serial.print("Data Sent ");
   Serial.print(dataToSend);
   if (rslt)
   {
      Serial.println("  Acknowledge received");
   }
   else
   {
      Serial.println("  Tx failed");
   }
}

Receiver:

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

int recvData;

bool newData = false;

//===========

void setup()
{
   Serial.begin(9600);

   Serial.println("SimpleRx Starting");
   radio.begin();
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   //radio.setChannel(10);
   radio.setPALevel(RF24_PA_LOW);
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &recvData, sizeof(recvData) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("number = ");
      Serial.println(recvData);
      
      newData = false;
   }
}

Update, I have just tried the example code you provided groundFungus, thank you very much. My transmitters are working correctly, so the error must be somewhere in original code. I am going to try and adapt this example code to my project.

I think that you need to start listening before you check for available data.

THANK YOU!
I tested this code and it is the first time i have had my transcievers working
I am running these off of Nanos and getting slow reads, ie itll read one out of 100 messages sent.
Is this simply a power issue on the receiver?

How are the rf24 modules powered?

Are the modules the high power modules with the external antenna?

Here is my "usual suspects list"

Make sure the rf24 power supply can provide enough current. This is especially true for the high power (external antenna) modules. I use homemade adapters like these. They are powered by 5V and have a 3.3V regulator on the board. Robin2 also has suggested trying with a 2 AA cell battery pack.

If using the high powered radios make sure to separate them by a few meters. They may not work too close together. Try the lower power settings.

Reset the radios by cycling power to them after uploading new code. I have found that to help. They do not reset with the Arduino.

Switch to 1MB data rate to catch the not so cloned clones.
'radio.setDataRate( RF24_1MBPS );'

Also for some clones, change TMRh20's RF24.cpp line 44
_SPI.setClockDivider(SPI_CLOCK_DIV2);
Into
_SPI.setClockDivider(SPI_CLOCK_DIV4);
Thanks @6v6gt

I do not use the antenna ones. I just have the small ones, not sure how else to describe them. But they are powered by the 3.3V power supply on the arduino. I have the uno so the 3.3V supply was along the same row as the analog pins.

The Uno 3.3V often will not supply enough current. See below.

Make sure the rf24 power supply can provide enough current. This is especially true for the high power (external antenna) modules. I use homemade adapters like these. They are powered by 5V and have a 3.3V regulator on the board. Robin2 also has suggested trying with a 2 AA cell battery pack.

about how much current do you think they should get?
I have some knockoff breadboard power supplies i am about to try
otherwise i will snag those adapters if the knockoff doesnt work

I don't know a number for sure, but I have read 300mA. I did a search for "breadboard power supply output current" and it seems they are good for up to about 700mA which is plenty.

A 10uF cap across the module power pins (as close to the module as possible) is also known to help.

I’ve got a capacitor across the 3.3 and ground
Oddly enough switching 3.3 and ground off of the nano and to the supply board stops the transmitting entirely.
At the moment it has seemingly stopped receiving, lots of transmit failed

I have no idea why that would happen. I have no experience with those supplies so can't comment on their reliability or functionality.

Post photos of your setup.

Post the code that you are using. Not screenshots. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

transmitter
// SimpleTx - the master or the transmitter

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int dataToSend = 123;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

void setup()
{
   Serial.begin(9600);

   Serial.println("SimpleTx Starting");

   radio.begin();
   //RF24_1MBPS, RF24_2MBPS, RF24_250KBPS
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   //radio.setChannel(10);
   //RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
   radio.setPALevel(RF24_PA_LOW);
   radio.openWritingPipe(slaveAddress);
}
//====================

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      dataToSend++;
      send();
      prevMillis = millis();
   }
}

//====================

void send()
{
   bool rslt;
   rslt = radio.write( &dataToSend, sizeof(dataToSend) );
   // Always use sizeof() as it gives the size as the number of bytes.
   // For example if dataToSend was an int sizeof() would correctly return 2

   Serial.print("Data Sent ");
   Serial.print(dataToSend);
   if (rslt)
   {
      Serial.println("  Acknowledge received");
   }
   else
   {
      Serial.println("  Tx failed");
   }
}

reciever

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

int recvData;

bool newData = false;

//===========

void setup()
{
   Serial.begin(9600);

   Serial.println("SimpleRx Starting");
   radio.begin();
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   //radio.setChannel(10);
   radio.setPALevel(RF24_PA_LOW);
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &recvData, sizeof(recvData) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("number = ");
      Serial.println(recvData);
      
      newData = false;
   }
}

currently it is Soley powered by the nano as my external supply board doesn't seem to power it
i likely plugged something in wrong but cant seem to figure it out
or the board is dead
I should be getting the adapter chips in, hoping that solves the problem
it will occasionally receive what is transmitted, when adding capacitators nothing changed


It is only on the breadboard so I could try capacitators
I tried a 10 micro farad and a 100 micro farad

Revisit post #9. High powered modules being the ones with the external antenna like yours.

they are set on low power
unless there is a power setting lower that I didn't see in the library
Which version of the rf library re you using?
I am on the 1.17 mentioned in the robin tutorial
I will see if i can get another computer up to test this farther apart


I am now getting the error where the receiver prints the message received continuously
So I loaded up and ran robins test connection code but i do not know what to make of the result.

edit
fixed the receiver issue i think
no more of the random printing, the test connection page remains the same
Now i am back to the transmitter giving tx failed and no receiving from the receiver
Both units are using the linked power adapter boards
separated by at least 5 meters and still nothing

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.