Connect two Mega 2560 boards with ADXL335 sensors

Hi,
I'm currently working on a project where I should use multiple ADXL335 sensors (8 total), since the arduino mega 2560 only have 16 analog pins which allow me to add 5 sensors, I brought an extra board so I can add twice the amount of sensors.

I'm not aware how I can get the two boards to communicate, so the sensors (accelerometers) record at the same time with the same sample frequency.

I have uploaded the code for 4 sensors working on one arduino board, pls help with the connection.

#include <math.h>

const int x_out = A0; /* connect x_out of module to A1 */
const int y_out = A1; /* connect y_out of module to A2 */
const int z_out = A2; /* connect z_out of module to A3 */

// Calibration variables
const double x_offset = 0.8790;  /* Fill in the x-axis offset value */
const double y_offset = 0.8642;  /* Fill in the y-axis offset value */
const double z_offset = 0.2975;  /* Fill in the z-axis offset value */

// Take multiple samples to reduce noise
const int sampleSize = 10;

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

void loop() {
  int x_raw, y_raw, z_raw;
  double x_accel, y_accel, z_accel;

  x_raw = analogRead(x_out); /* Digital value of voltage on x_out pin */
  y_raw = analogRead(y_out); /* Digital value of voltage on y_out pin */
  z_raw = analogRead(z_out); /* Digital value of voltage on z_out pin */

  x_accel = ((((double)(x_raw * 5) / 1024) - 1.08) / 0.660) - x_offset ;   /* Acceleration in x-direction in m/s^2 */
  y_accel = ((((double)(y_raw * 5) / 1024) - 1.08) / 0.660) - y_offset ;   /* Acceleration in y-direction in m/s^2 */
  z_accel = ((((double)(z_raw * 5) / 1024) - 1.83) / 0.660) - z_offset ;   /* Acceleration in z-direction in m/s^2 */
 
  Serial.print("x = ");
  Serial.print(x_accel, 4); /* Display with 4 decimal places */
  Serial.print("G");
  Serial.print(",  y = ");
  Serial.print(y_accel, 4); /* Display with 4 decimal places */
  Serial.print("G");
  Serial.print(",  z = ");
  Serial.print(z_accel, 4); /* Display with 4 decimal places */
  Serial.print("G");
  Serial.println();
  delay(100);
}
// Take samples and return the average
int ReadAxis(int axisPin)
{
  long reading = 0;
  analogRead(axisPin);
  delay(1);
  for (int i = 0; i < sampleSize; i++)
  {
  reading += analogRead(axisPin);
  }
  return reading/sampleSize;
}

You can use any of the available communication interfaces - UART (Serial), I2C, SPI ...

UART might be easiest, as you can test & monitor that against a terminal on a PC ...

Seems a bit excessive to add a whole extra Mega 2560 board just for that!

Have you considered an analogue multiplexer?

2 Likes

An analog multiplexer would be a far better approach. You could do this with a much smaller Arduino, if the analog signals are all your wishing to attach.
You need to tell us how often you want to sample, if there's any problem doing round-robin sampling(i.e. non-simultaneous sampling), etc. As well as what you're doing with the data. This will all play into a 'best answer'.
Remember, "as fast as possible" and "simultaneous" are wishes, not specifications.

1 Like

"..record at the same time..." What accurazy of timing us needed?

Hi again,
I also have a analog mulitplexer. The sampling frequency should at least be 30Hz, the higher the better.
I have never touched Arduino before, the sensors is placed on a aero-elastic bridge model which is later tested in a wind tunnel.

That's glacially slow in microcontroller terms! No problem achieving that on a single Arduino.

A further advantage of keeping it all on a single Arduino is that you avoid the need to time-synchronise multiple boards...

So how is this task usually accomplished (without Arduino)?

Presumably there must be a standard approach?

Can you provide more info on how to add the mulitplexer physically and code wise?
The code I provide only allows me to measure with a sampling frequency of 30Hz only.

Yes, ofc we also have much more expensive equipment here at the university, but currently we are trying to minimize the cost.

What multiplexer, exactly, do you have? Please give a link.

A multiplexer allows you to switch multiple source to one input.
So you just need to select the first source, read the input, then select the 2nd source, read it, etc...

Why?

Have you considered using digital accelerometers - then you could connect them all to just one SPI bus ... ?

CD74HC4067 16 Channel Analog Multiplexer Module

Hmm.. not sure, I collect all the data with an SD card micro unit, so full code I use is here.
I also use my PC to monitor the recordings.

#include <math.h>
#include <SPI.h>
#include <SD.h>

File myFile;
const int sensorCount = 4;
const int x_out[sensorCount] = {A1, A4, A7, A10}; /* connect x_out of each module to respective pins */
const int y_out[sensorCount] = {A2, A5, A8, A11}; /* connect y_out of each module to respective pins */
const int z_out[sensorCount] = {A3, A6, A9, A12}; /* connect z_out of each module to respective pins */

// Calibration variables
const double offsets[sensorCount][3] = {
  {0.8420, 0.8420, -0.7456}, /* Fill in the offsets for sensor 1 */
  {0.8790, 0.8642, -0.7086}, /* Fill in the offsets for sensor 2 */
  {0.9012, 0.8420, -0.7900}, /* Fill in the offsets for sensor 3 */
  {0.8679, 0.8494, -0.7456} /* Fill in the offsets for sensor 4 */
};

// Take multiple samples to reduce noise
const int sampleSize = 4;

// SD card settings
const String fileName = "data.txt";  // File name to save the data
const int chipSelect = 4;            // Pin used for the SD card chip select

unsigned long startTime;
const unsigned long duration = 360000; // Duration of data collection in milliseconds (10 seconds)

void setup() {
  Serial.begin(115200);
  pinMode(chipSelect, OUTPUT);

  // Initialize SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed!");
    while (1);
  }

  // Create or open the file for writing
  myFile = SD.open(fileName, FILE_WRITE);
  if (!myFile) {
    Serial.println("Error opening file!");
    while (1);
  }

  // Write header to the file
  myFile.println("Sensor 1 x (m/s^2), Sensor 1 y (m/s^2), Sensor 1 z (m/s^2), Sensor 2 x (m/s^2), Sensor 2 y (m/s^2), Sensor 2 z (m/s^2), Sensor 3 x (m/s^2), Sensor 3 y (m/s^2), Sensor 3 z (m/s^2), Sensor 4 x (m/s^2), Sensor 4 y (m/s^2), Sensor 4 z (m/s^2)");
  // insert again if time ("Time(ms),)
  startTime = millis(); // Store the start time
}

void loop() {
  unsigned long currentTime = millis();
  if (currentTime - startTime >= duration) {
  
  // Data collection time has elapsed, close the file and stop the program
    myFile.close();
    Serial.println("Data collection complete!");
    while (1);
  }

  double x_accel[sensorCount];
  double y_accel[sensorCount];
  double z_accel[sensorCount];

  for (int i = 0; i < sensorCount; i++) {
    int x_raw = ReadAxis(x_out[i]); /* Digital value of voltage on x_out pin */
    int y_raw = ReadAxis(y_out[i]); /* Digital value of voltage on y_out pin */
    int z_raw = ReadAxis(z_out[i]); /* Digital value of voltage on z_out pin */

    x_accel[i] = (((double)(x_raw * 5) / 1024) - 1.08) / 0.660 - offsets[i][0];   /* Acceleration in x-direction in m/s^2 */
    y_accel[i] = (((double)(y_raw * 5) / 1024) - 1.08) / 0.660 - offsets[i][1];   /* Acceleration in y-direction in m/s^2 */
    z_accel[i] = (((double)(z_raw * 5) / 1024) - 1.83) / 0.660 - offsets[i][2];   /* Acceleration in z-direction in m/s^2 */
  }

    Serial.print("x = ");
  for (int i = 0; i < sensorCount; i++) {
    Serial.print(x_accel[i], 6); /* Display with 4 decimal places */
    Serial.print(" m/s^2\t");
  }
    Serial.print("y = ");
  for (int i = 0; i < sensorCount; i++) {
    Serial.print(y_accel[i], 6); /* Display with 4 decimal places */
    Serial.print(" m/s^2\t");
  }
    Serial.print("z = ");
  for (int i = 0; i < sensorCount; i++) {
    Serial.print(z_accel[i], 6); /* Display with 4 decimal places */
    Serial.print(" m/s^2\t");
  }
  Serial.println();

  // Write data to the file
    //myFile.print(currentTime);
    //myFile.print("\t");
  for (int i = 0; i < sensorCount; i++) {
    myFile.print(x_accel[i], 6);
    myFile.print("\t");
    myFile.print(y_accel[i], 6);
    myFile.print("\t");
    myFile.print(z_accel[i], 6);
    myFile.print("\t");
  }
  myFile.println();

  delay(0.05);
}

// Take samples and return the average
int ReadAxis(int axisPin) {
  long reading = 0;
  analogRead(axisPin);
  delay(0.05);
  for (int i = 0; i < sampleSize; i++) {
    reading += analogRead(axisPin);
  }
  return reading / sampleSize;
}

Yes, but we only have analog sensors.

What, exactly, do you mean by "sensor" here?

The ADXL335 is an electronic component; there are digital versions in the ADXLxxx range - see the 'Output Type' column here:

Perhaps, the equipment has been purchased, and budget prevents buying more?

@jakob1314 We really do need a complete project description, along with a schematic of what you have so far. It's disappointing to try to help, only to see more details come out that make the help meaningless.

1 Like

Yes, I'm getting the impression that "sensor" here means something more than just the accelerometer chip...

Sorry, no by sensor I only mean the ADXL335 accelerometer.
I would like to increase sample freq. and add 4 ADXL335 components more to the system.


So why not use a digital accelerometer instead?

Or, at least, make the extras digital?