Reputation: 11
I have a simple H-bridge circuit set up following this schematic:
I'm trying to control a small DC motor through this H-bridge with an Arduino Uno, but I have never programmed one of these controllers before. I need the motor to rotate in different directions when I press left and right on a keyboard. So far I have this code:
// Right Motor
/** Adjust these values for your servo and setup, if necessary **/
int resistor1 = 3;
int resistor2 = 5;
int resistor3 = 6;
int resistor4 = 10;
int moveServo;
void setup() {
Serial.begin(9600);
pinMode(resistor1, OUTPUT); // Set servo pin as an output pin
pinMode(resistor2, OUTPUT);
pinMode(resistor3, OUTPUT);
pinMode(resistor4, OUTPUT);
}
void loop() {
// Wait for serial input
if (Serial.available() > 0) {
// Read the incoming byte:
moveServo = Serial.read();
// ASCII left = 37, up = 38, right = 39, down = 40
if (moveServo == 37)
{
digitalWrite(resistor4, HIGH);
digitalWrite(resistor1, HIGH);
}
}
I am however, having trouble modifying the PWM so that the motor would stay on and also the output pins aren't being set as I specified.
How can I fix this problem?
Upvotes: 1
Views: 4227
Reputation: 1
Schematic for H-bridge circuit using IRF7105 dual channel Mosfet
Mosfet based H bridge, miniature size and good for up to 2 amps continuous current using 20V, The schematic is tested in Proteus simulation software and replicated.
Upvotes: 0
Reputation: 5410
Explanation of vcc2gnd's answer
Assuming 5V is given to H-Bridge circuit, PNP's turn ON when they have 0v to their base. NPN's turn ON when they have 5v to their base. When transistor is ON (saturated) it conducts current.
When Q4 and Q1 ON while others OFF(cutoff), motor turns in one direction. To have that direction, R1,R2,R3,R4 should given 5v,5v,0v,0v respectively.
Upvotes: 0
Reputation: 137
The solution for your problem (keep the motor running) is not PWM, but to set pin state in correct combination. Be aware that code you use is for all-NPN transistors H-Bridge. Your circuit is built with PNP - NPN combination, thus the control is different (NPN transistor is delivering current when saturated while PNP transistor is prohibiting current when saturated).
Try modify your code like this:
if (moveServo == 37)
{
digitalWrite(resistor1, LOW);
digitalWrite(resistor2, LOW);
digitalWrite(resistor3, HIGH);
digitalWrite(resistor4, HIGH);
}
else if (moveServo == 39)
{
digitalWrite(resistor3, LOW);
digitalWrite(resistor4, LOW);
digitalWrite(resistor1, HIGH);
digitalWrite(resistor2, HIGH);
}
Note that the order is important. Set one pair to LOW first before set the other to HIGH, otherwise you'll short the circuit between function calls.
Note: You can use PWM with analogWrite() function to control motor speed, but you need to slightly modify your circuit: put additional NPN transistor before ground (collector on H-Bridge, emitter on ground), connect it's base with PWM-capable Arduino pin via a limiting resistor.
Upvotes: 0
Reputation: 383
If you want to produce PWM outputs, you must use the analogWrite() function.
Upvotes: 1