Brandon Roots
  • INTERACTIVE
  • FILM
  • ABOUT
  • CONTACT
November 15, 2020

Lab: DC Motor Control Using an H-Bridge

broots ITP, Physical Computing

In this lab I learned to control the functions of a DC motor through a motor driver board that handles power functions for the motor and communicates with an Arduino.

The motor controller on hand was a A4988 Stepper Motor Driver Carrier from Pololu. The A4988 board pinout and basic wiring diagram here:

My setup with the Arduino Mega and a 12-volt DC computer fan looked like this:

While the motor controller was designed for a stepper motor, which has two separate coils, the DC motor I was using only had one so I only need to wire up the “A” motor pins.

Next I added a switch to the circuit that would be read by the Arduino Mega to then set the DC motor to ON or OFF.

Using a tutorial from MarkerGuides.com as a starting point I revised their Arduino sketch into the code below and was able to turn the DC fan motor ON and OFF. I a not entirely sure why but it was necessary toggle the digitalWrite() for the motor driver pin multiple times in the code to set the state to ON or OFF.

/*Example sketch to control a stepper motor with A4988 stepper motor driver and Arduino without a library. More info: https://www.makerguides.com */

// Define stepper motor connections and steps per revolution:
#define dirPin 2
#define stepPin 3
#define switchPin 4
#define stepsPerRevolution 200
int switchState = LOW;

void setup() {
  // configure the serial connection:
  Serial.begin(9600);
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop() {
  
  // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPin) == HIGH && switchState == LOW) {
      digitalWrite(stepPin, LOW);  // set motor driver high
      digitalWrite(stepPin, HIGH);  // set motor driver high
      digitalWrite(stepPin, LOW);   // set motor driver low
      digitalWrite(stepPin, HIGH);  // set motor driver high
      Serial.println("HIGH");
      switchState = HIGH;
    }
    // if the switch is low, motor will turn in the other direction:
    else if(digitalRead(switchPin) == LOW && switchState == HIGH) {
      digitalWrite(stepPin, LOW);   // set motor driver low
      digitalWrite(stepPin, HIGH);  // set motor driver high
      digitalWrite(stepPin, LOW);  // set motor driver high
      digitalWrite(stepPin, HIGH);  // set motor driver high
      Serial.println("LOW");
      switchState = LOW;
    }

  delay(10);

}
Lab: Using a Transistor to Control High Current Loads with an Arduino Lab: Controlling a Stepper Motor With an H-Bridge

Related Posts

Fractal Plant – Foiled by  Registers

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Foiled by Registers

Since receiving the PCBs and successfully soldering the board together I have been trying to rewrite code for the I2C port expander. This has been immensely difficult! The Inkplate Arduino Library makes considerable use of an “Mcp” class, which is written to work with the MCP23017 GPIO expander IC. These chips are quite difficult to […]

“Handling” Playtest Week

Handling, ITP

“Handling” Playtest Week

Last week we attended “Playtest Thursday” on the second floor of 370 Jay St with our games. I came away from the experience with some very specific feedback. Seeing a number of people play the game showed me things I didn’t anticipate. Some folks approached the cabinet and immediately treated it as a touch screen. […]

Fractal Plant – Beta Build

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Beta Build

The boards arrived! Amazingly within an hour of one another. Based on the experience I think that JLCPCB is a better value. With shipping OSHPark was $55.50 for 3 boards. JLCPCB was $26.36 for 10 boards. Aside from a higher cost OSHPark also left sharp bits of tabs around the edges of the boards which […]

Recent Posts

  • Fractal Plant – Foiled by  RegistersFractal Plant – Foiled by Registers
    May 9, 2022
  • “Handling” Playtest Week“Handling” Playtest Week
    May 5, 2022
  • Fractal Plant – Beta BuildFractal Plant – Beta Build
    April 24, 2022
Brandon Roots