Help - Two way communication of MPU6050 Gyroscope data and RC brushless motor with RF-NANO/NRF24L01

2022-01-15T16:00:00Z

Hi everyone, I am new to Arduino programming and have recently run into a couple of issues while trying to set up a two way communication between an Arduino UNO with a NRF24L01 module and a RF-NANO. The schematics of my set-up and my code are included below.

On the right side of the diagram, a brushless motor and a MPU6050 gyroscope is attached to an RF-NANO module. On the left side of the diagram, a potentiometer and NRF24L01 module is connected to an Arduino UNO module. I am trying to remotely control the rotation speed of the brushless motor with the potentiometer while wirelessly receiving gyroscopic data from the MPU6050 module, but with the current set-up and programming, it the two-way connection only works sometimes and appears to be unreliable. I haven't found any helpful suggestions besides switching to a steady external power source, which I've done, and I don't really know where else to look for answers. Any sort of advice and criticism is appreciated! (My code is based on the source code on ACBR)

Schematics:

CODE FOR RF-NANO:

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

#define MPU 0x68
#define CE_PIN 10
#define CSN_PIN 9

const byte address[][6] = {"00001", "00002"};
double AcX,AcY,AcZ;
float Pitch, Roll;
float GYRO[2];  // two element array holding the pitch and roll data
int potValue; //value from the analog pin
Servo ESC; //electronic speed controller of brushless motor 

RF24 radio(CE_PIN, CSN_PIN); // activate the radio

void setup()   
{
  Serial.begin(9600);
  ESC.attach(9, 1000, 2000);//(pin, min pulse width, max pulse width in miliseconds)
  radio.begin();
  radio.openWritingPipe(address[1]);
  radio.openReadingPipe(1, address[0]);
  init_MPU();

}


void loop()   
{
  
 delay(5);
 radio.stopListening();  
 
  radio.write( GYRO, sizeof(GYRO) );
  FunctionsMPU(); // returning our accelerometer data AcX, AcY, AcZ.
  Roll = FunctionsPitchRoll(AcX, AcY, AcZ);   
  Pitch = FunctionsPitchRoll(AcY, AcX, AcZ);  
  
  GYRO[0] = Pitch;
  GYRO[1] = Roll;
  
 delay(5);
  
  radio.startListening();
    
   if (radio.available()) 
   {
        
   radio.read(&potValue, sizeof(potValue));
   ESC.write(potValue);
   Serial.println(potValue); //send signal to ESC
 
   } 
}
   
void init_MPU()
{
  
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  delay(1000);
  
}
  
double FunctionsPitchRoll(double A, double B, double C)
{
  
  double DatoA, DatoB, Value;
  DatoA = A;
  DatoB = (B*B) + (C*C);
  DatoB = sqrt(DatoB);
  
  Value = atan2(DatoA, DatoB);
  Value = Value * 180/3.14;
  
  return (float)Value;
}

void FunctionsMPU()
{
  
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,6,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();      
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read();

}

CODE FOR UNO:

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

#define CE_PIN 10
#define CSN_PIN 9

const byte address[][6] = {"00001", "00002"};
float GYRO[2]; // two element array holding the pitch and roll data
int potValue; //potentiometer value from the analog pin

  
RF24 radio(CE_PIN, CSN_PIN); // activate the radio


void setup()  
{
  
  Serial.begin(9600); 
  radio.begin();
  radio.openWritingPipe(address[0]);   
  radio.openReadingPipe(1, address[1]);  

}

void loop()   
{

 delay(5);  
 radio.startListening();

  if ( radio.available() )
  {
    bool done = false;
    while (!done)
    {
      done = radio.read(GYRO, sizeof(GYRO));
      Serial.print(GYRO[0]);
      Serial.print("\t");
      Serial.println(GYRO[1]);
      Serial.print("\n");
    }
  }
    
  else
  {    
      Serial.println("GYRO Error!");
  }
    
    
   delay(5);
   radio.stopListening(); 
           
        
    potValue = analogRead(A0);
    potValue = map(potValue, 0, 1023, 0, 180); // mapping potentiometer value
    Serial.println(potValue);
    radio.write(&potValue, sizeof(potValue));
}

Why are you connecting 5V power sources to the Vin pins? The Vin pins need at least 7V for the regulator to function properly. Connect 5V to the 5V pins.

Here are things that I found while trying to get my rf24 radios to work.
If you read and, closely, follow Robin2's simple rf24 tutorial you should be able to get them working. That tutorial sure helped me. Run the CheckConnection.ino (look in reply #30) to verify the physical wiring between the radio module and its processor (Arduino).

Make sure the rf24 power supply can provide 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. A 10uF cap across the RF24 3.3V power pins can help.

If using the high powered radios make sure to separate them by a few meters. They may not work too close together. Try the lower power settings.

Reset the radios by cycling power to them after uploading new code. I have found that to help. They do not reset with the Arduino.

Switch to 1MB data rate to catch the not so cloned clones.
radio.setDataRate( RF24_1MBPS );

Motors generate large amounts of electrical noise, both radiated through the air and induced in the wiring, as well as cause severe power supply fluctuations.

It can be very difficult to treat, but as a first test, disconnect the motor and ESC from your setup and test communications.

With a little effort, you can substitute the motor and ESC with an LED to test the motor control.

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