Your phone has an accelerometer in it that lets it measure acceleration in different directions, which can help the phone determine its own orientation. You can see this in action when using the compass in your maps app or when using motion controls in a game. However, you can also add this same capability to your Arduino project with a simple sensor.
Accelerometers are remarkably tiny sensors that use micro-machined structures to measure the acceleration that the sensor is feeling along three axes, denoted by X, Y, and Z. When the sensor moves in a certain direction, it returns a value for how fast it moved along all its axes, measured in meters per second squared.
Since the acceleration measured by the sensor includes the force that gravity is exerting on it—and since we know that the acceleration of gravity (barring any other forces applied to an object) is 9.8m/s/s—this lets the sensor determine which direction its facing. While at rest, the sensor will return no acceleration forces on two of its axes, but one axis should be feeling acceleration of 9.8m/s/s. This is also sometimes referred to as 1 “g” of force.
This is why your phone can silence itself when turned face down. The accelerometer allows the device to know which way is down and how fast it’s moving in a given direction. So, when your phone is facing upwards, the accelerometer is feeling 1g of force in one direction, but when you flip the phone over, it’s now feeling 1g of force in the opposite direction along the same axis.
This same principle can be used in your Arduino project to detect what orientation the sensor is in, and act accordingly based on that data. This guide to Arduino accelerometer sensors will show you how to wire up an DIY accelerometer and read the sensor data it returns. There are several kinds of accelerometer sensors, but we’ll be using an ADXL345 sensor.
We’ll also be using two Adafruit libraries: Adafruit ADXL345, and Adafruit Unified Sensor. To include them, in your Arduino IDE, head to Tools > Manage libraries… and search for both of the bolded library names above and click Install on each.
Since there are several variations of accelerometer sensors for Arduino, your supply list might look slightly different than the one here, but here’s what you’ll need for our specific guide using the ADXL345 sensor.
An Arduino Uno | ![]() |
ADXL345 accelerometer sensor | ![]() |
Wires | ![]() |
USB cable | ![]() |
Arduino IDE | ![]() |
Once you have everything you need, review the code in the next section and upload it to your Arduino before moving on to the wiring.
For this Arduino accelerometer project, we’re simply going to connect the accelerometer and read its output using the Serial monitor. With this data, you can program your Arduino to take different actions based on its orientation. First, here’s the full code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include #include #include Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(); void setup(void) { Serial.begin(9600); if(!accel.begin()) { Serial.println("ADXL345 sensor not detected."); while(1); } } void loop(void) { sensors_event_t event; accel.getEvent(&event); 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); } |
Next, let’s go through each section of the code to see how it works.
1 2 3 |
#include #include #include |
In these lines, you’ll include the three libraries you need for this sketch. If you try to compile and upload the sketch and these aren’t found, double check your library manager (see above).
1 2 3 4 5 6 7 8 9 10 11 |
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(); void setup(void) { Serial.begin(9600); if(!accel.begin()) { Serial.println("ADXL345 sensor not detected."); while(1); } } |
In the setup section, we’ll create one simple conditional to test whether the sensor is detected properly. If it’s not, the Serial monitor will return an error.
1 2 3 4 5 6 7 8 9 10 11 |
void loop(void) { sensors_event_t event; accel.getEvent(&event); 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); } |
Finally, in the loop section, we’ll create an event using the sensor library that will store the acceleration values for the X, Y, and Z axes from your DIY accelerometer. Then, it will return those values, measured in m/s^2.
Finally, this section will pause for 500 milliseconds before checking the accelerometer position again. This should make it a little easier to read, although you can adjust this for any Arduino accelerometer project you use this sensor in, if you want to make your device respond more quickly than half a second.
To wire up the ADXL345 sensor, you won’t need much. Just a few basic wires. To connect your sensor to your Arduino, follow these steps:
For the SCL and SDA pins, you’ll find them at the far end of the row of digital pins on your Arduino. You might have to look on the underside of the Arduino to find these labels, or check this diagram.
Once everything is hooked up, open the Serial monitor in your Arduino IDE by clicking Tools > Serial Monitor. You should now see values for the movement on each axis, with one axis constantly returning around ~10m/s^2 while at rest. Try using this information to turn an LED on when the sensor is in one orientation, and turn it off when you flip the sensor over.