Arduino DC Motors

The hexagonal Crickets at left all have a similar Motor port which can drive two DC motors. 

DC motors are controlled by 4 PWM output pins, the 4 PWM pins let you control speed and direction. And we'll use our seesaw_Motor library to help us manage the throttle (speed) and direction for us, making it very easy to control motors

Note that each DC motor is a little different, so just because you have two at the same throttle does not mean they'll rotate at the exact same speed! Some tweaking may be required

The two wires of the DC motor can be plugged in either way into each Crickit Motor port. If the motor spins the opposite way from what you want to call 'forward', just flip the wires!
#include "Adafruit_Crickit.h"
#include "seesaw_motor.h"

Adafruit_Crickit crickit;

seesaw_Motor motor_a(&crickit);
seesaw_Motor motor_b(&crickit);

void setup() {
  Serial.begin(115200);
  Serial.println("Dual motor demo!");
  
  if(!crickit.begin()){
    Serial.println("ERROR!");
    while(1);
  }
  else Serial.println("Crickit started");

  //attach motor a
  motor_a.attach(CRICKIT_MOTOR_A1, CRICKIT_MOTOR_A2);

  //attach motor b
  motor_b.attach(CRICKIT_MOTOR_B1, CRICKIT_MOTOR_B2);
}

void loop() {
  motor_a.throttle(1);
  motor_b.throttle(-1);
  delay(1000);

  motor_a.throttle(.5);
  motor_b.throttle(-.5);
  delay(1000);

  motor_a.throttle(0);
  motor_b.throttle(0);
  delay(1000);

  motor_a.throttle(-.5);
  motor_b.throttle(.5);
  delay(1000);

  motor_a.throttle(-1);
  motor_b.throttle(1);
  delay(1000);

  motor_a.throttle(0);
  motor_b.throttle(0);
  delay(500);
}

This guide was first published on May 16, 2018. It was last updated on May 16, 2018. This page (Arduino DC Motors) was last updated on Nov 10, 2019.