Project tutorial
Level Platform Using Accelerometer

Level Platform Using Accelerometer

Uses an accelerometer to level a platform.

  • 18,476 views
  • 16 comments
  • 33 respects

Components and supplies

Apps and online services

About this project

Intro

I made a earlier project using the pan/tilt kit and the Wii Nunchuk and thought it might be fun to control the pan/tilt with the accelerometer but then came up with a better idea.  Make a self leveling platform using the pan/tilt kit and an accelerometer. It's pretty basic and the code is very simple.  There is lots of room for improvement but hopefully can be some inspiration for you.

Parts used (I get a lot of my parts from Adafruit but use anything you have)

Step 1 - Acceleromter

Connect the accelerometer as follows:

  • VIN  -->  5V on breadboard 5V rail
  • GND  -->  Ground on breadboard GND rail
  • SDA  -->  Analog A4 on Arduino
  • SCL  -->  Analog A5 on Arduino

Step 2 - Servo

Connect the servos as follows:

Servo 1

  • Brown Wire  -->  Ground on breadboard GND rail
  • Red Wire  -->  5V on breadboard 5V rail
  • Brown Wire  -->  Pin 10 (Used Yellow Wire)

Servo 2

  • Brown Wire --> Ground on breadboard GND rail
  • Red Wire --> 5V on breadboard 5V rail
  • Brown Wire --> Pin 10 (Used Blue Wire)

Step 3 - Build Housing and Mount Parts

I used foam board to make a housing to hold the servos, accelerometer and Arduino.

Step 4 - Code

The code has a few variables that can be adjusted.

  • xOffset - Used to level out the platform on the x-axis
  • yOffset - Used to level out the platform on the y-axis
  • sensitivity - Used to decrease the sensitivity of the accelerometer

There are two libraries needed:

The code is pretty well commented but if you have any questions, feel free to leave a comment and I will answer as best I can.

See it in Action!

Lights, camera, action!

Final Thoughts

Other ideas:

  • Use the accelerometer to aim a laser attached to the pan/tilt
  • Use accelerometer in a robot 
  • User stepper motors for better resolution

As always, feel free to comment and hope this is entertaining and useful. Thanks for looking!

Code

AccelerometerC/C++
Upload to UNO
/*
 * Accelerator demo
 * 
 */
#include <Servo.h>
#include <Adafruit_ADXL345_U.h>

// Create servo objects for x and y servos
Servo xServo;
Servo yServo;

// Used to level the platform, if needed
int xOffset = -7;
int yOffset = -1;

// Used to decrease sensitivity
int sensitivity = 50;

// Assign a unique ID to this sensor at the same time
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);


void setup() {
      Serial.begin(9600);
  
// Initialize sensor
      if(!accel.begin())
      {
// Sensor not detected
            Serial.println("No Sensor detected");
      while(1);

      }

// Connect servos to pins
      xServo.attach(9);
      yServo.attach(10);

// Setup sensor Range and datarate
      accel.setRange(ADXL345_RANGE_16_G);
      accel.setDataRate(ADXL345_DATARATE_25_HZ);

}

void loop() {
 
      sensors_event_t event; 
      accel.getEvent(&event);

// Get x and y values from sensor
      int x = event.acceleration.x;
      int y = event.acceleration.y;

// map sensor value (-10 - 10) to servo position value (30 - 150)
      int x1 = map(x, -10, 10, 130, 50);
      int y1 = map(y, -10, 10, 50, 130);

// Troubleshoot info - show sensor reading and mapping
      Serial.print("X: "); Serial.print(x);
      Serial.print("\tY: "); Serial.print(y);
      Serial.print("\tX1: "); Serial.print(x1);
      Serial.print("\tY1: "); Serial.println(y1);

// move servos based on sensor mapping
      xServo.write(x1 + xOffset);
      yServo.write(y1 + yOffset);
      delay(sensitivity);         //delay to decrease sensitivity
}

Schematics

Schematic
Parts Layout
Schematic

Comments

Similar projects you might like

Memsic2125 Accelerometer (Mx2125)

by MicroBob

  • 3,327 views
  • 0 comments
  • 12 respects

Visual Accelerometer

Project tutorial by Reid Paulhus

  • 3,322 views
  • 1 comment
  • 10 respects

Sound Sensor Activated LEDs with LCD for Sound Level Data!

Project tutorial by Heathen_Hacks-v2

  • 1,414 views
  • 2 comments
  • 8 respects

Food Detector

Project tutorial by crispylel

  • 35,424 views
  • 7 comments
  • 91 respects

Servo-Controlled, Light-Tracking Solar Panel Platform

Project tutorial by scott mangiacotti

  • 13,576 views
  • 10 comments
  • 45 respects

Accelerometer Using Arduino 101

by Jefferson Paredes

  • 2,664 views
  • 0 comments
  • 3 respects
Add projectSign up / Login