This tutorial of Robo India explains how to control servo on NodeMCU using Arduino IDE.
1. Introduction:
A servo is an actuator that rotates to a precise angle through command. The servo example included in this chapter rotates between 0 to 180 degree. It can move to any angle between 0-180 degree.
A servo receives command from the NodeMCU, moves to the commanded angle and stops there. A servo has three interface in which two are for power supply and one is for signal input.
This tutorial is for NodeMCU on Arduino IDE. Please note that the same tutorial can be performed on LUA as well.
1.2 Hardware Required:
S.No. | Item | Quantity |
1 | NodeMCU | 1 |
2 | Breadboard | 1 |
3 | Servo Motor | 1 |
4 | Jumper wire male to male | 3 |
2. Building Circuit
Schematic Diagram:
Circuit Layout:
3. Programming:
Once the circuit part is done, NodeMCU is needed to be programmed. Here is the code to run this circuit on NodeMCU.
You may download this code (Arduino Sketch) from here.
#include <Servo.h> // including servo library. Servo servo_1; // Giving name to servo. void setup (){ servo_1.attach(0); // Attaching Servo to D3 } void loop(){ servo_1.write (45); // Servo will move to 45 degree angle. delay (1000); servo_1.write (90); // servo will move to 90 degree angle. delay (1000); }
4. Output
As the code executes, servo comes to 45 degree from whatever angles it was. Then there is a delay of one second. After that it goes to 90 degree stays there for one second and come back to 45 degree. This operation is performed in a continuous loop. Try this code for different angles.
5. Troubleshooting
Make connections and code as mentioned above, the servo should work as mentioned in the output. If still not working try changing the servo. It is recommended to use an external power supply while operating a servo. A computer’s USB power supply may not supply adequate current.
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team
Robo India
https://roboindia.com