esp8266servo - JamesNewton/JamesNewton.github.io GitHub Wiki

Arduino w/ ESP-8266, Part 2: Move!

Presentation, Source

2019/03/16 San Diego CoLab

Lights are great, but to really control the world, we need to make stuff move!


Servos

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


Connection

To connect a servo, you need three pins:

  1. Ground (brown or black wire)
  2. Power (5 to 6 volts, on the red or center wire)
  3. Pulses from a GPIO pin on the Arduino. (usually a yellow or orange wire)

Header

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

  1. Solder the bottom row to the D0-D3 pins on the NodeMCU.
  2. Solder a red wire to each pin in the center row and the other end to the "Vin" pin.
  3. 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


Code

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);
   }
 }

⚠️ **GitHub.com Fallback** ⚠️