This lab consisted of two parts that explored the use of transistors and h-bridges for dc motor control.
Transistor Lab
H-Bridge Lab
An H-Bridge circuit allows the DC motor to spin in both directions, while using a transistor only allows it to spin in one direction. Red and green LED indicators were added to easier tell when the motor changed direction.
Code:
const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const int redledPin = 12; // red LED indicator (CCW)
const int greenledPin = 13; // green LED indicator (CW)
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(enablePin, OUTPUT);
pinMode(redledPin, OUTPUT);
pinMode(greenledPin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}
void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(redledPin, HIGH);
digitalWrite(greenledPin, LOW);
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(greenledPin, HIGH);
digitalWrite(redledPin, LOW);
}
}