Using Servo without a delay using sleep-function

Hi!

I’m using a Sunfounder Robot Hat and a Raspberry Pi 5 to control Servos.
So far I am very happy. But there is a sleep delay I have to put. Otherwise it doesn’t work properlly in every case.

In most cases I want to control all 5 connected servos simultaneously.
Unfortunately it doesn’t work without the time.sleep which I had to include.
A lower value than 0.01s leads to not responding servos.
Is it possible to find a way to reduce the delay?
May be it is possible to set the PWM frequency higher?

I’m using a code like this.
(somehow the indentations for the Python-code in the for-loops are filtered out.
So I included “—>”)

from robot_hat import Servo
import time

Servos = [Servo(f"P{i}") for i in range(5)]

ServoDelay = 0.01

#All Servos to 90°
for servo in Servos:
—>servo.angle(90)
—>time.sleep(ServoDelay)

time.sleep(2) # Wait 2 seconds

#All Servos to 0°
for servo in Servos:
—>servo.angle(0)
—>time.sleep(ServoDelay)

Many thanks in advance.
Andreas

We didn’t reproduce your problem, the results of our tests time.sleep(ServoDelay) is not required, please tell me the version of the robot_hat library (pip3 list | grep robot_hat) and also what robot_hat looks like.

Thanks a lot for your answer.
I’m using 5x servos from AZDelivery MG996R.
Could it be that these are taking to much current when giving a signal to all 5 servos in one shot? (There is no load/torque on the servos.)

When the robot-hat runs in battery-mode (without usb-c power) the pi 5 will experience low voltage shutdowns immediately when trying to use the servos.
The same is happening when the original 27W Pi 5 Power Supply is connected to the charge connectior on the Robot-Hat.

Would it be possible to buy a second 27W Pi 5 Power Supply to connect to the Robot-Hat and charge the battery all the time? (And of cause have the other 27W Power Supply connected to the Pi 5.)

Or is it even possible to use the Robot-Hat without the battery?

You can find the requested infos below:

pip3 list | grep robot-hat
DEPRECATION: Loading egg at /usr/local/lib/python3.11/dist-packages/keyboard-0.13.5-py3.11.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at Remove support for installed `.egg` distributions when using the `importlib.metadata` backend · Issue #12330 · pypa/pip · GitHub
DEPRECATION: Loading egg at /usr/local/lib/python3.11/dist-packages/robot_hat-2.2.17-py3.11.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at Remove support for installed `.egg` distributions when using the `importlib.metadata` backend · Issue #12330 · pypa/pip · GitHub
robot-hat 2.2.17
robot-hat 2.2.17

If running it causes the pi5 to shut down, then it could indeed be a momentary overcurrent. robot_hat is capable of delivering 5.1v, 3A output. As I don’t have the servo you are using I can’t test it. But the way you are hoping to power the raspberry pi5 by using the usb cable at the same time is not valid, the pi5 unlike the pi4 will not receive current from the usb when there is input from the gpio 5v. My suggestions are: a. use servos that draw less current or b. add a delay if adding a delay between moving each servo doesn’t affect your application or c. robot-hat is powered separately from pi5 separate robot-hat from pi5 and use only dubang cables to connect scl, sda, gnd.

Hi!
Thank you again very much for your quick reply.
That’s amazing!
So far I’m not 100% sure which way to go. The delay of 10ms for each servo is in fact noticable. But it’s also not that big. So I propably will go with option b.
If i test other options I will let you know.
Thanks and best regards
Andreas

By the way:
Are you interested to know what I’m doing with this robot-hat?
If yes, I would post here my homepage, if you like.
(The language is German.)

Very interested, please share with us. :hugs:

The project is about a rotary target system for air pistol disciplines.
The targets can be turned by 90° using the servos.
There is for example a random-mode where randomly 1 of the 5 targets is rotating to get the shoot.
See:

1 Like

Hi!
One update from my side:

  • We just tried use a switchable plug socket.
  • Connected the pi 5 to the 27W official power supply
  • Connected the Robot Hat to a mobile phone power supply
  • Turned on the switch of the plug socket… And it works! The Pi 5 is booting. The Robot Hat is charging the battery
  • Still there is the issue, that I need to use a delay to adress the servos. But for this I also had a closer look to the behavior. It looks like the Robot Hat tries to turn the first two servos and immediately is looks like there is a loss of power. Afterwards the Robot Hat needs to be turned off and on again to be able to work.
  • So I guess its because of too high current. But its acceptable with the delay this way. The question to use 2 power supply was anyway intended to know how to avoid to charge the batterie.

BR
Andreas

This version of robot hat is only able to provide 5.1v, 3A output, while the maximum operating power of PI5 is 5V 5A.
So when robot hat needs larger working current, it may cause PI5 not getting enough power supply, it will show low voltage alarm, or PI5 will show shutdown and reboot problem. It is only perfectly compatible with PI4 and below Raspberry Pi boards.