Advice for building my own as simple as possibile stepper motor driver

Hi, I am currently trying to build my own circuit to drive a bipolar stepper motor (or rather 3 of them) on a breadboard. So far i build an H-Bridge for every coil, with IRF9540N and IRF520N mosfets. Then i connected the gate of each N-channel mosfet with the gate of the P-channel mosfet on the same side and this directly to the arduino uno, so i only need two pins for each h-bridge and, at least that is my thought, i could assure that not both mosfets on one side are on. I tested it and it worked for some days, but then one motor started stuttering, then another one. I discovered that some N channel mosfets were suddenly always conducting. My guess is that they overheated, because when one side switches from high to low shortly both n and p mosfets on that side conduct. I mean the n channels sometimes (i didnt recognize any pattern) got rather hot. But still barely touchable, so i cant really imagine this killing them. I would be rather thankful if somebody had any idea what was happening or if it was a mistake i already fixed but forgot about that damaged them.
Also i dont know if i need pull-down/up resistors in this setup (or if the connection to the arduino prevents leaving the gates in a floating state) or if a resistor between the arduino and the gates would be useful or just slows the switch time.
As a power supply i am using a bench power supply currently set to 3 A (max rating of the motors) and 5 V.
Thanks for any help in advance, but please bear in mind that i am a total beginner.
Edit:

#include <util/delay.h>
#include "registers.h"

// bit macros
#define biton(register, bit) *register |= (1 << bit)
#define bitoff(register, bit) *register &= ~(1 << bit)
#define readbit(register, bit) (*register & (1 << bit))

// stepper motor functions
struct stepper
{
  // keeps track of next step to know which coils are needed
  int iStepPos;
  // microcontroller Port for controlling the coils
  volatile unsigned char *pPort;
  // bits in port
  char cCoil1Forwards;
  char cCoil1Backwards;
  char cCoil2Forwards;
  char cCoil2Backwards;
};

// stepper functions
void
initStepper (struct stepper *motor, volatile unsigned char *pDDR,
             volatile unsigned char *pPort, char cBitCoil1Forwards,
             char cBitCoil1Backwards, char cBitCoil2Forwards,
             char cBitCoil2Backwards)
{
  // set coil pins to write
  biton (pDDR, cBitCoil1Forwards);
  biton (pDDR, cBitCoil1Backwards);
  biton (pDDR, cBitCoil2Forwards);
  biton (pDDR, cBitCoil2Backwards);

  // initialize vars in struct motor
  motor->pPort = pPort;
  motor->cCoil1Forwards = cBitCoil1Forwards;
  motor->cCoil1Backwards = cBitCoil1Backwards;
  motor->cCoil2Forwards = cBitCoil2Forwards;
  motor->cCoil2Backwards = cBitCoil2Backwards;
  motor->iStepPos = 0;
  motor->dTimeOfLastStep = 0;

  // set all coils to starting value
  bitoff (motor->pPort, cBitCoil1Forwards);
  bitoff (motor->pPort, cBitCoil1Backwards);
  bitoff (motor->pPort, cBitCoil2Forwards);
  bitoff (motor->pPort, cBitCoil2Backwards);
}

void
step (struct stepper *motor, char bClockwise)
{
//TODO: upgrade to halfstep`

`
#define NROFSTEPS ((200 / 50) - 1)
  // set iStepPos depending on direction and if cycle is compleded reset
  // to the other end of the cycle
  if (bClockwise > 0) // clockwise
    {
      if (motor->iStepPos < NROFSTEPS)
        {
          motor->iStepPos++;
        }
      else
        {
          motor->iStepPos = 0;
        }
    }
  else // counterclockwise
    {
      if (motor->iStepPos > 0)
        {
          motor->iStepPos--;
        }
      else
        {
          motor->iStepPos = NROFSTEPS;
        }
    }
  // depending on sign of step execute the next or the previous step
  switch (motor->iStepPos)
    {
    case 0:
      bitoff (motor->pPort, motor->cCoil2Backwards);
      biton (motor->pPort, motor->cCoil1Forwards);
      break;
    case 1:
      bitoff (motor->pPort, motor->cCoil1Forwards);
      biton (motor->pPort, motor->cCoil2Forwards);
      break;
    case 2:
      bitoff (motor->pPort, motor->cCoil2Forwards);
      biton (motor->pPort, motor->cCoil1Backwards);
      break;
    case 3:
      bitoff (motor->pPort, motor->cCoil1Backwards);
      biton (motor->pPort, motor->cCoil2Backwards);
      break;
    }
}

int main ()
{
    // init stepper
  struct stepper test;
  initStepper (&test, DDRD, PORTD, 0, 1, 2, 3);

  //do one turn for testing
  for (int i = 0; i < 200; i++)
  {
    step(&test; 1);
    _delay_ms(3); //3 is not final, but the minimum that works and will be adjusted to match intended speed
  }
}

registers.h contains only define statements for register names like this:

#define PORTB ((volatile unsigned char *)0x25)

I wrote it before I discovered avr/io.h.

Schematic of the h bridge I currently have:

Stepper motor datasheet:

  • Google H bridge shoot - through

Hi, @plaguebump
Welcome to the forum.

Sory but had to spread your post out a bit.

Can you please post your code and a schematic?
How to do this is in the link above.
What stepper are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Extremely poor choices, not at all suitable for direct connection to 5V or 3.3V logic circuits.

Also, as mentioned, you need to do a bit more research on H-bridge design.

1 Like

I think this illustrates that your powers of imagination are not as good as you think they are.

This is something that will kill them, as other posts illustrate.

1 Like

First thanks for every piece of advice.
So as i understand it currently i basically get a short every time my h bridge switches, what kills the mosfets. Ok, this makes sense and calls for a redesign(I am always happy about hints). I only dont understand why only some of them died and not all, since they all will have this problem.
Also i will (as i get it) have to add flyback diodes, so the induced current wont kill anything.
And my mosfets are a poor choice? Totally possible, but why? And how do i make a good choice?
And I still dont know if a resistor between arduino and gate would just slow down the switch time or is a essential component.

Sorry, but your steppers are not designed to be driven by a simple H-bridge. They need a current controling stepper driver, e.g. a TB6600.
The rated current as stated in the datasheet is per coil, not per stepper.

If you really want to build your own simple H-bridge ( why? ) to drive a stepper, you need a high impedance ( voltage drven ) stepper.

2 Likes

I wondered that too. OP, is there something you need that an off the shelf driver can't accomplish?

Its only for educational purposes: When I can do it on my own, without any black boxes or abstractions (or as less as possible) I feel assured that i know what is actually going on. If i only read about "how does a stepper motor driver work?" (or really any topic) I get an idea of the general concept, but miss out on so many details.

But especially then it makes no sense to bring things together that don't fit together.

Not everything dies if the abuse is not catastrophe abuse. When you exceed the specifications of a device in a less radical way then you damage it but the life of the component will be shortened. That is why they don't all fail at once.

This is less spectacular than gross abuse, but it is what happens. The device may run for another year but it will eventually it dies prematurely.

You can measure the mean time between failures by testing say one hundred for six months at elevated temperature and humidity. Something you can't easily do at home. But that is what they do in industry.

This applies to lots of things, like smoking and lung cancer. Especially if nothing else gets you first.

1 Like

Those are not logic level devices, and are rated to be fully turned on at +/- 10V (N/P) gate to source voltage. You appear to lack the analog electronics background required for putting together such circuitry.

2 Likes

Then you should consider using a simulator like LTspice. It will be much more educational and you don't have to worry about burning up MOSFETs.

So after testing around in ltspice I used IRf9540N(there are better ones, but i had these at home) and IRLU024NPbF mosfets for my hbridge. To my schematic i added 1kohm pulldown resistors and between the arduino and the gates 220 ohm resistors. Now it runs as expected.

Great.
Have a nice day!

As expected in real life?

Can you please post a undated schematic?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

2 Likes

You are damaging your FETs due to a condition called "shoot-through," where both MOSFETs are on simultaneously. For example, assuming a 5V supply and using the N-channel source as the reference, when your input voltage is between 2V and 3V, both the P-channel and N-channel devices are on at the same time. As the MOSFETs heat up, their characteristics can change, worsening the problem. If the voltage goes much above 5V, you risk releasing the "magic smoke" and permanently damaging the components.

To address this, you need to redesign your circuit to ensure that no more than one MOSFET on each side is ON at any given time. Additionally, you must select MOSFETs that are appropriate for the voltages used in your circuit.

1 Like

Must be in Sim. How many got an H-bridge right for the very first time?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.