How to connect multiple sensors to Arduino UNO/MEGA

Hi, everyone! I am new to arduino and I want to connect multiple sensors, Myoware and MPU6050 to arduino. However, when we combine the programs for each sensor, the MPU6050 is not working. It is working fine separately. Is it because of the address problem? Thank you all!

Here is our code:

//Threshold for servo motor control with muscle sensor. 
//You can set a threshold according to the maximum and minimum values of the muscle sensor.
#define THRESHOLD 480

//Pin number where the sensor is connected. (Analog 0)
#define EMG_PIN 0


int samples[500];
float average;
int i;

#include <Wire.h>
//Declaring some global variables
int gyro_x, gyro_y, gyro_z;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
boolean set_gyro_angles;

long acc_x, acc_y, acc_z, acc_total_vector;
float angle_roll_acc, angle_pitch_acc;

float angle_pitch, angle_roll;
int angle_pitch_buffer, angle_roll_buffer;
float angle_pitch_output, angle_roll_output;

long loop_timer;
int temp;

void setup() {

      myStepper.setSpeed(60);
//       myStepper.attach(Stepper_Pin);
  Wire.begin();                                                        //Start I2C as master
  setup_mpu_6050_registers();                                          //Setup the registers of the MPU-6050 
  for (int cal_int = 0; cal_int < 1000 ; cal_int ++){                  //Read the raw acc and gyro data from the MPU-6050 for 1000 times
    read_mpu_6050_data();                                             
    gyro_x_cal += gyro_x;                                              //Add the gyro x offset to the gyro_x_cal variable
    gyro_y_cal += gyro_y;                                              //Add the gyro y offset to the gyro_y_cal variable
    gyro_z_cal += gyro_z;                                              //Add the gyro z offset to the gyro_z_cal variable
    delay(3);                                                          //Delay 3us to have 250Hz for-loop
  }

  // divide by 1000 to get avarage offset
  gyro_x_cal /= 1000;                                                 
  gyro_y_cal /= 1000;                                                 
  gyro_z_cal /= 1000;                                                 
  Serial.begin(9600);
  loop_timer = micros();                                               //Reset the loop timer
}


void run_gyro(){
    read_mpu_6050_data();   
 //Subtract the offset values from the raw gyro values
  gyro_x -= gyro_x_cal;                                                
  gyro_y -= gyro_y_cal;                                                
  gyro_z -= gyro_z_cal;                                                
         
  //Gyro angle calculations . Note 0.0000611 = 1 / (250Hz x 65.5)
  angle_pitch += gyro_x * 0.0000611;                                   //Calculate the traveled pitch angle and add this to the angle_pitch variable
  angle_roll += gyro_y * 0.0000611;                                    //Calculate the traveled roll angle and add this to the angle_roll variable
  //0.000001066 = 0.0000611 * (3.142(PI) / 180degr) The Arduino sin function is in radians
  angle_pitch += angle_roll * sin(gyro_z * 0.000001066);               //If the IMU has yawed transfer the roll angle to the pitch angel
  angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);               //If the IMU has yawed transfer the pitch angle to the roll angel
  
  //Accelerometer angle calculations
  acc_total_vector = sqrt((acc_x*acc_x)+(acc_y*acc_y)+(acc_z*acc_z));  //Calculate the total accelerometer vector
  //57.296 = 1 / (3.142 / 180) The Arduino asin function is in radians
  angle_pitch_acc = asin((float)acc_y/acc_total_vector)* 57.296;       //Calculate the pitch angle
  angle_roll_acc = asin((float)acc_x/acc_total_vector)* -57.296;       //Calculate the roll angle
  
  angle_pitch_acc -= 0.0;                                              //Accelerometer calibration value for pitch
  angle_roll_acc -= 0.0;                                               //Accelerometer calibration value for roll

  if(set_gyro_angles){                                                 //If the IMU is already started
    angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;     //Correct the drift of the gyro pitch angle with the accelerometer pitch angle
    angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;        //Correct the drift of the gyro roll angle with the accelerometer roll angle
  }
  else{                                                                //At first start
    angle_pitch = angle_pitch_acc;                                     //Set the gyro pitch angle equal to the accelerometer pitch angle 
    angle_roll = angle_roll_acc;                                       //Set the gyro roll angle equal to the accelerometer roll angle 
    set_gyro_angles = true;                                            //Set the IMU started flag
  }
  
  //To dampen the pitch and roll angles a complementary filter is used
  angle_pitch_output = angle_pitch_output * 0.9 + angle_pitch * 0.1;   //Take 90% of the output pitch value and add 10% of the raw pitch value
  angle_roll_output = angle_roll_output * 0.9 + angle_roll * 0.1;      //Take 90% of the output roll value and add 10% of the raw roll value
  Serial.print(" | Angle  = "); Serial.println(angle_pitch_output);


 while(micros() - loop_timer < 4000);                                 //Wait until the loop_timer reaches 4000us (250Hz) before starting the next loop
 loop_timer = micros();//Reset the loop timer
}

void setup_mpu_6050_registers(){
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x6B);                                                    //Send the requested starting register
  Wire.write(0x00);                                                    //Set the requested starting register
  Wire.endTransmission();                                             
  //Configure the accelerometer (+/-8g)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1C);                                                    //Send the requested starting register
  Wire.write(0x10);                                                    //Set the requested starting register
  Wire.endTransmission();                                             
  //Configure the gyro (500dps full scale)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1B);                                                    //Send the requested starting register
  Wire.write(0x08);                                                    //Set the requested starting register
  Wire.endTransmission();                                             
}


void read_mpu_6050_data(){                                             
  Wire.beginTransmission(0x68);                                        
  Wire.write(0x3B);                                                    
  Wire.endTransmission();                                              
  Wire.requestFrom(0x68,14);                                         
  while(Wire.available() < 14);                                        
  acc_x = Wire.read()<<8|Wire.read();                                  
  acc_y = Wire.read()<<8|Wire.read();                                  
  acc_z = Wire.read()<<8|Wire.read();                                  
  temp = Wire.read()<<8|Wire.read();                                   
  gyro_x = Wire.read()<<8|Wire.read();                                 
  gyro_y = Wire.read()<<8|Wire.read();                                 
  gyro_z = Wire.read()<<8|Wire.read();                                 
}

void myoware()
          {
                     float sensorValue = analogRead(A0);   // read the input on analog pin 0:
                     Serial.println("sensor Value:");
                     Serial.println(sensorValue);
           
          for (i=0; i< 100; i++)  //take 100 samples
            { 
             samples[i] = abs( analogRead(A0)); //read voltage of EMG
             delay(10);
            }
          for (i=0; i< 100; i++) 
            { //take the average of all samples
                average += samples[i];
              //       Serial.println(i);
            }
          average /= 101;

          Serial.println("average of myoware:");

          Serial.println(average);
          

          }

  void loop()
                {
                           
          
                 run_gyro();
                 myoware();


                }

I'm missing the links to the used hardware (MyoWare is a system and not single sensor, I doubt that you connect the MPU6050 chip directly to the Arduino, so a link to the breakout board is necesary). We also need a wiring diagram of your complete setup.

The code doesn't show an address conflict but that doesn't mean there is none. But without the wiring diagram it's impossible for us to make any statement.

Certainly doesn't look like a pin conflict, since the myoware sensor's on A0 and the other being I2C is on A4 and A5.

But I wonder if it's a timing thing? There's a 100x10ms delays in the myware "for" so run_gyro() is only called every second.

If it's something to do with that, or to test if it is, you will need to take the myoware readings out of the for, and do them 100 individual times in loop(), by which I mean individual passes through loop(). In fact if you need the readings 10ms apart, you'll need blink without delay to do the timing.

neiklot:
Certainly doesn't look like a pin conflict, since the myoware sensor's on A0 and the other being I2C is on A4 and A5.

But I wonder if it's a timing thing? There's a 100x10ms delays in the myware "for" so run_gyro() is only called every second.

If it's something to do with that, or to test if it is, you will need to take the myoware readings out of the for, and do them 100 individual times in loop(), by which I mean individual passes through loop(). In fact if you need the readings 10ms apart, you'll need blink without delay to do the timing.

Thank you! we solve the problem by deleting the delay and reducing the number of i in for loop. But we still want to take average, any possible way to take without increasing the number of for loop? Thanks!

Get rid of the "for" althogether, and take the readings in loop() with blink without delay timing managing how far apart the readings are, and perhaps just use a counter to keep track of how many you have taken and do the average when the counter is 10 or whatever.

(Or you could get more sophisticated and used a circular buffer to keep a rolling average of the last 10. I've never used such a buffer, only read about them, but I'm sure there are threads on how to do it.)

Turns out there’s a moving average library that you can install via the library manager, movingAvg.

This sketch is based on the example there, but puts the reading code in a function and uses blinkwithoutdelay thinking to manage the timing with no blocking.

So you can ditch the whole “for” and let loop() do the looping.

// Arduino Moving Average Library
// https://github.com/JChristensen/movingAvg
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// This one uses a pot and bwod's the readings so no blocking

#include <movingAvg.h>                  // https://github.com/JChristensen/movingAvg
byte howManyReadingsToAverage = 10;  //set how many readings you want to run over
const uint8_t POT_PIN(A0);        // connect pot wiper, ends of pot to 0 and 5v
movingAvg myPot(howManyReadingsToAverage);                // define the moving average object
unsigned long currentMillis;
unsigned long previousMillisSensor;
int sensorReadInterval = 1000;  //set how long between readings

void setup()
{
  Serial.begin(9600);

  Serial.println("Using movingAvg library, use library manager to install");
  Serial.println("This example by neiklot, based on example from library");
  Serial.print("Prints a moving average over last ");
  Serial.print(howManyReadingsToAverage);
  Serial.println(" points");
  Serial.println("Uses bwod to take the readings with no blocking");
  Serial.println(" ");
  Serial.println("Last\tAvg");
  myPot.begin();
}

void loop()
{
  currentMillis = millis();
  doSensorAvg();
}

void doSensorAvg()
{
  if (currentMillis - previousMillisSensor >= sensorReadInterval)
  {
    previousMillisSensor = currentMillis;
    int potVal = analogRead(POT_PIN); // read the photocell
    int avg = myPot.reading(potVal);    // calculate the moving average
    Serial.print(potVal);                   // print the individual reading
    Serial.print('\t');
    Serial.println(avg);                // print the moving average
    //delay(1000);
  }
}

neiklot:
Turns out there’s a moving average library that you can install via the library manager, movingAvg.

This sketch is based on the example there, but puts the reading code in a function and uses blinkwithoutdelay thinking to manage the timing with no blocking.

So you can ditch the whole “for” and let loop() do the looping.

// Arduino Moving Average Library

// GitHub - JChristensen/movingAvg: A simple Arduino library for calculating moving averages.
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// This one uses a pot and bwod’s the readings so no blocking

#include <movingAvg.h>                  // GitHub - JChristensen/movingAvg: A simple Arduino library for calculating moving averages.
byte howManyReadingsToAverage = 10;  //set how many readings you want to run over
const uint8_t POT_PIN(A0);        // connect pot wiper, ends of pot to 0 and 5v
movingAvg myPot(howManyReadingsToAverage);                // define the moving average object
unsigned long currentMillis;
unsigned long previousMillisSensor;
int sensorReadInterval = 1000;  //set how long between readings

void setup()
{
  Serial.begin(9600);

Serial.println(“Using movingAvg library, use library manager to install”);
  Serial.println(“This example by neiklot, based on example from library”);
  Serial.print(“Prints a moving average over last “);
  Serial.print(howManyReadingsToAverage);
  Serial.println(” points”);
  Serial.println(“Uses bwod to take the readings with no blocking”);
  Serial.println(" ");
  Serial.println(“Last\tAvg”);
  myPot.begin();
}

void loop()
{
  currentMillis = millis();
  doSensorAvg();
}

void doSensorAvg()
{
  if (currentMillis - previousMillisSensor >= sensorReadInterval)
  {
    previousMillisSensor = currentMillis;
    int potVal = analogRead(POT_PIN); // read the photocell
    int avg = myPot.reading(potVal);    // calculate the moving average
    Serial.print(potVal);                  // print the individual reading
    Serial.print(’\t’);
    Serial.println(avg);                // print the moving average
    //delay(1000);
  }
}

Thank you so much!! our problem is solved!! :slight_smile: