esp8266servo - JamesNewton/JamesNewton.github.io GitHub Wiki
2019/03/16 San Diego CoLab
Lights are great, but to really control the world, we need to make stuff move!
The shaft of a servo will move to a position that matches the width of the pulse it is sent. A pulse of 1mS will rotate the shaft all the way in one direction, and a pulse of 2mS will move to the other end. A 1.5mS pulse will center the shaft. You can send a pulse about 50 times a second. But the Arduino system does all that for you.
What's in a servo?
https://www.youtube.com/watch?v=ZZhuD78BLDk
To connect a servo, you need three pins:
- Ground (brown or black wire)
- Power (5 to 6 volts, on the red or center wire)
- Pulses from a GPIO pin on the Arduino. (usually a yellow or orange wire)
To connect a servo to the NodeMCU, we can use these 3 row by 4 headers:
https://www.mouser.com/ProductDetail/200-TSW10407TT (the picture is wrong, it has 3 rows) and solder them on. video. To learn to solder, we can solder up a maker badge
- Solder the bottom row to the D0-D3 pins on the NodeMCU.
- Solder a red wire to each pin in the center row and the other end to the "Vin" pin.
- Solder a black wire to each pin in the top row and the other end to a "GND" pin.
P.S. For the standard Arduino, or anything with female headers, you can use this hack instead:
https://www.youtube.com/watch?v=Zf5mRlO-N38
Based on the LED pulse code from the prior session
#include <Servo.h> //Bring in the servo library
Servo myServo; //deliberately
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
myServo.attach(D0); // attaches the servo on D0 / GPIO16
}
#define HIGHEST 180 //RC Servos usually move about 180 degrees
#define LOWEST 0
void loop() { //arduino.cc/en/Reference/For
for (int i=LOWEST; i<HIGHEST+1; i++) { //i will go from low to high
myServo.write(i);
delay(10); //notice how short the delay is? 10*180=1800mS
} //do it HIGHEST - LOWEST times.
for (int i=HIGHEST; i>=LOWEST; i--) { //Back down again
myServo.write(i);
delay(7);
}
}