Servo library

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
mflmartin
Posts: 43
Joined: Sat Jul 23, 2016 7:30 pm

Re: Servo library

Post by mflmartin » Mon Jan 02, 2017 11:44 am

pythoncoder wrote:An easy way might be to use a standard 180 degree servo and gear it up 2:1. The only snag is that at (say) 11.59 the clock's hand would sweep through 354 degrees backwards to get to 12.00.
Thanks... But that is not an option, due to the "aesthetics" of the movement. I want seamless analog like movement. But your suggestions is a way, for sure. Thanks!


Sent from my iPhone using Tapatalk

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Servo library

Post by pythoncoder » Mon Jan 02, 2017 1:46 pm

Fair enough, in which case I'd go for stepper motors.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Servo library

Post by dhylands » Fri Jan 06, 2017 7:45 am

You can also setup the PWM yourself.

For example, this code:

Code: Select all

servo_pin = pyb.Pin.board.X1
t5 = pyb.Timer(5, prescaler=83, period=19999);
servo = t5.channel(1, pyb.Timer.PWM, pin=servo_pin)
servo.pulse_width(1000)
allows 1 usec resolution on the pulse width. The regular servo library only allows 10 usec resolution.

othoma
Posts: 1
Joined: Sat Jun 16, 2018 12:17 pm

Re: Servo library

Post by othoma » Sat Jun 16, 2018 12:19 pm

Hello, has anyone tested the library on ESP32 ?

I have some trouble to make it work.
Thanks

User avatar
apollodriver
Posts: 34
Joined: Tue Jan 29, 2019 8:54 am

Re: Servo library

Post by apollodriver » Sat Aug 10, 2019 7:54 pm

hello dear all - well that would be great
Hello, has anyone tested the library on ESP32 ?

I have some trouble to make it work.
Thanks
i would love to use it on esp32

:)


BTW one question: - do we need that library - cannot we just start without that... - cf this post:

https://icircuit.net/micropython-contro ... demcu/2385
Servos are exciting devices. We can get precise posting with minimal code,
all we need is a controller that can produce PWM at 50Hz.
In this post we will understand how to use ESP32 to control Servo.
We will be using micropython to program ESP32/NodeMCU.
The grate thing about MicroPython is that it is platform independent (mostly),
which means code written for NodeMCU (ESP8266) can be used on ESP32 as well.

To use MicroPython on ESP32, First we need to load MicroPython Interpreter. Check out ESP32
– Getting started with MicroPython post to load interpreter and tools you can use with micro python. https://icircuit.net/esp32-micropython- ... arted/1999 If you are using NodeMCU, then refer to this post https://icircuit.net/nodemcu-getting-st ... ython/2406
MicroPython has PWM support. You can find full documentation of PWM library here http://docs.micropython.org/en/latest/e ... l/pwm.html

Environment requirements:
we need a ESP32 or NodeMCU

Machine with uPyCraft to load python files to the device

Controlling Servo:
Connect servo signal pin to GPIO2 of the ESP, you need to power the servo as well

Code: All you need is couple of python lines to control the servo, how easy is that

i

Code: Select all

mport machine
p4 = machine.Pin(4)
servo = machine.PWM(p4,freq=50)
# duty for servo is between 40 - 115
servo.duty(100)
The PWM method of machine takes two inputs, the pin to which we want connect servo
(in case of ESP8266 we can choose one from 0, 2, 4, 5, 12, 13, 14 and 15) and frequency of the PWM signal.
Most of the hobby servos work with 50Hz PWM, so we choose that. Then we can use duty method to set the anglewe can call servo.duty() method to change the set the servo angle



love to hear from you
WPGear.org is a compendium of useful developer tools for working with WordPress.

User avatar
Ised
Posts: 6
Joined: Mon Aug 26, 2019 7:03 am
Location: UA

Re: Servo library

Post by Ised » Mon Aug 26, 2019 6:13 pm

Good day to all! I am a beginner user esp8266 / connected servo sg 90
(red vin,
black gnd,
orange pin14)
. I use the code
from machine import Pin, PWM
servo = PWM (Pin (14), freq = 50, duty = 77)
servo.duty(122)
but it does not work. But Arduino uno, this servo drive works. What am I doing wrong?
servo.duty(30)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Servo library

Post by dhylands » Mon Aug 26, 2019 6:35 pm

The way that servos work is that they send a 1-2 msec long pulse at about 50 or 60 Hz.

Lets say we wanted to use 50 Hz. That corresponds to a period of 20 msec. Since we want the pulse to be between 1 and 2 msec, then we want it to be between 1/20 = 0.05 and 2/20 = 0.10 or 5-10%

The duty is specified as 0-1023 so you want duty to be 5-10% of 1023 = 51 to 102.

Are you powering the servo from a separate supply (i.e. 5v)?
the signal from the esp8266 will only be 3.3v, but that should be enough if you're using a 5v supply.. If your supply is higher you might need to levelshift the PWM signal.
Have you connected the ground from the servo to the ground from the esp8266?

User avatar
Ised
Posts: 6
Joined: Mon Aug 26, 2019 7:03 am
Location: UA

Re: Servo library

Post by Ised » Mon Aug 26, 2019 6:52 pm

dhylands wrote:
Mon Aug 26, 2019 6:35 pm
The way that servos work is that they send a 1-2 msec long pulse at about 50 or 60 Hz.

Lets say we wanted to use 50 Hz. That corresponds to a period of 20 msec. Since we want the pulse to be between 1 and 2 msec, then we want it to be between 1/20 = 0.05 and 2/20 = 0.10 or 5-10%

The duty is specified as 0-1023 so you want duty to be 5-10% of 1023 = 51 to 102.

Are you powering the servo from a separate supply (i.e. 5v)?
the signal from the esp8266 will only be 3.3v, but that should be enough if you're using a 5v supply.. If your supply is higher you might need to levelshift the PWM signal.
Have you connected the ground from the servo to the ground from the esp8266?

Yes, I connected servo to the ground esp8266. powered by esp8266. used this recipe
https://github.com/lvidarte/esp8266/wik ... Servo-sg90

User avatar
Ised
Posts: 6
Joined: Mon Aug 26, 2019 7:03 am
Location: UA

Re: Servo library

Post by Ised » Mon Aug 26, 2019 6:55 pm

dhylands wrote:
Mon Aug 26, 2019 6:35 pm
The way that servos work is that they send a 1-2 msec long pulse at about 50 or 60 Hz.

Lets say we wanted to use 50 Hz. That corresponds to a period of 20 msec. Since we want the pulse to be between 1 and 2 msec, then we want it to be between 1/20 = 0.05 and 2/20 = 0.10 or 5-10%

The duty is specified as 0-1023 so you want duty to be 5-10% of 1023 = 51 to 102.

Are you powering the servo from a separate supply (i.e. 5v)?
the signal from the esp8266 will only be 3.3v, but that should be enough if you're using a 5v supply.. If your supply is higher you might need to levelshift the PWM signal.
Have you connected the ground from the servo to the ground from the esp8266?



I fixed the code as you said. he answered. You were right. thank you very much

Post Reply