Advertisement
Guest User

Accelerometer_1_functional

a guest
Sep 20th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Code copy-pasted and then modified from public domain, available at https://www.electronicwings.com/arduino/adxl335-accelerometer-interfacing-with-arduino-uno */
  2. /* Code for getting readings from an ADXL335 - accelerometer */
  3. #include <math.h>
  4. const int x_out = A1; /* connect x_out of module to A1 of UNO board */
  5. const int y_out = A2; /* connect y_out of module to A2 of UNO board */
  6. const int z_out = A3; /* connect z_out of module to A3 of UNO board */
  7.  
  8. void setup() {
  9.   Serial.begin(9600);
  10. }
  11.  
  12. void loop() {
  13.   int x_adc_value, y_adc_value, z_adc_value;
  14.   double x_g_value, y_g_value, z_g_value;
  15.   x_adc_value = analogRead(x_out); /* Digital value of voltage on x_out pin, range 0 to 1023 */
  16.   y_adc_value = analogRead(y_out); /* Digital value of voltage on y_out pin */
  17.   z_adc_value = analogRead(z_out); /* Digital value of voltage on z_out pin */
  18.   Serial.print("x = ");
  19.   Serial.print(x_adc_value);
  20.   Serial.print("\t\t");
  21.   Serial.print("y = ");
  22.   Serial.print(y_adc_value);
  23.   Serial.print("\t\t");
  24.   Serial.print("z = ");
  25.   Serial.print(z_adc_value);
  26.   Serial.print("\t\t");
  27.   //delay(100);
  28.  
  29.  
  30. /* Calculate and print the voltage value of x, y and z ; can be used to calibrate the sensor to give out correct values */
  31. /*  double x_voltage, y_voltage, z_voltage;
  32.   x_voltage = ( (double)((x_adc_value * 3.3) / 1023) );
  33.   y_voltage = ( (double)((y_adc_value * 3.3) / 1023) );
  34.   z_voltage = ( (double)((z_adc_value * 3.3) / 1023) );
  35.  
  36.  
  37.   Serial.print("X voltage = ");
  38.   Serial.print(x_voltage);
  39.   Serial.print("\t");
  40.   Serial.print("Y voltage = ");
  41.   Serial.print(y_voltage);
  42.   Serial.print("\t");
  43.   Serial.print("Z voltage = ");
  44.   Serial.print(z_voltage);
  45.   Serial.print("\n\n");
  46.   delay(2000);
  47. */
  48.  
  49. /* calculate and print the readings as factors of g */
  50.   x_g_value = ( ( ( (double)(x_adc_value * 3.3)/1023) - 1.08 ) / 0.24 ); /* Acceleration in x-direction in g units */
  51.   y_g_value = ( ( ( (double)(y_adc_value * 3.3)/1023) - 1.08 ) / 0.240 ); /* Acceleration in y-direction in g units */
  52.   z_g_value = ( ( ( (double)(z_adc_value * 3.3)/1023) - 1.08 ) / 0.24 0 ); /* Acceleration in z-direction in g units */
  53.  
  54.   Serial.print("X g = ");
  55.   Serial.print(x_g_value);
  56.   Serial.print("\t");
  57.   Serial.print("Y g = ");
  58.   Serial.print(y_g_value);
  59.   Serial.print("\t");
  60.   Serial.print("Z g = ");
  61.   Serial.print(z_g_value);
  62.   Serial.print("\n\n");
  63.   delay(2000);
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement