multiple ADXL335 with Arduino UNO problem.

Hi guys, having a little trouble with my accelerometer results.

I need to monitor and print out the data from two accelerometer at one time, but the UNO doesn’t seem to connect to both of my sensor.Since, the data print out from the sensor is read only from one sensor only.

I appreciate I’m probably missing something very simple as I am new to this so please be kind and offer any advice you may have.

This is the program for multiple accelerometer

int firstSensorx = 0; // first analog sensor
int firstSensory = 0;
int firstSensorz = 0;
int secondSensorx = 0; // second analog sensor
int secondSensory = 0;
int secondSensorz = 0;

void setup()
{
// start serial port at 9600 bps and wait for port to open:
Serial.begin(9600);
}

void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {

// read first analog input:
firstSensorx = analogRead(A0);
firstSensory = analogRead(A1);
firstSensorz = analogRead(A2);

// read second analog input:
secondSensorx = analogRead(A3);
secondSensory = analogRead(A4);
secondSensorz = analogRead(A5);

// send sensor values:

Serial.print(firstSensorx);
Serial.print(",");
Serial.print(firstSensory);
Serial.print(",");
Serial.print(firstSensorz);
Serial.print(",");
Serial.print(secondSensorx);
Serial.print(",");
Serial.print(secondSensory);
Serial.print(",");
Serial.print(secondSensorz);
Serial.print(",");

}
}

void establishContact() {
while (Serial.available() <= 0) {
Serial.println(“0,0,0”); // send an initial string
delay(300);
}
}

AND HERE IS THE PROGRAM FOR ONE SENSOR used only

//Analog read pins
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;

//to hold the caculated values
double x;
double y;
double z;

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

pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(zPin, INPUT);

}

void loop(){

//read the analog values from the accelerometer
int xRead = analogRead(xPin);
int yRead = analogRead(yPin);
int zRead = analogRead(zPin);

x = xRead;
y = yRead;
z = zRead;

//Output the caculations
Serial.print("x: ");
Serial.print(x);

delay (1);

Serial.print(" | y: ");
Serial.print(y);

delay (1);

Serial.print(" | z: ");
Serial.println(z);

delay(500);
}

Thanks

Regards
Hana

This does not do what the comment says:

  // start serial port at 9600 bps and wait for port to open:
  Serial.begin(9600);

This is where it waits:

  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {

You have declared a function called establishContact() but you never use it. Please look closely at your code before posting it to remove any unnecessary items. Then try to run that code. Sometimes the error may be in an item that you thought was unnecessary.

This looks good:

    // read first analog input:
    firstSensorx = analogRead(A0);
    firstSensory = analogRead(A1);
    firstSensorz = analogRead(A2);
    
    // read second analog input:
    secondSensorx = analogRead(A3);
    secondSensory = analogRead(A4);
    secondSensorz = analogRead(A5);

Can you show us your output and maybe a schematic of how it's wired up?

Thank you for replied.

Attached file are the schematic diagram of my UNO Project and the output from the sensor

Regards
Hannah

HANNAH.pdf (169 KB)

So that's the output from the single-accelerometer version of the program. What happens when you upload the other version?

Does this make any difference?

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

void loop()
{
  if (Serial.available() > 0) {
 
    (void) analogRead(A0);
    int firstSensorx = analogRead(A0);
    (void) analogRead(A1);
    int firstSensory = analogRead(A1);
    (void) analogRead(A2);
    int firstSensorz = analogRead(A2);
   
    (void) analogRead(A3);
    int secondSensorx = analogRead(A3);
    (void) analogRead(A4);
    int secondSensory = analogRead(A4);
    (void) analogRead(A5);
    int secondSensorz = analogRead(A5);
   
    Serial.print(firstSensorx);
    Serial.print(",");
    Serial.print(firstSensory);
    Serial.print(",");
    Serial.print(firstSensorz);
    Serial.print(",");
    Serial.print(secondSensorx);
    Serial.print(",");
    Serial.print(secondSensory);
    Serial.print(",");
    Serial.print(secondSensorz);
    Serial.print(",");
  }
}

Still, it doesn't work.

OK, thanks for letting us know.

Good luck.

What output do you get from the two sensor code?

Already success . Just edited my first program.
Thank you guys :slight_smile:

//Analog read pins
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;
const int x2Pin = A3;
const int y2Pin = A4;
const int z2Pin = A5;


//to hold the caculated values
double x;
double y;
double z;
double x2;
double y2;
double z2;



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

  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);  
  pinMode(zPin, INPUT);
  pinMode(x2Pin, INPUT);
  pinMode(y2Pin, INPUT);
  pinMode(z2Pin, INPUT);
  
}


void loop(){

  //read the analog values from the accelerometer
  int xRead = analogRead(xPin);
  int yRead = analogRead(yPin);
  int zRead = analogRead(zPin);
  int x2Read = analogRead(x2Pin);
  int y2Read = analogRead(y2Pin);
  int z2Read = analogRead(z2Pin);

  x = xRead;
  y = yRead;
  z = zRead;
  x2 = x2Read;
  y2 = y2Read;
  z2 = z2Read;


  //Output the caculations
  Serial.print("sensorx: ");
  Serial.print(x);
  
  delay (1);
  
  Serial.print(" | sensory: ");
  Serial.print(y);
  
  delay (1);
  
  Serial.print(" | sensorz: ");
  Serial.print(z);
  
  delay (1);
  
  Serial.print(" | sensorx2: ");
  Serial.print(x2);
  
  delay (1);
  
  Serial.print(" | sensory2: ");
  Serial.print(y2);
  
  delay (1);
  
  Serial.print(" | sensorz2: ");
  Serial.println(z2);
  
  
  
  delay(500);
}