servo.ino
The esp8266 package Servo library generates the 50 Hz pulse by interrupt: Wi-Fi keeps running while the servo sweeps. The real topic is the separate supply, not the code.
Hardware required
An ESP-12E module (ESP8266EX, 2.4 GHz Wi-Fi) mounted on a NodeMCU / Wemos D1 mini board, powered over micro-USB. 1 SG90 servo + external 5 V supply (2 AA cells or a dedicated regulator).
Wiring
Signal on D1, + to the external supply, GND tied to the board. A servo pulling on 3.3 V/USB makes the module reboot in loops.
How it works
write(angle) places the pulse between 1 and 2 ms; the ESP8266 port uses timer 1 by interrupt, hence coexistence with Wi-Fi (at the cost of micro-jitters on the radio). In the Arduino IDE, install the « esp8266 » package, select « NodeMCU 1.0 (ESP-12E Module) » then Upload (hold GPIO0 LOW if flashing fails). The A0 ADC reads 0-1 V (NodeMCU divider: 0-3.3 V).
Going further
Drive the angle from the dedicated web-server sketch, or close the loop on an ultrasonic distance sensor.
servo.ino
// Servo SG90 sur D1 (GPIO5) — bibliotheque Servo (port ESP8266).
// Alimenter le servo par une source 5 V EXTERNE, masses communes.
#include <Servo.h>
Servo servo;
int angle = 0;
int pas = 7;
void setup() {
Serial.begin(115200);
servo.attach(5); // D1
Serial.println("Balayage 0-180 (le 8266 reste dispo pour le Wi-Fi)");
}
void loop() {
angle += pas;
if (angle >= 180 || angle <= 0) pas = -pas;
servo.write(angle);
if (angle % 35 == 0) Serial.printf("%d degres\n", angle);
delay(25);
}