MiniMoto DRV8830 Hookup Guide

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Pages
Contributors: SFUptownMaker
Favorited Favorite 1

Example

Here's a simple example of an Arduino sketch, using the library to control the two DC motors on a Tamiya twin motor gearbox. We'll take the sketch bit by bit, and explain it as we go.

#include <minimoto.h>  // Include the MiniMoto library

// Create two MiniMoto instances, with different address settings.
MiniMoto motor0(0xCE); // A1 = 1, A0 = clear
MiniMoto motor1(0xD0); // A1 = 1, A0 = 1 (default)

This first section lays the groundwork for the use of the library. In this case, we're using two MiniMoto boards, one with the address jumpers in the default state and one with jumper A0 cleared.

#define FAULTn  16     // Pin used for fault detection.

The FAULTn pin is an open drain output, so any number of MiniMoto boards can be connected to a single input. A pullup resistor is included on the MiniMoto board. Pin 16 corresponds to pin A2.

void setup()
{
  Serial.begin(9600);
  Serial.println("Hello, world!");
  pinMode(FAULTn, INPUT);
}

setup() is pretty simple; initialize the serial port, print a welcome message, and initialize the FAULTn detection pin.

void loop()
{
  Serial.println("Forward!");
  motor0.drive(10);
  motor1.drive(10);
  delayUntil(10000);
  Serial.println("Stop!");
  motor0.stop();
  motor1.stop();
  delay(2000);
  Serial.println("Reverse!");
  motor0.drive(-10);
  motor1.drive(-10);
  delayUntil(10000);
  Serial.println("Brake!");
  motor0.brake();
  motor1.brake();
  delay(2000);
}

loop() turns the motors one way for a bit, then stops or brakes them, then goes the other way for a bit. We'll talk about delayUntil() momentarily; for now, just know that it's a custom function which includes polling for fault conditions.

void delayUntil(unsigned long elapsedTime)
{
  unsigned long startTime = millis();
  while (startTime + elapsedTime > millis())
  {

delayUntil() is a custom replacement for delay() which polls for fault conditions on the MiniMoto boards and reports them if they exist. The delay portion of the function is handled by this while() loop; for more information on how that works, see the built-in Arduino example "BlinkWithoutDelay".

    if (digitalRead(FAULTn) == LOW)
    {

Our first-level check for faults is done by watching the FAULTn pin. We could skip this and go directly to polling the devices by calling getFault() for each one; this is faster, but requires the use of an additional IO pin.

      byte result = motor0.getFault();

The return value from getFault() is nothing more than the contents of register 0x01 of the DRV8830. We abstract it a bit to make this easier for the user, who doesn't have to care about registers and things like that to use this part.

      if (result & FAULT)
      {

This is where we definitively detect whether a fault has occurred or not. It is possible (and indeed, not uncommon) for the FAULTn pin to go low without a fault actually having occurred. The FAULT constant is a single bit (bit 0, in fact) which, if set, indicates that a fault condition has actually occurred. If the result of an AND of that constant with the result from getFault() is non-zero, we know that bit is set and can then proceed to identify which fault bit is set.

        Serial.print("Motor 0 fault: ");
        if (result & OCP) Serial.println("Chip overcurrent!");
        if (result & ILIMIT) Serial.println("Load current limit!");
        if (result & UVLO) Serial.println("Undervoltage!");
        if (result & OTS) Serial.println("Over temp!");
        break; 
      }

We use the same method (ANDing the result with a single-bit constant) to determine which of the bits in the fault register was set, then we report that to the user via the serial port. Since a fault occurred, we want to bail out of the loop, so we include a break statement.

      result = motor1.getFault();
      if (result & FAULT)
      {
        Serial.print("Motor 1 fault: ");
        if (result & OCP) Serial.println("Chip overcurrent!");
        if (result & ILIMIT) Serial.println("Load current limit!");
        if (result & UVLO) Serial.println("Undervoltage!");
        if (result & OTS) Serial.println("Over temp!");
        break;
      }
    }
  }
}

Repeat for the second motor.

This example code is included with the library, and the library can be found by downloading the zip file of the product GitHub repository.