Go Down

### Topic: ADXL 335 accelerometer MEGA 2560 help (Read 5675 times)previous topic - next topic

#### Destroyyoutoo

##### Mar 31, 2011, 10:54 am
Hey all,

I'm new to the forum and very new to electronics and microcontrollers. This is my first project and unfortunately its been a big one. Payload to put in a rocket, being launched Saturday. Time to ask for some help! haha.

First thing is first, is the accelerometer. I want it to output g forces, so that later we can detect events that occurred. Here is the code:

Code: [Select]
`int xAxisPin = 2;int yAxisPin = 3;int zAxisPin = 4;float xVal;float yVal;float zVal;void setup() {    Serial.begin(9600);  pinMode(xAxisPin,INPUT);  pinMode(yAxisPin,INPUT);  pinMode(zAxisPin,INPUT);  }void loop() {    // Read the voltage difference    xVal = analogRead(xAxisPin);  yVal = analogRead(yAxisPin);  zVal = analogRead(zAxisPin);    // Multiply by 5 volts    xVal = xVal*5/1023;  yVal = yVal*5/1023;  zVal = zVal*5/1023;    Serial.print("X Val:  ");  Serial.print(xVal, DEC);  Serial.print("  Y Val:  ");  Serial.print(yVal, DEC);  Serial.print("  Z Val:  ");  Serial.print(zVal, DEC);  Serial.println();    delay(1000);}`

I'm using this breakout board:

http://www.sparkfun.com/products/9269

Everything is wired up correctly I am sure of that. I also am aware that all I did was find the voltage difference (correct terms? its 5 am, who cares), hopefully. So I need to convert it to g's but I'm very confused as to how.

Any help would be much appreciated, don't be afraid to talk down to me, if my code is bad etc. I am new so I'm aware I'm not doing much right. Thanks for your time!

#### AWOL

#1
##### Mar 31, 2011, 12:07 pmLast Edit: Mar 31, 2011, 12:18 pm by AWOL Reason: 1
The maximum (measuring) rating for this device is only +/- 3.6 - a rocket will do way more than that initially, won't it?

To convert to g, you need to offset the zero g reading (which is half the supply voltage usually), and divide by the sensitivity, which depends on the supply voltage, but is 300mV per g with a 3V supply.
So, if you measure 600mV, you know you're accelerating at 2g.

Concentrate on getting one axis working (Z is my favourite because the test equipment is free) and then scale to the other axes.

You don't need to set pinMode for an analogue input, because it's an...input.

Code: [Select]
`Serial.print(yVal, DEC);`
It isn't realistic to print "float" values to ten decimal places.

Isn't the rocket going to have its performance slugged a bit by the trailing USB cable?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Destroyyoutoo

#2
##### Mar 31, 2011, 07:05 pm
Initially yes, but we have two accelerometers, one that is +/- 250 g in one direction and another that is +/- 3g in 3 directions. The one-D accelerometer is being used to measure initial accelerations. Its also being used so that when a certain amount of g's occur, like 4 I think I have it set to, the data starts recording and records for 10 minutes.

Quote
You don't need to set pinMode for an analogue input, because it's an...input.

Quote
It isn't realistic to print "float" values to ten decimal places.

I am  bad with deciding what to declare my variables as, is there something else that would be more appropriate so I wouldn't be working with as much memory and therefore hogging memory? Otherwise I'll just print it to two decimal places.

Quote
Isn't the rocket going to have its performance slugged a bit by the trailing USB cable?

A USB module that utilizes the serial port is being used to record the data, somewhat successfully. Its had its problems but I believe its in working condition now. So all the data records to a USB drive that can be taken out and looked at immediately after launch.

Thanks for the advice, I'll start working on it when I get back from campus, I appreciate it.

#### AWOL

#3
##### Mar 31, 2011, 07:39 pm
Quote
I am  bad with deciding what to declare my variables as, is there something else that would be more appropriate so I wouldn't be working with as much memory and therefore hogging memory? Otherwise I'll just print it to two decimal places

The amount of memory is immaterial; a float occupies four bytes and that's that.
However, if you only want it to print to 2 decimal places, use
Code: [Select]
`Serial.print(yVal, 2);` or, if you insist
Code: [Select]
`Serial.print(yVal, BIN);`

(The USB cable bit?  I was kidding)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Destroyyoutoo

#4
##### Mar 31, 2011, 08:03 pm
Hmmm I see.

And yeah I knew you were kidding just figured you might have been curious about data storage haha

#### Sergegsx

#5
##### Apr 01, 2011, 08:57 am
interesting project!!

so what are you using to store the data exactly??

#### Destroyyoutoo

#6
##### Apr 01, 2011, 11:30 am
Alrighty. I thought I had it coded correctly by now, but I'm not getting the results I want. When it is oriented with the z-axis down and the xy plane horizontal, I get 1 g in the z direction and 0 in the x and y directions. As expected.

However when I orient it with x facing up and down, it gives me:

X Val:  -1.5809711456  Y Val:  0.1459760665  Z Val:  0.7161939144

Here's the code right now:

Code: [Select]
`int xAxisPin = 2;int yAxisPin = 3;int zAxisPin = 4;float input_voltage = 5;const float sensor_power_supply = 3;float zero_g_bias = 1.5;float x_voltage;float y_voltage;float z_voltage;float xVal;float yVal;float zVal;void setup() {    Serial.begin(9600);  }void loop() {    // Read the voltage difference    x_voltage = input_voltage*analogRead(xAxisPin)/1023;  xVal = (x_voltage - zero_g_bias) * 1000 / 300;  xVal = xVal - 3;    y_voltage = input_voltage*analogRead(yAxisPin)/1023;  yVal = (y_voltage - zero_g_bias) * 1000 / 300;    yVal = yVal - 3;    z_voltage = input_voltage*analogRead(zAxisPin)/1023;  zVal = (z_voltage - zero_g_bias) * 1000 / 300;    zVal = zVal - 3;  Serial.print("X Val:  ");  Serial.print(xVal, BIN);  Serial.print("  Y Val:  ");  Serial.print(yVal, BIN);  Serial.print("  Z Val:  ");  Serial.print(zVal, BIN);  Serial.println();    delay(1000);}`

I have it wired to 5V on the arduino and its properly grounded. What else could be wrong?

#### AWOL

#7
##### Apr 01, 2011, 12:16 pmLast Edit: Apr 01, 2011, 12:22 pm by AWOL Reason: 1
If the accelerometer supply is 3V, why don't you use the 3.3V for Aref?

Quote
Val:  -1.5809711456  Y Val:  0.1459760665  Z Val:  0.7161939144

Code: [Select]
`Serial.print(xVal, BIN);`
Huh?

(I was joking about using BIN too - it's the default)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Destroyyoutoo

#8
##### Apr 01, 2011, 12:45 pm
Oh no, my noobness is showing.

Didn't know bin was default, googled it and saw it was to two dec. places to I used it. Whoops haha, now I know. Is it always the default for the print function?

uhhhh I've never used Aref before..am I supposed to have something wired up to it?

also:

Quote
Val:  -1.5809711456  Y Val:  0.1459760665  Z Val:  0.7161939144

I switched it to bin like right after that. Sorry I stayed up all night and my brain is pretty scattered :/.

#### AWOL

#9
##### Apr 01, 2011, 04:06 pm
What does this
Code: [Select]
`xVal = xVal - 3;` do?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Destroyyoutoo

#10
##### Apr 01, 2011, 04:16 pm
If the acceleration was maxed out, it would give me 6 g's. I wanted to make it so that the max acceleration was 3 g's and that it would tell me -3 g's if it were maxed the opposite direction. This is correct no?

#### AWOL

#11
##### Apr 01, 2011, 04:57 pm
Quote
This is correct no?

This is incorrect, yes.
It is maxed-out, it will simply read close to the supply voltage, or close to zero.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### Destroyyoutoo

#12
##### Apr 02, 2011, 06:00 am
Oh man sorry I didn't notice this post earlier

Quote
interesting project!!

so what are you using to store the data exactly??

I guess I should describe the project in full. I plan on releasing all the code that was made (some was used from others and I need to find who to reference as well).

The project is to design a payload for a rocket that will go 5280 feet in the air. The payload consists of two accelerometers, a humidity sensor, a temperature/pressure sensor, an irradiance sensor, a uv sensor, a gps locator, a transmitter, cameras to record the flight, and a usb module to record the data.

The usb module is:

http://www.dontronics-shop.com/ftdi-vdip1-mcu-to-usb-host-controller-development-module.html

That has been the bane of our existence. I think if we loosen the screws on the board it'll help. We bought a new one though since the first one was damaged in a failed launch. Hopefully that helps. Also lost the irradiance sensor, and bought a new one but it isn't right :/. Then the photodetector has been giving me fits but I just bought a multimeter today so I'll see if I can get that worked out.

Thanks again for all your help AWOL, I fixed the the 1-D accelerometer because of your help, and the 3-D accelerometer is well on its way. I have learned an increeeeeeedible amount from this project and I hope I can continue to learn.

Does anyone have any suggestions for books to buy?

#### WillR

#13
##### Apr 02, 2011, 06:05 pm
Does anyone have any suggestions for books to buy?

Based on the nature of your questions...

Arduino Cookbook

#### Destroyyoutoo

#14
##### Apr 02, 2011, 07:29 pm

Alright. I give. The rocket launch got delayed for other reasons, but my accelerometers are still off. I have tried to figure it out on my own but at this point am incapable, I have been awake for 53 hours straight and my brain is fried.

The code for my 1-D accelerometer is as follows:

Code: [Select]
`// 1-D Accelerometer Variablesfloat G_force = 0;float Calib_Diff;float Accel;int AccelPin = A1;float val = 0;const float arduino_voltage_1D = 5;const float zero_g_bias = 0;void setup() {  Serial.begin(9600);  //Use Accelerometer to detect liftoff  AutoCalib_250();  Serial.println("250-G Accelerometer Calibrated");  //startRecording();}void loop() {    Accel = analogRead(AccelPin);  Accel = Accel + Calib_Diff;  Accel = (arduino_voltage_1D - zero_g_bias_1D)*Accel/1023;  Accel = Accel*1000/10;  Accel = 250 - Accel;    delay(100);}// A function made so the Arduino knows when to start recording datavoid startRecording() {    while (G_force <= abs(.5)){      // read the sensor:  Accel = analogRead(AccelPin);  Accel = Accel + Calib_Diff;  Accel = (arduino_voltage_1D - zero_g_bias_1D)*Accel/1023;  Accel = Accel*1000/10;  Accel = 250 - Accel;    delay(100);// record every 1/10th of a second    }}//Auto-Calibrate the 250-G Accelerometervoid AutoCalib_250() {int Time_init = millis();float SensorAvg = 0;int CurrSense = 0;float i = 1; while (millis()-Time_init < 5000) {   CurrSense = analogRead(AccelPin);   SensorAvg = SensorAvg+CurrSense;   i = i + 1.00; } SensorAvg = SensorAvg/i; Calib_Diff = 512.00 - SensorAvg; }`

I tried basing my 3-D accelerometer off of this, but if its incorrect then that's my problem. So is there anything wrong with this, and if so what is?

I wired it to 5V.

This is currently my 3-D accelerometer code:

Code: [Select]
`int xAxisPin = 2;int yAxisPin = 3;int zAxisPin = 4;const float input_voltage_3D = 5;const float sensor_power_supply_3D = 3;const float zero_g_bias_3D = 0;float x_voltage;float y_voltage;float z_voltage;float xVal;float yVal;float zVal;void setup() {    Serial.begin(9600);  }void loop() {    // Read the voltage difference    x_voltage =  analogRead(xAxisPin);  x_voltage = (sensor_power_supply_3D - zero_g_bias_3D)*x_voltage/614;  xVal = x_voltage * 1000 / 500;  y_voltage =  analogRead(yAxisPin);  y_voltage = (sensor_power_supply_3D - zero_g_bias_3D)*y_voltage/614;  yVal = y_voltage * 1000 / 500;    z_voltage =  analogRead(zAxisPin);  z_voltage = (sensor_power_supply_3D - zero_g_bias_3D)*z_voltage/614;  zVal = z_voltage * 1000 / 500;    Serial.print("X Val:  ");  Serial.print(xVal);  Serial.print("  Y Val:  ");  Serial.print(yVal);  Serial.print("  Z Val:  ");  Serial.print(zVal);  Serial.println();    delay(1000);}`

Supply voltage is 3V (tested it with multimeter)

Yeah so I don't even know. Like I said my brain is fried. I'm frustrated.

What needs to be changed in those two?

Oh and on a bright note, the UV sensor, which was the one I was having the most troubles with, appears to be working now .

Also if it appears as if I ignored your advice from earlier, I probably had it in my code but then started changing random things in the hope that it'd work. Bad plan haha.

Go Up