Control Servo Motor Using Potentiometer

by CodeChamp in Circuits > Electronics

6803 Views, 30 Favorites, 0 Comments

Control Servo Motor Using Potentiometer

Main.png
28.jpg
21.jpg

Hello Makers,

This Instructable is part of a series of Instructables with the NodeMCU, my previous Instructables shows you how to Interfacing Servo with NodeMCU and lots more. So make sure, you check that first before trying out this. So, let's get started !!

Components Needed

12.jpg

All that you need is:

Hardware Required

  • NodeMCU
  • 10K POT
  • Servo Motor
  • Bread Board
  • Male Header pins
  • Micro USB cable
  • Connecting Wires

Software Required

  • Arduino IDE (with ESP8266 Library installed)

Description

4.jpg
0.jpg
3.jpg
5.jpg
1.jpg
2.jpg
13.jpg

About Potentiometer

  • A potentiometer is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider.
  • Potentiometers are commonly used to control electrical devices such as volume controls on audio equipment.
  • Potentiometers operated by a mechanism can be used as position transducers, For example: In a joystick

Generally Potentiometer is called as variable resistors that change its resistance value (in ohms) when the rotating contact is turned/adjusted.

Therefore, using NodeMCU, we can control the servo arm to a specified position by turning the potentiometer.

As simple as that!

Soldering

6.jpg
7.jpg
8.jpg
9.jpg
10.jpg
11.jpg

Solder Male Header pins to the Potentiometer, so it makes the connection to bread board easy.

Circuit Wiring

Circuit.png
14.jpg
15.jpg
16.jpg
17.jpg
18.jpg
19.jpg
20.jpg
26.jpg
25.jpg
24.jpg
27.jpg

Servo connections:

The Orange wire connects to Digital pin D4.

The Brown wire connects to GND pin.

The red wire connects to 3v3 pin.

Pot connections:

The first pin is connected to the 3v3 pin.

The second pin is connected to Analog input A0 pin.

The third pin is connected to GND pin.

Coding Time

Code.PNG

Make sure you include the Arduino Servo library.

CODE:

#include <Servo.h>
Servo servo;  // create servo object to control a servo
int pot = 0;  // analog pin used to connect the potentiometer<p>int temp;     // temporary variable to read the value from the analog pin</p>
void setup() {<p>  Serial.begin(9600);</p><p>  servo.attach(4);  // D2</p><p>  servo.write(0);</p><p>}</p>
void loop() {<p>  temp = analogRead(pot);            // reads the value of the potentiometer (value between 0 and 1023)</p><p>  Serial.println(temp);</p><p>  temp = map(temp, 0, 1023, 0, 180);  // scale it to use it with the servo (value between 0 and 180)</p><p>  servo.write(temp);                 // sets the servo position according to the scaled value
  delay(50);                         // waits for the servo to get there
}</p>

Download the "POT_NodeMCU.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.

You can tinker with it if you like based on the application, or just use it as it is.

Downloads

Output

22.jpg
POT_Servo

Using Serial monitor you can check the output from the analog pin connected to POT.

As you turn the rotating contact the servo arm is turned.

That's all makers!

I hope you liked this, Stay Tuned for more Projects!