Arduino Tutorials – Lesson 26 – Accelerometer sensor with servo motor control using Arduino UNO

Creating accelerometer sensor program using Arduino UNO

Required Components

  1. Accelerometer sensor (MPU 6050) -1 no
  2. Arduino UNO board -1 no
  3. Jumper cable -4 no
  4. Servo motor -1 no

Circuit

Steps

  1. Make sure the components are working properly.
  2. Connect the Arduino UNO pin SDA & SCL to accelerometer sensor SDA & SCL pin.
  3. Connect the servo motor data pin to the Arduino UNO pin 2.
  4. Connect the +5v and ground connections respectively.
  5. Check the Arduino program.
  6. Check the Circuit Connections.
  7. Run the Arduino program.

Arduino Program

#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>   
Servo sg90;          
int servo_pin = 2;
MPU6050 sensor ;
int16_t ax, ay, az ;
int16_t gx, gy, gz ;

void setup ()
{ 
  sg90.attach ( servo_pin );
  Wire.begin ( );
  Serial.begin  (9600); 
  Serial.println  ( "Initializing the sensor" ); 
  
  sensor.initialize ( ); 
  Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed"); 
  delay (1000); 
  Serial.println ( "Taking Values from the sensor" );
  delay (1000);
}

void loop () 
{ 
  sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
  ax = map (ax, -17000, 17000, 0, 180) ;
  Serial.println (ax);
  sg90.write (ax); 
  delay (200);
}

Usage

  1. Mobile phones
  2. Drone camera stabilization
  3. To detect faults in rotator machine
  4. To display images in an upright position on screens of digital cameras

Leave a Reply

Your email address will not be published. Required fields are marked *