Jason's notes

Week10 Motors

This week’s assignment is about Controlling DC motors and stepper motors with arduino.

Lab1: Transistor and relay lab

By adding a Bipolar transistor / MOSFET to the circuit, we can control components like motors which requires higher current. Note that we also need to add a diode to protect the transistor when dealing with motors.

Motor Circuit

A problem of controlling a motor like this is that the motor can only be turned in one direction. To be able to reverse the direction of the motor, an H-bridge circuit is required.

Lab2: Controlling a DC Motor with an H-Bridge

For this lab, I was using DRV8833 which is a different H-bridge as in the document. It is more common and easy to find in China compare to tb6612 and they should work the same.

const int motorA1 = 9;
const int motorA2 = 10;

void setup() {
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);

void loop() {
  int speed = map(analogRead(A0), 0, 1023, -255, 255);

  if (speed < 0) {
    analogWrite(motorA1, speed);
    digitalWrite(motorA2, LOW);
  } else {
    digitalWrite(motorA1, LOW);
    analogWrite(motorA2, -speed);

Controlling the speed and direction perfertly.

Controlling the speed and direction of a DC motor

Lab3: Controlling a Stepper Motor With an H-Bridge

Sadly, it didn’t work. I think it’s because of the DRV8833 H-bridge which has less pin than a tb6612. I’ve ordered a tb6612 and it’s on the way. TBD