Introduction: Interfacing Servo Motor With NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @makers_bee & Motivate me on YouTube @MakersBee

HelloEverybody,

This here is my first Instructable project.

So you want to get started with NodeMCU? Well, I'm here to share with you.Today, I'm going to show you how to get started with NodeMCU. Let's GO !

NodeMCU has on board ESP8266-12E makes the board suitable for IoT (Internet Of Things). In this Instructable I will show you how to get started with Servo using NodeMCU.

Step 1: Materials Required

Here is the list of components required to get started with the NodeMCU,

Hardware Components

  1. NodeMCU
  2. Servo Motor
  3. Bread Board
  4. Jumper Wires
  5. Micro USB Cable

Software Components

  1. Arduino IDE

Step 2: Hardware Description

What is a Breadboard?

Its a prototyping platform, where you can plug components and remove them easily. Please refer to the photo to see how its made inside. There is usually 2 bands on each sides indicating the power rails. Its made to easily connect all of the (-) and (+) together.

What is a Servo?

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

Usually, they have a servo arm that can turn 180 degrees. Using the NodeMCU, we can control a servo to go to a specified position. As simple as that! Here we will see how to connect a servo motor and then how to turn it to different positions.

Connection to Servo

The next job is to connect your servo motor. There are two common types of servo:

  1. White - Red - Black wired servo
  2. Orange - Red - Brown wired servo

If your servo has White - Red - Black wires, then connect it as follows

  • White wire connects to Digital pin D4
  • Black wire connects to GND pin
  • Red wire connects to 3V3 pin

If your servo has Orange - Red - Brown wires, then connect it as follows

  • Orange wire connects to Digital pin D4.
  • Brown wire connects to GND pin
  • Red wire connects to 3V3 pin

Step 3: Download Arduino IDE

To get started we need to download the Arduino IDE (Integrated Development Environment) and some necessary drivers.

  1. To download the software head over to the Arduino site: https://www.arduino.cc
  2. Click on Software Click on either Windows, Mac or Linux based on your Operating System.
  3. You can donate if you want or just download.
  4. When this is done, you will simply need to continue the steps to download it to your computer.
  5. You are done!

Step 4: Preparing the Arduino IDE

After downloading the Arduino IDE navigate to

  1. File tab and then click on Preferences.
  2. In the additional Boards Manager URLs add the following link (http://arduino.esp8266.com/stable/package_esp8266com_index.json)
  3. Click OK and then navigate to
  4. Tools - Boards - Boards Manager

In the search field type esp8266 > click the esp8266 by ESP8266 Community - Click Install

Now you have setup the Arduino IDE to work along with the NodeMCU.

Step 5: Coding Time

The next step is to write some code to control the Servo.

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

#include <Servo.h>

Servo servo;

void setup() {

servo.attach(2); //D4

servo.write(0);

delay(2000);

}

void loop() {

servo.write(90);

delay(1000);

servo.write(0);

delay(1000);

}

The code will take a few minutes to upload and then you should see the Servo changing angle from 0° to 90° at the interval set in the code.

You can tinker with it if you like, or just use it as it is.

Step 6: Upload Your Program

  1. Goto Tools
  2. Board > NodeMCU 1.0 (ESP - 12E Module)
  3. Port ( Choose the right Port )

**Make sure you have your NodeMCU model selected and the correct serial port ticked (see pics).

Then just click the Upload button**

Ready To GO