Pages: [1] 2   Go Down
 Author Topic: ADXL 335 accelerometer MEGA 2560 help  (Read 2145 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 23
 « on: March 31, 2011, 03:54:21 am » Bigger Smaller Reset

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:
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!
 Logged

If it aint broke, stop touching it

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23657
I don't think you connected the grounds, Dave.
 « Reply #1 on: March 31, 2011, 05:07:26 am » Bigger Smaller Reset

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:
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?
 « Last Edit: March 31, 2011, 05:18:50 am by AWOL » Logged

"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.

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #2 on: March 31, 2011, 12:05:48 pm » Bigger Smaller Reset

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.

Thanks, I read about this at one point but never acted upon it, I'll fix it in all my code.

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.
 Logged

If it aint broke, stop touching it

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23657
I don't think you connected the grounds, Dave.
 « Reply #3 on: March 31, 2011, 12:39:21 pm » Bigger Smaller Reset

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:
Serial.print(yVal, 2);
or, if you insist
Code:
Serial.print(yVal, BIN);

(The USB cable bit?  I was kidding)
 Logged

"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.

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #4 on: March 31, 2011, 01:03:43 pm » Bigger Smaller Reset

Hmmm I see.

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

If it aint broke, stop touching it

0
Online
Edison Member
Karma: 17
Posts: 1401
Arduino rocks
 « Reply #5 on: April 01, 2011, 01:57:08 am » Bigger Smaller Reset

interesting project!!

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

* Si preguntas, pon el código de tu programa, hace mucho mas fácil ayudarte. Y me ahorro un mensaje pidiendo que lo hagas.
* Si consigues solucionar tu problema, dedica unos minutos a explicar en tu post como lo conseguiste para beneficio de todos.
* Cambia el 'Subject' de tu hilo y añade 'SOLUCIONADO' cuando hayas llegado a una solución al problema que planteaste.
* Utiliza un 'Subject' para tu hilo que explique de que va el hilo.
Si estas empezando:
* Comienza a usar Arduino
* Guías de iniciación a Arduino
* Ejemplos
* Referencia del Lenguaje
* Conceptos básicos
Guia de usuario de arduino
Tutoriales en Ingles
Si necesitas que alguien te escriba el código: http://www.freelancer.com/  o esta  http://www.guru.com/

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #6 on: April 01, 2011, 04:30:14 am » Bigger Smaller Reset

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:
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?
 Logged

If it aint broke, stop touching it

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23657
I don't think you connected the grounds, Dave.
 « Reply #7 on: April 01, 2011, 05:16:10 am » Bigger Smaller Reset

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:
Serial.print(xVal, BIN);
Huh?

(I was joking about using BIN too - it's the default)
 « Last Edit: April 01, 2011, 05:22:30 am by AWOL » Logged

"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.

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #8 on: April 01, 2011, 05:45:48 am » Bigger Smaller Reset

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 :/.
 Logged

If it aint broke, stop touching it

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23657
I don't think you connected the grounds, Dave.
 « Reply #9 on: April 01, 2011, 09:06:41 am » Bigger Smaller Reset

What does this
Code:
xVal = xVal - 3;
do?
 Logged

"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.

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #10 on: April 01, 2011, 09:16:12 am » Bigger Smaller Reset

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?
 Logged

If it aint broke, stop touching it

Global Moderator
UK
Offline
Brattain Member
Karma: 225
Posts: 23657
I don't think you connected the grounds, Dave.
 « Reply #11 on: April 01, 2011, 09:57:00 am » Bigger Smaller Reset

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.
 Logged

"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.

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #12 on: April 01, 2011, 11:00:13 pm » Bigger Smaller Reset

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?
 Logged

If it aint broke, stop touching it

Georgina Ontario
Offline
Sr. Member
Karma: 5
Posts: 437
Arduino rocks
 « Reply #13 on: April 02, 2011, 11:05:34 am » Bigger Smaller Reset

Does anyone have any suggestions for books to buy?

Based on the nature of your questions...

Arduino Cookbook
About \$18 on Amazon Kindle
 Logged

Just another Hacker

Offline
Newbie
Karma: 0
Posts: 23
 « Reply #14 on: April 02, 2011, 12:29:51 pm » Bigger Smaller Reset

Looked at the table of contents and its exactly what I need, thanks for the suggestion.

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:
// 1-D Accelerometer Variables
float 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 data

void 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 Accelerometer
void 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:
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.
 Logged

If it aint broke, stop touching it

 Pages: [1] 2   Go Up
Jump to: