Lachlan's avatar@lachlanjc/edu
All Physical Computing

Motor Driver Experimentation

Today I experimented with connecting a motor driver to the Arduino. Here’s the circuit I used:

Circuit

While initially I had the motor turn on only when the button is depressed, I switched it to be on all the time, changing directions & speed using PWM when the button is pressed.

Code for this demo:
const int switchPin = 2; // switch input
const int motor1Pin = 3; // Motor driver leg 1 (pin 3, AIN1)
const int motor2Pin = 4; // Motor driver leg 2 (pin 4, AIN2)
const int pwmPin = 5; // Motor driver PWM pin
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(pwmPin, OUTPUT);
// set PWM enable pin high so that motor can turn on:
digitalWrite(pwmPin, HIGH);
}
void loop() {
// if the switch is depressed, motor will turn on one direction slowly:
if (digitalRead(switchPin) == LOW) {
analogWrite(pwmPin, 72);
digitalWrite(motor1Pin, LOW); // set leg 1 of the motor driver low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the motor driver high
}
// if the switch is low, motor will turn in the other direction quickly:
else {
analogWrite(pwmPin, 255);
digitalWrite(motor1Pin, HIGH); // set leg 1 of the motor driver high
digitalWrite(motor2Pin, LOW); // set leg 2 of the motor driver low
}
}

For an extra challenge, I wanted to add a potentiometer to control the speed, leaving the button to control direction. Reviewing how I wired/coded a potentiometer week 2, Sophie-Ana and I configured that:

Code for this demo:
const int switchPin = 2; // switch input
const int motor1Pin = 3; // Motor driver leg 1 (pin 3, AIN1)
const int motor2Pin = 4; // Motor driver leg 2 (pin 4, AIN2)
const int pwmPin = 5; // Motor driver PWM pin
int dialValue = 0; // Current value from pot
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(pwmPin, OUTPUT);
// set PWM enable pin high so that motor can turn on:
digitalWrite(pwmPin, HIGH);
}
void loop() {
dialValue = analogRead(A0);
analogWrite(pwmPin, 255 - (dialValue / 4));
// if the switch is depressed, motor will turn in one direction:
if (digitalRead(switchPin) == LOW) {
analogWrite(pwmPin, 72);
digitalWrite(motor1Pin, LOW); // set leg 1 of the motor driver low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the motor driver high
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the motor driver high
digitalWrite(motor2Pin, LOW); // set leg 2 of the motor driver low
}
}