Arduino and SN754410 H-Bridge Motor Driver
14 september 2011 | Classified in: Electronic | Tags: arduino H-Bridge, SN754410, L293D, pwm
A simple set-up to control one motor speed (PWM) and rotational direction over serial using SN754410 (L293D) motor driver.
Video:
Code:
/*SCHEMATIC USING ONLY ONE MOTOR:
(N/A MEANS FREE PIN)
5V N/A N/A GND GND N/A N/A N/A
| | | | | | | |
------------------------------------
| SN754410 |
------------------------------------
| | | | | | | |
N/A PIN5 MOT1 GND GND MOT2 PIN6 VIN
*/
#define motorPinOne 5 //The chosen pin must have PWM
#define motorPinTwo 6 //The chosen pin must have PWM
/* Define the rotational direction of the motor. (0 = clockwise and 1 = counter-clockwise) */
int val = 0;
/* Define the rotational speed of the motor. MUST be between 0 and 255. */
int pulse = 0;
/* Used to store the current rotational direction */
int storage = 0;
void setup() {
Serial.begin(9600);
Serial.println("Enter 0 or 1 to choose rotational direction");
Serial.println("Then enter + or - to increase/decrease the rotational speed.");
Serial.println("Warning: Rotational speed MUST be between 0 and 250.");
}
void loop() {
val = Serial.read(); //
switch (val) {
case '0':
clockwise();
break;
case '1':
counterClockwise();
break;
case '+':
pulse = pulse + 10;
updateSpeed();
break;
case '-':
pulse = pulse - 10;
updateSpeed();
break;
}
}
void clockwise(){
storage = 0;
Serial.print("Rotation is clockwise and speed is ");
Serial.println(pulse);
analogWrite(motorPinOne,pulse); // set leg 1 of the H-bridge low
analogWrite(motorPinTwo,0);
}
void counterClockwise(){
storage = 1;
Serial.print("Rotation is counter-clockwise and speed is ");
Serial.println(pulse);
analogWrite(motorPinOne,0); // set leg 1 of the H-bridge low
analogWrite(motorPinTwo,pulse);
}
void updateSpeed(){
if (storage == 0){
clockwise();
}
if(storage == 1){
counterClockwise();
}
}


1 comment