1
\$\begingroup\$

I have a 5V "SG90 Servo motor" and would like to control it to move 0 degrees, 90 and 180 degrees, by sending it appropriate pulses from my NodeMCU Development kit.

Having searched on the net - I could not find a right example source on how to essentially program it.

Arduino IDE has some examples -but they are for Arduino hardware and not for NodeMCU/ESP 8266. ( I am using this to upload code to the NodeMCU )

There is a "sweep" sketch/program and I used/modified it a bit to suit the NodeMCU pin out, that did NOT work. Hence reaching out to any engineers here who can share code snippet ( prefer python) and show how to control the Servo motor .

The Pins on the Node MCU I would like to use are D1..D4. I have a 5V wall adapter from which I can power the Servo's Power ( Vcc) and GND pins. The orange pin (i.e PWM ) on the SG90 Servo, is how I am trying to control the Servo from the NodeMCU's D1.. D4 pins.

Any help would be appreciated.

\$\endgroup\$

closed as off-topic by laptop2d, Dmitry Grigoryev, Andy aka, uint128_t, Dave Tweed Dec 25 '16 at 19:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on the use of electronic devices are off-topic as this site is intended specifically for questions on electronics design." – laptop2d, Andy aka, uint128_t, Dave Tweed
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Do you have preference for which approach to ESP8266 programming you use, ie, native, Lua, or the Arduino port? I'm getting plenty of results if I do a web search for "ESP8266 servo" \$\endgroup\$ – Chris Stratton May 9 '16 at 17:19
  • 1
    \$\begingroup\$ Are you using the NodeMCU firmware (that is, the Lua-based firmware)? A servo requires a pulse between 1ms and 2ms (1.5ms is middle), and the NodeMCU firmware provides a PWM library that allows you to set frequency, so all that's left for you to do is math. \$\endgroup\$ – uint128_t May 10 '16 at 1:00
3
\$\begingroup\$

With NodeMCU you can use the pwm module like this:

local pin4 = 4
--setup at position 0
pwm.setup(pin4, 50, 71)
--turn to position -90
pwm.setduty(pin4, 27); pwm.start(D5); tmr.delay(500000); pwm.stop(D5)
--turn to position 0 again
pwm.setduty(pin4, 71); pwm.start(D5); tmr.delay(500000); pwm.stop(D5)
--turn to position +90
pwm.setduty(pin4, 123); pwm.start(D5); tmr.delay(500000); pwm.stop(D5)

27, 71 and 123 are the appropriated duty cycle "time" to set the desired position from 0 to 180 degrees up. I found this by experimenting.

For more information you can check the sg90 manual here and pwm module manual here.

\$\endgroup\$
0
\$\begingroup\$

With Nodemcu you can drive the servo by generating high and low pulses with any GPIO pin. In the code below a potentiometer is used to vary the duty cycle.

Potentiometer = 0   --setting the pot value  level adc pin
servo_PIN = 2       --pin for connection of servo
Max = 2400
Min = 700

tmr.alarm(0,20,1,function () -- 50Hz
value = adc.read(Potentiometer) --Read the adc pin
value2 = (value-2)*(Max-Min)/(1024-2)+Min 
gpio.write(servo_PIN, gpio.HIGH)
tmr.delay(value2)               --Delay for setting the servo position  
gpio.write(servo_PIN, gpio.LOW)
end)

tmr.wdclr() -- Internal Nodemcu OS function
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.