Control Servo Motor Using Accelerometer

Lets begin with another cool instructable. In this Instructable we will learn how to control the position of Servo Motor using Accelerometer with NodeMCU.

Step 1: Things to Be Collected

To begin with this instructable all you need is :

Hardware Requirement

  • ADXL335 : Accelerometer Sensor
  • NodeMCU
  • Servo Motor
  • Connecting Wires ( Optional )
  • Bread Board
  • Jumper Wires
  • Micro USB Cable

Software Requirements

  • Arduino IDE

Step 2: Description

Servo motors are great devices that can turn to a specified position.

Usually, they have a servo arm that can turn 180 degrees. Using NodeMCU, we control a servo to go to a specified position. As simple as that!

ADXL335 is 3v compatible device. It has three outputs for each axis, i.e. X, Y & Z these are analog outputs.

In my previous Instructables you would have seen how to connect a servo motor and then how to turn it to different positions and how to interface accelerometer.

If you didn’t see my previous Instructable, then you can click the link below to check those Instructables.

Interfacing Servo Motor With NodeMCU by TheCircuit

Interface Accelerometer With NodeMCU by TheCircuit

Step 3: Circuit Connections

Connection to Servo

Orange wire connects to Digital Pin D2.

Brown wire connects to GND Pin.

Red wire connects to 3v3 Pin.

Connections to Accelerometer

VCC – To be connected to NodeMCU +3.3v.

X – To be connected to Analog Pin A0.

GND – To be connected to Ground Pin (GND).

To get started with coding you need to

  • Download Arduino IDE
  • Setup NodeMCU

Step 4: Coding Time

/* Control Servo Motor using Accelerometer
   By TheCircuit*/

#include <Servo.h> // add servo library which is present in Arduino IDE

Servo myservo;     // create servo object to control a servo
int xpin = A0;     // analog pin used to connect the Accelerometer
int pos;           // variable to read the value from the analog pin</p>
void setup() {
 myservo.attach(2);  // D4
}
void loop() {

  pos = analogRead(xpin);           // reads the value of the accelerometer (value between 0 and 1023)
  pos = map(pos, 380, 555, 0, 180); // scale it to use it with the servo (value between 0 and 180)
  myservo.write(pos);               // sets the servo position according to the scaled value
  delay(15);                        // waits for the servo to get there
}

Download the “Accelerometer_Servo.ino” file and open it up in the Arduino IDE.

Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload.

The code will take a few minutes to upload and then you will see the servo changing angle from  to 180° at the interval when the accelerometer is tilted in X-axis.

You can also choose Y and Z axis by connecting to analog pin A0.

Step 5: Output Demonstration

Spending a bit more time on tweaking the X-axis constants would give us a better result.

That’s it for now Makers.
Thanks for your time. Don’t forget to leave your thoughts in the comments section.

Source: Control Servo Motor Using Accelerometer


About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

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

Scroll to Top