Thanks for getting back to me. Would this be a good power supply to power up the servo?
Also, Im using Python. I’ve noticed that when the Pi initially boots, the servo will rotate a bit, is this because its being powered from the GPIO and will this stop if I power is using the external power supply?
If your are using this common servo then yes, that power supply is a good choice.
Different servos may require different power supplies. If you’re feeling unsure, shoot us a link to your servo and we can help.
Bob has noted the dangers of using GPIO headers for powering a motor. Note also the importance of a common ground between the servo’s power supply and the pi power supply.
That is, you can EITHER power both the pi and the servo from the same power supply, OR you can power each separately, but you must tie there ground pins together.
Check it out, CE have done a guide on this you can read.
It has circuits, pictures, and some python code to get you started.
Thanks so much Pixmusix I will go through the tutorial. With the GPIO, I just wanted to double check as I have seen alot of videos where the Pi is being used to power a servo and the user doesnt report any problems or potential issues.
I guess on this forums most are relatively conservative in their advice. I think that’s a good thing.
It is technically possible to damage your raspberry pi that way and we just wanted to warn you. This is because the traces on the pi are small and not rated for fast and hard inrush current like you find on a motor. This is especially true because you are powering on boot.
That said, electricity in real life is not always predictable. If you want experiment with powering it off the pi, and you accept the risks, that’s cool
I’d lastly like to remind you that some online might not know the risk. I know I didn’t when I started with motors.
For the record, I think I’ve got a good handle on these simple servos at the moment, and I have decided that an external power supply is sensible and well worth it.
I personally think you are a bit crazy. Why risk damage (true it might never happen, But…) when it is so easy not to. But it is your baby so do as you wish.
And there are a whole lot of videos that are just a load of absolute garbage. A situation that Pix just summed up.
Indeed I think that sometimes this AI stuff has a bit of revision to do. Quite recently a contributor to this Forum complained about the trouble ChatGp (whatever that is, some form of AI I think) caused him when it advised to fit a capacitor (which was required) to the wrong position in an Op Amp input circuitry. He finally got it fitted in the correct place. He came out of that a bit disillusioned with AI I think.
Some of these tools are not really the holy grail they seem to be. The problem is for the inexperienced hobbyist to filter this information and to know when and what to cross check and mark for further research.
This is a very real problem and can be difficult to spot and address.
Your case regarding powering a motor via GPIO is easy. The risk of destroying your RPi is real and is there. Why do it when the alternative is so easy??? Just because some people have done it successfully (there are many others that have not been successful) and made some videos???.
Like I say the choice is yours. Make up your own mind.
Cheers Bob
Add on:
You say you are powering from a GPIO pin. I don’y think this is strictly true. GPIO is General Purpose In Out pin and will only sink/source very few mA. Unfortunately RPi have chosen to put power delivery pins on the same 40 pin header so everybody refers to ALL these pins as “GPIO”. A monumental mistake I really think as intends to confuse thinking somewhat.
I had some discussion with Jane recently regarding this.
I think the pins you are referring to are one of these power pins. They will supply a few more mA to power light loads but nothing serious.
Your servo might be seen as a light load which it probably ie when rotating. BUT a motor (particularly brushed type) is an entirely different beast when starting and stopping. All sorts of voltages (some reversed) are involved hence the suggestion for using a separate supply.
Robert, all I was trying to say was that I have seen other users power their servos directly off the pi, never said that was the best or only way…I was merely looking for some information and seeing other users’ experiences. I didnt say thats what I was going to…it was merely a question but thank you both for the information.
I was initially testing using GPIO 12 as that is one of the pins for PWM.
Not a problem though and lesson learned. I will defintily not be powering the servo off the pi and will be using the power supply I have linked above.
Thank you both for the vaulable information..and saving my Pi! Much appreciated:)
Sorry if I seemed a bit harsh BUT:
I think we are running into a pin function description problem here. As I described above i think referring to ALL of the 40 pins as “GPIO” is a disastrous mistake.
Firstly I just had a look and I can’t find anywhere you say what flavour RPi you have, so GPIO12 probably means nothing (to me anyway, might as well be swahili) but I will take your word for it that it is a PWM capable pin.
This pin would not be used to POWER the servo. It provides a controllable logic level (5V or 3.3V is common) pulse width stream to CONTROL something else. Ir will not provide any supply POWER. Commonly used for lamp dimming, brushed motor speed control etc. Usually controls some form of high or low side switch (Mosfet ???) which would control the ON time of the controlled device. The pulse period could be whatever the RPi or Arduino etc defaults to. Probably about 500Hz or 2mSec.
A servo on the other hand requires a logic level control pulse of between 1000µSec to 2000µSec at a period of 20mSec (50Hz) which will determine the servo position. 1500µSec being centre (90º) position. This could vary between brands of servo but not by much and will be documented. A different servo is a continuous rotation type where this control apparently will change speed and direction. I personally have never had any joy with the speed part of this but that is probably me or the servo problem. BUT this PWM signal IS NOT a power supply.
There should be 3 wires to the servo. Ground, Control, and Power. It is the Control and Power difference of concern here. There may be some sort of feedback signal line as well but this is a different matter.
The Power we have been concerned about would be a suitable DC supply for the servo. Could be anything as long as within servo specs. it is unrelated and can be isolated from the control signal and MCU supply. But Control and Power grounds should be connected.
The control signal I think cannot be derived from the native PWM system as this will be too fast. Should be a period of 20mSec (50Hz as said above) Don’t know about RPi but Arduino have dedicated servo libraries. I would be surprised if RPi don’t have similar. Or you could roll your own control signal in software and you WOULD use a GPIO for this function.
A Bit more info from you might help like What RPi are you using, A sketch of EXACTLY how you have it connected etc.
I hope his has made things a bit clearer.
Cheers Bob
Hi BellAmi
Add on to post 10:
I just expanded Pix code sample at post 2 to have a look at the whole thing.
I do not know what programming language that is or claim to know anything about it.
However there are some patterns to suggest that on this RPI at least (unknown at the moment, to e anyway) GPIO12 is hardware PWM 0. And this PWM output is fairly easily externally controllable in regard to Period and Pulse Width and can indeed be set to Period = 20mSec and Pulse Width between 2 extremes to control position. and thus the required control signal to position a servo.
Note: Some time ago I had occasion to do this with an Arduino UNO R3 and with a lot of assistance from Alan73922 and a lot of research was successful by writing directly to the associated registers.
Now I repeat here that this signal will be a logic level CONTROL signal and will have nothing to do with supply power.
And also I have no idea what flavour of RPi was used in that demo sketch. Nor do I know what RPi you are using. The point I am making is you are going to have to do some cross checking. I don’t know how much compatibility exists across different RPi models or if there is any at all.
If you expand Pix’s example you will get back to the original GitHub project where digging might reveal the Pi type and programming language
Or if you look up your specs to see if GPIO12 appears on the same pin and does the same function (PWM 0) that might work.
Although I don’t profess to know much about RPi I have started to believe that as soon as anyone at the Pi Foundation changes their socks everything else changes with it and you almost have to start again. BUT there are a lot of users so it can’t all be bad. Just seems to have to be a lot of juggling with operating systems etc to get some things working at a lot of times. When something finally works I don’t think anyone is really sure why or what was wrong in the first place. Very confusing.
But that is only me.
Cheers Bob
Edited:
Par 2. “Period = 20mSec” was “Period = 20µSec”
Hi love
That is one of the better, more informative U-Tube videos I have seen.
I think one of the problems with U-Tube that there is a lot of garbage mixed up with good useful videos you need to know something about the subject to enable you to filter out the useless stuff successfully. While the concept can and often is very useful it is not the holy grail and the answer to everything.
The author deals with power via RPi Pins (I will NOT call them GPIO Pins) and separate power supply and highlights both which is good and the subject of much discussion above.
It is now 11 days since any posts on this topic and one can only assume that BellAmi has solved the problem and does not see fit to share results. The outcome therefore is that the Forum does not know whether the suggestions and advice have been implemented and have been successful or not.
Cheers Bob
Hi everyone,
Apologies for being MIA for a few weeks and apologies for forgetting to include some information earlier.
The model of Pi i am using is the Raspberry Pi 4 Model B and yes there are 3 wires coming out of the servo which I have wired as follows:
Ground wire to pin 25, power wire to pin 2 and control wire to pin 12.
With the Rasperry Pis (all models) there are 2 different sets of numbers, BCM (the GPIO numbers refering to the designations on the internal chip) and BOARD (referring to the pin’s actual number on the PCB).
For the Pi, I’m using a PoE hat and powering it off a small 4 port PoE switch.
The only thing I will do differently is move the servo’s power wire to an external power supply and connect both the ground wire of the servo and ground pin of the Pi to the ground wire of the power supply. Would this work?
I am using python as the programming language as its native to the Pi.
Should work. Moving the Servo supply connection bypasses the RPi 5V output (I think it is an output) and removes any funny behaviour from this source. Also it is I think the usual way to power servos and will negate ant interference getting back into the RPi from the servo action.
Cheers Bob
This is true and a bit misleading.
I don’t know much about the RPi and Arduino is pretty much the same.
You could have a mixture here.
PIN 25 is indeed Ground (one of a few) and pin 2 is 5V.
BUT GPIO 12 as referred to in the sketches I think is actually PIN 32. I think this because GPIO12 is a dedicated PWM output.
So if you are struggling to get the servo to work with a sample sketch try moving the control wire to PIN 32. You say it is currently on pin 12
Cheers Bob
All very confusing.
If you expand Pix’s sketch you will find it enables PWM0 on GPIO12 at PIN 32 so to use this example your control wire goes to PIN32.
Hi BellAmi.
As I said I don’t know much about RPi but I do note this
I don’t know about the cleanup bit but why have you got the pwm.stop ???
The pulse has to keep going to maintain position. If it stops the servo can be forced to move.
I suppose that code should move the servo 90º I don’t know but is it possible you stop the servo before it has reached this point and then somehow it remains stopped. Your “sleep” times need to be long enough for the servo to reach the desired position. Maybe your sleep times are not long enough. The required time to reach a position should be published in the servo specs, usually specified as time per 60º travel. You have to allow start up and slow down time as it is not instantaneous.
Cheers Bob
I’ve had some success. I have managed to find code that moves the servo exactly 90 degrees however it doesnt seem to stop and instead seems to jitter once it reaches the end of the rotation.
Doing some reading and found that using 2 commands, except KeyboardInterrupt: and servo.value = None, it should safely power down the servo by stopping the PWM signal and safely closing it.
When I run the script though, it rotates but as you can see from the screenshot, it keeps spinning 90 degrees to center position. The only way I can stop it is by manually stopping the script.
Hi BellAmi
I am afraid I still don’t understand Python or whatever that is.
What I do know however is that a servo irrespective of what is generating the control signal will only sort of “float” in the absence of the 50Hz control pulse. or in other words it is free to take up any position it sees fit. It MUST have the control signal to guarantee holding position. Disabling the PWM with keypad interrupts is OK if you just want to turn the whole thing off.
I am having having great difficulty finding a DECENT data sheet. The couple I did find which stated max and min pulse width figures state min at 1mSec and max at 2mSec with centre position at 1.5mSec. Where did the 0.5mSec and 2.5mSec come from ?? Also some servos state movement from 0 to 180º with centre or neutral at 90º while others say centre is 0º and movement is +/- 90º. This does nothing to avoid confusion.
The calculation you mention
I think is OK if your range is 0.5mSec to 2.5mSec for 180º rotation but is only true at 90º for the 1mSec to 2mSec for 180º rotation which I think is pretty much the “normal” control.
I have only fiddled with Arduino control of a couple of servos which tend to use the “<servo.h>” library and uses the 0º to 180º scaling. I have not even done much of that, only enough to satisfy my curiosity a bit. Probably could be more complex if I looked a bit deeper.
I did though look a bit suspiciously at the “precision positioning” statements bandied about. The timers which are used to generate the PWM signals have a resolution of a finite number of µSec, in the case of a UNO “1µSec”. Now in the range 0 to 180 there are not too many instances that will divide into 1000000 and produce a whole number without any appreciable fractions. In my book that takes care of eliminating the “precision” part of the statement and reducing it to “approximate”, To be fair an extremely close approximation and for most applications could be ignored.
Cheers Bob