Hello,
I am new to Arduino and haven't learned much yet, but I'm starting a project where I want to connect two MSA301 Accelerometers to an Arduino UNO and OLED display so that both accelerometers display data at the same time in two different lines on the OLED display. I have successfully gotten one accelerometer connected and displaying data (I changed the unit so that it is in g) but I cannot come up with the right code to connect the second one.
I am afraid that this is an issue with I2C communication, because after doing some research I read that you cannot connect multiple devices with the same I2C address to the same Arduino. However, a different source said that it is possible to connect two of the same accelerometer to one Arduino, so I am confused. Is it possible to connect 2 accelerometers with the same I2C address and an OLED display to one Arduino?
The current code below displays two lines of data on the OLED screen, but it's just the data from the first accelerometer copied. I have connected them both through STEMMA QT connectors.
// OLED demo for accelerometer readings from Adafruit MSA301
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MSA301.h>
#include <Adafruit_Sensor.h>
Adafruit_MSA301 msa;
Adafruit_MSA301 msa_2;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
void setup(void) {
Serial.begin(115200);
Serial.println("Adafruit MSA301 demo!");
// Try to initialize!
if (! msa.begin()) {
Serial.println("Failed to find MSA301 chip 1");
while (1) { delay(10); }
}
if (! msa_2.begin()) {
Serial.println("Failed to find MSA301 chip 2");
while (1) { delay(10); }
}
Serial.println("MSA301 Chips Found!");
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(500); // Pause for 2 seconds
display.setTextSize(1);
display.setTextColor(WHITE);
display.setRotation(0);
}
void loop() {
sensors_event_t event;
msa.getEvent(&event);
msa_2.getEvent(&event);
display.clearDisplay();
display.setTextSize(1);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tX: "); Serial.print(event.acceleration.x);
Serial.print(" \tY: "); Serial.print(event.acceleration.y);
Serial.print(" \tZ: "); Serial.print(event.acceleration.z);
Serial.println(" m/s^2 ");
display.setCursor(0,0);
display.print("("); display.print(event.acceleration.x*.10197); display.print(", ");
display.print(event.acceleration.y*.10197); display.print(", ");
display.print(event.acceleration.z*.10197); display.print(")");
display.setCursor(0,8);
display.print("("); display.print(event.acceleration.x*.10197); display.print(", ");
display.print(event.acceleration.y*.10197); display.print(", ");
display.print(event.acceleration.z*.10197); display.print(")");
display.display();
delay(100);
}