logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

How to detect motion in your Arduino with an accelerometer

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at November 12, 2021
Categories
  • Arduino
Tags
How to detect motion in your Arduino with an accelerometer

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.

How to detect motion in your Arduino with an accelerometer

How Accelerometers Work

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.

What You’ll Need

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 Arduino Uno
ADXL345 accelerometer sensor ADXL345 accelerometer sensor
Wires Wires
USB cable USB Cable
Arduino IDE 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.

The Code

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.

The Wiring

Work Desk Image

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:

  • Connect the 3v3 pin on the sensor to the 3.3V pin on your Arduino.
  • Connect the GND pin on the sensor to GND on your Arduino.
  • Connect the SCL pin on the sensor to the SCL pin on your Arduino.
  • Connect the SDA pin on the sensor to the SDA pin on your Arduino.

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.

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • How to Use Map Function Using ArduinoHow to Use Map Function Using Arduino
  • Using Sensors with the Arduino: Create a Simple Device to Detect Movement and ReactUsing Sensors with the Arduino: Create a Simple Device to Detect Movement and React
  • How to Build a Motion Activated LightHow to Build a Motion Activated Light
  • How to control several lights with a joystick and an ArduinoHow to control several lights with a joystick and an Arduino
  • Use an Arduino to find out when to turn on your humidifierUse an Arduino to find out when to turn on your humidifier
  • How to Send IR Commands to Your Project with a Remote ControlHow to Send IR Commands to Your Project with a Remote Control
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort