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
- Embedded Sensor – ADXL345
- Embedded Interface – Servo Control (Add Soon)
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
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
- 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.
You must be logged in to post a comment.