Getting a servo motor to work with esp8266

i'm trying to connect a servomotor with esp8266MOD 12-f and the "ESP32Servo.h" library but i get error codes that i dont know how to solve.
this is the code:

#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo

// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27, 32
int servoPin = 13;

void setup() {
  myservo.setPeriodHertz(50); 
  myservo.attach(servoPin);
}
 
void loop() {
  myservo.write(0); 
  delay(2000);
  myservo.write(180);
  delay(2000);
}

and the error message i am getting:

Sketch uses 257245 bytes (19%) of program storage space. Maximum is 1310720 bytes.
Global variables use 21580 bytes (6%) of dynamic memory, leaving 306100 bytes for local variables. Maximum is 327680 bytes.
esptool.py v4.5.1
Serial port COM5

A serial exception error occurred: Cannot configure port, something went wrong. Original message: PermissionError(13, 'Un p�riph�rique attach� au syst�me ne fonctionne pas correctement.', None, 31)
Note: This error originates from pySerial. It is likely not a problem with esptool, but with the hardware connection or drivers.
For troubleshooting steps visit: Troubleshooting - ESP32 - — esptool.py latest documentation
Failed uploading: uploading error: exit status 1

Citation

The code is not the problem, the problem is the communication with the ESP.
Try pressing the "flash" or "ioi" button on the board (if it has one) before uploading the .

1 Like

Thanks I'll try that

no it doesent work, still the same error message

Can you upload the blink example sketch successfully? If not, that proves the problem is not your code (although there could be problems with your code, but right now they are irrelevant!)

what do you mean by the blink example sketch? I have already tried with the example sketch that come in the folder but i get the same problem

Can you upload a photo of the module and adapter you are using, please?



Try a servo library compatible with ESP8266 instead (as opposed to ESP32Servo.h).

Wasn't it easier to say "NodeMcu ESP-8266? :wink:

Try pressing reset and flash, holding flash until "uploading..." appears

got a new error message now

WARNING: library ESP8266_ISR_Servo claims to run on esp8266 architecture(s) and may be incompatible with your current board which runs on esp32 architecture(s).
In file included from c:\Users\eytan kars\OneDrive\Documents\Arduino\libraries\ESP8266_ISR_Servo\src/ESP8266_ISR_Servo.h:48,
                 from C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino:13:
c:\Users\eytan kars\OneDrive\Documents\Arduino\libraries\ESP8266_ISR_Servo\src/ESP8266_ISR_Servo.hpp:49:4: error: #error This code is designed to run on ESP8266 platform! Please check your Tools->Board setting.
   #error This code is designed to run on ESP8266 platform! Please check your Tools->Board setting.
    ^~~~~
In file included from c:\Users\eytan kars\OneDrive\Documents\Arduino\libraries\ESP8266_ISR_Servo\src/ESP8266_ISR_Servo.hpp:86,
                 from c:\Users\eytan kars\OneDrive\Documents\Arduino\libraries\ESP8266_ISR_Servo\src/ESP8266_ISR_Servo.h:48,
                 from C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino:13:
c:\Users\eytan kars\OneDrive\Documents\Arduino\libraries\ESP8266_ISR_Servo\src/ESP8266FastTimerInterrupt.h:46:4: error: #error This code is designed to run on ESP8266 platform! Please check your Tools->Board setting.
   #error This code is designed to run on ESP8266 platform! Please check your Tools->Board setting.
    ^~~~~

exit status 1

Compilation error: exit status 1

sill does not work :sob:

Did you select an appropriate ESP8266 core (board) ?

I don't konw, I'm new with arduino I have ESP32 dev module selected, it is the one used on video i used. I imagine that is why it does'nt work

Time to change that.
The board selected has to be the board that you are using.

But you have nodemcu esp-8266, not ESP32 DEV!

thanks got it. Got one step further, not i got another problem :sweat_smile:

C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino:15:1: error: 'Servo' does not name a type
   15 | Servo myservo;  // create servo object to control a servo
      | ^~~~~
C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino: In function 'void setup()':
C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino:20:3: error: 'myservo' was not declared in this scope
   20 |   myservo.attach(2);  // attaches the servo on GIO2 to the servo object
      |   ^~~~~~~
C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino: In function 'void loop()':
C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino:28:5: error: 'myservo' was not declared in this scope
   28 |     myservo.write(180);              // tell servo to go to position in variable 'pos'
      |     ^~~~~~~
C:\Users\eytan kars\AppData\Local\Temp\.arduinoIDE-unsaved20231012-22812-cefv25.31cnu\sketch_nov12a\sketch_nov12a.ino:32:5: error: 'myservo' was not declared in this scope
   32 |     myservo.write(360);              // tell servo to go to position in variable 'pos'
      |     ^~~~~~~

exit status 1

Compilation error: 'Servo' does not name a type

yes that part is solved

Is that from uploading an example sketch that came with that library?
Or did you just change the library name in that ESP32 sketch?

But this code is for ESP32, not for ESP8266

Try

#include <Servo.h>
Servo myservo; // create servo object to control a servo

int servoPin = D7;  // for ESP8266 microcontroller

void setup() {
  myservo.setPeriodHertz(50); 
  myservo.attach(servoPin);
}
 
void loop() {
  myservo.write(0); 
  delay(2000);
  myservo.write(180);
  delay(2000);
}