0
$\begingroup$

I want to use my atmega8 uC as a .

Can anybody give me the source code using C, so that the microcontroller acts as an H-Bridge.

$\endgroup$
3
  • $\begingroup$ Can you clarify the purpose? There is a lot more to motor control theory than just pinging a few pins as a bridge. Also, I suggest that the atmega probably cannot give the current that a H-bridge would need. $\endgroup$
    – Andrew
    Commented Aug 12, 2013 at 18:50
  • $\begingroup$ Thanks for your suggestion, actually i have got an atmega8 microcontroller and two B.O motor. Can you suggest me some alternative way to control the two motor simultaneously without using H-Bridge, if it's possible. $\endgroup$ Commented Aug 13, 2013 at 17:56
  • 2
    $\begingroup$ @NabajyotiDas, we are not a "give me the codez plz" community. Please show us your efforts, what code you have already written and where you are stuck. $\endgroup$
    – Shahbaz
    Commented Aug 14, 2013 at 17:29

3 Answers 3

2
$\begingroup$

It is not possible to use the micro-controller directly as an H-Bridge. A micro-controller is a micro-controller and an H-bridge is an H-bridge. You need both of them to control a motor (or something equivalent).

I will suppose that you are using motors and not servo. Let's say you want to use your micro-controller as an H-bridge. You have two cables from the motor. One for the ground and the other one for the current. When both of them are plugged, how are you going to wired it to the micro-controller to control the motor ? Or are you going to wire directly the current supply wire to the micro-controller ?

The thing is you will not be able to input enough current to move the motor with just the pin of a micro-controller. You will need another source of current and thus, something to control it, if you want to control the direction and speed of your motor. That's where the H-bridge is used.

You should read some documentation about H-Bridge, it's not that complicated to understand.

And if you really want not to use H-Bridge, you can create one with transistors on the bread-board. That tutorial is pretty good for that.

If in the end you want to use H-Bridge, the tutorial that EDDY74 gave you is perfect for that. Really simple and easy to understand. To, then, control the motor via a PWM is fairly easy since you just have to modulate the enable pin or the motor logic pins (I guess it depend on the H-bridge you are using). There a discussion about speed control on the Arduino forum.

$\endgroup$
1
$\begingroup$

I'm not really familiar with Atmel microcontrollers, but I'm assuming you're using some Arduino variant. Here's a pretty thorough tutorial about using an Arduino with a SN754410 h-bridge (found it via the SN754410 Sparkfun entry). It seems the only thing the tutorial lacks is controlling the motors via PWM, which of course means you will only be able to control the motors direction, and whether the motor is on or off. Just google Arduino PWM, I'm sure there's loads of code examples. [Update] See also Adafruit Arduino tutorials.

One thing to keep in mind is to never enable both directional signals at the same time (which obviously causes a straight short across your motor rail). One way of ensuring this never happens is by using only one micro-controller output (eg MOTOR_DIR_FORWARD), and then feeding that into a not-gate, and then into your other directional signal (in other words, instead of having a motor enable ouput, and two directional outputs, you'll only have a motor enable output, and a single directional output).

Also remember that if your motors and micro are running off the same supply that insufficient decoupling may cause your micro to reset.

$\endgroup$
0
$\begingroup$

Just for completeness -- although you will typically need an external H bridge to drive motors, you can use just the AVR pins directly if you are certain that the current will be low enough (for driving a really tiny motor, or maybe a LED pair).

The code would look something like this:

DDRB |= _BV(PB0) | _BV(PB1); // This sets PB0 and PB1 as output.
// After setting it as output, both the pins are driven low.

PORTB |= _BV(PB1); // This sets PB1 high and start spinning the motor in one direction
PORTB ^= _BV(PB0) | _BV(PB1); // This toggles state of both PB0 and PB1 -- change direction of the rotation
PORTB &= ~(_BV(PB0) | _BV(PB1)); // This sets both pins to low -- stop the motor
$\endgroup$
1
  • 1
    $\begingroup$ Even though the I/O pin might be strong enough to drive the motor (which I doubt - this link states max current at I/O pins to be 40 mA), I'd still definitely not recommend it. The back EMF of your motor would most likely fry your I/O pin (you could try and add a flyback diode as protection though). Best to add a proper driver. $\endgroup$
    – EDDY74
    Commented Apr 28, 2015 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.