- Connect SCL on the Metro to SCL (yellow wire) on the ADXL343
- Connect SDA on the Metro to SDA (blue wire) in the ADXL343
- Connect GND on the Metro to GND (black wire) on the ADXL343
- For 3.3V LOGIC boards: connect 3.3V on the Arduino/Metro to VIN (red wire) on the ADXL343
- For 5.0V LOGIC boards: Connect 5V on the Arduino/Metro to VIN (red wire) on the ADXL343
The final results should resemble the illustration above, showing an Adafruit Metro development board.
Installation
The Adafruit_ADXL345 library can be installed using the Arduino Library Manager, accessible through the Manage Libraries ... menu item.
Click the Manage Libraries ... menu item, search for Adafruit ADXL343, and select the Adafruit ADXL343 library:
Load Example
To make sure that everything is wired up correctly, you can run the sensorttest example available in the Adafruit_ADXL343 examples folder, loadable via the File -> Examples -> Adafruit ADXL343 -> sensortest menu item.
Upload the sketch to your board and open up the Serial Monitor (Tools->Serial Monitor). You should see some acceleration data for for X/Y/Z.
#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_ADXL343.h> #define ADXL343_SCK 13 #define ADXL343_MISO 12 #define ADXL343_MOSI 11 #define ADXL343_CS 10 /* Assign a unique ID to this sensor at the same time */ /* Uncomment following line for default Wire bus */ Adafruit_ADXL343 accel = Adafruit_ADXL343(12345); /* NeoTrellis M4, etc. */ /* Uncomment following line for Wire1 bus */ //Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1); /* Uncomment for software SPI */ //Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_SCK, ADXL343_MISO, ADXL343_MOSI, ADXL343_CS, 12345); /* Uncomment for hardware SPI */ //Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_CS, &SPI, 12345); void displayDataRate(void) { Serial.print ("Data Rate: "); switch(accel.getDataRate()) { case ADXL343_DATARATE_3200_HZ: Serial.print ("3200 "); break; case ADXL343_DATARATE_1600_HZ: Serial.print ("1600 "); break; case ADXL343_DATARATE_800_HZ: Serial.print ("800 "); break; case ADXL343_DATARATE_400_HZ: Serial.print ("400 "); break; case ADXL343_DATARATE_200_HZ: Serial.print ("200 "); break; case ADXL343_DATARATE_100_HZ: Serial.print ("100 "); break; case ADXL343_DATARATE_50_HZ: Serial.print ("50 "); break; case ADXL343_DATARATE_25_HZ: Serial.print ("25 "); break; case ADXL343_DATARATE_12_5_HZ: Serial.print ("12.5 "); break; case ADXL343_DATARATE_6_25HZ: Serial.print ("6.25 "); break; case ADXL343_DATARATE_3_13_HZ: Serial.print ("3.13 "); break; case ADXL343_DATARATE_1_56_HZ: Serial.print ("1.56 "); break; case ADXL343_DATARATE_0_78_HZ: Serial.print ("0.78 "); break; case ADXL343_DATARATE_0_39_HZ: Serial.print ("0.39 "); break; case ADXL343_DATARATE_0_20_HZ: Serial.print ("0.20 "); break; case ADXL343_DATARATE_0_10_HZ: Serial.print ("0.10 "); break; default: Serial.print ("???? "); break; } Serial.println(" Hz"); } void displayRange(void) { Serial.print ("Range: +/- "); switch(accel.getRange()) { case ADXL343_RANGE_16_G: Serial.print ("16 "); break; case ADXL343_RANGE_8_G: Serial.print ("8 "); break; case ADXL343_RANGE_4_G: Serial.print ("4 "); break; case ADXL343_RANGE_2_G: Serial.print ("2 "); break; default: Serial.print ("?? "); break; } Serial.println(" g"); } void setup(void) { Serial.begin(115200); while (!Serial); Serial.println("Accelerometer Test"); Serial.println(""); /* Initialise the sensor */ if(!accel.begin()) { /* There was a problem detecting the ADXL343 ... check your connections */ Serial.println("Ooops, no ADXL343 detected ... Check your wiring!"); while(1); } /* Set the range to whatever is appropriate for your project */ accel.setRange(ADXL343_RANGE_16_G); // accel.setRange(ADXL343_RANGE_8_G); // accel.setRange(ADXL343_RANGE_4_G); // accel.setRange(ADXL343_RANGE_2_G); /* Display some basic information on this sensor */ accel.printSensorDetails(); displayDataRate(); displayRange(); Serial.println(""); } void loop(void) { /* Get a new sensor event */ sensors_event_t event; accel.getEvent(&event); /* Display the results (acceleration is measured in m/s^2) */ Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" "); Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 "); delay(500); }
You should get something resembling the following output when you open the Serial Monitor at 115200 baud:
Setting the Range
You can adjust the response range of the accelerometer by setting an appropriate value in your setup loop, using one of the lines in the code shown below:
/* Set the range to whatever is appropriate for your project */ accel.setRange(ADXL343_RANGE_16_G); // accel.setRange(ADXL343_RANGE_8_G); // accel.setRange(ADXL343_RANGE_4_G); // accel.setRange(ADXL343_RANGE_2_G);
By default, the sensor will be set to the maximum range of +/- 16g.
Text editor powered by tinymce.