Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 8:09 pm

Hi All,
I am having problems controlling more than one Servo with my Raspberry Pi.
Here is my hardware setup:
  • GPIO Pin 7, 11, 13, and 15 connected to servo control lines (Using BCM pinout)
  • Ground Pin on Pi Connected to Servo Ground and 5v regulator ground
  • Positive on regulator connected to servo positive lines and Pi's 5v pin.
  • 11.1v 1.3amp lipo connected to 5v regulator.
So essentially a 11.1v lipo is powering both the Pi and 4 servos, and the Pi is controlling the 4 servos. Here is my problem:
When controlling a servo via software PWM, I get the servo to go to any position I want. But when I control two servos at the same time, the Raspberry Pi reboots. I can control any of the four servos individually, just not at the same time. Below is the code I use, if it is any help.

Code: Select all

#Servo Control
#By Tyler Spadgenske

#import libraries
import time
import RPi.GPIO as GPIO

class Servo():
    def __init__(self):
        self.DEBUG = True
        #Setup GPIO
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(11, GPIO.OUT)
        GPIO.setup(7, GPIO.OUT)
        GPIO.setup(13, GPIO.OUT)
        GPIO.setup(15, GPIO.OUT)
        
        #Setup PWM
        self.frequency = 50#Hertz
        self.left_foot = GPIO.PWM(11, self.frequency)
        self.left_knee = GPIO.PWM(7, self.frequency)
        self.right_knee = GPIO.PWM(13, self.frequency)
        self.right_foot = GPIO.PWM(15, self.frequency)

        #Setup duty cycles
        self.RIGHT = .4
        self.CENTER = 1.5
        self.LEFT = 2.5

        self.foot_left = 2

        self.msPerCycle = 1000 / self.frequency

    def move(self, position, servo):
        self.dutyCyclePercentage = position * 100 / self.msPerCycle
        if self.DEBUG:
            print 'Position: ' + str(position)
            print 'Duty Cycle: ' + str(self.dutyCyclePercentage) + '%'
            print

        servo.start(self.dutyCyclePercentage)
        
    def cleanup(self):
        self.left_foot.stop()
        self.left_knee.stop()
        self.right_knee.stop()
        #GPIO.cleanup()

if __name__ == '__main__':
    test = Servo()
    test.move(test.RIGHT, test.left_knee)
    time.sleep(1) #Remove for reboot
    test.cleanup()#Remove for reboot
    time.sleep(.5)#Remove for reboot
    test.move(test.RIGHT, test.right_knee)
    time.sleep(1)
        
    test.cleanup()
    GPIO.cleanup()
In the code, I am controlling one servo, stopping it, then controlling another. The Pi does not reboot when I run the above code. If I were to remove the lines with the comment "Remove for reboot" then the Pi would reboot. Any ideas?
Thanks,
Tyler

User avatar
DougieLawson
Posts: 36836
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 8:13 pm

The GPIO pins are rated at 16mA. The max current for all GPIO pins in a bank is 50mA.

If your servo needs more than that you need a transistor or opto-isolator between your pi and your servo. You're risking frying your pi otherwise.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
joan
Posts: 14579
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 8:16 pm

Servos can draw a lot of power. Why not power the servos separately from the Pi? 1.3 amps is really not enough for the Pi and servos.

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 9:19 pm

Thanks for the feedback.
Do you think I could power both on the same battery if it put out more amps? If so, how much power would I need? I don't really want to have to charge two lipos to power my robot. Also, if I were to use transistors, how would I go about that?

User avatar
PeterO
Posts: 5344
Joined: Sun Jul 22, 2012 4:14 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 9:27 pm

What is the max curent rating of the 5V regulator ?

And your battery is probably 11.1v 1.3 Ahr lipo (not 1.3 Amp). That means it will supply 1.3A for an hour (or 2.6A for 30 min ....). What is the peak discharge rate (normally given as a multiple of "C" like "10C" or "20C".

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 9:33 pm

This is the regulator, or UBEC.
http://www.ebay.com/itm/Hobbywing-3A-UB ... 2336211b86
5A max rating.
The battery is 20-30C. Right here:
http://www.ebay.com/itm/ZIPPY-1300Mah-1 ... 58af37dc42

Thanks guys for the help. :)

User avatar
joan
Posts: 14579
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 9:38 pm

I suspect it'll be the servo rather than the battery. You could probably drive 3 or 4 of the 9g type servos (tiny ones) but only one of the S3003 types.

Which servos are you using?

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 9:59 pm

I suspect it'll be the servo rather than the battery. You could probably drive 3 or 4 of the 9g type servos (tiny ones) but only one of the S3003 types.

Which servos are you using?
TowerPro MG996 servos.
http://www.ebay.com/itm/1-x-MG996R-Digi ... 27e9c1bb90

User avatar
joan
Posts: 14579
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 10:13 pm

That's about 5 times as strong as a S3003. I think it's even more likely that the starting current draw causes the Pi voltage to drop too low and crashes the Pi.

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Sep 15, 2014 10:23 pm

joan wrote:That's about 5 times as strong as a S3003. I think it's even more likely that the starting current draw causes the Pi voltage to drop too low and crashes the Pi.
So what do you think I should do?

User avatar
joan
Posts: 14579
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry Pi Loses Power When Controlling Servos

Tue Sep 16, 2014 7:32 am

Mach_5 wrote:
joan wrote:That's about 5 times as strong as a S3003. I think it's even more likely that the starting current draw causes the Pi voltage to drop too low and crashes the Pi.
So what do you think I should do?
Simplest would be to use two power sources. One to power the Pi and one to power the servos.

Perhaps a large capacitor across the power supply would remove the power dip which is probably killing the Pi, but that is an electrical question, outside my knowledge.

https://www.youtube.com/watch?v=HBFMTbcNLtg shows the Pi being powered via the microUSB and four medium servos being powered via a UBEC.

PiGraham
Posts: 3678
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Raspberry Pi Loses Power When Controlling Servos

Tue Sep 16, 2014 7:46 am

Just seperate the Pi 5V from the servo power. That battery should have more than enough current capability. Attach two UBECs to the battery so that the regulated power to the Pi is not disturbed by the motors.

It's not about maximum current a regulator can supply, it's how good the regulation is. This is a key specification for most regulators, but I haven't seen it mentioned on UBEC listing. It probably won't be an issue for just powering a Pi, but motors can draw high transient currents and that's where the regulation performance matters.

A B+ would probably cope with your original configuration because it has an onboard switch-mode regulator. If you are using USB peripherals (which are on the 5V rail) they could still have problems though.

User avatar
PeterO
Posts: 5344
Joined: Sun Jul 22, 2012 4:14 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Tue Sep 16, 2014 8:37 am

PiGraham wrote:
It's not about maximum current a regulator can supply, it's how good the regulation is.
It's a bit of both in reality... In this case the peak start up current of the servo could easily be exceeding the 5A current limit of the UBEC. As you say the regulation will determine how much the voltage droops as the current increases from zero up to the current limit, but at the current limit the voltage will start to drop muck quicker if the currnt increases beyond that point. Two different behaviours on each side of the current limit level.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PiGraham
Posts: 3678
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Raspberry Pi Loses Power When Controlling Servos

Wed Sep 17, 2014 7:50 am

PeterO wrote:
PiGraham wrote:
It's not about maximum current a regulator can supply, it's how good the regulation is.
It's a bit of both in reality... In this case the peak start up current of the servo could easily be exceeding the 5A current limit of the UBEC. As you say the regulation will determine how much the voltage droops as the current increases from zero up to the current limit, but at the current limit the voltage will start to drop muck quicker if the current increases beyond that point. Two different behaviours on each side of the current limit level.

PeterO

You are correct, of course. In this case here could perhaps be a >5A transient due to the motors, but even if there is not rapid changes of load below the limit could cause problems.

In either case using seperate regulators for servos and Pi will be a solution. If th servo supply dips it will not affect the Pi.

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Wed Sep 17, 2014 10:46 pm

PiGraham wrote:
PeterO wrote:
PiGraham wrote:
It's not about maximum current a regulator can supply, it's how good the regulation is.
It's a bit of both in reality... In this case the peak start up current of the servo could easily be exceeding the 5A current limit of the UBEC. As you say the regulation will determine how much the voltage droops as the current increases from zero up to the current limit, but at the current limit the voltage will start to drop muck quicker if the current increases beyond that point. Two different behaviours on each side of the current limit level.

PeterO

You are correct, of course. In this case here could perhaps be a >5A transient due to the motors, but even if there is not rapid changes of load below the limit could cause problems.

In either case using separate regulators for servos and Pi will be a solution. If the servo supply dips it will not affect the Pi.
Ok. Thanks for the help everyone, I really appreciate it.

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Wed Oct 29, 2014 5:45 pm

Hello everyone,
I finally got back to this project (been busy with school). I used two UBECs one for servos, one for Pi. It still didn't work. The Pi still powered down when attempting to drive two servos. I then powered the Pi from the wall over micro USB, and the servos via the same battery and regulator. This setup worked. So, it looked like the UBEC's are bad. (The ones linked in below) Please correct me if I am wrong.
Tyler

http://www.ebay.com/itm/Hobbywing-3A-UB ... 2336211b86

User avatar
mikronauts
Posts: 2734
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Raspberry Pi Loses Power When Controlling Servos

Wed Oct 29, 2014 6:51 pm

I've built quite a few robots with a Pi.

You are basically asking for trouble if you don't use separate power for the Pi and the motors / servos.

Yes, it is in some cases possible to use one battery, however it is FAR simpler and easier to use two.

I normally use one of the "USB powerbanks" to power the Pi & RoboPi, and a separate battery pack for the motors/servos.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Raspberry Pi Loses Power When Controlling Servos

Thu Oct 30, 2014 6:29 am

I agree with micronauts, it isn't the UBECs, the draw from the servos is causing the battery voltage to dip, the UBEC can't respond in time so the pi browns out.
A small "powerbank" style 5v supply to run the pi will solve your problems.
Doug.
Building Management Systems Engineer.

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Thu Nov 06, 2014 5:50 pm

It is still not working.

What I did was I took two 11.1v lipos and connected each to two UBECs. 5v from one UBEC (and battery) went to servos, and one UBEC and battery powered the Pi. The Pi still rebooted when I tried driving multiple servos. So, even when I use two batteries it doesn't work. :(

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Jan 12, 2015 2:10 am

How about adding a servo controller between the Pi and the Servos to take off the processing load from the Pi?

LinuxCircle has tested these two controllers. They both run Python scripts and don't crash your Pi so easily, unless you overload the servos, like climbing your robot up the tree.

Check them out:
www.linuxcircle.com

vistalizer
Posts: 3
Joined: Tue Jan 13, 2015 6:38 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Tue Jan 13, 2015 6:44 pm

Hye, I am a newbie. May I know what type of RasPi and servo u're using? Because I can't even get my servo to rotate as what I needed. Thanks. :)

Mach_5
Posts: 70
Joined: Fri Jun 07, 2013 9:22 pm

Re: Raspberry Pi Loses Power When Controlling Servos

Sun Feb 22, 2015 11:01 pm

vistalizer wrote:Hye, I am a newbie. May I know what type of RasPi and servo u're using? Because I can't even get my servo to rotate as what I needed. Thanks. :)
Sorry for the late reply.
I am using a Raspberry Pi Model B. (The model shouldn't really matter though) I am using MG996R servos. I don't recommend doing what I am doing, I would get a servo controler like this one: https://www.adafruit.com/product/2327 Otherwise you run into trouble like I did. Also the type of servo doesn't really matter, unless you are using a insanely huge one like I did. (I need the extra torque)

User avatar
mikronauts
Posts: 2734
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Raspberry Pi Loses Power When Controlling Servos

Mon Feb 23, 2015 12:48 am

There is another great servo/pwm/more controller, see

http://RoboPi.com

It's more expensive due to the cost of the parts, but it does a not more.

eight channels of 12 bit ADC

twenty four flexible digital I/O's each of which can be set for
- servo
- pwm
- ultrasonic range sensor
- digital output
- digital input

it adds eight 32 bit RISC cores for the I/O :)

I designed it to be the most advanced robot controller for the Pi
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

Return to “Automation, sensing and robotics”