# H-bridge, Control circuit - thingy.

#### Roos

Joined Jan 29, 2011
6
Hello everyone!

I'm in the process of trying to create a vehicle that can drive around using 2 motors, thus changing the direction of the motors to turn corners.

I use an ATMega16 to control the vehicle- to control each H-bridge 2 signals will be delivered.

where i'm stuck is, that I an uncertain how I should connect these 2 signal to the h-bridge so that it will work as intended. I have drawn a basic Diagram which I hope will clarify what i'm asking

I think I might be able to somehow hook up an ekstra pair of transistors so that (Q2, Q3) and (Q1, Q4) opens and closes as pairs.

Not completely sure how to proceed though, also, I imagin i'll be making a small circuit using discreete logic to make sure the invalid input will never reach the bridge.(incase that will have an effect on the solution)

I hope my question is clear (And has not been asked before)

- Roos

#### Attachments

• 43.4 KB Views: 54

#### SgtWookie

Joined Jul 17, 2007
22,210
See the attached schematic and simulation.

You're going to lose anywhere between 0.7v and 1.6v per Darlington depending on your motor's load.

#### Attachments

• 61.2 KB Views: 39

#### hgmjr

Joined Jan 28, 2005
9,029
Since the ATMEGA16 has 36 IO lines (provided you use a crystal), You may find it worthwhile to consider allocating 4 lines rather than 2 lines to the H-bridge. The added flexibility will prove useful. For example, you can implement dynamic braking by turning on both upper or both lower legs of the H-bridge.

hgmjr

#### Roos

Joined Jan 29, 2011
6
@SGTWookie
In the setup you've made, my input signals (A,B) will only activate the lower part of the H-Bridge, but I see the upper part is connected to the collector.

What will happen when for example A = 1 and B = 0, so that Q1 opens up for a current, will this cause Q4 to open up as well?

(Sorry for the "stupid" question, i'm kinda new to transistors.)

@hgmjr

What you are suggesting sounds quiet interesting - However, as I had understood turning on all transistors will burn off the H-bridge?

how would this dynamic braking work, if you care to elaborate?

#### arthur92710

Joined Jun 25, 2007
307
By turning on both Q3 and Q4, the motor is 'shorted'. While spinning on its own it produces a voltage. The voltage then tries to spin the motor in the opposite the direction. It will slow down faster then just opening the transistors.

You might use if if the robot stops on an incline...

#### SgtWookie

Joined Jul 17, 2007
22,210
@SGTWookie
In the setup you've made, my input signals (A,B) will only activate the lower part of the H-Bridge, but I see the upper part is connected to the collector.
The PNP Darlington bases are wired to the opposite side NPN Darlington collectors.

What will happen when for example A = 1 and B = 0, so that Q1 opens up for a current, will this cause Q4 to open up as well?
In the case of A=1 (5v), B=0 (0v):
Q1 is turned ON, and starts sinking current via its collector to its emitter. There is a wire connecting the collector of Q1 to the right side of R6, which limits base current for Q4, the PNP on the upper right side, turning it on. Current begins flowing from the emitter through the collector of Q4, through L1 (the motor), then down through Q1 to ground.
Q2 remains OFF; if it turned on then there would be a dead short from Q4 through Q2 to ground.

You may find that R1 through R4 are a bit too large in value if your motor current is heavy. You could reduce them to 10k or so with little penalty.

(Sorry for the "stupid" question, i'm kinda new to transistors.)
Not a stupid question at all.

@hgmjr

What you are suggesting sounds quit interesting - However, as I had understood turning on all transistors will burn off the H-bridge?

how would this dynamic braking work, if you care to elaborate?
You'd need to add a couple of NPN transistors to pull down the bases of the PNP transistors to get the braking functionality.

Basically, if both high-side transistors are OFF and both low-side transistors are ON, you get a current path through the lower transistors and the motors. But, the same kind of thing happens with the diodes in the middle of the bridge.

The TIP120 and TIP125 transistors both have integral body diodes, but it's a good idea to have fast-recovery external diodes like you've shown.

Do a search for their datasheets to see what the electrical connections inside are.

#### hgmjr

Joined Jan 28, 2005
9,029
By turning on both Q3 and Q4, the motor is 'shorted'. While spinning on its own it produces a voltage. The voltage then tries to spin the motor in the opposite the direction. It will slow down faster then just opening the transistors.

You might use if if the robot stops on an incline...
You can obtain the same braking action by turning on Q1 and Q2 at the same time with Q3 and Q4 turned off.

hgmjr

Last edited:

#### hgmjr

Joined Jan 28, 2005
9,029
It should be noted that this braking action is only available if you provide individual controls to all four transistor switches in the H-bridge from the microcontroller.

hgmjr

#### Roos

Joined Jan 29, 2011
6
alright, I think I get it now, most of it anyway

Basically, if both high-side transistors are OFF and both low-side transistors are ON, you get a current path through the lower transistors and the motors. But, the same kind of thing happens with the diodes in the middle of the bridge.
Does that mean, the Diodes I have (Which I added to make sure the inductive current had a place to go when the robot stops) will give the same functionality as controlling all the transistors?

Since the ATMEGA16 has 36 IO lines (provided you use a crystal), You may find it worthwhile to consider allocating 4 lines rather than 2 lines to the H-bridge. The added flexibility will prove useful. For example, you can implement dynamic braking by turning on both upper or both lower legs of the H-bridge.
What was meant here is to have the abilllity to turn on the 2 lower transistors also, while the two top ones stay turned off, right?
Would the same function then be achieved if it was reversed so that the 2 top ones was on and the 2 lower ones was off?

#### hgmjr

Joined Jan 28, 2005
9,029
alright, I think I get it now, most of it anyway

Does that mean, the Diodes I have (Which I added to make sure the inductive current had a place to go when the robot stops) will give the same functionality as controlling all the transistors?
The diodes will give you a measure of braking. The ability to turn on both bottom or both top transistors in the H-bridge provide a more aggressive braking control.

What was meant here is to have the abilllity to turn on the 2 lower transistors also, while the two top ones stay turned off, right?
Would the same function then be achieved if it was reversed so that the 2 top ones was on and the 2 lower ones was off?
yes.

hgmjr

#### Roos

Joined Jan 29, 2011
6
Awesome, I think I got everything I need now to nail this down (Or rather Solder I guess )

Thank you everyone - Much appreciated. Have a nice weekend.

Oh and btw, the abillity to control all 4 Transistors should be able to be done with 2 signals, if I mess around with some logic gates, I imagin. I'll have to play around with those "bricks".

#### hgmjr

Joined Jan 28, 2005
9,029
I have done a lot of programming with AVR micros and ATMEGA16 in particular. When you are ready to code be sure to come back and gives us a chance to assist you.

Which programmer have you chosen?

Are you using the free software development tools AVRSTUDIO4 and WINAVR?

hgmjr

#### Roos

Joined Jan 29, 2011
6
I am using the Codevision C-compiler, and I guess to some extend the AVRStudio (mostly if I need to write some code in Assembly)

I don't know what is best for it, I mostly use it because we use it in my school so it's close to what I use in my everyday life

If you want to see my code, I actually have a little test code snipet, maybe you can tell me if my basic Idea works?

Rich (BB code):
#include <mega16.h>
#include <stdio.h>

void main()
{
unsigned int x;
DDRA = 1; // PortA is set as Output
DDRB = 0; // PORTB is set as Input

while(1)
{
x = PORTB.0;

while(x == 0) // x is 0 when no object is in front of the robot
{
// Motor1 drives forward
PORTA.0 = 1;
PORTA.1 = 0;
// Motor2 drives forward
PORTA.2 = 1;
PORTA.3 = 0;
}
// if x is 1, an object should be infront on the robot
PORTA.0 = 0; // motor1 stopped
PORTA.2 = 0; // motor2 stopped
}
}
It's extremely simple so far, and is mostly meant to prototype my circuits, when I get them build, to PORTB.0 I was hoping to hook up this nifty little thing: GP2Y0D805Z0F

So to start off with the thing would be able to sense something infront of it and stop all motors (is the theory).

#### SgtWookie

Joined Jul 17, 2007
22,210
In your initialization sequence, make certain that PORTA.0 = 0;
PORTA.1 = 0;
and that PORTA is set to OUTPUT.

#### Roos

Joined Jan 29, 2011
6
ah yes of course, I better initialize all the H-bridge bits to 0, so that I am certain the robot will not run random in the beginning.

The command "DDRA = 1;" should set alle pins in portA as output.

Know anything about the Sensor thing, or maybe better / cheaper solutions than the one I have found?

#### hgmjr

Joined Jan 28, 2005
9,029
Without a schematic, it is not entirely certain that you have taken care of all of the housekeeping chores that need to be addressed.

hgmjr