Go Down

Topic: Acceleration Experiment using MMA8451 (Read 769 times) previous topic - next topic

d0minik96

Sep 20, 2019, 12:18 pm Last Edit: Sep 20, 2019, 12:18 pm by d0minik96
Hello Guys,

i already asked  in a different topic about the meaning of the Data Refresh Rate of my Sensor and the actual sample rate of my measurement:
https://forum.arduino.cc/index.php?topic=636703.0

Im testing an electrorheological damper. For that im reading the voltage (0-5V) of a friction sensor and the acceleration with the mma8451 adafruit board in order to integrate the velocity and displacement of the mass. I want to transmit the data to my PC to a matlab app. So that i can show other students the effect of this damper for different electric fields.

Im working on a code for a automatic measurement of different motor rotation speed.
Goal is to get a Sample Rate of 800HZ or close of the Data and transmit it Serial to Matlab.


My code:
Code: [Select]
//Librarys
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MCP4725.h>
#include <arduino.h>
//Variablen
uint32_t vmot = 0;
String cmd;
int16_t adc0, adc1, adc2, adc3;
//Modulobjekte
Adafruit_MMA8451 mma = Adafruit_MMA8451();//Acceleration_Sensor
Adafruit_MCP4725 dac;//Digital_Analog_Converter
//SamplingRate
#define INTERVAL 2000 //400Hz Sampling Rate
unsigned long lastMicros = 0;
unsigned long Micros;
int sensorPin0 = A0;    // select the input pin for the potentiometer
int sensorPin1 = A1;      // select the pin for the LED
int sensorValue0;  // variable to store the value coming from the sensor
int sensorValue1;

//Funktionen
void readsensors();
void serialtransmitData();

void setup() {
  Serial.begin(250000);
  /******************************************************************/
  //Setup MMA8451
  //Serial.println("Adafruit MMA8451 test!");
  if (! mma.begin()) {
    //Serial.println("Couldnt start");
    while (1);
  }
  //Serial.println("MMA8451 found!");
  mma.setRange(MMA8451_RANGE_8_G);
  mma.setDataRate(MMA8451_DATARATE_800_HZ);
  //Serial.print("Range = "); Serial.print(2 << mma.getRange()); 
  //Serial.println("G");
  /******************************************************************/
  /******************************************************************/
  //Setup DAC MCP4725
  //Serial.println("Connecting DAC...");
  dac.begin(0x62);
  //Serial.println("DAC connected.");
  /******************************************************************/
  /******************************************************************/
 
}

void loop() {
if (micros() - lastMicros > INTERVAL) { // Samplerate
    lastMicros = micros(); //
    readsensors(); //Function to read all data
    serialtransmitData();
  }

 
   }


void readsensors(){
 mma.read();//Read raw data Acceleration: stored mma.x, mma.y,mma.z --------> 1 g/1024 counts
 sensorValue0 = analogRead(sensorPin0);
 sensorValue1 = analogRead(sensorPin1);
}
void serialtransmitData(){
Transmitting Data
Serial.print(mma.x);  Serial.print(",");
Serial.print(mma.y);  Serial.print(",");
Serial.print(mma.z);  Serial.println(",");
Serial.print(sensorValue0); Serial.print(",");
Serial.print(sensorValue1); Serial.print(",");
Serial.print(vmot);   Serial.print(",");
Serial.print(lastMicros); Serial.println();
 
}


I setup the Acceleration Sensor to 800HZ Data Refreshrate and i try to get a constant sample rate for Acceleration Data and the ADC Data of analog Read using the blink without delay method.

I tested the code and the time between 2 datapoints is about 2,7ms. I need some help boosting this up.
Im using a Arduino Uno.

For now i want to speed up the i2c Speed by changing the twi.h:
Code: [Select]
#ifndef TWI_FREQ
#define TWI_FREQ 400000L
#endif

The mma8451 supports this, if i understood the datasheet correctly.

Are there some other options to speed things up ?
will i get problems with the serial communication ? Can i set a even higher baudrate?
I read something about saving the data local and than transmit it serial afterwards. Does the Arduino Uno have enough storage to save the data?
I want to Measure many motor frequencies about 20 seconds each with this.

Thanks for any help!

MarkT

#1
Sep 23, 2019, 04:50 pm Last Edit: Sep 23, 2019, 04:51 pm by MarkT
Since the serial data takes longer to send than the sampling period, you need to save the readings to RAM,
then output them on serial.

2.7ms is about 28 characters at 115200 baud, so perhaps encoding the data more densely is enough,
suggest a binary packet.

Or a higher baudrate - what values are possible is dependent on the computer and OS more than the Arduino, as USB serial drivers tend to be limited to standard rates.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up