Trouble with setting my nrf24lo1+ modules to work

Hello everyone,

I am working on making a hovercraft. I want to do this using radiosignals.

It used to work but for some reason I cant get the radio connection working anymore.
It is possible I accidently changed somethings. Thats why I want to go back to basics to make sure the modules are still working. Can someone help me out here. Also it is for a schoolproject so I am in a bit of a hurry.

Greetings Lucas

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R

Okay so with ur code I got it all working with and without my base modules (see attachment for serial monitor). So that mean everything still works and the problem is probably in my code right?

If thats the case can u help me write a code that is able to transmit the data of two joystick. And then the receiver, depending on that values received must be able to control a servo and two brushless motors/esc (uses servo library/pwm signals between 1000 and 2000)

Insanewolf01:
If thats the case can u help me write a code that is able to transmit the data of two joystick.

You already have most of that in my examples.

Have a go at adapting one of them (probably the first example) to send two integer values in an array. The first value can represent the first joystick and the second value ...

If you get stuck post the pair of programs that represent your best attempt.

...R

Hey thanks for the reply I mixed ur code with a code from a tutorial on the internet and made these codes

transmitter:

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN   9
#define CSN_PIN 10

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

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

struct dataStruct {
  byte valX;          // The Joystick position values
  byte valY;
  byte valSW;
  byte valX2;
  byte valY2;
  byte valSW2;
}; 
dataStruct myData; 

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


void setup() {
    Serial.begin(9600);
    Serial.println("SimpleTx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
    myData.valX = 127;
    myData.valY = 127;
    myData.valSW = 0;
    myData.valX2 = 127;
    myData.valY2 = 127;
    myData.valSW2 = 0;
}

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

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

//====================
void GetJoyValues(){
    myData.valX = map( analogRead(A0), 0, 1024, 0, 255);
    myData.valY = map( analogRead(A1), 0, 1024, 0, 255);
    myData.valSW = digitalRead(2);
    myData.valX2 = map( analogRead(A2), 0, 1024, 0, 255);
    myData.valY2 = map( analogRead(A3), 0, 1024, 0, 255);
    myData.valSW = digitalRead(4);
}
void send() {
    bool rslt;
    rslt = radio.write( &myData, sizeof(myData) );
        // 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 ");
    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>
#include <Servo.h>

#define CE_PIN   9
#define CSN_PIN 10

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

RF24 radio(CE_PIN, CSN_PIN);
Servo myServo;
Servo myMotorThrust;
Servo myMotorLift;

int liftValue = 0;
int thrustValue = 0;
int angleValue = 0;
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

struct Received_data {
  byte valX;          // The Joystick position values
  byte valY;
  byte valSW;
  byte valX2;
  byte valY2;
  byte valSW2;
}; 
Received_data received_data; 

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

void setup() {

    Serial.begin(9600);
    myServo.attach(3);
    myMotorThrust.attach(9);
    myMotorLift.attach(5);
    received_data.valX = 127;
    received_data.valY = 127;
    received_data.valSW = 0;
    received_data.valX2 = 127;
    received_data.valY2 = 127;
    received_data.valSW2 = 0;
    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

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

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

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

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

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        newData = false;
    }
}
void RunComponents() {
  
    thrustValue = map(received_data.valX2,0,255,1000,2000);
    angleValue = map(received_data.valY,0,255,0,180);
    liftValue = map(received_data.valX,0,255,1000,2000);
    
    myServo.write(angleValue);
    myMotorThrust.writeMicroseconds(thrustValue);
    myMotorLift.writeMicroseconds(liftValue);
    
    Serial.print("Angle: ");
    Serial.print(angleValue);
    Serial.print("\n");
    Serial.print("Thrust: ");
    Serial.print(thrustValue);
    Serial.print("\n");
    Serial.print("Lift: ");
    Serial.print(liftValue);
}

But it doesn't really work

Can you please help me make this work I can read code but writing my own is still hard.
Note that I deleted some of your code. Otherwise I could'nt compile the code (Did this when mixing up with the other code before hand it worked).

Thanks already

Okay played around with it some more and made these two codes

Transmitter:

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN   9
#define CSN_PIN 10
#define SW_PIN   2
#define SW2_PIN   4

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

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

int JoyValues[6];

void setup() {
    Serial.begin(9600);
    pinMode(SW_PIN, INPUT);
    digitalWrite(SW_PIN, HIGH);
    pinMode(SW2_PIN, INPUT);
    digitalWrite(SW2_PIN, HIGH);
    Serial.println("SimpleTx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
    JoyValues [0] = 1500;
    JoyValues [1] = 1500;
    JoyValues [2] = 1;
    JoyValues [3] = 1500;
    JoyValues [4] = 1500;
    JoyValues [5] = 1;
}

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

void loop() {
    GetJoyValues();
    send();
  
}

//====================
void GetJoyValues(){
    JoyValues[0] = map( analogRead(A0), 0, 1024, 1000, 2000);
    JoyValues[1] = map( analogRead(A1), 0, 1024, 1000, 2000);
    JoyValues[2] = digitalRead(2);
    JoyValues[3] = map( analogRead(A2), 0, 1024, 1000, 2000);
    JoyValues[4] = map( analogRead(A3), 0, 1024, 1000, 2000);
    JoyValues[5] = digitalRead(4);
}
void send() {
    bool rslt;
    rslt = radio.write( &JoyValues, sizeof(JoyValues) );
        // 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(JoyValues[0]);
    Serial.print(" ,");
    Serial.print(JoyValues[1]);
    Serial.print(" ,");
    Serial.print(JoyValues[2]);
    Serial.print(" ,");
    Serial.print(JoyValues[3]);
    Serial.print(" ,");
    Serial.print(JoyValues[4]);
    Serial.print(" ,");
    Serial.print(JoyValues[5]);
    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>
#include <Servo.h>

#define CE_PIN   9
#define CSN_PIN 10

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

RF24 radio(CE_PIN, CSN_PIN);
Servo myServo;
Servo myMotorThrust;
Servo myMotorLift;

int JoyValues[6]; // this must match dataToSend in the TX
bool newData = false;

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

void setup() {

    Serial.begin(9600);
    myServo.attach(3);
    myMotorThrust.attach(9);
    myMotorLift.attach(5);
    JoyValues[0] = 1500;
    JoyValues[1] = 1500;
    JoyValues[2] = 0;
    JoyValues[3] = 1500;
    JoyValues[4] = 1500;
    JoyValues[5] = 0;
    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

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

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

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

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

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(JoyValues[0]);
        Serial.println(JoyValues[1]);
        Serial.println(JoyValues[2]);
        Serial.println(JoyValues[3]);
        Serial.println(JoyValues[4]);
        Serial.println(JoyValues[5]);
        newData = false;
    }
}

I got rid of only transmitting once per second because my hovercraft needs constant adjustment.
Now I notice it not being so reliable anymore sometimes it sends the values over succesful
but sometimes it doesnt (see serial monitor in attachment). It help to wiggle the modules in their base modules a bit then suddenly it sends the values over to the other module. So i guess somewhere in the connection something is probably broken. This also happened without the base modules then I would wiggle cables a bit to get it working. But what I dont understand why with ur code it works so reliable compared to mine.

Insanewolf01:
But it doesn't really work

You need to tell us in detail what it actually does and what you want it to do that is different.

...R

PS.. the same applies to Reply #5

Okay so using the same procedure (wiring and wiggling) I get different results depending if I am using my codes or your codes.

With your codes I get this serial monitor

With my current codes I get this serial monitor

Now you can only see zero's in the receiver serial monitor thats probably short circuit because I am wiggling them around but they have also sent over succesful sometimes

My current codes

Transmitter:

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN   9
#define CSN_PIN 10
#define SW_PIN   2
#define SW2_PIN   4

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

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

int JoyValues[6];

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

void setup() {
    Serial.begin(9600);
    pinMode(SW_PIN, INPUT);
    digitalWrite(SW_PIN, HIGH);
    pinMode(SW2_PIN, INPUT);
    digitalWrite(SW2_PIN, HIGH);
    Serial.println("SimpleTx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
    JoyValues [0] = 1500;
    JoyValues [1] = 1500;
    JoyValues [2] = 1;
    JoyValues [3] = 1500;
    JoyValues [4] = 1500;
    JoyValues [5] = 1;
}

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

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

//====================
void GetJoyValues(){
    JoyValues[0] = map( analogRead(A0), 0, 1024, 1000, 2000);
    JoyValues[1] = map( analogRead(A1), 0, 1024, 1000, 2000);
    JoyValues[2] = digitalRead(2);
    JoyValues[3] = map( analogRead(A2), 0, 1024, 1000, 2000);
    JoyValues[4] = map( analogRead(A3), 0, 1024, 1000, 2000);
    JoyValues[5] = digitalRead(4);
}
void send() {
    bool rslt;
    rslt = radio.write( &JoyValues, sizeof(JoyValues) );
        // 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(JoyValues[0]);
    Serial.print(" ,");
    Serial.print(JoyValues[1]);
    Serial.print(" ,");
    Serial.print(JoyValues[2]);
    Serial.print(" ,");
    Serial.print(JoyValues[3]);
    Serial.print(" ,");
    Serial.print(JoyValues[4]);
    Serial.print(" ,");
    Serial.print(JoyValues[5]);
    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>
#include <Servo.h>

#define CE_PIN   9
#define CSN_PIN 10

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

RF24 radio(CE_PIN, CSN_PIN);
Servo myServo;
Servo myMotorThrust;
Servo myMotorLift;

int JoyValues[6]; // this must match dataToSend in the TX
bool newData = false;

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

void setup() {

    Serial.begin(9600);
    myServo.attach(3);
    myMotorThrust.attach(9);
    myMotorLift.attach(5);
    JoyValues[0] = 1500;
    JoyValues[1] = 1500;
    JoyValues[2] = 0;
    JoyValues[3] = 1500;
    JoyValues[4] = 1500;
    JoyValues[5] = 0;
    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

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

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

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

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

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(JoyValues[0]);
        Serial.println(JoyValues[1]);
        Serial.println(JoyValues[2]);
        Serial.println(JoyValues[3]);
        Serial.println(JoyValues[4]);
        Serial.println(JoyValues[5]);
        newData = false;
    }
}

Got a succesful transmission once but it is so unconsistent compared to your code
succesful transmission.PNG

succesful transmission.PNG

Please make your images visible in your Posts. See this Simple Image Guide

I suspect what you have posted in the images are pictures of text. If so, please don't post pictures of text, just copy and paste it and put it inside code tags.

...R

I made the images visable. Also I played around with it a bit more and it just seems like my modules have a hard time sending over intergers while characters are easy. So i am thinking about sending over characters and convert that into intergers on the receiver but I have no idea how to do that.

Insanewolf01:
I made the images visable.

Your should have noticed by now that some of them are unreadable - which is why I asked you to copy and paste the text

Also I played around with it a bit more and it just seems like my modules have a hard time sending over intergers while characters are easy. So i am thinking about sending over characters and convert that into intergers on the receiver but I have no idea how to do that.

Slow down. If you keep changing things I can't keep up.

An nRF24 just sends bytes. It has no idea whether they are characters or integers. Sending integers will be easier for the Arduino.

I don't immediately see anything wrong with the code in Reply #7.

Did you try my example with absolutely NO CHANGES and did it work perfectly?

...R