59 Replies Latest reply on Oct 11, 2012 4:57 AM by wallarug

    Wiring a motor to the RPi?

    wallarug

      What do I need?

       

      I am told that I need some transitors.  I have a 12v powerpack and 2 x12v motors.  How do I set this up so that I don't blow-up the RPi and that I can control the motors through the GPIO?

       

      I have a gertboard on the way but I would like to see if I can do it without it first.

       

      I have little knowlegde about transitors and electronics but I do know how to build circuits.

        • Re: Wiring a motor to the RPi?

          This is fairly easy using modulation interpreters. I don't know how savvy you are but I drew up a schematic for you! It consists of a few drivers (which cost like 2 bucks) and a couple of transistors:

           

          Electronic Schematic Motor Driver.png

            • Re: Wiring a motor to the RPi?
              wallarug

              What do all the lines mean? (I know they are wires/joining components) Could you label all the individual parts please.  This looks a bit complicated as well.

                • This reply has been hidden. This can happen if the message has been hidden by a moderator, or has been reported as abusive.
                  • Re: Wiring a motor to the RPi?
                    wallarug

                    tom smith wrote:

                     

                    I already did you faggot. I put all that effort in...

                    Srry, I should have been more specfic.  Also, can someone explain why/how some connections go in three directions (I don't know how I would wire that).

                    image_png.png

                     

                    Anything with a yellow circle.  I worked out where the motors were.

                     

                    PS: Please be nice to people on the forums, not everyone is an expert.

                      • Re: Wiring a motor to the RPi?
                        michaelkellett

                        @ fergus, At the risk of being called something worse than you were - the schematic that Tom has posted is not suitable for a beginner to use to build a motor driver so I suggest you look elsewhere. The Gertboard has  a motor driver section and the ciruit is published so you could either buy a Gertboard (best bet for the inexperienced) or build the relevant parts on stripboard if you are up to it. Either way you can use the software published for the Gertboard and have  a bit more confidence that the design will work.

                         

                        Michael Kellett

                • Re: Wiring a motor to the RPi?
                  John Beetem

                  If all you want to do is turn a small DC motor on and off, all you need is Gert van Loo's circuit in this thread's 7th post: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=13473.  The motor is driven by an NPN Darlington Pair (http://en.wikipedia.org/wiki/Darlington_pair), which is a great way to convert a small current into a really big current.  The three diodes in series are for people are afraid the Darlington pair might accidentally conduct from collector to base.  Gert says they're not necessary, but with 12V present I'd worry about framboise flambée and put them in. 

                   

                  The diode next to the motor coil is critically important.  It's called a flyback diode (http://en.wikipedia.org/wiki/Flyback_diode).  When you turn on the transistor, the flyback diode is reverse-biased and has no effect.  However, when you turn off the transistor, the magnetic field in the motor will cause current to continue to flow for a while.  If that current can't go anywhere (because the transistor is off), it will charge the node to a very high voltage and turn the transistor into a fuse and possibly destroy anything connected to the transistor.  It's how spark plugs work.  The flyback diode gives the current somewhere safe to go to prevent Spitzensparken.  The diode should have a current rating higher than the current through the motor.

                   

                  Gert doesn't give a value for the resistor between the GPIO and the base of the Darlington pair.  I'd start with 1 KOhm which gives you 3.3 mA.  But then, I usually start with 1K

                    • Re: Wiring a motor to the RPi?
                      wallarug

                      Could someone confirm if the GPIOs are a ground or send out signals.  I think they are grounds but I can't be certain.

                       

                      If they are grounds, then how would the transitor work if it is a NPN?  wouldn't you need some positive current comming from somewhere?

                       

                      Sorry if these are silly questions but I am only learning.

                       

                      could someone also point me towards a sample code please.  I have heard that the code is the tricky bit.

                        • Re: Wiring a motor to the RPi?
                          Colin Barnard

                          The GPIO outputs can be high or low in output, in other words they can be made to switch from 0v (ground) to 3v3 or be held at 3v3 and switch low. You get the choice of deciding which level equals on or off.

                           

                          If you decide low (0v) is to be off then if using an NPN transistor switching the output high (3v3) would turn the transistor on. If you wanted it the other way around you'd would then use a PNP. However in your case it probably is better for you to start by using an NPN transistor, as from a beginners point of view they work in a more logical fashion.

                           

                          Do you have a circuit in mind that you've seen where you could provide a link so that we can see if it achieve what you are hoping it will.

                           

                          Do you know the type of motor - voltage, current requirements you are going to use?

                           

                          As for code, I think the code for the Gertboard would be a good start especially as it also can run a small motor. I'll have to find the link but on the Pi forums is some example code. are you looking for code in Python or C ?

                           

                           

                          Colin

                           

                          P.S. I live in the same country as you, so feel free to contact me if you need any further assistance.

                           

                          Message was edited by: Colin Barnard  corrected maximum voltage to show 3v3 instead of the incorrect 5v

                            • Re: Wiring a motor to the RPi?
                              wallarug

                              bodgy wrote:

                               

                              The GPIO outputs can be high or low in output, in other words they can be made to switch from 0v (ground) to 5v or be held at 5v and switch low. You get the choice of deciding which level equals on or off.

                               

                              If you decide low (0v) is to be off then if using an NPN transistor switching the output high (5v) would turn the transistor on. If you wanted it the other way around you'd would then use a PNP. However in your case it probably is better for you to start by using an NPN transistor, as from a beginners point of view they work in a more logical fashion.

                               

                              Do you have a circuit in mind that you've seen where you could provide a link so that we can see if it achieve what you are hoping it will.

                               

                              Do you know the type of motor - voltage, current requirements you are going to use?

                               

                              As for code, I think the code for the Gertboard would be a good start especially as it also can run a small motor. I'll have to find the link but on the Pi forums is some example code. are you looking for code in Python or C ?

                               

                               

                              Colin

                               

                              P.S. I live in the same country as you, so feel free to contact me if you need any further assistance.

                              Firstly, what firmware do I need to swap between 5v and 0v.  Currently I am using GPIO-0.2.0 which does not have this capability.

                               

                              Secondly, I would like it to be in python becasue I know that a lot better than C.

                               

                              I am thinking of using a motor rated at 12 volts, 14A. 

                               

                              wallarug

                                • Re: Wiring a motor to the RPi?
                                  Colin Barnard

                                  I'm about to go to bed, so this will be quick

                                   

                                  To switch 14A you would be better off using either a MOSFET or an IGBT.  You'd need to find a BJT (an NPN transistor) that has a maximum collector current rating of more than 14A, probably 20A for a safety margin, that is to cover the possibility of keeping the transistor happy. 

                                   

                                  An MJH6284G  for example would require at least 800mA into its base to switch it on, so you would need either as has been suggested a Darlington instead, or a smaller transistor between the GPIO and the base of the main BJT to switch it on. There are other  BJTs that would work but they have a less beginner friendly footprint being TO-3s such as the PMD16K80.  Personally I would use what are called logic gate MOSFETS, or you could use a dedicated motor driver chip which would save you some work.

                                  Others of course might see things differently, there is always more than one answer to solve an electronics problem.

                                   

                                   

                                  Colin

                                  • Re: Wiring a motor to the RPi?
                                    John Beetem

                                    Fergus Byrne wrote:

                                     

                                    bodgy wrote:

                                     

                                    The GPIO outputs can be high or low in output, in other words they can be made to switch from 0v (ground) to 5v or be held at 5v and switch low. You get the choice of deciding which level equals on or off.

                                     

                                    Firstly, what firmware do I need to swap between 5v and 0v.  Currently I am using GPIO-0.2.0 which does not have this capability.

                                     

                                    Secondly, I would like it to be in python becasue I know that a lot better than C.

                                     

                                    I am thinking of using a motor rated at 12 volts, 14A. 

                                     

                                    wallarug

                                     

                                    First, I'm sure bodgy meant that RasPi GPIO outputs can switch between 0V and 3.3V, not between 0 and 5V.  GPIOs can be configured as inputs or outputs, along with pin-specific special functions.  RasPi GPIOs  are not 5V tolerant.

                                     

                                    There's a nice treatment of RasPi GPIOs including software examples in C and Python at the RasPi Hardware Wiki: http://elinux.org/RPi_Low-level_peripherals.

                                     

                                    If you're switching 14A you might be better off with a mechanical or solid-state relay.  You'd still need an NPN transistor (or Darlington pair) or N-channel MOSFET to control the relay from RasPi, and you'd need a flyback diode across the relay.  This is assuming you just want to turn the motor on and off.  If you want to do variable torque and/or switch direction, you'll need more interesting circuits.

                            • Re: Wiring a motor to the RPi?
                              evan.stoddard

                              I think with the new gert board, it comes with software so you should be able to run it just fine.

                              • Re: Wiring a motor to the RPi?
                                KenSeymour

                                Sorry, I have been out of town most of the week.

                                 

                                I have an update to the schematic to correct errors caught by Michael Kellet.

                                 

                                He made me think about small amount of current heeded by the L239D chip.

                                 

                                Another way to think of it is a high impeadance input.  So we don't need

                                 

                                a lot of current gain in the transistor.

                                 

                                While darlingtons are high current gain.

                                 

                                The current on the chip input/transistor output will be set low by the high chip input impeadance.

                                 

                                When you use a transistor as a switch, you set the Ib * Hfe to be larger than where the ciruit

                                 

                                will operate in order for the transistor to alternate between cut off and saturation.

                                 

                                So I upped the resistor values but did not set Ib * Hfe to match the current needed by the

                                chip but rather to provide 10 or more times as much.

                                 

                                The real current will by limited by the chip input impeadance and you have a low voltage

                                dropped across R4/R5/R6.

                                 

                                Ib is the transistor base current.

                                Hfe is the transistor current gain.

                                 

                                By lowering the base current, we ask the Raspberry Pi to provide less current.

                                 

                                I drove motors with an Arduino.  It runs on 5 Volts.  It has analog inputs.  It has lots of PWM

                                outputs.  It won't run Linux or Python.

                                 

                                I have not yet tried the one PWM output on the Pi.  I don't know if a PWM driver is ready or not.

                                An 8 but micro-controller normally has PWN hardware outputs.

                                 

                                Software pwm is where you turn the GPIO output off and on and increase the speed

                                by having it on a higher percentage of the time.

                                 

                                I've thought of connecting an Arduino to the PI with 5 Volt serial or I2C and having the

                                Arduino do motor control but probably not get to it.

                                 

                                I have driven stepper motors directly of a GPIO with Darlingtons but without an H bridge chip.

                                 

                                Any way here is the updated schenmatic in PNG. I am trying to attach the Eagle schematic.

                                • Re: Wiring a motor to the RPi?
                                  yvanttt

                                  One option is to use the gertboard and drop in an atmega 328.

                                   

                                  With this setup  the PI will be the brain part and the arduino  the muscle portion of the setup.

                                  the Gertboard has already 2 motor interface on it.

                                  And the arduino has 6 PWM build in.

                                   

                                   

                                  It will then be easy for the PI to just send the PWM values to the arduino via the serial port or I2C as you wish.

                                    • Re: Wiring a motor to the RPi?
                                      wallarug

                                      48. Sep 27, 2012 4:37 PM (in response to Fergus Byrne)

                                      Re: Wiring a motor to the RPi?

                                      One option is to use the gertboard and drop in an atmega 328.

                                       

                                      With this setup  the PI will be the brain part and the arduino  the muscle portion of the setup.

                                      the Gertboard has already 2 motor interface on it.

                                      And the arduino has 6 PWM build in.

                                       

                                       

                                      It will then be easy for the PI to just send the PWM values to the arduino via the serial port or I2C as you wish.

                                       

                                      The problem is: How do I do that?

                                      • Re: Wiring a motor to the RPi?
                                        wallarug

                                        Ok,


                                        I have bought a freetronics 'Eleven' which is exactly the same as the Uno. Here is what I want to do:


                                        1. Two motors + one servo:

                                        The two motors will be moving the robot and will be changing speed according to the position of a Joystick. The servo will either be the steering or for the camera (depending on whether or not it is possible to turn with the two motors and PWM). Both will be connected


                                        2. The Joystick:

                                        It will be connected to a PC running Linux (which I still have to setup) and controlling the direction and speed of the robot on the x-axis (forward/backward) and y-axis(left/right).


                                        3. Camera

                                        I have not got the camera working correctly yet (speed issues) but I will get this to work. I think that it is a combination of incorrect settings and network lag (I will see to this).


                                        4. The Eleven

                                        All the motors will be connected to this including the servo. The Two motors will be connect to PWM channel via the double H-bridge chip. I have a schematic to do this.


                                        5. Software

                                        I have no clue on how the software will work except that I need 3 pieces: Ardunio, raspberry Pi and PC. All I know is that I want dynamic speed control with the joystick. The formula that I came up with was:

                                        (100/255)*i

                                        where 'i' = position of co-ordinate of Joystick on the y-axis.


                                        This will give out a percentage number which I want to replicate the percentage that motors are "ON" through PWM. I don't know if this can be done but I would like advice on how to go about it.

                                      • Re: Wiring a motor to the RPi?
                                        yvanttt

                                        as an example use pins 5 and 6 (pins 20 and 22 on J25) on the atmega328 and connect them to the motor_a and motor_b input (J5) on the gertboard.

                                        Then the uart TXD (gpio pin22) of the raspberry to the RXD (pin 2 of the atmega, pin 1 of J64)

                                         

                                        Then it is a simple mater to program the atmega328 to intercept the bytes sent from the raspbery and copy them to the 2 PWM ports.

                                         

                                        http://arduino.cc/en/Reference/AnalogWrite

                                        http://arduino.cc/en/Reference/Serial

                                          • Re: Wiring a motor to the RPi?
                                            wallarug

                                            Don't have a gertboard and won't get it for a while (Farnel issue).  But I am going to get an adruino eleven with ATMEGA328p which will help me do what I want.  I will look at your links though. 

                                             

                                            The only problem I have with coding such a chip.  I have no experience and will require massive amount of help with the coding in python (I know some already) or C (which sounds easy to learn).

                                          • Re: Wiring a motor to the RPi?
                                            yvanttt

                                            These Arduino are the easiest to program.

                                            They made an IDE package that works on Windows, OS max and Linux 32 or 64 bits. These devices uses C

                                             

                                            They have all the major function already available on their site "www.arduino.cc".

                                            Plus they have "sheild" that can get you started then use the sheild schematics to build your permanent setup.

                                              • Re: Wiring a motor to the RPi?
                                                wallarug

                                                Ok, here is my idea:

                                                Using some of the set-up features that @aonsquared used with his Robot/Rover thing.

                                                 

                                                layout1.png

                                                Using a 5 volt supply to power the Raspberry Pi and the Powered hub.

                                                 

                                                The Raspberry Pi will send commands to the Ardunio Eleven (which I still have to get), which will control the Two Motors through a H-bridge and PWM.

                                                 

                                                That camera will be powered through the 10 port hub, along with the Ardunio and the W/LAN dongle.

                                                 

                                                The Motors will be powered though a 12 volt supply (8 AA batteries).

                                                 

                                                All the commands will be sent through an Ad-Hoc Network to a Laptop running Windows 7 Pro 64 bit.  I think that I will use by Joystick to move the Motors but I don't know how that is going to work as @aonsquared used a PS3 controller instead. (Saitek ST90 is the Joystick).

                                                 

                                                The LCD display will have the IP address so that I know exactly what it is whenever.

                                                 

                                                The Raspberry Pi will host a live webcam feed though an old Logitec Camera I have lying around.

                                                 

                                                 

                                                One question is: Will PWM work on the motors through the Ardunio if I have the 12 volts going directly into the H-bridge Chip?   

                                                 

                                                 

                                                What do you think?   

                                              • Re: Wiring a motor to the RPi?
                                                yvanttt

                                                Yes it can be done.

                                                 

                                                Look at this chip the L298, it can accept input voltage for the motor up to 46 volts. The logic side is at 5 votls so it is safe for your Arduino.

                                                You can drive up to 2 amps per output.

                                                This chips can 2 motors or you can connect the outputs in parallel to double the current.

                                                The datasheet even has some  circuits examples to try.

                                                 

                                                http://www.datasheetcatalog.org/datasheet2/2/052daje928cw7pc0uqs1ipyryppy.pdf

                                                • Re: Wiring a motor to the RPi?
                                                  yvanttt

                                                  For the motor control via the joystick it is easy if you can convert the joystick position into values from 0 to 255.

                                                   

                                                  for example:

                                                   

                                                  move the stick forward, both PWM will increase simulatneously. this will increase your speed.

                                                  move the stick to the left or right, then deduct the values on one motor to make it turn.

                                                    • Re: Wiring a motor to the RPi?
                                                      wallarug

                                                      Would you be able to help me code this?

                                                       

                                                      Also, if the neutral position of the stick is (0,0) then that would mean the range would be from -255 to +255 on both the x- and y-axis.  Right?

                                                      • Re: Wiring a motor to the RPi?
                                                        wallarug

                                                        Yvan Turcot wrote:

                                                         

                                                        For the motor control via the joystick it is easy if you can convert the joystick position into values from 0 to 255.

                                                         

                                                        for example:

                                                         

                                                        move the stick forward, both PWM will increase simulatneously. this will increase your speed.

                                                        move the stick to the left or right, then deduct the values on one motor to make it turn.

                                                        I just wiped up this python script.

                                                        # Converting Joystick Position to a Percentage that the PWM

                                                        # Channel can simluate on the motors

                                                        # By Wallarug

                                                        # Change the input value "i" to something that relates to the Joystick

                                                         

                                                         

                                                        f = float(100)/float(255)

                                                        i = input("Enter a number between -255 and +255: ")

                                                         

                                                         

                                                        while i!="":

                                                            x = float(f)*int(i)

                                                            print "PWM, " + str(x) + "%"

                                                            i = input("Enter a number between -255 and +255: ")

                                                         

                                                         

                                                        # By Wallarug

                                                        Should work if it is converted to a different language and add in the motor commands and Joystick.