loading

Step 3: Arduino Sketch

Once you have your h-bridge circuit completed, you can upload the sketch to your Arduino.  The sketch will cycle back and forth on the dc motors driving them forward and backwards and then light an led connected to pin 13 on the arduino.

// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// —————————————————————————  Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int ledPin =  13;    // LED connected to digital pin 13

// ————————————————————————— Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}

}

// ————————————————————————— Loop
void loop() {

drive_forward();
delay(1000);
motor_stop();
Serial.println("1");

drive_backward();
delay(1000);
motor_stop();
Serial.println("2");

turn_left();
delay(1000);
motor_stop();
Serial.println("3");

turn_right();
delay(1000);
motor_stop();
Serial.println("4");

motor_stop();
delay(1000);
motor_stop();
Serial.println("5");

digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);                  // wait for a second

}

// ————————————————————————— Drive

void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}



Advertisement

<p>this project include joystick?</p>
<p>good</p>
link to &quot;You may also download a printable PDF file of this illustration.&quot; isn't working, anybody have this?
Good instructable, I would like to have had a schematic however.
Can the 5V pin on the Hbridge be powered by the 5V power from the arduino? Also for PWM do we just use analogWrite(pin,0-255) on the digitalWrite() that is HIGH or can we just analogWrite() to the enable pin on the Hbridge?
&nbsp;This is stupid!Jou coud use just 2 digial pins 2 transistors and no other chips!
oshondrom,<br>1) You obviously don't understand the purpose of an H-bridge.<br>2) You are wrong, you could not drive the motors depicted in AruinoFun's instructable with only 2 transistors. Trying to do so would put strain on the arduino board. Even then, the arduino does not source enough current to fully saturate a high current MOSFET w/o preamp transistors .<br>3) &quot;This is stupid!&quot; violates the &quot;be nice&quot; policy...
Your links to the diagrams are dead. other than that you have a good instrucable. your code is good and simple.
I'm confused as to why you have in the parts list, the Arduino as well as the build your own arduino.<br><br>Is it a choice? as in either a off the shelf arduino or a homebuilt one?
So can you control the direction the motors go or is it only single directional circute
&nbsp;I really don't understand anything of your code.

About This Instructable

85,985views

71favorites

License:

More by ArduinoFun:Arduino Lilypad Interactive Passion Sensing Scarf Creating Printed Circuit Boards with a INKJET Printer Dual Motor Driver with Arduino using a SN754410NE Quad Half H-Bridge 
Add instructable to: