Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Servo motor interfaced with nodemcu esp8266 12e: Controlling servo motor over WiFi

By EG Projects July 30, 2021

This tutorial is about interfacing a servo motor with nodemcu esp8266 WiFi module. A web page is served by the nodemcu which enlists the servo motor controls. User can connect to nodemcu esp8266 and access the web page. To open the web page in browser user has to type the HTTP address of the server serving the web page. There are two types of servo motors ac and dc servo motors. Ac servos work on alternating current and consumes lot of power. Lot of power means they can pick heavier loads. Ac servos are used in industrial applications. Ac servos can move their shaft precisely to any angular or linear position. They have a controller with feed back mechanism which is used for adjusting the control variables. Dc servo motors on the other hand are popular in medium applications like toys etc. Ac and dc servo motors can rotate forward and backward with 360 rotation. One can control the speed and velocity of servo motors easily using any servo motor controller available in market.   

Servo motor in the tutorial

In this tutorial i am going to use dc servo motor. Dc servo motors are popular among the diy circuit makers. They are also used in toys and are know as RC servo motors. Rc servo motors are small, cheap and easy to use with systems involving microcontrollers or chips(Used in Toys) dedicated for a singular purpose. RC servos can rotate only 180 degree. Their purpose is to provide precise location in 0 to 180 degree angular field. Rc servo motor which i am going to use in the project is tower pro micro servo sg90. Sg90 servo motor works on 4.8 volts. Small torque produced by sg90 at 4.8 volts can displace a load of 1.8 kg per cm. sg90 servo motor shaft rotation depends on pwm signal frequency and duty cycle. Pwm frequency requirement for most RC servo motors is 50 Hz. They can rotate between 0 to 180 degree on pwm signal duty cycle between 1 milli seconds to 2 milli seconds. 1 milli second duty cycle at 50 hz frequency moves the servo shaft to 0 degree angle. 1.5 ms moves to 90 degree and 2 ms to 180 degree. You can calculate the duty cycles for other angles by your self. For example for 45 degree shaft rotation the duty cycle will be 45/180= 0.25 so 1(0 degree)+0.25= 1.25 ms.

Sg90 servo motor pwm signal requirements duty cycle and frequency

Sg90 servo motor pwm signal requirements duty cycle and frequency
Tower pro sg90 servo motor picture and pin out is given below.  Tower pro servo motor has 3 pins. Two are power pins and the third one is the pwm control pin. You can to connect the red lead to +5 volts. Make black ground. Connect the yellow lead with the pwm signal of the controller you are using in the project.

Picture

tower pro sg90 servo motor

Project circuit diagram

Nodemcu esp8266 works on 3.3 volts and servo motor requires 5 volts for its operations. So we need different power supplies for both the peripherals. Nodemcu is powered by the usb port of the computer and servo is powered with an another 5 volt adopter. GPIO-2 or D4 pin of nodemcu esp8266 12e is outputting pwm signal for servo motor rotation. Pwm signal output by the nodemcu is also in 3.3v TTL form. It must also be raised to 5v TTL before feeding the servo motor. I used a pair of transistors for this purpose. First transistor raises the pwm signal to 5 volts but inverts the signal. First transistor output is feed to input/base of the second transistor. Second transistor again inverts the signal and brings it back to the original shape of origination with TTL level raised to 5 v. Note the ground of both the nodemcu and 5 volt power supply are common grounded in the circuit.        

Servo motor with nodemcu esp8266 WiFi module

Servo motor with nodemcu esp8266 WiFi module
Project code is written in arduino ide. Luckily arduino has plenty of libraries for each function it offers. For interfacing servo with arduino or boards supported by arduino ide their is a pre defined library named “Servo.h”. I included this library in my code and called its functions in code which are pretty easy to use. First the ‘ESP8366WiFi.h’ library us imported in code. This library contains functions used to initialize the nodemcu server and start the nodemcu esp8266 WiFi. Then ‘Servo.h’ library us imported. After it please enter the SSID and Password of the WiFi network to which you want your nodemcu to be connected to. I hope you are making this diy project at home, in this case you have to connect your nodemcu with your home WiFi router.  

const char* ssid = “Your SSID”;
const char* password = “Your Wifi Password”;  

Enter the SSID and Password in block quotes. In the setup function arduino serial communication channel is opened at 115200 bps baud rate. After wards nodemcu pwm(pulse width modulation) output pin is defined. Then the nodemcu request to your WiFi for an IP allotment and starting the server code statements are written.
Just make the circuit and upload the above code in your nodemcu WiFi module. Before uplaoding make sure the correct board is selected in the boards manage. After upload open the serial monitor of nodemcu. As soon as you open the serial monitor you will see nodemcu requesting your router for an IP allotment. After allotment of IP nodemcu will start its server and prints the server address on the serial monitor. This address is actually the address of the web page which has servo motor controls. You have to enter this address in your browser. If you see nothing in the serial monitor or just arbitrary and in complete characters look for the baud rate in serial monitor window. It must be set to 115200. Check for all possible lose connections.    

Picture

Note: Both the server(nodemcu) and client(mobile, desktop, laptop and notebook) must be connected to the same WiFi network. In order to communicate with each other. If any one is on other network you will be unable to load the web page in browser.
When you get the server address in arduino serial monitor window like the one given above. You have to enter it in your browser. If your server and client is on the same network you will see the control page in your browser. Control page controls are given below.

Servo motor with nodemcu web page control buttons

Servo motor with nodemcu esp8266 WiFi module web page control buttons
Control page shows the current status of the servo motor shaft. It also contains 3 buttons You can rotate the servo motor shaft at these three defined angles. What happens when you press the button? Loop function in code is continuously running. It is waiting for client request. When a request arrives it checks the contents of the request decodes them and performs the function as requested by the client(Moves the servo motor shaft). Before closing the connection loop function replies back the client with the current status of the servo motor shaft.
Future Work
This tutorial is limited to three angles only. For future one can define a text box in HTML page and takes the shaft rotation angle as input from user and rotates the shaft as inputted. This can be done using HTML forms and submit button.  
Download the project code. Folder contains arduino project .ino file. Code is open source. One can edit and modify it according to his needs. Please provide us your feed back on the project.
Codes/Files

You may also like:


  • What are the top 4G modems available in India?

  • What are the top development boards for AI and ML?

  • What is LoRa and LoRaWAN?

  • What are LoRa gateways and what types are available?

  • What is the LoRaWAN network and how does it work?

  • What is IP geofencing?

Filed Under: Electronic Projects, ESP8266

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe