Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Code copy-pasted and then modified from public domain, available at https://www.electronicwings.com/arduino/adxl335-accelerometer-interfacing-with-arduino-uno */
- /* Code for getting readings from an ADXL335 - accelerometer */
- #include <math.h>
- const int x_out = A1; /* connect x_out of module to A1 of UNO board */
- const int y_out = A2; /* connect y_out of module to A2 of UNO board */
- const int z_out = A3; /* connect z_out of module to A3 of UNO board */
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- int x_adc_value, y_adc_value, z_adc_value;
- double x_g_value, y_g_value, z_g_value;
- x_adc_value = analogRead(x_out); /* Digital value of voltage on x_out pin, range 0 to 1023 */
- y_adc_value = analogRead(y_out); /* Digital value of voltage on y_out pin */
- z_adc_value = analogRead(z_out); /* Digital value of voltage on z_out pin */
- Serial.print("x = ");
- Serial.print(x_adc_value);
- Serial.print("\t\t");
- Serial.print("y = ");
- Serial.print(y_adc_value);
- Serial.print("\t\t");
- Serial.print("z = ");
- Serial.print(z_adc_value);
- Serial.print("\t\t");
- //delay(100);
- /* Calculate and print the voltage value of x, y and z ; can be used to calibrate the sensor to give out correct values */
- /* double x_voltage, y_voltage, z_voltage;
- x_voltage = ( (double)((x_adc_value * 3.3) / 1023) );
- y_voltage = ( (double)((y_adc_value * 3.3) / 1023) );
- z_voltage = ( (double)((z_adc_value * 3.3) / 1023) );
- Serial.print("X voltage = ");
- Serial.print(x_voltage);
- Serial.print("\t");
- Serial.print("Y voltage = ");
- Serial.print(y_voltage);
- Serial.print("\t");
- Serial.print("Z voltage = ");
- Serial.print(z_voltage);
- Serial.print("\n\n");
- delay(2000);
- */
- /* calculate and print the readings as factors of g */
- x_g_value = ( ( ( (double)(x_adc_value * 3.3)/1023) - 1.08 ) / 0.24 ); /* Acceleration in x-direction in g units */
- y_g_value = ( ( ( (double)(y_adc_value * 3.3)/1023) - 1.08 ) / 0.240 ); /* Acceleration in y-direction in g units */
- z_g_value = ( ( ( (double)(z_adc_value * 3.3)/1023) - 1.08 ) / 0.24 0 ); /* Acceleration in z-direction in g units */
- Serial.print("X g = ");
- Serial.print(x_g_value);
- Serial.print("\t");
- Serial.print("Y g = ");
- Serial.print(y_g_value);
- Serial.print("\t");
- Serial.print("Z g = ");
- Serial.print(z_g_value);
- Serial.print("\n\n");
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement