Sie sind auf Seite 1von 6

Adxl355 codigo ////////////////////////////////////////////////////////////////// //2011 bildr //Released under the MIT License - Please reuse change and share

//Simple code for the ADXL335, prints calculated orientation via serial ////////////////////////////////////////////////////////////////// //Analog read pins const int xPin = 0; const int yPin = 1; const int zPin = 2; //The minimum and maximum values that came from //the accelerometer while standing still //You very well may need to change these int minVal = 265; int maxVal = 402; //to hold the caculated values double x; double y; double z; void setup(){ Serial.begin(9600); } void loop(){ //read the analog values from the accelerometer int xRead = analogRead(xPin); int yRead = analogRead(yPin); int zRead = analogRead(zPin); //convert read values int xAng = map(xRead, int yAng = map(yRead, int zAng = map(zRead, to degrees -90 to 90 minVal, maxVal, -90, minVal, maxVal, -90, minVal, maxVal, -90, - Needed for atan2 90); 90); 90);

//Caculate 360deg values like so: atan2(-yAng, -zAng) //atan2 outputs the value of - to (radians) //We are then converting the radians to degrees x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI); //Output the caculations Serial.print("x: "); Serial.print(x); Serial.print(" | y: "); Serial.print(y); Serial.print(" | z: "); Serial.println(z);

delay(100);//just here to slow down the serial output - Easier to read }

ADXL335 Acelermetro (eje X, Y, Z)


Este diminuto componente de baja potencia, es un acelermetro analogico completo de 3 ejes con seales reguladas de voltaje de salida. Mide la aceleracin con una escala de 3G y utiliza un nivel de tensin de 3.3 V. Se puede medir la aceleracin de la gravedad esttica en aplicaciones de deteccin de inclinacin, as como la aceleracin dinmica resultante del movimiento, choque o vibraciones. ( hoja de datos ). Que es un Acelermetro? Un acelermetro es un es un tipo de sensor analogico transductor que detecta el movimiento o el giro, es decir, es capaz de responder con una seal elctrica ante una perturbacin inducida por la aplicacin de una fuerza o la gravedad. Este dispositivo es capaz de detectar si est en horizontal o vertical o por ejemplo si los agitamos en el aire.

El ADXL335 proporcionar a Arduino, tres voltajes proporcionales a la aceleracin de cada eje X, Y y Z. Algo muy importante es que se trata de un dispositivo que se alimenta a 3.3 V, por lo que hay que tener cuidado al conectarlo a nuestro Arduino de no hacerlo a la salida de 5v, ya que daaramos el sensor. Conexion:

Codigo: const int xPin = 0; const int yPin = 1; const int zPin = 2; //Pines analogicos de lectura

int minVal = 265; // Valores mnimos y mximos del acelerometro en reposo int maxVal = 402; double x; double y; double z; // para guardar los valores calculados

void setup ( ) { Serial.begin(9600); } void loop ( ) { int xRead = analogRead(xPin); //Lee los valores analogicos del acelerometro int yRead = analogRead(yPin); int zRead = analogRead(zPin); // mapea los valores leidos a un rango -90 a 90 grados (- a ) int xAng = map(xRead, minVal, maxVal, -90, 90); int yAng = map(yRead, minVal, maxVal, -90, 90); int zAng = map(zRead, minVal, maxVal, -90, 90); //Convertimos los radianes a grados x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI); //Imprimimos en el monitor serial los caluculos Serial.print(x: );

Serial.print(x); Serial.print( | y: ); Serial.print(y); Serial.print( | z: ); Serial.println(z); delay(100); } //Espera 1 decima de segundo

This example is for Wiring version 1.0 build 0100+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know. Acceleromenter ADXL335 Sparkfun breakout by BARRAGAN http://barraganstudio.com Reads values from an accelerometer with X, Y and Z connected to analog inputs 0, 1 and 2 respectively The values are printed to the serial monitor

int x, y, z; void setup() { Serial.begin(9600); } void loop() { x = analogRead(0); y = analogRead(1);

// sets the serial port to 9600

// read analog input pin 0 // read analog input pin 1

z = analogRead(2); // Serial.print("accelerations Serial.print(x, DEC); // Serial.print(" "); // Serial.print(y, DEC); // Serial.print(" "); // Serial.println(z, DEC); // delay(100); // }

read analog input pin 1 are x, y, z: "); print the acceleration in the X axis prints a space between the numbers print the acceleration in the Y axis prints a space between the numbers print the acceleration in the Z axis wait 100ms for next reading

Das könnte Ihnen auch gefallen