Simple Bidirectional Wireless Communication using NRF24L01 but with a twist.

Hi, I was working with this example i saw on the internet to see how NRF24L01 works nRF24L01 - How It Works, Arduino Interface, Code, Schematic. When I set up the circuit using two arduinos UNO, it worked well according to the explanation of the web page. Now the problem I have is that, in the arduino I use as my master I added a stepper motor (28BYJ-48) and motor driver (UNL2003) from the kit I have. Components that I use in the Arduino one (master) are: led, 220 ohm resistor, joy stick, stepper, motor driver and NRF24L01. In the arduino that I use as a slave I changed the button and resistance of 10k by an ultrasonic sensor HC-SR04 and added a led with resitor 220 omh. The components on the slave side are: ultrasonic HC-SR04, microservo motor (SG09), led, 200 ohm resistor and NRF24L01. The objective one is that when assembling the circuits the master that has the joy stick can move the micro servo (SG90) that is on the slave side in the x-axis from 90 to 180 or 90 to 0 degrees, in which it went out working all right. The second objective is that the ultrasonic sensor HC-SR04 can move the stepper motor according to how close an object is. Example when the ultrasonic sensor HC-SR04 detect an object at 30cm the steppermotor will remain still, but when an object gets closer, the stepper motor must move according to how close the object is, and if this object moves away the motor stepper should move in reverse. I'm just trying to get the stepper motor to behave like the servo in the part that I want it to move within the angles 0 -180 degrees.

In the code the objective one is fulfilled, my problem is the objective two since the stepper feels the object but moves in a very slow in one direction. I will put my code to see if you can help me in this activity, remember that I m trying to learn thanks.

For move the stepper motor backwards and forward i test this code i came with, and did not work:

if(sensorFoward >= 1 && sensorFoward < 30){
int sf = 30 - sensorFoward;
int walk = sf * 34;
steppermotor.step(walk);
radio.available();
sensorBackward = constrain(sensorLectur,1,30);
int new_sf = 30 - sensorBackward;

if(new_sf < sf){
walk = new_sf *34;
steppermotor.step(-walk);
}
}

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

About this board: some people are here to help, some people ran off everybody they have to argue with so they come here. ignore them. this message board gets crabby about people who were not born knowing how to use this message board. read the bottom part of my signature line

Hi, I was working with this example i saw on the internet to see how NRF24L01 works:

nrf24l01tutorial.

When I set up the circuit using two arduinos UNOs, it worked well according to the explanation of the web page. Now the problem I have is that, in the arduino I use as my master I added a stepper motor (28BYJ-48) and motor driver (UNL2003) from the kit I have. Components that I use in the Arduino one (master) are:

  • led,
  • 220 ohm resistor,
  • joy stick,
  • stepper,
  • motor driver and
  • NRF24L01.

In the arduino that I use as a slave I changed the button and resistance of 10k by an ultrasonic sensor HC-SR04 and added a led with resitor 220 omh. The components on the slave side are:

  • ultrasonic HC-SR04,
  • microservo motor (SG09),
  • led,
  • 200 ohm resistor and
  • NRF24L01.

The objective one is that when assembling the circuits the master that has the joy stick can move the micro servo (SG90) that is on the slave side in the x-axis from 90 to 180 or 90 to 0 degrees, in which it went out working all right.
The second objective is that the ultrasonic sensor HC-SR04 can move the stepper motor according to how close an object is. Example: when the ultrasonic sensor HC-SR04 detects an object at 30cm the stepper motor will remain still, but when an object gets closer, the stepper motor must move according to how close the object is, and if this object moves away the motor stepper should move in reverse. I’m just trying to get the stepper motor to behave like the servo in the part that I want it to move within the angles 0 -180 degrees.

In the code the objective one is fulfilled, my problem is the objective two since the stepper feels the object but moves in a very slow in one direction. I will put my code to see if you can help me in this activity, remember that I m trying to learn thanks.

For move the stepper motor backwards and forward i test this code i came with, and did not work:

if(sensorFoward >= 1 && sensorFoward < 30){
        int sf = 30 - sensorFoward;
        int walk = sf * 34;
        steppermotor.step(walk);
        radio.available();
        sensorBackward = constrain(sensorLectur,1,30);
        int new_sf = 30 - sensorBackward;

        if(new_sf < sf){
          walk = new_sf *34;
          steppermotor.step(-walk);
          }
        }

this code appears to require a library. if you include all the code, you include the name of the library you used. that makes other peoples task simpler

about:

radio.available();

that is typically used after if or while, in this fashion:

{
  while (Serial3.available())                           // look for data from GPS module
  {
     char c = Serial3.read();                           // read in all available chars   
     gps.encode(c);                                     // and feed chars to GPS parser
     //Serial.print(c);                                 // for diagnostics
  }

}

which makes me think you’re missing something.

Is that aimed at me?

...R

Is that aimed at me?

no, it is aimed at those people who are never the first to answer anything, but find a fault in everything. notice how many people have a signature line saying they won't answer PMs. that comes from experience

Hello and sorry if I offended someone in this forum with my question. I was just looking for a little help from people who know about this topic, I did not want a novel to be formed because of this. Thanks.

By the way i was reading my code and i make the stepper motor to go backward. Now im trying to get it to stop when reach the position 0 or 180 degrees. Thanks to the help of those who gave constructive criticism.

Here my master code:

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

#define led 7

RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = {"00005", "00006"};

//boolean buttonState = 0; BUTTON

long rangeState;//SENSOR
int sensorLectur;
//long distance;

// Define Constants of the Stepper
 
// Number of steps per internal motor revolution 
const float STEPS_PER_REV = 32; 
 
//  Amount of Gear Reduction
const float GEAR_RED = 64;
 
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;//2048 == 360

// Number of Steps Required
int StepsRequired = STEPS_PER_OUT_REV / 2; //1024 == 180
//#define STEPS 180

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 2,3,5,6 
// Connected to ULN2003 Motor Driver In1, In2, In3, In4 
// Pins entered in sequence 1-3-2-4 for proper step sequencing

 int limp = constrain(StepsRequired,1023,0);
 //int steps = map(StepsRequired,0,1023,180,0);
 //int steps = map(limp,0,1023,180,0);
 
//Stepper steppermotor(limp,2, 5, 3, 6);
Stepper steppermotor(StepsRequired,2, 5, 3, 6);
//Stepper steppermotor(STEPS_PER_REV,2, 5, 3, 6);
//Stepper steppermotor(STEPS,2, 5, 3, 6);

void setup() {
  
  pinMode(7, OUTPUT);
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00005
  radio.openReadingPipe(1, addresses[0]); // 00006
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  
  delay(5);
  Serial.begin(9600);
  
  radio.stopListening();
  //Serial.println("NO LISTENING");
  
  int potValue = analogRead(A0);
  int angleValue = map(potValue, 0, 1023, 0, 180);//0 a 180
  radio.write(&angleValue, sizeof(angleValue));
  //Serial.println(angleValue);
  //Serial.println("WRITTEN MESSAGE");
  delay(5);

  

 ////////////////MODIFY HERE FORWARD ---> WHAT THIS ARDUINO HEARS////////////////////
  
  radio.startListening();
  //Serial.println("LISTENING");
  
  if(!radio.available()){
    //Serial.println("NOT AVAILABLE");
    }

  //radio.read(&rangeState, sizeof(rangeState));
  
  
  //int sensorBackward = constrain(sensorLectur,30,2);
  
   //Serial.print(sensorLectur);
   //Serial.println("cm");
   delay(5);
  
  //steppermotor.step(StepsRequired);

   //Serial.println(steps);
   
if(radio.available()){
  //int distance = 0;
  //radio.read(&distance, sizeof(distance));
  
  radio.read(&sensorLectur, sizeof(sensorLectur));
  
  int sensorFoward = constrain(sensorLectur,0,30);
  //int sensorFoward = constrain(distance,0,30);
  
   Serial.println(sensorFoward);
    
    if(sensorFoward < 30){
      steppermotor.setSpeed(sensorLectur);
      int sf = 30 - sensorFoward;
      int walk = sf * 34;
      steppermotor.step(walk);
    }

    if(sensorFoward > 1){
      steppermotor.setSpeed(sensorLectur);
      int sf = 30 - sensorFoward;
      int walk = sf * 34;
      steppermotor.step(-walk);
    }
//Serial.println(distance);

      //Serial.println(StepsRequired);  
      delay(5);     
    }
           
}

Slave Code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#include "SR04.h"// for Sensor

//BUTTON
//#define button 4

//LED
#define led 4


#define TRIG_PIN 6
#define ECHO_PIN 7

SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
int sensorLectur;
long duration;
float distance;

//WIFI
RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = {"00005", "00006"};

//SERVO
Servo myServo;

//boolean buttonState = 0; BUTTON
long rangeState; //SENSOR

void setup() {
  //pinMode(button, INPUT); BUTTON
  pinMode(TRIG_PIN,OUTPUT);//------->TESTING
  pinMode(ECHO_PIN,INPUT);//-------->TESTING
  pinMode(4,OUTPUT);
  myServo.attach(5);
  radio.begin();
  radio.openWritingPipe(addresses[0]); // 00006
  radio.openReadingPipe(1, addresses[1]); // 00005
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  
  delay(5);
  
  radio.startListening(); 
  Serial.begin(9600);
  //Serial.println("LISTENING");
  
 if(radio.available()){
  
    if (radio.available()){
      int angleV = 0;
      radio.read(&angleV, sizeof(angleV));
      //Serial.println(angleV);
      myServo.write(angleV);
      
     
     if( angleV < 90 || angleV > 90){
      digitalWrite(led,HIGH);
      //Serial.println("READING_ANALOG");
      } else {
        digitalWrite(led,LOW);
        }
      
    delay(5);
    
    radio.stopListening();
    Serial.begin(9600);
    sensorLectur = sr04.Distance();
    //int senconst = constrain(sensorLectur,2,30);
    //int distance = map(senconst,2,30,180,12);
    
    //Serial.print(sensorLectur);
    //Serial.println("cm");
    //delay(300);
    
    //rangeState = analogRead(sensorLectur);
    //Serial.println(rangeState);
    //radio.write(&rangeState, sizeof(rangeState));

   radio.write(&sensorLectur, sizeof(sensorLectur));
    //radio.write(&distance, sizeof(distance));
    //radio.write(&readSensor(), sizeof(readSensor()));
    //Serial.println(distance);
    //Serial.println("cm");
    
    //radio.write(&distance, sizeof(distance));
    //Serial.println(distance);
    
//CODE FOR BUTTON
    //buttonState = digitalRead(button);
    //radio.write(&buttonState, sizeof(buttonState));
//END CODE BUTTON
    }
  }
}

Wikiki:
Hello and sorry if I offended someone in this forum with my question.

I don't think you have offended anyone. I have no idea what has upset @Geek Emeritus.

Have you written a program for a single Arduino that makes all your motors work using push-buttons that update the values in some variables? When that works the you can replace the data provided by the buttons with equivalent data received by wireless.

...R