Inconsistents values of acceleration with acceleration sensor ADXL335 and MMA776

Hey there.

We're making a project at school, and I chose a little car RX12 (a toy for kids). The goal was to study the car, its acceleration (to integer and get its maximum velocity which is told to be 28km/h) and how much time could the battery work (it is told to be 12min).
We had to use Arduino that we never really used up to now but we managed to make 3 programs : one for the acceleration sensor ADXL335, one for the acceleration sensor MMA776 and one for Current and Voltage with Phidgets 1135 for voltage and MR003-006.1 for current. In our case, we used Arduino Uno R3 and a SD Card Shield v4.1 on it.
Here are the 3 programs :

//Acceleration with ADXL335
#include <SD.h>
File Fichier;

boolean test;
int increment = 50;                   //Increment en ms
int xpin = 0.;                        // Variable numérique de l'accélération
float temps = 0.;                     //Timer en s
float AcX = 0.;                       // Variable de l'accélération
float Somme_acc = 0. ;                //Somme de Acx / 10ms
float Ax = 0.  ;                      // Valeur de l'accélération



void setup() {
  // Initialisation
  
  Serial.begin(9600);
  pinMode(10, OUTPUT); // laisser la broche SS en sortie - obligatoire avec librairie SD
  test=SD.begin(4); // initialisation de la carte SD avec broche 4 en tant que CS - renvoie true/false
  if (test!=true)
  {
     while(1);
  }
  Fichier = SD.open("Valeurs2.txt", FILE_WRITE);
  Fichier.println("--------------Nouvelle mesure---------------");
  
}

void loop() {
//Loop
  
//Timer
  
   Fichier.print(temps);
   Serial.print(temps);
    
//Accélération

   for ( int i=0 ; i<=4 ; i++) {  
     xpin = analogRead(A3);
    AcX = -0.0568*xpin + 25.05;
    Somme_acc = AcX + Somme_acc;
    delay(10);
   }
       Ax = Somme_acc/5.;
   Fichier.print("                    ");
   Fichier.println(Ax);
   Serial.print("                    ");
   Serial.println(Ax);
   delay(increment);
   Somme_acc = 0. ;
   temps = temps + increment*0.002 ;
   if(temps > 20.)
   //Fin des mesures
   {Fichier.println("---------------Fin des mesures---------------");
    {Fichier.close();
    }
   }
    
}
//Acceleration with MMA776
#include <Wire.h>
#include "MMA7660.h"
MMA7660 accelemeter;
#include <SD.h>

File fichier; 
boolean test;
char touche;
float temps = 0.;
int increment = 100;
float ax,ay,az;

void setup()
{ 
  accelemeter.init(); 
  Serial.begin(9600);
  pinMode(10, OUTPUT); // laisser la broche SS en sortie - obligatoire avec librairie SD
  test=SD.begin(4); // initialisation de la carte SD avec broche 4 en tant que CS - renvoie true/false
  if (test!=true)
  {
     while(1);
  }
  fichier = SD.open("/MMA7660",FILE_WRITE); 
  fichier.seek(fichier.size());
  fichier.println();
  fichier.println("+++++++++++++ Nouvelles valeurs +++++++++++++");
  fichier.println();
  fichier.println("Duree (s) ; Ax (m/s2) ; Ay (m/s2) ; Az (m/s2)");
  fichier.println();
} 

void loop()
{
      fichier.print(temps);
      fichier.print(" ; "); 
      accelemeter.getAcceleration(&ax,&ay,&az);
      fichier.print(ax*9.81); fichier.print(" ; "); fichier.print(ay*9.81); fichier.print(" ; "); fichier.println(az*9.81);
      temps = temps + increment*0.001;
      delay(increment);
      if (temps >= 60)
      {
       fichier.println();
       fichier.println("+++++++++++++ Arret des mesures +++++++++++++");
       fichier.close();
       temps = 0.;
       while(1);
      }
 }
//Current with MR003-006-1 and Voltage with Phidgets 1135
#include <SD.h>
File Fichier;
 
boolean test;
int increment = 1000;
int nI = 0.;                           // Variable numérique de l'intensité
int nU = 0.;                          // Variable numérique de la tension
float temps = 0.;                    // Timer
float I = 0.;                       // Variable de l'intensité
float U = 0.;                      // Variable de la tension

void setup() {
  // Initialisation
  Serial.begin(9600);
  pinMode(10, OUTPUT); // laisser la broche SS en sortie - obligatoire avec librairie SD
  test=SD.begin(4); // initialisation de la carte SD avec broche 4 en tant que CS - renvoie true/false
   if (test!=true)
  {
     while(1);
  }
 Fichier = SD.open("Valeurs2.txt", FILE_WRITE);
 Serial.println("--------------Nouvelle mesure---------------");
}

void loop() {

 
  
//Timer
  
   Serial.print(temps);
   Fichier.print(temps);
    
//Intensité
 
     nI = analogRead(A0);
  //  I=0.0489*nI + 0.0589;
   I= 0,0403*nI - 13,668;
    Serial.print("                    ");
    Fichier.print("                    ");
    Serial.print(I);
    Fichier.print(I);
    
//Tension

   int nU = analogRead(A1);
  U= 0.0713*nU - 36.634;
  Serial.print("                    ");
  Fichier.print("                    ");
  Serial.println(U);
  Fichier.println(U);
  
    
//delay
    delay(increment);
   temps = temps + increment*0.001 ;
//Fin des mesures
    if(temps > 40.)
     {Serial.println("--------------Fin des mesures---------------");
      Fichier.close();}
     }

However, we got late as the time would go on and went on an issue with acceleration. Whenever we would try to measure acceleration with either of the 2 sensors, the acceleration would be positive then negative, and alternate like this continuously until the car stops.
Here is the acceleration when MMA776 wasn't mooving :

Here is the acceleration when MMA776 was mooving (2 series of measures) :

There we go with ADXL335 while mooving (2 series of measures) :

Here we have the latest serie (previous picture of the seconde measure of ADXL335) with all the values negative put in positive :

(It's always acceleration in function of the time in ms, before we improved the programme to get the time in second (s) )

We told this to the teacher in charge of the project but he couldn't really help us. We tought that it was due to the car that was moving too much and that it would have an influence on the acceleration sensor. This car doesn't hold the road well too. So we tried to make the car accelerate between a wall and another wall-like-linked-boxes so that the car stays in one direction and doesn't move too much. It didn't really work, the problem was still there.
That put aside, there is a problem with the calibration of the acceleration sensor ADXL335 (going from N, a numeric number to an acceleration in m.s-2) as we had to recalibrate it pretty often (but it was a problem that could be handled pretty fast)
So it was a real mess for acceleration.

We tried to make something of current and voltage. We calibrated both Phidgets1135 and MR003-006.1.
Our goal there was to measure current and voltage that was getting out of the battery while the car was moving and accelerating. However, even though the programs seemed ok (we calibreted for the equations in the programs and made them with the teacher by our side) we had a constant value around 25 for current and values between -1 and 1 for voltage. We changed the program a bit and the electrical circuit, then the voltage was working. However, when we changed it again, everything was going back to 25 for current and between -1 and 1 for voltage.
Here is what looked like the circuits :


So, it was wasn't really better for current and voltage.

In the end, we made all that we could in these 3 past days but we went out of time. The teacher tried as he could to help us but we're in pretty bad shape. He gave me the car and the arduino card with the sensors (even though he's not allowed to as it's from the high school). We have until May 9. And I have 2 weeks of holidays which I would like to use to get everything working... This project is evaluated fort the bac (a diploma in France).
So, if I can say, we're in deep shit, me and my team (we're 4).
I tried to be the most complete as possible. I'm new to Arduino and to this website, thank you to those who will to help.
P.S : Excuse me if my english is bad, I'm french.
P.S² : I tried to be complete but if I forgot something, don't hesitate to ask.

Hi,
Its not good form to double post your enquiry as you will end up with two possibly different trains of thought.

Tom... :slight_smile:

Other thread removed.

Hey there.
Yeah, my bad, I didn't know if it really was a problem of Arduino/programs so I would have to put my post in "Installation & Troubleshooting" or as it's kind of a project, I could put it here. Finally I made both. I never posted before so I couldn't be sure which one was the best for my problem. Anyway, tell me if you have any clues, anything would be of great help.
Yesterday, I looked for similar problems here and there and I went here :
https://forum.arduino.cc/index.php?topic=541306.0
It's for a poteniometer but it seemed like we had similar problems.
Someone put this program that seemed nice.

// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground

int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;

void setup() {
  Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}

void loop() {
  // read input twice
  rawValue = analogRead(A0);
  rawValue = analogRead(A0); // double read
  // ignore bad hop-on region of a pot by removing 8 values at both extremes
  rawValue = constrain(rawValue, 8, 1015);
  // add some deadband
  if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
    oldValue = rawValue;
    // convert to percentage
    potPercentage = map(oldValue, 8, 1008, 0, 100);
    // Only print if %value changes
    if (oldPercentage != potPercentage) {
      Serial.print("Pot percentage is: ");
      Serial.print(potPercentage);
      Serial.println(" %");
      oldPercentage = potPercentage;
    }
  }
}

This part particulary seemed to be of use in my case if i understood it right :

int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;

void setup() {
  Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}

void loop() {
  // read input twice
  rawValue = analogRead(A0);
  rawValue = analogRead(A0); // double read
  // ignore bad hop-on region of a pot by removing 8 values at both extremes
  rawValue = constrain(rawValue, 8, 1015);
  // add some deadband
  if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
    oldValue = rawValue; }

I may be able to put this in the if loop for i from 0 to 4 in the ADXL accelerometer program. By the way, do you know any way to know what values can take the ADXL accelerometer ?
In the case of the current and voltage, I tought that I would take pictures of the circuit and try to change it to get something like that :

(Sorry for the poor quality of this paint)
As i don't have the tools to weld, i tought that I would use scotch at some places. Is it ok ?

I don't know that much about Arduino but if you could point me to a direction, give me some toughts about it or have any lead it could help.
P.S : Sorry if i pissed you off with the double post, in fact, i also tought that I could get an answer faster this way, I won't do it again.

Best regards,
Kevin

Hi,
Sorry but ALL your pictures are way too small, you need each image to be about 500 x 500 pixels or what ever proportion each may have.

What is the power supply for your project?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks..Tom.... :slight_smile:

TomGeorge:
Hi,
Sorry but ALL your pictures are way too small, you need each image to be about 500 x 500 pixels or what ever proportion each may have.

You can click on the previews of the pictures of the car and the excel graph. For the othere ones, you have to past the link. When you're in the website you can click on the picture to be able to zoom again. I tought that the size was okay but if needed I can take a picture again, yes.

TomGeorge:
What is the power supply for your project?

If we take Q = I * t
with Q the capacity of the battery, t, the duration of the battery and I the current delivered by the battery, we have I = Q / t, knowing that Q = 0,5 Ah and t = 12min = 0,2h (told by the manufacturer);
I = 0,5 / 0,2 = 0,25 A
Power Pa=UI and the battery can and has to deliver 7,4 V
Pa =7,4 * 0,25 = 18,5 W
I believe it's around this value.
EDIT : Maybe did you talk about the battery in the car and the battery which will feed arduino ? The battery for the car is a Li-ion 7,4 V, 500mAh. The one for the Arduino when the car is mooving is a 9V, 270 mAh.

TomGeorge:
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

I don't really understand why the circuit was put this way in some places but ok i will do it.
EDIT : Here it is,

Thank you for replying !
Kevin

I'm really getting desperete actually. As I'm pretty bad with Arduino and as I don't know how to program, it took me several hours juste to write a few lines. Moreover, the program i wrote finally didn't work as I expected : the variations of acceleration are always too high. I'm in a cul-de-sac right now. Can't anyone give me a clue ?
Here is what my program looks like, inspired by Leo's program in the post i mentionned before :

#include <SD.h>;
File Fichier;

boolean test;
     int increment = 31;                         //Increment en ms
int raw_xpin = 0.;                         // Variable numérique de l'accélération
int old_xpin =0.;                         // Variabe numérique de l'accélération
int n=0.;
   float temps = 1.81;                        //Timer en s
float AcX = 0.;                         // Variable de l'accélération
float Somme_acc = 0. ;                 // Somme de Acx 
float Ax = 0.  ;                      // Valeur de l'accélération



void setup() {
  // Initialisation
  
  Serial.begin(9600);
  pinMode(10, OUTPUT); // laisser la broche SS en sortie - obligatoire avec librairie SD
  test=SD.begin(4); // initialisation de la carte SD avec broche 4 en tant que CS - renvoie true/false
  if (test!=true)
  {
     while(1);
  }
  Fichier = SD.open("Valeurs2.txt", FILE_WRITE);
  Fichier.println("--------------Nouvelle mesure---------------");
  
}

void loop() {
//Loop
  
//Timer
  
   Fichier.print(temps);
    Serial.print(temps);
    
//Accélération

   for ( int i=0 ; i<=4 ; i++) { 
      // read input twice
  raw_xpin = analogRead(A3);
  raw_xpin = analogRead(A3); // double read
  // ignore bad hop-on region of a pot by removing 8 values at both extremes( lot more here, to keep btw -7 and 7 in acceleration )
 raw_xpin = constrain(raw_xpin,318,564 ); // contraindre xpin dans bornes
  // add some deadband
  if (raw_xpin >318 && raw_xpin <564) //vérifier les bornes et comprendre l'accélération numérique entre -7 et 7
  {
   if (raw_xpin < (old_xpin - 4) || raw_xpin > (old_xpin + 4)) // || = OU  //si xpin est assez différent, tolérance
  {  old_xpin = raw_xpin;
   AcX = -0.0568*old_xpin + 25.05;
    Somme_acc = AcX + Somme_acc;
    n=n+1; }
  }
   
    }
   if (n!=0)
   {    Ax = Somme_acc/n ; }
      Fichier.print("                    ");
   Fichier.print(Ax);
   Fichier.println("");
   Serial.print("                    ");
   Serial.print(Ax);
   Serial.println("");
   Somme_acc = 0. ;
   n=0.;
   temps = temps + increment*0.001;
   if(temps > 20.)
   //Fin des mesures
   {
    Fichier.println("---------------Fin des mesures---------------");
    Fichier.close();
    }
   }

It just look as bad, here is a screen of acceleration while i vibrate a little my arm (not mooving the sensor) :

So the ADXL, after all I tried, doesn't seem ok... Let's try with the MMA...

Volcat:
You can click on the previews of the pictures...

You can attach the images then embed them in your post so we don't have to visit a yet another virus infested image hosting site...

http://forum.arduino.cc/index.php?topic=519037.0

I can't find any specifications on your car. How much time, do you estimate, does it take for the car to reach it's maximum speed? I am suspicious my the time your "loop" gets around to reading a second acceleration value, it's already zero and beginning to slow down.

Paul

I changed all the pictures as asked. Sorry, I'm not used to forums.

Paul_KD7HB:
I can't find any specifications on your car. How much time, do you estimate, does it take for the car to reach it's maximum speed?

I estimate that the car takes 2 seconds to get its maximum speed but I don't know if it's justified. The manufacturer says less than 8 seconds. The matlab version says 10-20 seconds.

Paul_KD7HB:
I am suspicious my the time your "loop" gets around to reading a second acceleration value, it's already zero and beginning to slow down.

Paul

The whole serie is inconsistent. Why does it accelerate then decelerate continuously ? As the car is accelerating first to then get its usual speed, we should see a big acceleration then acceleration = 0 as the car is not accelerating anymore (constant speed). It's not the case... But as I told before, the problem comes from the vibrations, even tiny vibrations, that make the sensor laggy. I tried to smooth this lag by making a program that print an average acceleration on 5 values of acceleration, eliminating extreme values ( constrain between -7 and 7 in acceleration ), not taking in count values if they are too close to each other. At least, it's what I tried to do. But everything is useless finally as this accelerometer is always making shit. Sorry for the word but it pisses me off a little.

About current and voltage, here is few values taken on Arduino when i was getting the car on my hand but accelerating :

In this picture, i used the code i linked before (with few changes but nothing so worthy) but with this formula :

I= 0.0403*nI - 13.668

This is the one the teacher helped us to find.

I changed a bit the formule because adding a -7.20 thinking that it would be better because i could see that from t=12s to t=14s while I was mooving the car, the current changed ( up to 10.). But when it had to be at 0, it was around 7.20 so i substracted 7.20.

I=0.0403*nI -13.668 -7.20;

Here are the values i got from it :
(1st serie of values)

-------------------------(2nd serie of values)--------------------------------

It's going from 0.8 to 2.0+ approximately, with an average of 1.5.
It seems ok I guess ? The battery delivers 0.5 Ah and has can be used until 12m approximately from what the manufacturer is saying.
What do you think?
P.S : I forgot to say I changed the circuit a little. It now looks like that :

Hi,
Can you post pictures of your project with the sensors connected and mounted in the car please?

Can you post a proper circuit diagram, showing ALL your connections to sensors and batteries?
Label the connections to your components.
The circuit you supplied did not show any needed information.
Have you got any bypass capacitors on the motor and have you checked your wiring.
Electrical noise from the motor can easily get into the sensor circuitry.
Have you kept the motor power wires away from the sensors and their wiring?

Can you post links to spec/data of your sensors, are they just the IC, or are they mounted on a breakout board.
Both sensors work with 3.3V, but if mounted on breakout PCBs there may be a voltage regulator included.

Tom... :slight_smile:

Hey there.

TomGeorge:
Hi,
Can you post pictures of your project with the sensors connected and mounted in the car please?

TomGeorge:
Can you post a proper circuit diagram, showing ALL your connections to sensors and batteries?
Label the connections to your components.
The circuit you supplied did not show any needed information.

TomGeorge:
Have you got any bypass capacitors on the motor and have you checked your wiring.
Electrical noise from the motor can easily get into the sensor circuitry.

I'm sorry, I don't know what this is but with the picture of the car with its sensors I posted just before you should be able to figure it out.

TomGeorge:
Have you kept the motor power wires away from the sensors and their wiring?

The sensors don't get their energy/power from the same batteries as the car. However, the sensors are wired to the motor and the batteries of the car (seems kind of logic). I don't know if I answered your question.

TomGeorge:
Can you post links to spec/data of your sensors, are they just the IC, or are they mounted on a breakout board.

A link for Voltage Sensor-Phidgets 1135 : 1135 User Guide - Phidgets Support
For Current Sensor -MR003-006.1 : http://www.microbot.it/documents/mr003-006_datasheet.pdf

TomGeorge:
Both sensors work with 3.3V, but if mounted on breakout PCBs there may be a voltage regulator included.

I mounted both sensors with 5V. I hope it's still good though.
Did you see the pictures of what I got on Arduino when accelerating the car? Do you think the values of current and voltage are good?

I don't measure acceleration and Current/Voltage at the same time. So in the pictures I showed before was the mounting when I should measure Current/Voltage.
Here is the mounting when I measure acceleration :

We can't see that much, but the accelerometer is on the front, scotched. It is linked to the arduino card by 3 wires : a black one for the GND, a blue one for the output and a white one, the 5V.

Hi,
You are having problems with your accelerometer readings, where is the link to those sensors.
I know you have 2 different accelerometers, which one do you use?

In your circuit where is it connected to the controller?
Why have you not included it in your diagram?
If you know how to connect the accelerometer, then you must have a circuit to get the connections from.

Keep the accelerometer and its wiring AWAY from the motor and its wiring.
WHY?
Because even though the motor and sensor is on different power supplies, the motor EMITTS electromagnetic interference, EMI.

The accelerometers work on very small voltages and currents, which means the EMI can cause niose problems with your sensor.

If you lift the car of the floor so its wheels spin, what sort of readings do you get from the accelerometer when you spin the wheels?
That is with the car not in motion.

Wrapping all the wires together, motor and sensor, is inviting EMI from the motor the add noise to the accelerometer signal.

What school level is this project?

Are you using one of these batteries to power the UNO?

If the sensor is the little red PCB sticky taped to the front of the car, that is not going to give you consistent readings.
The PCB MUST be properly fixed to the car so it cannot move around.
The PCB MUST be oriented so that the x,y and y axis are defined with respect to the car.

Tom... :slight_smile:

Hey there.

TomGeorge:
You are having problems with your accelerometer readings, where is the link to those sensors.

I had problems with current and voltage too but the biggest one was with acceleration. It seems ok for current and voltage now. So there is no link to those sensors anymore.

TomGeorge:
I know you have 2 different accelerometers, which one do you use?

I mainly prefer use the ADXL335 but the MMA suffers from the same problems.

TomGeorge:
In your circuit where is it connected to the controller?

By controller did you mean the controller for the car or the accelerometer ? The controller for the car is wireless. For the accelerometer, it is connected to Arduino only, not to the circuit.

TomGeorge:
Why have you not included it in your diagram?

I didn't include it in the diagram because I never use the sensors of [current+voltage] and acceleration at the same time. So there is 2 circuits. The one I drawed just before to measure current and voltage only and this one when I need to measure acceleration :

TomGeorge:
Keep the accelerometer and its wiring AWAY from the motor and its wiring.
WHY?
Because even though the motor and sensor is on different power supplies, the motor EMITTS electromagnetic interference, EMI.

The accelerometers work on very small voltages and currents, which means the EMI can cause niose problems with your sensor.

So I need to put the arduino and the accelerometer ADXL335 with the wires included that link both away? How should I do that ? Put those on the back of the car? It would be near the motor. On the front, you say later that the accelerometer is not at his best there. Should I create a box with aluminium on it and put that all on the back with Arduino, ADXL and the wires at the back ?

TomGeorge:
If you lift the car of the floor so its wheels spin, what sort of readings do you get from the accelerometer when you spin the wheels?
That is with the car not in motion.

I get those values with time on the left and acceleration on the right. The few first seconds are when the car is not accelerating.

TomGeorge:
Wrapping all the wires together, motor and sensor, is inviting EMI from the motor the add noise to the accelerometer signal.

Our teacher talked to us about the EMI but not how that was working. Then, I will put the other sensors, of current and voltage, away when I make the measures of acceleration.

TomGeorge:
What school level is this project?

It's a 12th grade school project.

TomGeorge:
Are you using one of these batteries to power the UNO?

Yeah, we are using one of these. Here is a picture :

TomGeorge:
If the sensor is the little red PCB sticky taped to the front of the car, that is not going to give you consistent readings.
The PCB MUST be properly fixed to the car so it cannot move around.
The PCB MUST be oriented so that the x,y and y axis are defined with respect to the car.

This is properly oriented in the axis x and only this one as we only need 1 acceleration, that of the car while it is mooving.
How should I fix the ADXL335 so that it doesn't move around ?

Hi,

So I need to put the arduino and the accelerometer ADXL335 with the wires included that link both away? How should I do that ? Put those on the back of the car? It would be near the motor. On the front, you say later that the accelerometer is not at his best there. Should I create a box with aluminium on it and put that all on the back with Arduino, ADXL and the wires at the back ?

The accelerometer is in the right place, but you must mount/fix it solidly to the car, not with sticky tape.

PLEASE post a link to data/specs of your Accelerometer Board.

Tom.. :slight_smile:

TomGeorge:
PLEASE post a link to data/specs of your Accelerometer Board.

I used the ADXL335 this far : SparkFun Triple Axis Accelerometer Breakout - ADXL335 - SEN-09269 - SparkFun Electronics
However, the wires and the accelerometer are not stick together anymore (it detached when I tried to fix the accelerometer on the car properly). I will have to buy equipments to weld again those two.

I have another accelerometer, the MMA7760 https://www.seeedstudio.com/Grove-3-Axis-Digital-Accelerometer(%26plusmn%3B1.5g)-p-765.html
I think I'm gonna use this one now as the other one can't be used for now.

Thank you for replying this far.
Kevin

Hi,
Ops Accelerometers ;



Tom... :slight_smile:

Hey there.

Yeah, I use those accelerometers.
I will try to fix properly the arduino card, the MMA7760 and its wires on the front of the car.
Do you have any advice concerning what I should do now or do you need anymore information? The only problems were that I didn't fix the accelerometer properly and and there was the EMI, so now it should work fine right?

Greetings,
Kevin