HELP, NRF24L01 SERVO AND POTENTIOMETER

Need some help on how to use a potentiometer to control a servo wirelessly using the nrf24l01 modules. My nrf24l01 modules are working completely well ,but i cannot get the pot to interact with the servo because the code isnt working. Here is the code:

Transmitter

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

RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
void setup() {
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MIN);
}
void loop() {
  delay(5);
  radio.stopListening();
  int potValue = analogRead(A0);
  Serial.write(potValue);
  int angleValue = map(potValue, 0, 1023, 0, 180);
  radio.write(&angleValue, sizeof(angleValue));
  delay(5);
  }

Receiver:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
Servo myServo;
void setup() {
  myServo.attach(8);
  radio.begin();
  radio.openWritingPipe(addresses[0]); // 00001
  radio.openReadingPipe(1, addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MIN);
}
void loop() {
  delay(5);
  radio.startListening();
  if ( radio.available()) {
    while (radio.available()) {
      int angleV = 0;
      radio.read(&angleV, sizeof(angleV));
      Serial.write(angleV);
      myServo.write(angleV);
    }
  }
}

Here is the wiring
CE pin to 9
CSN pin to 10
SCN pin to 13
Mosi pin to 11
Miso pin to 12
on both boards

pot to A0

Sg90 servo digital pin to 8

You have posted the Tx code twice.

You only have a 10 millisec delay between transmissions. Try increasing it to 100msecs. That should be sufficient to give a good response and also allow time for the wireless and the receiver code to do its thing.

I'm not sure why you are posting your nRF24 wiring if you say the wireless part is working correctly.

If you have not got the wireless working reliably then 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

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

...R

ive already tried your examples and they work fine, i meant that ive got the modules to work using your code but im stuck with it now that iv'e tried using it with a pot and servo

Did you change the code in response to Robin2's post? If so, post the new version so that we can keep up.

What happens when you run the code? Does the receiver get any data (serial print output)?

How is the servo powered?

Apologies, it seems to have been me who copied your Tx code twice thinking I was copying the Rx code. Senior moment!

Plus what @groundFungus said.

...R

i didnt change the code, but i whenever i run it the servo motor keeps twitching, it is powered via the 5v arduino nano, i wired the 5v from the arduino nano to a breadboard so i can power both my nrf24l01 and my servo

snakeviper123:
i didnt change the code, but i whenever i run it the servo motor keeps twitching,

I made a suggestion for a change to your Tx program. Please try it and let us know the result.

If it does not solve the problem please post the revised code.

...R

Oh sorry forgot to mention, i already tried extending the delay to 100 but it still doesnt work

What happens when you run the code? Does the receiver get any data (serial print output)?
I repeat.

nothing at all, even without the tx wired the rx still twitches for some reason

You said that you had the radios working with Robin2's example code:

ive already tried your examples and they work fine,

It is a simple matter to modify his example code to do what you want. May I suggest that you go back and get the (unmodified) example to work again. I mean the simple RX and simple TX examples. Then we can help you to modify those to get your project to work.

Aaaaaand now its outputting this when i use the simple tx and rx code:
SimpleTx Starting
Data Sent Message 0 Acknowledge received
Data Sent Message 1 Tx failed
Data Sent Message 1 Acknowledge received
Data Sent Message 2 Acknowledge received
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
Data Sent Message 3 Acknowledge received
Data Sent Message 4 Acknowledge received
Data Sent Message 5 Tx failed
Data Sent Message 5 Tx failed

here is the code from robin's tutorial:

Rx:

// SimpleRx - the slave or the receiver

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

#define CE_PIN   9
#define CSN_PIN 10

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

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

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

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

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

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

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

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

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

Tx:

// 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

char dataToSend[10] = "Message 0";
char txNum = '0';


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();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

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

void loop() {
    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        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");
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }
}

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

void updateMessage() {
        // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[8] = txNum;
}

It is useful to see the output from the sender and receiver at the same time. To do that, open 2 different instances of the IDE (do not use File, New to open a new window) and select each Arduino's comm port in a separate instance of serial monitor. Now you can see the transmit and receive messages at the same time.

What does the receiver show?

How close together are the radio modules? It sometimes helps if you move them apart some.

I have 2 radios hooked to 2 Unos and running the same code, as I write this, successfully.

How are the radios powered (from the Nano 3.3V)? Do you have a 10uF to 100uF cap across the radio module power pins?

I have there a simple TX and RX code to control a servo with a pot.

Tx:

#include <SPI.h>                      //the communication interface with the modem
#include "RF24.h"  //the library which helps us to control the radio modem

int msg[1];

RF24 radio(5,10);                     //5 and 10 are a digital pin numbers to which signals CE and CSN are connected.
                                      
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.


void setup(void){
  radio.begin();                      //it activates the modem.
  radio.openWritingPipe(pipe);        //sets the address of the receiver to which the program will send data.
}

void loop(void){
  msg[0] =  map (analogRead(0), 0, 1023, 0, 179); 
  radio.write(msg, 1);
}

Rx:

#include <Servo.h>    //the library which helps us to control the servo motor
#include <SPI.h>      //the communication interface with the modem
#include "RF24.h"     //the library which helps us to control the radio modem

Servo myServo;        //define the servo name

RF24 radio(5,10);     /*This object represents a modem connected to the Arduino. 
                      Arguments 5 and 10 are a digital pin numbers to which signals 
                      CE and CSN are connected.*/

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.

int msg[1];

void setup(){
  myServo.attach(3);                //3 is a digital pin to which servo signal connected.
  radio.begin();                    //it activates the modem.
  radio.openReadingPipe(1, pipe);   //determines the address of our modem which receive data.
  radio.startListening();           //enable receiving data via modem
  }

void loop(){
  if(radio.available()){            //checks whether any data have arrived at the address of the modem
    bool done = false;              //returns a “true” value if we received some data, or “false” if no data.
    while (!done) {
      done = radio.read(msg, 1);
      myServo.write(msg[0]);
    }
  }
}

There is the schematic:

It's working great for me

groundFungus:
It is useful to see the output from the sender and receiver at the same time. To do that, open 2 different instances of the IDE (do not use File, New to open a new window) and select each Arduino's comm port in a separate instance of serial monitor. Now you can see the transmit and receive messages at the same time.

What does the receiver show?

How close together are the radio modules? It sometimes helps if you move them apart some.

I have 2 radios hooked to 2 Unos and running the same code, as I write this, successfully.

How are the radios powered (from the Nano 3.3V)? Do you have a 10uF to 100uF cap across the radio module power pins?

groundFungus:
It is useful to see the output from the sender and receiver at the same time. To do that, open 2 different instances of the IDE (do not use File, New to open a new window) and select each Arduino's comm port in a separate instance of serial monitor. Now you can see the transmit and receive messages at the same time.

What does the receiver show?

How close together are the radio modules? It sometimes helps if you move them apart some.

I have 2 radios hooked to 2 Unos and running the same code, as I write this, successfully.

How are the radios powered (from the Nano 3.3V)? Do you have a 10uF to 100uF cap across the radio module power pins?

Winchesters:
I have there a simple TX and RX code to control a servo with a pot.

Tx:

#include <SPI.h>                      //the communication interface with the modem

#include "RF24.h"  //the library which helps us to control the radio modem

int msg[1];

RF24 radio(5,10);                    //5 and 10 are a digital pin numbers to which signals CE and CSN are connected.
                                     
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.

void setup(void){
  radio.begin();                      //it activates the modem.
  radio.openWritingPipe(pipe);        //sets the address of the receiver to which the program will send data.
}

void loop(void){
  msg[0] =  map (analogRead(0), 0, 1023, 0, 179);
  radio.write(msg, 1);
}




Rx:



#include <Servo.h>    //the library which helps us to control the servo motor
#include <SPI.h>      //the communication interface with the modem
#include "RF24.h"    //the library which helps us to control the radio modem

Servo myServo;        //define the servo name

RF24 radio(5,10);    /This object represents a modem connected to the Arduino.
                      Arguments 5 and 10 are a digital pin numbers to which signals
                      CE and CSN are connected.
/

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.

int msg[1];

void setup(){
  myServo.attach(3);                //3 is a digital pin to which servo signal connected.
  radio.begin();                    //it activates the modem.
  radio.openReadingPipe(1, pipe);  //determines the address of our modem which receive data.
  radio.startListening();          //enable receiving data via modem
  }

void loop(){
  if(radio.available()){            //checks whether any data have arrived at the address of the modem
    bool done = false;              //returns a “true” value if we received some data, or “false” if no data.
    while (!done) {
      done = radio.read(msg, 1);
      myServo.write(msg[0]);
    }
  }
}




There is the schematic:
https://ibb.co/k6Ckzrh

It's working great for me

Ground fungus i think the problem was that they were too close together and i have a breakout board so i didnt use a capacitor, ive used Winchester's code and modified it as it didnt work and the code didnt make sense, here is the code that i edited

So you have working code?

It's nto working but the codes looks correct ,as in i expect it to function properly but it isnt, here is the code.

TX:

#include <SPI.h>                      //the communication interface with the modem
#include "RF24.h"  //the library which helps us to control the radio modem

const int potPin = A0;

RF24 radio(9,10);                     //5 and 10 are a digital pin numbers to which signals CE and CSN are connected.
                                     
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.
int value;

void setup(void){
  radio.begin();                      //it activates the modem.
  radio.openWritingPipe(pipe);        //sets the address of the receiver to which the program will send data.
}

void loop(void){
  value = analogRead(potPin);
  value =  map(analogRead(A0), 0, 1023, 0, 179);
  radio.write(&value, sizeof(value));
}

RX:

#include <Servo.h>    //the library which helps us to control the servo motor
#include <SPI.h>      //the communication interface with the modem
#include "RF24.h"     //the library which helps us to control the radio modem

Servo myServo;        //define the servo name

RF24 radio(9,10);     /*This object represents a modem connected to the Arduino.
                      Arguments 5 and 10 are a digital pin numbers to which signals
                      CE and CSN are connected.*/

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.

int msg;

void setup(){
  myServo.attach(3);                //3 is a digital pin to which servo signal connected.
  radio.begin();                    //it activates the modem.
  radio.openReadingPipe(1, pipe);   //determines the address of our modem which receive data.
  radio.startListening();           //enable receiving data via modem
  }

void loop(){
  if(radio.available()){            //checks whether any data have arrived at the address of the modem
    bool done = false;              //returns a "true" value if we received some data, or "false" if no data.
    while (!done) {
      radio.read(&msg,sizeof(msg));
      myServo.write(msg);
    }
  }
}

When i run the code i get the occasional twitching from the servo but nothing else much, can someone help me here.

The servo is to pin 3 (nano) im powering it via 5v on a breadboard which powers the nrf and teh servo
The pot is to A0 (uno) im powering it via 5v on a breadboard which powers the nrf and the pot

So,
One thing I learn from building with arduino: if the code is allright, then it's must be a problem with the material.

Check if you have plug everithing corectly. Even if you thing everything is good. Maybe there is something you miss place.

could you check my code to see if it is al right because im very new to nrf24l01 library so i mightve made some rookie mistakes though im not sure :slight_smile:

Your TX code has no delay between sends. You are sending data very fast. There is no reason for that as you cannot move the pot that quickly. A send every 100ms should be fine for your application.

You have no serial prints in either code to tell you what is happening. Serial prints are the best troubleshooting tool that you have.

The while in the receive code is in error. The variable done is not changed in the while loop so done is always false.

I fixed those problems and the code is working on my setup. The servo is following the pot. I have it sending at 1 second intervals, but you can change that.

send code:

#include <SPI.h>                      //the communication interface with the modem
#include "RF24.h"  //the library which helps us to control the radio modem

const int potPin = A0;

RF24 radio(9, 10);                    //5 and 10 are a digital pin numbers to which signals CE and CSN are connected.

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.
int value;

void setup(void)
{
   Serial.begin(9600);
   radio.begin();                      //it activates the modem.
   radio.openWritingPipe(pipe);        //sets the address of the receiver to which the program will send data.
}

void loop(void)
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      value = analogRead(potPin);
      value =  map(analogRead(A0), 0, 1023, 0, 179);
      Serial.print("sent data  ");
      Serial.println(value);
      radio.write(&value, sizeof(value));
   }
}

receive code:

#include <Servo.h>    //the library which helps us to control the servo motor
#include <SPI.h>      //the communication interface with the modem
#include "RF24.h"     //the library which helps us to control the radio modem

Servo myServo;        //define the servo name

RF24 radio(9,10);     /*This object represents a modem connected to the Arduino.
                      Arguments 5 and 10 are a digital pin numbers to which signals
                      CE and CSN are connected.*/

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.

int msg;

void setup(){
   Serial.begin(9600);
  myServo.attach(3);                //3 is a digital pin to which servo signal connected.
  radio.begin();                    //it activates the modem.
  radio.openReadingPipe(1, pipe);   //determines the address of our modem which receive data.
  radio.startListening();           //enable receiving data via modem
  }

void loop(){
  if(radio.available()){            //checks whether any data have arrived at the address of the modem
    //bool done = false;              //returns a "true" value if we received some data, or "false" if no data.
    //while (!done) {
      radio.read(&msg,sizeof(msg));
      Serial.print("incoming  ");
      Serial.println(msg);
      myServo.write(msg);
    //}
  }
}

Serial monitor output
sent

sent data  24
sent data  25
sent data  24
sent data  25
sent data  24
sent data  116
sent data  116
sent data  116
sent data  116

received

incoming  24
incoming  25
incoming  24
incoming  25
incoming  24
incoming  116
incoming  116
incoming  116
incoming  116