Using two ADAfruit MMA8451 accelerometers with one arduino uno

hello

i have a code working for one mma8451 accelerometer (code attached below) i want to run the exact same code for another mma8451 accelerometer on the same arduino uno.

i seen people just define the accelerometers 1 and 2 and they get it to work.

any suggestions to get to this to work would be greatly appreciated.

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
Adafruit_MMA8451 mma = Adafruit_MMA8451();

#include <EEPROM.h>  
int addr = 0;  

const int buttonPin = 2; 
int buttonState = 0;   
      
int measurementCount =1;



void setup(void) {
  Serial.begin(9600);
   pinMode(buttonPin, INPUT_PULLUP);
   
     
  Serial.println("Adafruit MMA8451 test!");
  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");
  }



void loop() {
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
 
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");
  
    buttonState = digitalRead(buttonPin);
    Serial.println(buttonState); 
    if(buttonState == LOW){  

          
     float valx = event.acceleration.x;
     // EEPROM.put(addr, valx);
      Serial.print("valx    ");
      Serial.print(valx);
           
     float valy = event.acceleration.y;
     // EEPROM.put(addr, valy);
      Serial.print("   valy    ");
      Serial.print(valy);      

     float valz = event.acceleration.z;
     // EEPROM.put(addr, valz);
      Serial.print("   valz    ");
      Serial.print(valz);
      Serial.println();


     
     for(measurementCount; measurementCount<100; measurementCount++) {

      sensors_event_t event; 
      mma.getEvent(&event);

     float NewX;    
     NewX = event.acceleration.x - valx;
     Serial.print("Newx    ");
     Serial.print(NewX);
   

     float NewY;   
     NewY = event.acceleration.y - valy;
     Serial.print("   NewY    ");
     Serial.print(NewY);
     
     
     float NewZ;    //NEW Z 
     NewZ = event.acceleration.z - valz;
     Serial.print("   NewZ    ");
     Serial.print(NewZ);
     Serial.println();
     
  
      Serial.print("measurementcout");
     Serial.println(measurementCount);
      }

    }
  delay(500);
  
}

You will need to change the I2C address of one of them, and inform the library.

Adafruit documentation: Overview | Adafruit MMA8451 Accelerometer Breakout | Adafruit Learning System

1 Like

okay ill try this now

do You know how to do this ? i am a bit of a beginner

Yes, because I read the Adafruit documentation, which is pretty good. Try it!

Ive read the data sheet and the link you sent multiple times i just dont understand the language. perhaps i am reading the wrong adafruit documentation.

On the "pinouts" page of the Adafruit documentation, this is the most relevant part:

A is the I2C Address select pin. By default this is pulled up to 3.3V with a 10K resistor, for an I2C address of 0x1D. You can also connect it to the GND pin for an address of 0x1C

Connect this pin to ground for ONE of the sensors, and that one will have I2C address of 0x1C. The other will have I2C address of 0x1D.

One approach is to modify the code to create TWO accelerometer objects, named differently, and distinguish them by their I2C address. Another approach is to use digital pins to control the "A" pins of the two accelerometers, and to alternate which one is selected.

If you don't know how to do any of that, you will need to do some research and reading. There are plenty of tutorials on line.

i seen people just define the accelerometers 1 and 2 and they get it to work.

Take a very close look at their code.

okay thank you. would this work as making two locations ? for eg A2

Adafruit_MMA8451 mma = Adafruit_MMA8451();
Adafruit_MMA8451 mma2 = Adafruit_MMA8451(2);

I don't think so.

solution found here for anybody n the future.

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