echo '' ;

Archives

Embedded Sensor – ADXL345

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.

Read more… →

Arduino Project – Servo Control Using Accelerometer ADXL345

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

Components Needed:

  • Arduino IDE (Web or Installed on the machine)
  • PC
  • Servo Motor
  • ADXL345 Accelerometer Sensor
  • Arduino Board (e.g., Arduino Uno)
  • Jumper wires
  • Breadboard

Connection Diagram

Arduino Project Servo Control Using Accelerometer ADXL345
Arduino Project Servo Control Using Accelerometer ADXL345

Wiring

  • 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

Code

/* 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

  1. Power up your Arduino board.
  2. Tilt the ADXL345 accelerometer in different directions to observe changes in the accelerometer data on the Serial Monitor.
  3. 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.

NEXT

Arduino-Get Start
Arduino Interface
Arduino Interface-LED
Arduino Interface-Button
Arduino Interface -Buzzer
Arduino Interface-ADC
Arduino Interface-UART(Serial)
Arduino Interface-PWM
Arduino Interface-RGB LED
Arduino Interface-LCD
Arduino Tutorials
Random Number Generator
Voltage Measurement
Arduino Projects
Therimine
Water Flow Meter
Servo Control Using Accelerometer ADXL345
Others
Arduino-Course
Arduino-Sitemap
Arduino-FAQ

ESP8266 NodeMCU Interface – ADXL345

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.

Read more: ESP8266 NodeMCU Interface – ADXL345

ADXL345

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

LineCodeExplanation
1sda = 2Assigns pin 2 to the variable sda, representing the Serial Data (SDA) pin.
2scl = 1Assigns pin 1 to the variable scl, representing the Serial Clock (SCL) pin.
4print("ArunEworld")Prints the message “ArunEworld” to the console.
7adxl345.init(sda, scl)Initializes the ADXL345 sensor with the SDA and SCL pins defined earlier.
10tmr.delay(3000000)Delays the execution of the program for 3 seconds (3000000 microseconds).
13print(adxl345.read())Reads data from the ADXL345 sensor and prints it to the console.
16function ADXL_read() ... endDefines a function named ADXL_read to read accelerometer data from the ADXL345 sensor.
19tmr.alarm(0, 1000, tmr.ALARM_AUTO, ADXL_read)Sets up a timer to call the ADXL_read function every 1000 milliseconds (1 second) repeatedly.

NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 NodeMCU Project – Home Automation
Others
NodeMCU All Post
Sitemap