This article is a continuation of the series on “Embedded Sensor –ADXL345” and carries the discussion on Size, Functional Diagram, Pin Configuration, Application, Features and Benefits, ADXL345 Interface accelerometer, and Projects with this accelerometer. This sensor from Analog Devices
Digital output data is formatted as 16-bit twos complement and is accessible through either an SPI (3- or 4-wire) or I2C digital interface.
This article is a continuation of the series on Arduino Project – Servo Control Using Accelerometer ADXL345 and carries the discussion on how to control the motor using the sensor.
Prerequisites
To continue with this tutorial, you must have read the below articles
Connect the VCC pin of the ADXL345 to the 3.3V pin on the Arduino.
Connect the GND pin of the ADXL345 to the GND pin on the Arduino.
Connect the SDA pin of the ADXL345 to the A4 pin on the Arduino.
Connect the SCL pin of the ADXL345 to the A5 pin on the Arduino.
Connect the signal wire of the servo motor to a digital pin on the Arduino (e.g., Pin 5).
Connect the power (VCC) and ground (GND) of the servo motor to the corresponding pins on the Arduino.
Arduino Library used List
Arduino libraries are collections of pre-written code that simplify complex tasks and allow users to easily integrate functionality into their Arduino projects. Libraries provide functions and routines that can be used to perform various tasks without the need for users to write the code from scratch. Arduino libraries are written in C or C++ and can be shared and reused by the Arduino community
/* ArunEworld - @2024
Accelerometer connection pins (I2C) to Arduino are shown below:
Arduino Accelerometer ADXL345
A5 SCL
A4 SDA
3.3V CS
3.3V VCC
GND GND
Arduino Servo No. 1
5V 5V (Red Wire)
GND GND (Black Wire)
D5 (last wire for control - might be white color)
Arduino Servo No. 2
5V 5V (Red Wire)
GND GND (Black Wire)
D6 (last wire for control - might be white color)
*/
#include <Wire.h>
#include <ADXL345.h>
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2;
ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
int x, y, z;
int rawX, rawY, rawZ;
int mappedRawX, mappedRawY;
void setup() {
Serial.begin(9600);
adxl.powerOn();
servo1.attach(5);
servo2.attach(6);
}
void loop() {
adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z
rawX = x - 7;
rawY = y - 6;
rawZ = z + 10;
if (rawX < -255) rawX = -255; else if (rawX > 255) rawX = 255;
if (rawY < -255) rawY = -255; else if (rawY > 255) rawY = 255;
mappedRawX = map(rawX, -255, 255, 0, 180);
mappedRawY = map(rawY, -255, 255, 0, 180);
servo1.write(mappedRawX);
delay(15);
servo2.write(180 - mappedRawY);
delay(15);
Serial.print(" mappedRawX = "); Serial.print(mappedRawX); // raw data with offset
Serial.print(" mappedRawY = "); Serial.println(mappedRawY); // raw data with offset
}
Testing
Power up your Arduino board.
Tilt the ADXL345 accelerometer in different directions to observe changes in the accelerometer data on the Serial Monitor.
The servo motor should move in response to the accelerometer data, adjusting its position based on the tilt of the accelerometer.
Troubleshooting
If the servo doesn’t move as expected, check the wiring and make sure all connections are secure.
Adjust the servo angle mapping and constraints in the code if needed.
Ensure that the accelerometer is correctly connected and functioning.
That’s it! You should now have a working setup where the servo motor is controlled based on the tilt data from the ADXL345 accelerometer. Feel free to experiment with the code and make modifications to suit your specific requirements.
The ESP8266 NodeMCU is a popular development board based on the ESP8266 microcontroller, which offers built-in Wi-Fi capabilities, making it ideal for Internet of Things (IoT) projects. In this project, we’ll explore how to interface the ESP8266 NodeMCU with the ADXL345 accelerometer sensor.
The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. It’s suitable for various applications, including tilt sensing, motion detection, and vibration monitoring.
By combining the ESP8266 NodeMCU with the ADXL345 sensor, we can create IoT applications that can monitor and analyze motion or vibrations remotely over Wi-Fi. This interface allows us to gather data from the accelerometer sensor and transmit it to a server, cloud platform, or display it on a web page.
Throughout this project, we’ll cover the hardware setup, including connecting the ADXL345 sensor to the ESP8266 NodeMCU, and the software implementation, which involves programming the ESP8266 NodeMCU to communicate with the ADXL345 sensor and transmit the data. With this interface, you’ll be able to leverage the power of the ESP8266 NodeMCU and the capabilities of the ADXL345 sensor to create versatile IoT applications for motion sensing and monitoring.
Code
Make sure you have the necessary libraries and setup to run this code on your hardware.
-- www.ArunEworld.com
sda = 2 -- GPIO-4
scl = 1 -- GPIO-5
print("ArunEworld")
-- Initialize the ADXL345 sensor
adxl345.init(sda, scl)
-- Delay for 3000000 microseconds (3 seconds)
tmr.delay(3000000)
-- Read data from the ADXL345 sensor
print(adxl345.read())
-- Define a function to read accelerometer data
function ADXL_read()
-- Read accelerometer data
local x, y, z = adxl345.read()
-- Print accelerometer data
print("X-axis:", x)
print("Y-axis:", y)
print("Z-axis:", z)
-- Alternatively, you can print all axes at once using the following line:
-- print(string.format("X = %d, Y = %d, Z = %d", x, y, z))
end
-- Set up a timer to call the ADXL_read function every 1000 milliseconds (1 second)
tmr.alarm(0, 1000, tmr.ALARM_AUTO, ADXL_read)
Code Explanation
Line
Code
Explanation
1
sda = 2
Assigns pin 2 to the variable sda, representing the Serial Data (SDA) pin.
2
scl = 1
Assigns pin 1 to the variable scl, representing the Serial Clock (SCL) pin.
4
print("ArunEworld")
Prints the message “ArunEworld” to the console.
7
adxl345.init(sda, scl)
Initializes the ADXL345 sensor with the SDA and SCL pins defined earlier.
10
tmr.delay(3000000)
Delays the execution of the program for 3 seconds (3000000 microseconds).
13
print(adxl345.read())
Reads data from the ADXL345 sensor and prints it to the console.
16
function ADXL_read() ... end
Defines a function named ADXL_read to read accelerometer data from the ADXL345 sensor.
19
tmr.alarm(0, 1000, tmr.ALARM_AUTO, ADXL_read)
Sets up a timer to call the ADXL_read function every 1000 milliseconds (1 second) repeatedly.
You must be logged in to post a comment.