http://www.perlmonks.org?node_id=1194581


in reply to Using a controllerless servo on the Raspberry Pi with Perl

Nice work.

"Note that the Pi may struggle to power the servo and it may cause a low-voltage situation, so it's best you power your 5v servo from an alternate source (I just happen to have a few powered-up Arduino's nearby all with 5v pins accessible). Also note that even though the +/- of the servo is 5v, you can safely connect the signal pin on it to the 3.3v GPIO on the Pi as on the servo, the pin is input only (ie. it doesn't feed back to the Pi)"

I think it'd be a good idea to epmhasize this early on, in addidtion to causing the pi to freeze/bounce (hitting lower than 4.7v IIRC), powering an inductive load from the pi directly can cause a voltage spike to flow back on the 5v rail, I dare say many a pi has been fried this way :) Use of decoupling capacitors can negate such problems. Safety first and all that. Keep up the good work.

Replies are listed 'Best First'.
Re^2: Using a controllerless servo on the Raspberry Pi with Perl
by afoken (Chancellor) on Jul 12, 2017 at 19:40 UTC
    powering an inductive load from the pi directly can cause a voltage spike to flow back on the 5v rail
    Use of decoupling capacitors can negate such problems

    You are mixing up two things there.

    Decoupling capacitors are required to keep the supply voltage stable when lots of transistors inside the controller switch simultanously and parasitic capacitors inside the controller are forced to change their charge. The PCB tracks to the supply pins behave like a resistor or inductor, so even a very good, nearly ideal power supply simply can not keep the voltage at the supply pins constant. The decoupling capacitors can, because their track to the supply pins is extremely short. As a welcome side-effect, the decoupling capacitors also short high frequency noise to ground.

    Inductive loads switched to DC suppiles create a more or less large voltage spike with reversed polarity. This voltage is usually large enough to kill nearby semiconductors, typically the switching transistor. To get rid of the voltage, connect a fast diode in parallel to the inductive load, but with reversed polarity. This is commonly called a flyback diode. While the load is powered, the diode is passive. Because the voltage spike has reversed polarity, the diode becomes conductive and shorts out the voltage from the load, converting the energy to heat in the diode and the load's wire. The remaining circuit sees a reverse voltage of about 0.7 V across the load that does not harm usual Si semiconductors.

    Another thing: Yes, the I/O pins of AVR microcontrollers have a significant capability to sink and source current. The ATmega328 (datasheet), commonly found on Arduino boards, can sink and source at least 20 mA @ 5 V, 10 mA @ 3 V, abs. max. 40 mA (see datasheet page 365 and following). But the total current must not exceed 100 mA per port, and it must not exceed 200 mA total. Other AVR processors have very similar ratings, and I would expect similar performance from PIC chips. But for anything but a simple LED or a TTL/CMOS compatible input, I would use a driver transistor. I/O pins also have a pair of diodes (see page 97), but those diodes are just clamping diodes that prevent killing the controller by ESD. They are not designed to discharge an inductive load.

    Oh, and don't expect other, younger microcontrollers to have similar driving capabilities. Let's look at the SAM4S series (datasheet), an ARM Cortex M4 with a lot of peripherals, more I/Os, more flash, more RAM, and faster than AVRs. The I/O pins can sink and source max. 2 mA (see page 1143), a few pins can sink and source 4 mA, and only two pins can sink and source 30 mA. Abs. max. total current is 100 mA for 64 pin devices, 150 mA for 100 pin devices. All of that at an I/O voltage of 3.3 V typ. Other ARM-based microcontrollers have similar limits. They can directly drive a low-current LED, or CMOS compatible logic inputs, but for everything else, you need a driver transistor.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Using a controllerless servo on the Raspberry Pi with Perl
by RonW (Parson) on Jul 11, 2017 at 21:14 UTC

    AVR and PIC IO pins are designed to handle loads like PWM controlled servos. Still, bypass capacitors should be used.

    Also, until recently, replacing an AVR or PIC was less expensive. Still is, except that the Pi Zero is inexpensive enough to use instead of an AVR or PIC in many applications. And, it gets Perl closer to hardware it's controlling.

    Update/addendum:

    The Pi is US$25 to US$35 and the micro in it not replaceable except by an expert, so if you damage the micro, you buy another Pi. while there are surface mount AVRs and PICs, you can still get ones you can plug into a socket, so are US$2 to US$5 to replace for many "hobby" projects. So, using an AVR or PIC as a "smart" I/O handler (under the control of a Pi or other, similar device) makes a lot of sense.

    The Pi Zero is US$5, so replacing it is almost as inexpensive as replacing an AVR/PIC. So, for some uses, the Pi Zero could be used with out an AVR or PIC as an intermediary.

    (Though, because the I/O pins of AVRs and PICs are more robust than those of the Pi's micro, there are still times when adding an AVR or PIC along side the Pi Zero makes sense.)

      Hi ron, the pi isn't AVR or PIC, and the GPIO pins have nothing to protect the board. Can you tell more about what you mean here?

        Thanks for your reply. I have appended to my post to better explain what I meant.