Arduino and NRF24lo1 code HELP!

Hi i made this code to work with the NRF24lo1 module but they are not connecting and iam very stuck i have attached the code for the transmitter and receiver below.

Receiver:

#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>
//receiver

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
//#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

RF24 radio(10, 9);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network
const uint16_t this_node = 012;  // Address of our node in Octal format ( 04,031, etc)
const uint16_t node01 = 01;    // Address of the other node in Octal format


int button_state = 0;
int addr1 = 0;
int addr2 = 0;
int addr3 = 0;
int addr4 = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
int n4 = 0;

void setup(){
Serial.begin(9600);
Serial.print("firmware 5 UPDATED");
  
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  SPI.begin();
  radio.begin();
  network.begin(90, this_node); //(channel, node address)       //This sets the module as receiver

display.display();
  delay(10);
  display.clearDisplay();
  display.setCursor(0,0);
  display.println("Locomotive");
  display.setCursor(0,15);
  display.println("Headcode");
  display.display();
  delay(2500);
  display.setFont(&FreeSansBold18pt7b);
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE); 
  display.setCursor(30,28); // second letter
  display.clearDisplay();
}

void loop(){
  network.update();
    while ( network.available() ) {     // Is there any incoming data?
    RF24NetworkHeader header;
    unsigned long button_state;
    network.read(header, &button_state, sizeof(button_state)); // Read the incoming data

    //button_state = incomingData;

 if (button_state == 2) {
    addr1 += 1;
  }
if (button_state == 3) {
    addr2 += 1;
  }
if (button_state == 4) {
    addr3 += 1;
  }
if (button_state == 5) {
    addr4 += 1;
  }
if (addr2 == 25) {
    addr2 = 0;


  if (addr1 == 10) {
    addr1 = 0;
  }
  if (addr3 == 10) {
    addr3 = 0;
  }
  if (addr4 == 10) {
    addr4 = 0;
  }


  
  }
  if (addr2 == 0) {
   display.print("A"); // a
  }
  if (addr2 == 1) {
    display.print("B");// b
  }
  if (addr2 == 2) {
    display.print("C");// c
  }
  if (addr2 == 3) {
    display.print("D");// d
  }
  if (addr2 == 4) {
    display.print("E");// e
  }
  if (addr2 == 5) {
    display.print("F");// f
  }
  if (addr2 == 6) {
    display.print("G");// g
  }
  if (addr2 == 7) {
    display.print("H");// h
  }
  if (addr2 == 8) {
    display.print("I");// i
  }
  if (addr2 == 9) {
    display.print("J");// j
  }
  if (addr2 == 10) {
    display.print("K");// k
  }
  if (addr2 == 11) {
    display.print("L");// l
  }
  if (addr2 == 12) {
    display.print("M");// m
  }
  if (addr2 == 13) {
    display.print("N");// n
  }
  if (addr2 == 14) {
    display.print("O");// o
  }
  if (addr2 == 15) {
    display.print("P");// p
  }
  if (addr2 == 16) {
    display.print("Q");// q
  }
  if (addr2 == 17) {
    display.print("R");// r
  }
  if (addr2 == 18) {
    display.print("S");// s
  }
  if (addr2 == 19) {
    display.print("T");// t
  }
  if (addr2 == 20) {
    display.print("U");// u
  }
  if (addr2 == 21) {
   display.print("V"); // v
  }
  if (addr2 == 22) {
   display.print("W"); // w
  }
  if (addr2 == 23) {
    display.print("X");// x
  }
  if (addr2 == 24) {
    display.print("Y");// y
  }
  if (addr2 == 25) {
    display.print("Z");// z
  }
   
  display.setCursor(0, 28); //first
  display.print(addr1);
  display.setCursor(70, 28); //third
  display.print(addr3);
  display.setCursor(100, 28); //fouth
  display.print(addr4);
  display.display();
  Serial.println(button_state);
    
    }
}

Transmitter:

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


RF24 radio(10, 9);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network
const uint16_t this_node = 00;   // Address of this node in Octal format ( 04,031, etc)

const uint16_t node012 = 012;

int buttonState1 = 0;

void setup() {
  SPI.begin();
  radio.begin();
  network.begin(90, this_node);  //(channel, node address)
  radio.setDataRate(RF24_2MBPS);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  network.update();

 if (digitalRead(5) == LOW) {
   buttonState1 = 2;
  }

   if (digitalRead(6) == LOW) {
   buttonState1 = 3;
  }

  if (digitalRead(7) == LOW) {
   buttonState1 = 4;
  }

  if (digitalRead(8) == LOW) {
   buttonState1 = 5;
  }
 if ((digitalRead(5) != LOW && digitalRead(6) != LOW) && (digitalRead(7) != LOW && digitalRead(8) != LOW)) {
buttonState1 = 0;     
}

    
  Serial.println(buttonState1);
  //unsigned long buttonState = buttonState1);
  RF24NetworkHeader header4(node012);    // (Address where the data is going)
  bool ok3 = network.write(header4, &buttonState1, sizeof(buttonState1)); // Send the data
delay(100);
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

I suggest that you get the radios to work on their own before you add a bunch of other code.

If you read and, closely, follow Robin2's simple rf24 tutorial you should be able to get them working. That tutorial sure helped me. The code in the examples has been proven to work many many times. If it does not work for you, there is likely a hardware problem.

Run the CheckConnection.ino (look in reply #30 in the tutorial) to verify the physical wiring between the radio module and its processor (Arduino).

It is very important that you get the 3.3V supply to the radio modules to supply 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.

Once you get the radios talking to each other, you could start to slim down the code. For example, this:

if (addr2 >= 0 && addr2 <= 25) {
   display.print('A' + addr2);  // note the single quotes
}

could replace all this:

if (addr2 == 0) { 
  display.print("A"); // a 
} 
if (addr2 == 1) { 
  display.print("B");// b 
} 
if (addr2 == 2) { 
  display.print("C");// c 
}
. . .
. . .
if (addr2 == 25) { 
  display.print("Z");// z
}

Thank you i think i have got it working!

1 Like

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