Can't get the servo SG90 working

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Can't get the servo SG90 working

Post by Lornioiz » Wed May 03, 2017 11:05 am

Hi all,

I dismantled a (working) SG90 servo from an old Arduino project and I tried to test it with Micropython.
With the code provided in the website I was not able to make it work.
Once I write these lines:

Code: Select all


from machine import Pin, PWM
servo = PWM(Pin(14), freq=50, duty=77)

servo.duty(30)
servo.duty(122)

The servo moves very quickly for an instant, then stand still while making a buzzing noise as it is somehow stuck.
I thought the problem in the current, so I attached the servo to an external 5v power source (I tried 3.3v as well with the same results) but it didn't solve the problem.
Tried it with both an Wemos D1 mini and an Amica mcu.
Un fortunately I have only one servo as of now, but as I said, it is working fine with Arduino.

Any idea why it is now working?

Thanks!

P.S.

I treid the same with Servo library but it still doesn't work.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Can't get the servo SG90 working

Post by deshipu » Wed May 03, 2017 7:43 pm

Try different values. You are probably trying to move the servo outside of its valid range, which is why it's buzzing.

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

Re: Can't get the servo SG90 working

Post by pythoncoder » Fri May 05, 2017 8:36 am

Indeed. The duty values of 30 and 122 look wrong to me. See https://en.wikipedia.org/wiki/Servo_control. The range is 1-2ms out of a 20ms period - this corresponds to something like 50-100 relative to the 1023 full scale. Exact values depend on the servo - some experimentation is called for.
Peter Hinch
Index to my micropython libraries.

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: Can't get the servo SG90 working

Post by Lornioiz » Thu May 11, 2017 9:11 am

Thank you all, that was the problem indeed. As I said, first time with servos.
I'm now using the great servo D1 mini shield from Deshipu... I'm now looking for a simple idea that can make use of a bunch of servos! ;)

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Can't get the servo SG90 working

Post by deshipu » Thu May 11, 2017 3:19 pm

How about something like this?
2402471489767401400.jpg
2402471489767401400.jpg (95.47 KiB) Viewed 9022 times

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

Re: Can't get the servo SG90 working

Post by pythoncoder » Fri May 12, 2017 7:08 am

Try looking here viewforum.php?f=5 - you'll find quite a few things people have built.
Peter Hinch
Index to my micropython libraries.

Zolee
Posts: 14
Joined: Thu Jul 27, 2017 12:09 pm

Re: Can't get the servo SG90 working

Post by Zolee » Thu Sep 28, 2017 5:30 pm

It is possible to control the servo speed with wemos d1 mini?

Zolee
Posts: 14
Joined: Thu Jul 27, 2017 12:09 pm

Re: Can't get the servo SG90 working

Post by Zolee » Sun Oct 01, 2017 6:14 pm

I have written a simple function to control the servo speed, but not work in parallel. (multiprocessing)

Code: Select all

speed = ['0.5','1']

servo = {'1a':'5','1b':'4'}
def motor_vezerles(servok, new_state, sebessegek):
    sebesseg=float(speed[sebessegek])
    pinnumber=int(servo[servok])
    last_state=PWM(Pin(pinnumber), freq=50)
    last_state=last_state.duty()
    if last_state<new_state:
        lepesek=new_state-last_state

        min_ido=lepesek*4

        if sebesseg!='1':

            osszido=min_ido/sebesseg
            
            osszido=osszido-min_ido
            milisec=round(osszido/lepesek)
            
        else:
            milisec=0
        for x in range(last_state+1, new_state+1):
            PWM(Pin(pinnumber), freq=50, duty=x)
            time.sleep_ms(milisec)
    else:
        lepesek=last_state-new_state

        min_ido=lepesek*4

        if sebesseg!='1':

            osszido=min_ido/sebesseg
            
            osszido=osszido-min_ido
            milisec=round(osszido/lepesek)
        else:
            milisec=0
        for x in range(last_state-1, new_state-1, -1):
            PWM(Pin(pinnumber), freq=50, duty=x)
            time.sleep_ms(milisec)

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

Re: Can't get the servo SG90 working

Post by pythoncoder » Mon Oct 02, 2017 5:40 am

This is probably a good use-case for the uasyncio library. If you're unfamiliar with asyncio, there is an unofficial tutorial here https://github.com/peterhinch/micropython-async - follow the link to tutorial.
Peter Hinch
Index to my micropython libraries.

Post Reply