The Stackr

In this section you will learn how to assemble the Stackr and how to program the onboard motors.

How to assemble the Stackr

The easiest way to assemble the Stackr is by following the instructions in the video below.

Controlling the Stackr Motors

The main functionality of the Stackr is provided by the two onboard DC motors. Watching the video below you can learn how the DC motors work, how to wire them to the motor controller and how to control them using the Arduino Nano ESP32 on your Stackr robot.

The code uses the SparkFun_TB6612 library which provides excellent support for DC motor control on the Arduino Nano ESP32 platform. You'll learn how to initialize the motor, set its direction and speed, and create custom movement patterns.

Stackr_Motor.ino

// Library for the motor controller
#include <SparkFun_TB6612.h>

// Microntroller pins connected to the motor controller
// AIN1, AIN2, BIN1, BIN2, PWMA, PWMB, STBY
#define AIN1 D5
#define BIN1 D6
#define AIN2 D4
#define BIN2 D7
#define PWMA D3
#define PWMB D8
#define STBY D9

// set the default forward direction
const int fwdDirectionA = 1;
const int fwdDirectionB = 1;

// Initializing motors  
Motor motor1 = Motor(AIN1, AIN2, PWMA, fwdDirectionA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, fwdDirectionB, STBY);

// Nothing to setup for the motors
void setup() {
}

// Move the robot forward for a second and backward for another second and repeat
void loop() {
    motor1.drive(128);
    motor2.drive(128);
    delay(1000);
    motor1.drive(-128);
    motor2.drive(-128);
    delay(1000);
}

Code Breakdown

Library Inclusion: The SparkFun_TB6612 library provides DC motor control functionality for the TB6612FNG motor driver on ESP32-based boards.

#include <SparkFun_TB6612.h>

Pin Definitions: The code defines the pins connected to the motor controller. AIN1/AIN2 and BIN1/BIN2 control the direction of each motor, PWMA/PWMB control the speed using PWM, and STBY is the standby pin.

#define AIN1 D5
#define BIN1 D6
#define AIN2 D4
#define BIN2 D7
#define PWMA D3
#define PWMB D8
#define STBY D9

Motor Objects: Two Motor objects are created (motor1 and motor2) to control the dual DC motors. Each motor is initialized with its control pins and forward direction configuration.

const int fwdDirectionA = 1;
const int fwdDirectionB = 1;

Motor motor1 = Motor(AIN1, AIN2, PWMA, fwdDirectionA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, fwdDirectionB, STBY);

drive() Function: This function controls the motor speed and direction. Positive values (0-255) drive forward, negative values (-255 to 0) drive backward. The value 128 represents approximately 50% speed.

motor1.drive(128);   // Forward at 50% speed
motor2.drive(128);   // Forward at 50% speed
motor1.drive(-128);  // Backward at 50% speed
motor2.drive(-128);  // Backward at 50% speed

Loop Pattern: The code demonstrates a simple movement pattern: both motors drive forward at 50% speed for 1 second, then reverse at 50% speed for 1 second, repeating continuously.

void loop() {
    motor1.drive(128);
    motor2.drive(128);
    delay(1000);
    motor1.drive(-128);
    motor2.drive(-128);
    delay(1000);
}

Library Installation

To use the code examples above, you'll need to install the SparkFun_TB6612 library in your Arduino IDE. Here are the resources and steps to get started:

Installation Method

Manual Installation

  • Download the library from the SparkFun GitHub repository
  • Extract the ZIP file
  • Copy the library folder to your Arduino libraries directory
  • Restart Arduino IDE

External Resources