How to control two servo motor each with a potentiometer controlled wirlessly through NRF24L01 and Arduino uno?

RE Edit :- I was trying to control pan tilt wirelessly using NRF24L01+ , two potentiometer and arduino uno. I was using this code:-

Transmitter

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

int msg[1];
RF24 radio(9,10);
const uint64_t pipe=0xE8E8F0F0E1LL;
int Potpin=A1;
int val;

void setup() {
radio.begin();
radio.openWritingPipe(pipe);
}

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

Receiver

#include <SPI.h>
#include "RF24.h"
#include <Servo.h>

Servo myservo;
RF24 radio(9,10);
const uint64_t pipe=0xE8E8F0F0E1LL;
int msg[1];

void setup() {
  myservo.attach(5);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();

}

void loop() {
  if(radio.available()){
    bool done = false;
    while(!done)
    {
      done = radio.read(msg,1);
      myservo.write(msg[0]);
    }
  }
}

The problem what I am facing with this code is only one servo motor and I want code for two. Please help me fixing this issue.

Your topic was MOVED to its current forum category as it is more suitable than the original as it is not an Introductory Tutorial

I am new to forum. Btw can you help me with this?

Please post your best attempt at writing the sketches for this and describe the problems with them

Are you really using pp3 9V batteries for this project ? If so, then they cannot supply enough current for long enough to be useful

No I am using buck and boost as a external power supply for receiver end. To power servo and a 12v dc adapter for arduino uno.

Servos pull too much current for the onboard 5V voltage-regulator.

There are a lot of circuits online that show supplying servos from the arduino 5V-pin.
This is unprofessional.
As soon as the servo has to deal with some load the current goes up and the voltageregulator shuts down => program-crash

Servos should be always supplied separately but the GND of the extra power-supply and GND of the arduino must be connected
This picture shows the principle
image

nRF24-modules are very cheap but are a little bit picky in communicating
read Robin2's tutorial

best regards Stefan

Thanks buddy for your help. But I need help with the topic which I had created for....

It does not compile and is an awful crap, in any aspect.

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.

Here is proven code to send and receive 2 Light Dependent Resistor (LDR) values using rf24 radios. Replace the LDR bits with your pots. These programs use information from the aforementioned Robin2 simple rf24 tutorial. Should not be hard to adapt these examples for your use.

Sender:


#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

struct LdrValues
{
  int ldr_1;
  int ldr_2; 
}ldrValues;

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

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{

   Serial.begin(115200);
   Serial.println("SimpleTx Starting");
   pinMode(LDR1, INPUT_PULLUP);
   pinMode(LDR2, INPUT_PULLUP);
   
   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      send();
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      prevMillis = millis();
   }
}

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

void send()
{
   ldrValues.ldr_1 = analogRead(LDR1);
   ldrValues.ldr_2 = analogRead(LDR2);
   radio.write( &ldrValues, sizeof(ldrValues) );
}

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);

struct LdrValues
{
   int ldr_1;
   int ldr_2;
} ldrValues;

bool newData = false;

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

void setup()
{

   Serial.begin(115200);

   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.stopListening();
   radio.startListening();
}

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

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

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

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

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      newData = false;
   }
}

No I will not crosscheck. What is your aim?

  1. beeing dependend for all and any small code-modification to ask again in the forum?
    or
  2. learning how it works and beeing able to modify your code yourself?

The second option still includes to ask all kinds of questions in the forum but you should show some more own effort in writing your code.

The microcontrollerworld is not superstandardised like USB-devices.
You have to take care of more details than just

"does the plug fit into the socket?"

If the code you have posted does not yet work you should do prelimary tests with the testcodes in robin2's nRF24-tutorial.

As soon as you show some own effort for example like this:

"I have uploaded robin2's democode from post # 2. The serial monitor shows this

........

This is how I have connected the nRF24-boards
(hand-drawn schematic)

What is still wrong that I get this error "XYZ" instead of successful datatransmission?

You should RE-edit your first post after reading how to do it here

best regards Stefan

Buddy what you think I didn't try at all I am trying for more than two weeks. When I feel all is going right then sometimes hardware creates problem and sometimes code. Between these period I learned something. I didn't even know include function first time slowly slowly I learned few functions. But facing lots of error after putting effort day and night. Lastly I came to forum.

So please help bro!

Very good. You haven't told this yet in the forum so how should I know it?

Your code in post # 1 is missing some function-calls
look up post # 2 of robin2's nRF24 tutorial.
Modify the code or your wiring to match the connections and upload this testcode to see if the nRF24-modules work

EDIT:
it is not really clear if the code you have posted in post # 1 is working or not.
As you can see from all these asking back:

You are working on an informatic project and what is most needed in an informatic project is information

Here are some tips that helped me to get my radios to work.

If you read and, closely, follow Robin2's simple rf24 tutorial (linked by @StefanL38 )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.

Before trying to get your own code to work, get the radios to work with known good example code like in the tutorial.

I did some quogling

https://www.google.de/search?q=arduino+nrf24+2+channel+remote+control

and found this

after a quick crossreading I say
This tutorial shows the typical step-by-step-process of testing

each component on its own

to make sure that each component is working properly
and after that putting things together.

There is one thing that jumped into my eyes: Robin2 recommends powering the nRF24-modules not from the arduino but separately. This tutorial says powering from arduino is OK.

It uses a joystick which means two potentiometers = matches your requirements
best regards Stefan

If you are sitting in front of a classical computer with mouse and keyboard then you just read

3 minutes

here

and then you are able to RE-edit your postings.
Is this too much effort for you??

1 Like

Bruh can't you see the message correctly I had tried joystick code that's why I can say output what problem I was facing.

I tried to change map value but didn't work at all.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination