Raspberry Pi, Python, and a TB6600 Stepper Motor Driver

Introduction: Raspberry Pi, Python, and a TB6600 Stepper Motor Driver

This Instructable follows the steps I took to connect a Raspberry Pi 3b to a TB6600 Stepper Motor Controller, a 24 VDC Power Supply, and a 6 wire Stepper motor.

I'm probably like many of you and happen to have a "grab bag" of left-over parts from many an old project.. In my collection I had a 6 wire stepper motor, and decided that it was time to learn a bit more about how I could interface this to a Raspberry Pi model 3B.

As a bit of a disclaimer, I didn't invent the wheel here, I simply pulled together a bunch of information readily available on the Web, added my little slant to it, and tried to make it work

The intent here was really just to pull a few things together (at minimal cost), write some Python code for my Raspberry Pi, and make the motor spin. This is exactly what I managed to accomplish.

So let's get started...

Step 1: The Raspberry Pi

As for the Raspberry Pi, I used three standard GPIO pins so this should work (I have not tested) with any Pi, or Orange board, Tinker board or clones that are available out there. You may (and should) comb through my overly commented Python code and select different GPIO pins if you are using a different processor, or just want to change things around a bit.

Please note that I am direct connecting to the GPIO pins on the RPi, so I'm limiting the voltage that the GPIO pins see to 3.3volts.

Step 2: TB6600 Stepper Motor Driver / Controller

As I previously noted, I opted to use a TB6600 Stepper Motor Driver / Controller.

This controller is:

  • Readily available (search eBay, Amazon, Ali Express or many others).
  • Very configurable with easy access switches.
  • Configuration and wiring details are silk-screened on the case.
  • Input voltage range of 9 VDC to 40 VDC
  • Capable up to 4 amp motor drive output.
  • Has an internal cooling fan and decent heat sink.
  • Is equipped with 3 removable connectors.
  • Has a small footprint,
  • Easy to mount.

But the low cost to purchase is really what sealed the deal on this one.

Step 3: The Stepper Motor...

The stepper motor I used is a bit of an unknown.. I've had it for many years, and don't recall the history of how I acquired it or what it's previous use was.

In this Instructable I'm not going to detail how to figure out it's capabilities - I don't have a real life use for it (other than experimental) so I'll skip that.

I used a fairly generic stepper motor. I did spend a bit of time on YouTube and here on Instructables to try and decipher the wires coming from it.

My motor actually has 6 wires on it... In this application, I left the two "Center Tap" wires insulated and unconnected.

If you have a similar "generic" type of stepper motor, I'm certain that with an Ohm Meter and a bit of time you too could figure out the wiring and make it work in this manner. There are plenty of YouTube videos that will guide you to easily sorting out your own motor.

Step 4: Power and Power Supplies.

Caution needs to be given here...

Depending on your build, you may need to connect to Line Voltages (house power). Be sure to use all appropriate safety precautions:

  • DO NOT try to make electrical connections to live power sources.
  • DO use appropriate size fuses and circuit breakers
  • DO use a power switch to power your PSU (this will make it easy to isolate the power supply from live line voltages).
  • DO properly terminate all wires and make robust connections. Don't use clips, or frayed wires, or ill fitting connectors.
  • DO NOT use Electrician's Tape as an insulator

I used a 24 VDC (5 Amp) Power supply to power the Stepper Motor Driver Controller. I also used the output of this same power supply to drive a DC to DC Buck PSU in order to generate 3.3 volts to use as the source for the ENA, PUL and DIR signals (see the wiring Diagram)

DO NOT try to use the RPi to sink current from a 5.0 VDC source.

I DO NOT recommend trying to source the " + " sides of the PUL, the DIR and the ENA signals with 3.3 VDC from the RPI.

Step 5: Circuit Protection...

Note that in the wiring diagram that follows, I do not mention how to connect the power supply to "AC Power", or list a circuit breaker for it. If you are intent on building a test system similar to this, you'll need to take the time to specify a Circuit Breaker and Fuse that will match the power supply(ies) that you will be using. Most modern power supplies have voltage and current specifications listed on them. These need to be followed, and appropriate circuit protections installed.

Please... Don't skip this important step.

Step 6: The Wiring Diagram

Power Supplies

The output of the 24 VDC power supply is fused with a 5 Amp fuse and then routed to:

  • TB6600 Stepper Motor Driver / Controller "VCC" pin (RED wire in the diagram).
  • It is also routed to the input of the 3.3 VDC "DC to DC Converter" (again a RED wire in the diagram).

The output of the 3.3 VDC "DC to DC Converter" is routed to pins "2", "4" and "6" of the TB6600 Stepper Motor Driver / Controller (BLUE wire in the diagram).

NOTE - the controller itself marks these pins as "5V".. It will work if 5V were supplied to those pins, but because the voltage ratings of the GPIO pins on the RPI, I opted to limit the Voltage to 3.3 VDC.

NOTE - I DO NOT recommend trying to source the " + " sides of the PUL, the DIR and the ENA signals with 3.3 VDC from the RPI.

GPIO Mapping


GPIO Mapping GPIO 17 PUL PINK wire in diagram GPIO27 DIR ORANGE wire in diagram GPIO22 ENA GREEN wire in diagram

Step 7: Operation

Basically, the Raspberry Pi hardware controls three signals:

GPIO Mapping GPIO 17 PUL GPIO27 DIR GPIO22 ENA

GPIO22 - ENA - Enables or disables the functionality of the Stepper Motor Driver / Controller.

When LOW, the controller is DISABLED. This means that if this line is HIGH or NOT connected, then the TB6600 is ENABLED, and if proper signals are applied, the motor will spin.

GPIO27 - DIR - Sets the motor spin direction.

When HIGH or Not Connected, the motor will spin in one direction. In this mode, if the motor is not spinning in the direction you want, you can swap the two A motor wires with each other, or the two B motor wires with each other. Do this on the green connectors at the TB6600.

When this pin goes LOW, the TB6600 will switch internal transistors, and the motor direction will change.

GPIO10 - PUL - Pulses from the RPI that tell the TB6600 Stepper Motor Driver / Controller how fast to spin.

Please refer to the attached images for the setting of the Stepper Motor Driver / Controller switch positions I used.

Step 8: Python Code

Attached is my overly commented code.

Feel free to use and edit this as you wish.. I found parts of it on the web, and added it for testing and evaluation purposes.

== == ==

Step 9: Synopsis

It worked.. there is a lot of room for improvement, and the code could be cleaned-up, but OK.

I'd appreciate hearing your thoughts suggestions and of any changes / updates you make.

Thanks.

Be the First to Share

    Recommendations

    • Fandom Contest

      Fandom Contest
    • First Time Author Contest

      First Time Author Contest
    • Metal Contest

      Metal Contest

    21 Comments

    0
    AlexeySorokin
    AlexeySorokin

    10 months ago

    Thank you, everything works.I used a different scheme. Connected RPi directly without a Converter DC DC.

    0
    AlexeySorokin
    AlexeySorokin

    10 months ago

    Hello! Help me. I have completed a scheme according your
    isntruction. I used RPi 4 with driver DM556D and DCDC
    converter XL4015, but nothing worked out. It is not shown on scheme
    should DCDC converter output ground should be connected to the GPIO
    ground.



    I tried both ways connecting DCDC converter
    output ground to the 39 pin or not.



    When ground was not connected, voltage on DM556D logic
    input was ( 2 V on false, and 1 V on
    true)

    With ground it is was ( 2.7 V on false, and 0.3
    V on true)





    I tried to increase the output voltage of DCDC converter up
    to 5V assuming wires was too long (but they not) - numbers changed to (1.4 V on false,
    and 3.5 V on true) with ground and

    (2 V on false, and 1 V on true) without ground.



    After I tried to use GPIO 5V as a source for DM556D logic input and it
    worked\fried?



    What can be wrong? Should DCDC converter be connected to
    GPIO ground?Thanks in advance.

    0
    Novellus
    Novellus

    Reply 10 months ago

    Good day Alexey.. I hope you remain well during this time of Covid. Thanks for following my Instructables.

    As a background, I have NOT used a DM556D Driver in the past, but looking at a manual for it ( http://www.leadshine.com/UploadFile/Down/DM556m.pdf) and ( https://www.dewochina.com/uploadfiles/103.224.250.31/webid156/Downloadfile/201601/56a99b7b7ba18.PDF ) it appears to be similar in many ways to the TB6600 (which is what I have and use).

    In the manual ( http://www.leadshine.com/UploadFile/Down/DM556m.pdf ) refer to Section #4 - figure #2 that is an almost identical circuit to what I describe using with my TB6600 modules. They again show the same circuit in section #9

    About the DC-DC converter in my scheme.. You are correct - the OUTPUT ground is NOT connected to a Ground.. So to be clear, in the module that I used - INTERNALLY - to the DC-DC converter, those two points ARE connected together. Based on your comment, I will update the drawing next to show that is should be grounded. So to answer your question:

    YES, the DC-DC Converter OUTPUT GROUND should be connected to the GPIO ground (pin #39 for example).

    Check your own DC-DC converter - I sense that the INPUT ground and the OUTPUT ground will also be connected together. But I could be wrong. Hopefully it is.

    == == ==

    For the circuit I provided, functionality of the DIR, ENA, and PUL is as follows.

    1 - the DC-DC converter generates a 3.3 VDC supply level. I wire that 3.3 VDC to the " + " side of each of those three signals. I used a DC-DC converter because I did NOT want to supply this from the RPi. NOTE that for short term testing, using the RPi as a 3.3 volt supply will function, but DO NOT use it this way for long term uses. Doing that will overstress the 3.3 VDC regulator on the RPI.

    2 - remember that the Signals that drive the DM556D Driver are LOW ACTIVE..

    2a - In your DM556D Driver, when the " ENA- " pin is LOW, then the DM556D Driver is ACTIVE (it will drive voltage to the motor. In my diagram, that is the GREEN line.. For testing purposes, you can connect that directly to GROUND (GND) (instead of GPIO # 22) to enable the controller.

    2b - In your DM556D Driver, when the " DIR- " pin is LOW, that will cause the motor to spin in one direction. When the DIR- pin is high (3.3v) that will cause your motor to spin in a different direction. I found that in my TB6600, if that pin is not connected, the motor spins in the same direction as if the pin was connected to 3.3 VDC.. That means that a LOW (ground for example) is required to change the motor spin direction. For testing purposes, you can connect that directly to GROUND (GND) (instead of GPIO # 27) to force a direction change.

    2c - In your DM556D Driver, the " PUL- " pin must be made to change state. You cannot simulate this - is must be driven by the GPIO pin and software.

    So effectively, for TESTING, on your DM556D Driver if you GROUND the ENA- pin, and GROUND the DIR- pin, all you then really need is supply from the DC-DC converter to the three " +" pins on your DM556D Driver, and a signal from your RPI on the PUL- connection of your DM556D Driver.

    I'll refer you you once again to Section #9 - Figure #9 of the manual. Effectively I replaced what they show as VCC (in their "controller") with the 3.3vdc DC-DC converter.

    Voltage Levels..

    I'll again refer you to Section #9 - Figure #9 of the manual. If you review the internal circuit of the DM556D Driver you'll find that from each of the three " + " connection points is a 270 Ohm resistor, connected to a Diode (LED of an OPTO-Isolator), and effectively we connect the cathode of that diode to a "LOW ACTIVE" signal at the " - " connection point on your DM556D Driver. That is the signal path.

    When you drive the " - " pin low, current flows from the 3.3 VDC DC-DC converter through the 270 Ohm resistor, through the LED diode (illuminating it and causing the Opto-isolator to function) and then to "a low point" (Low active GPIO pin or Ground) in your circuit. This means that voltage levels will be low.

    == == ==

    I am not understanding your comment:
    ----
    After I tried to use GPIO 5V as a source for DM556D logic input and it
    worked\fried?
    ----

    You will NOT fry or kill your DM556D Driver if you use 5 VDC instead of 3.3 VDC. I chose 3.3 vdc, to closely simulate a RPi GPIO voltage level. As a side note, the Arduinno GPIO levels are 5 VDC, and that controller would work fine using 5 VDC GPIO voltage levels.

    DO NOT supply 5 VDC to any of the RPi pins.

    == == ==

    My recommendations.

    1 - Check your DC-DC converter to see if the input ground is connected to the output ground.
    2 - DO connect the output ground of the DC-DC converter to RPi GD (pin #39).
    3 - Tie ENA- to GND (for testing purposes)
    4 - Tie DIR- to GND (for testing purposes)
    5 - Confirm that your RPI is generating a drive signal on it's assigned GPIO pin when software is running (I used GPIO #22 - both is software and in hardware).
    6- Consider trying to set the DC-DC converter output to 5.0 VDC (for testing purposes) instead of 3.3 vdc.

    7 - Do you trust your Stepper motor? Does it work? Is it connected properly? Does it get warm when being driven (even if not spinning)? Are the switches set correctly on the DM556D Driver?

    8 - What is your supply voltage the the DM556D Driver?

    9 - What is the supply voltage to the DC-DC converter?

    Finally...

    Refer to page 4 of the manual... They do not mention functionality of 3.3 vdc.. only 4-5 vdc... It is possible that for the DM556D Driver it will not work with 3.3 VDC in my configuration - specially if the PUL signal cannot reach the desired voltage level required by the controller.

    If that is the case, you may need to re-think this experiment.

    Please let me know how you make-out.

    0
    Novellus
    Novellus

    Reply 10 months ago

    Ok.. So I'm thinking more and more that the problem here is that you are trying to control the DM556D Driver with 3.3 VDC (RPi) and not 5.0 VDC (most other systems - Arduino for example) .. Both the DM556D Driver manual and the silkscreen on the side of the DM556D Driver refer to 5V Operation.

    Looking at the manual, the internal diagram of the DM556D Driver indicate that it has a 250 Ohm resistor in series with the OptoCoupler.



    That 270 Ohm resistor inside that DM556D Driver may be limiting current through it's diode, and not allowing the Optocoupler inside the DM556D Driver to come ON when the output of the RPi is high (3.3 vdc).

    Take a few minutes to read this Instructables: It helps explain things.

    https://www.instructables.com/id/How-to-Use-an-Optocoupler-to-Pass-Signals-Between-/

    Do you have an Arduino you can try this with? I could send you some code.

    BTW - today I decided to buy a DM5560D Driver.. once I receive I I'll make it work and put together an Instructable.

    0
    AlexeySorokin
    AlexeySorokin

    Reply 10 months ago

    Question:Do you trust your Stepper motor? Does it work? Is it connected properly? Does it get warm when being driven (even if not spinning)? Are the switches set correctly on the DM556D Driver?
    Answer : Yes, Stepper motor work correctly, I used several stepper motor, as well I used driver with LPT logic and everything worked. Yes, he get warm when being working, but no spinning.
    Question:What is your supply voltage the the DM556D Driver?
    Answer : Supply voltage on the 24 V.
    Question: What is the supply voltage to the DC-DC converter?
    Answer : Supply voltage on the 24 V.
    I was connected driver without DC-DC converter so directly to supply voltage on the 24 V. The voltage at the pins is 2.3 V.
    RPi work correctly.
    0
    Novellus
    Novellus

    Reply 10 months ago

    Thanks for the answers.. I left you a message to your other reply.. Please go check that one out.

    0
    AlexeySorokin
    AlexeySorokin

    10 months ago

    Hello. Stepper motor no spinning. I put together an updated circuit and got: when the voltage on conventer DC - DC 3.3 V, pin voltages are as follows DIR and ENABLE 2.3 V PULSE 1.3 V, when the voltage on conventer DC - DC 5 V, pin voltages are as follows DIR and ENABLE 3.6 V PULSE 2.7 V.
    As if there is not enough voltage on PULSE, but if you raise it, the logic breaks on ENABLE.
    What can I do?
    Thanks in advance.

    0
    Novellus
    Novellus

    Reply 10 months ago

    Alex.. I 've just uploaded a new EXPERIMENTAL circuit for you (see the attached). I moved the green "ENA-" wire to ground, and I moved the orange "DIR-" wire to ground.

    Still using 3.3 Volts

    Be sure that the "PUL-" wire is connected to GPIO17 (RPi Pin #11) and that the software you are using drives GPIO17


    Take a photo of the switches on your DM556D Driver and of your whole build, and post it here for me to see.

    Also take a photo of the meter you are using to measure the voltages on PUL- -==> While GPIO17 trying to drive the motor. Take another photo of the same but in the second photo - disconnect the "PUL-" wire at the DM556D Driver (we need to see what the RPi is driving with NO load.

    Troubleshooting...

    Do you have access to a LED and 330 or a 470 Ohm resistor?

    See the attached image. Copy that scheme and see if the LED comes ON... If it does, then move the BLACK wire from GND to GPIO17 and tell me the results when you run the software (really SLOW RMP mode).

    When driving the motor software at the slowest possible speed, does the LED blink? Slowly increase the RMP in the software.. does the LED blink faster?

    Wiring_Diagram_03.JPGAlex_03.JPG
    0
    nickmok
    nickmok

    1 year ago

    Hello! I am trying to do this exact setup. I am wondering why you do not recommend using the Pi's 3.3V pins to source the "+" ports of the ENA, DIR, and PUL sections?

    0
    Novellus
    Novellus

    Reply 1 year ago

    Hi to you.. I (and many others) do not recommend using the RPi to drive items such as these since the Pi has very low current driving capability.. Depending on what you are doing, you could quickly overload the Pi and damage the GPIO port.

    Using an external supply source alleviates this risk.

    If you are driving only one motor driver, you may well be successful and not damage your Pi.. but to be safe, you should use an external supply voltage to drive these motor drivers.

    0
    nickmok
    nickmok

    Reply 1 year ago

    Hello, thank you for your reply! I will give it a try with an external power source then.

    0
    jonbull
    jonbull

    1 year ago on Step 6

    Thanks a million. Answered a few basic questions on the setup as there's so little documentation on this & nothing in the box! Had this up & running in no time with your intro

    0
    LivingLearning
    LivingLearning

    Question 1 year ago

    Hello Novellus. Thank you so much for the help I was able to get this project completed but what I am trying to do requires a lot more pins. However I am unable to get the name 23 to move from a MCP23017 port expander chip. https://www.adafruit.com/product/732.

    Please if you can take a quick look and give any advice or if you spot a problem please let me know. I really am stuck

    python code below

    import wiringpi
    i2c_addr = 0x20
    i2c_addr_2 = 0x21
    pin_base = 65
    DIR = 65
    STEP = 66
    EN = 67
    CW =1
    CCW =0
    wiringpi.wiringPiSetup() # initialise wiringpi
    wiringpi.mcp23017Setup(pin_base,i2c_addr)
    wiringpi.mcp23017Setup(pin_base+16,i2c_addr_2)
    wiringpi.pinMode(DIR,1)
    wiringpi.pinMode(STEP,1)
    wiringpi.pinMode(EN,1)
    wiringpi.pinMode(68,1)
    while(True):
    wiringpi.digitalWrite(68,1)
    wiringpi.digitalWrite(EN,0)
    wiringpi.digitalWrite(DIR,CW)
    for x in range(1600):
    wiringpi.digitalWrite(STEP,1)
    wiringpi.delay(5)
    wiringpi.digitalWrite(STEP,0)
    wiringpi.delay(5)

    IMG-20200604-WA0000 (1).jpg
    0
    Novellus
    Novellus

    Answer 1 year ago

    Hi...

    For my own sake.. please confirm that you can get the NEMA 23 motor to function correctly with the TB6600.... then if I understand correctly, moving the TB6600 (4 wires) to the MCP23017 using the WIRINGPI code you provided (in your message) you cannot get the NEMA 23 motor to be driven.

    A question for you is: are you able to drive a simple LED on the same pins of the MCP23017 that you plan on using to drive the TB6600? A simple program like "Hello World" / "Blink" type of thing, just to see that you have your I2C bus properly communicating, and that you are able to effect a change on the MCP23017 GPIO pins.

    ALSO... MCP23017 addressing may need to be reviewed.. and please note that I don't know much about this chip - but... on your 1no you have all three address pins tied LOW (0).. and on your 2no - Pin #15 (A0) is toggled by the Pi / wiringpi... my concern is that unless you drive 2no Pin 15 to high (could be 0 or could be 1), then there are two devices (MCP23017) that could respond to address 0. consider wiring 1no in a manner like 2no so that it has a driven A0, and never a match to the that of 2no...

    or hard wire 20 - 15(A0) to VCC in order to keep it opposite that of 1no - 15(A0)

    or for testing purposes, remove power from 2no / remove 2no completely in order to ensure you do not have addressing conflicts.

    Let me know your findings, and in the mean time, I'll review your code.




    0
    Novellus
    Novellus

    Reply 1 year ago

    On further review, it looks like for your 2no, you have the A0 bit tied the RPI 3V3 Power pin - so that should be OK - addressing may not be the issue..

    0
    LivingLearning
    LivingLearning

    Question 1 year ago on Step 6

    Thanks for sharing this instructable. I would like to give it a go. Where can I find 3.3 VDC "DC to DC Converter"? I have looked for this part but I can not seem to find it. Could you share a link to where I may be able to purchase? Thanks

    0
    Novellus
    Novellus

    Answer 1 year ago

    These are readily available at most online DIY shops.. example from eBay or Amazon are:

    eBay has a good variety of them:

    https://www.ebay.com/sch/i.html?_from=R40&_trksid=...

    Amazon also has them - your choice...

    https://www.amazon.com/s?k=DC-DC+Buck+Step+Down+Co...


    Basically it is a BUCK "Step Down" converter that will convert (in my case) 24 VDC to 3.3 VDC. For this little Instructable, you need less that 1 Amp output on the 3.3VDC

    Let me know if you have any other questions.

    0
    LivingLearning
    LivingLearning

    Reply 1 year ago

    wait you said less than one amp. All of these are one amp. Will 1 Amp Work?

    0
    Novellus
    Novellus

    Reply 1 year ago

    1 amp or more than 1 Amp will work.. in truth you will only be using mAmps...

    The Dc to DC converter is a low cost way to generate 3.3 vdc. That 3.3 VDC will be used to drive the three LEDs inside the TB6600 and that is VERY LOW current draw.. Those LEDs are part of the Optocouplers that provide isolation between the RPI and the TB6600. Hence me saying that all you need is LESS THAN 1 amp.

    Research "Buck Converters" on those two sites, there are tons of models. select a low cost one.. nothing fancy..

    I actually used this one:

    https://www.ebay.com/itm/DC-DC-Buck-Step-Down-Modu...

    0
    LivingLearning
    LivingLearning

    Reply 1 year ago

    Thank you so much I was looking at something completely different. that was the missing piece. I will let you know if I do have any other questions but as of now I can't thank you enough for taking the time to answer my question so I can move forward!! thanks!