Go Down

Topic: Acceleration Experiment using MMA8451 (Read 1 time) previous topic - next topic


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:

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]
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MCP4725.h>
#include <arduino.h>
uint32_t vmot = 0;
String cmd;
int16_t adc0, adc1, adc2, adc3;
Adafruit_MMA8451 mma = Adafruit_MMA8451();//Acceleration_Sensor
Adafruit_MCP4725 dac;//Digital_Analog_Converter
#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;

void readsensors();
void serialtransmitData();

void setup() {
  //Setup MMA8451
  //Serial.println("Adafruit MMA8451 test!");
  if (! mma.begin()) {
    //Serial.println("Couldnt start");
    while (1);
  //Serial.println("MMA8451 found!");
  //Serial.print("Range = "); Serial.print(2 << mma.getRange()); 
  //Setup DAC MCP4725
  //Serial.println("Connecting DAC...");
  //Serial.println("DAC connected.");

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


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

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!


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