bobowman
Published © GPL3+

12V DC Motor Controlled By Adafruit TB6612 Motor Driver

This Arduino code controls a 12V DC motor (linear actuator) using an Adafruit TB6612 1.2A DC Motor Driver and external 12V power supply.

IntermediateFull instructions provided1,585
12V DC Motor Controlled By Adafruit TB6612 Motor Driver

Things used in this project

Hardware components

linear actuator with 12V DC motor, Mini Electric Linear Actuator Stroke 2"–Force 4.5 lbs–12V | High-Speed 1.97"/sec–Weight 0.15KG Ideal for Intelligent Range Hood, Fan Blades, Cabinets, Window Opener, Robotics, Home Automation
Any 12V DC motor is suitable for this project, even if it doesn't have a linear actuator attachment. If you plan to use a motor that has a different voltage that is between 5-12V, make sure to adjust your power supply voltage to be the same as the motor voltage). Don't pick any motor that has to operate above 13V voltage.
×1
Arduino UNO
Arduino UNO
Most Arduino versions will work. In my case, I chose an Arduino Uno R3, which comes with a blue power/connection plug that connects via USB to your laptop/computer.
×1
Adafruit TB6612 1.2A DC/Stepper Motor Driver Breakout Board
This is an awesome motor driver that can help you connect DC motors and stepper motors for your project. This particular project wiring is set up for DC motors.
×1
12V DC Power Supply with DC terminal connector, SHNITPWR 4V - 12V Power Supply 10A 120W AC to DC Adapter DC 4V 4.5V 5V 6V 7V 8V 9V 10V 11V 12V Voltage Adjustable Universal Power Converter Transformer 100-240V AC In with 14 Tips & Polarity Converter
Ensure that this is an adjustable power supply with a dial on it. Ensure it comes with different tips, specifically with a DC terminal connector so you can screw in wires into the red and black end of the power supply to connect to your project.
×1

Software apps and online services

Arduino IDE
Arduino IDE
Download the Arduino IDE interface from Arduino.cc

Hand tools and fabrication machines

Digilent Screwdriver
Digilent Screwdriver
Make sure the end of your screwdriver is compatible with the screw size of the screw heads that are on your DC terminal connector that should come with the power supply that you purchase. Worst-case, take your DC terminal connector with you to a hardward store and ask for assistance in finding the right size screw. Typically it's a standard electronics screwdriver. I used a Pratt-Read 88161 1/8 flat head standard electronics screwdriver,

Story

Read more

Schematics

Wiring Diagram

This is a Fritzing wiring diagram that shows you how to connect the wires (jumper wires) to the parts of this project.

Code

Linear_Actuator_DC_Motor

C/C++
This is Arduino code that you can upload in the Arduino IDE interface that controls the motor.
/******************************************************************************
linear_actuator_DCmotor12V 
Modified By: Bo Bomwan, November 2022 
NOTES: This code works for either 1 motor, or 2 motors. In this code, I have it controlling only 1 motor (no pins are connected for the 2nd motor). 

TestRun.ino
TB6612FNG H-Bridge Motor Driver Example code
Michelle @ SparkFun Electronics
8/20/16
https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library

Uses 2 motors to show examples of the functions in the library.  This causes
a robot to do a little 'jig'.  Each movement has an equal and opposite movement
so assuming your motors are balanced the bot should end up at the same place it 
started.

Resources:
TB6612 SparkFun Library

Development environment specifics:
Developed on Arduino 1.6.4
Developed with ROB-9457
******************************************************************************/

// This is the library for the TB6612 that contains the class Motor and all the
// functions
#include <SparkFun_TB6612.h>

// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
// the default pins listed are the ones used on the Redbot (ROB-12097) with
// the exception of STBY which the Redbot controls with a physical switch
#define AIN1 5
#define AIN2 4
#define PWMA 10
#define STBY 9

#define BIN1 7
#define BIN2 8
#define PWMB 6
  

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1 
const int offsetA = 1;
const int offsetB = 1;

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);


void setup()
{
 //Nothing here
}


void loop()
{ 
  
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor1.drive(255,1000);
   motor1.drive(-255,1000);
   motor1.brake();
   delay(1000);
   
}


// Drive in direction given by sign, at speed given by magnitude of the 
//parameter.
void drive(int speed);  

// drive(), but with a delay(duration)
void drive(int speed, int duration);  

//currently not implemented
//void stop();           // Stop motors, but allow them to coast to a halt.
//void coast();          // Stop motors, but allow them to coast to a halt.

//Stops motor by setting both input pins high
void brake(); 

//set the chip to standby mode.  The drive function takes it out of standby 
//(forward, back, left, and right all call drive)
void standby(); 

//Takes 2 motors and goes forward, if it does not go forward adjust offset 
//values until it does.  These will also take a negative number and go backwards
//There is also an optional speed input, if speed is not used, the function will
//use the DEFAULTSPEED constant.
void forward(Motor motor1, Motor motor2, int speed);
void forward(Motor motor1, Motor motor2);

//Similar to forward, will take 2 motors and go backwards.  This will take either
//a positive or negative number and will go backwards either way.  Once again the
//speed input is optional and will use DEFAULTSPEED if it is not defined.
void back(Motor motor1, Motor motor2, int speed);
void back(Motor motor1, Motor motor2);

//Left and right take 2 motors, and it is important the order they are sent.
//The left motor should be on the left side of the bot.  These functions
//also take a speed value
void left(Motor left, Motor right, int speed);
void right(Motor left, Motor right, int speed);

//This function takes 2 motors and and brakes them
void brake(Motor motor1, Motor motor2);

Credits

bobowman

bobowman

2 projects • 1 follower

Comments