Problem with the control of servo-motor with ESP8266

Hello there ! I’m really new to github community and this is my first post, i hope I’ll be clear (also my english may not be perfect all the time).

Here is my problem : For school, I’m working on a servo-motor controlled by an ESP8266. In fact, I need to be able to controle that from distance, so I use the ESP to create a server and wait for user to request if he want to servo-motor or not.

Here is the code i’m using :

#include ESP8266WiFi.h
#include ESP8266WebServer.h
#include Servo.h

#include "index.h";

#define LED 2
#define ServoPin 14 //D5 is GPIO14

//WiFi Connection configuration
const char *ssid = "my ssid but i removed it for security reason";
const char *password = "the same reason as my ssid";

Servo myservo;

ESP8266WebServer server(80);

void handleServo(){
  String POS = server.arg("servoPOS");
  int pos = POS.toInt();
  myservo.write(pos); // tell servo to go to position
  Serial.print("Servo Angle:");
  digitalWrite(LED,!(digitalRead(LED))); //Toggle LED
  server.send(200, "text/plane","");

void handleRoot() {
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page

void setup() {

  myservo.attach(ServoPin); // attaches the servo on GIO2 to the servo object

  //Connect to wifi Network
  WiFi.begin(ssid, password); //Connect to your WiFi router

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {

  //If connection successful show IP address in serial monitor
  Serial.print("Connected to ");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); //IP address assigned to your ESP

  //Initialize Webserver
  server.on("/setPOS",handleServo); //Sets servo position from Web request

void loop() {

Note that the library I include at the start are included with tag< and > before and after but the HTML editor don’t want me to put them.

Also, I include “index.h”, here is the code in “index.h” :

const char MAIN_page[] PROGMEM = R"=====(
title ESP8266 controle d'un servomoteur /title
meta name="viewport" content="width=device-width, initial-scale=1"
  width: 79px;
  height: 50px;
  position: absolute;
  vertical-align: middle;
  margin-top: 50px;
  margin-left: -114px;
  border: 0px none;
  background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;
  font: normal normal bold normal 20px Arial;
  text-align: center;
  color: rgb(34, 34, 34);
  padding: 0px;

  width: 79px;
  height: 50px;
  position: absolute;
  vertical-align: middle;
  margin-top: 50px;
  margin-left: -114px;
  border: 0px none;
  background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;
  font: normal normal bold normal 50px Arial;
  text-align: center;
  color: rgb(34, 34, 34);
  padding: 0px;

    padding: 5%;

  display: flex;

div style="width:100%;"
div style="width:50%; margin: 0 auto;"
  h4ESP8266 contrôle d'un servomoteur/h4

div style="width: 50%; margin: 0 auto;" class="flx"
  svg viewBox="0 0 500 500" width="250" height="250" id="mySVG" style="background:#fff; border: 1px solid black"
  path fill="none" stroke="#30D8D9" stroke-width="50" d="M 376.79805300444093 404.6682053761632 A 200 200 0 1 0 121.44247806269212 403.2088886237956" /path
  path id="arc1" fill="none" stroke="#00A8A9" stroke-width="50" style="stroke-linecap: round;"/
  text x="230" y="260" fill="#777" id="angle" class="spd">0/text
  text x="200" y="300" fill="#777" class="angle">Servo Angle/text

function sendData(pos) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
  };"GET", "setPOS?servoPOS="+pos, true);

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;

  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))

function describeArc(x, y, radius, startAngle, endAngle){

    var start = polarToCartesian(x, y, radius, endAngle);
    var end = polarToCartesian(x, y, radius, startAngle);

    var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";

    var d = [
        "M", start.x, start.y,
        "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
    ].join(" ");

    return d;

window.onload = function() {
  document.getElementById("arc1").setAttribute("d", describeArc(250, 250, 200, 220, 210));

var svg = document.getElementById("mySVG");
    pt = svg.createSVGPoint(),

  var loc = cursorPoint(evt);
  var degrees = Math.atan2(loc.x-250,loc.y-250)*180/Math.PI + 90;

  var offset = 220;

  degrees = (degrees + 90)
  degrees = degrees + offset;
  if(degrees > 360)
    degrees = degrees - 360;
  degrees = 360 - degrees;
  angle = degrees + offset;

  console.log(degrees, angle);

    document.getElementById("arc1").setAttribute("d", describeArc(250, 250, 200, offset, angle));
    var servoAng = Math.round(((angle - 220)/280) * 100);

// Get point in global SVG space
function cursorPoint(evt){
  pt.x = evt.clientX; pt.y = evt.clientY;
  return pt.matrixTransform(svg.getScreenCTM().inverse());



If needed, i putted the tag < and >
When I upload the code on my ESP8266, all seems to work fine, I can connect to WiFi, i can see the website, when I ask the website a new value for the servo and open the serial monitor, the ESP say that the information that i sent was received. But, the servo-motor don’t move at all. I tested the servo directly with an arduino and with a simple code, and the servo-motor is moving. But when I try to do the same on the ESP, even without the control by server, that doesn’t work. Of course, I tried to change the pin wich from the PWM signal is displayed, and i also changed all the power cable to be sure ! I’m working on it since a while, and it’s starting to be frustrating, that is the reason why I signed up in this forum. Here are a few image of the circuit, that could help !

I really don’t know why it’s not actually working, but if it’s not working it’s because there is an error somewhere and I wish someone could find it. The battery I’m using still have energy, and I use an buck regulator wich transform the 9V of the cell into a 5V for the servo-motor and 3.3V for the ESP8266.

Any help is welcomed and I can provide some more informations if needed !

Thanks for reading this post.


You have code which toggles an LED in handleServo(). Does the LED blink when /Setpos is invoked?

This might be a hardware issue, and if so you might get more help at someplace like

1 Like