Skip to content

Instantly share code, notes, and snippets.

@jkaflik
Created December 30, 2015 13:00
Show Gist options
  • Save jkaflik/9d6b91bef9d454437c59 to your computer and use it in GitHub Desktop.
Save jkaflik/9d6b91bef9d454437c59 to your computer and use it in GitHub Desktop.
TB503A2 driver module example
//motor A connected between A01 and A02
//motor B connected between B01 and B02
int STBY = 10; //standby
//Motor A
int PWMA = 3; //Speed control
int AIN1 = 9; //Direction
int AIN2 = 8; //Direction
//Motor B
int PWMB = 5; //Speed control
int BIN1 = 11; //Direction
int BIN2 = 12; //Direction
void setup(){
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
}
#define LOW_SPEED 25
int speed = LOW_SPEED;
bool increase = true;
bool right = true;
void loop(){
move(1, speed, !right);
delay(50+50*speed/255);
if (increase) {
speed += 1;
if (speed == 255) {
increase = !increase;
}
}
else {
speed -= 1;
if (speed == LOW_SPEED) {
right = !right;
increase = !increase;
}
}
}
void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}
if(motor == 1){
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
}else{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void stop(){
//enable standby
digitalWrite(STBY, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment