Introduction: Arduino Tank Car Lesson 3-Control the Motor

About: Osoyoo brand products are mainly focused on STEM education.User experience and customer feeling are always our first priority. Our goal is to create the best designed product to help students and hobbyist to e…

In this tutorial, we will use KOOKYE Smart DIY kit to make a simple

program controlled smart car. Once the car installation is completed, we will use a UNO R3 board to control the car movements including go forward, go back, left turn and right turn.

Detailed Tutorials: http://kookye.com/?p=5794

Robot Car Platform Tank Chassis: https://amzn.to/2xenp7H

Complete Arduino Robot Car KitBuy it for USA : https://amzn.to/2N4dP1K

Step 1: How It Work:

Three Parts of Car Motor: DC motor,gear reducer and encoder.

There are 6 pin wires from encoder motor,two of them (red and black wires) come from the DC motor.

The pinout K1 or K2 and K3 or K4 on drvier board can be used for driving the left motor and right motor respectively.

The pinout ENA and ENB can be used to adjust the motor speed via input PWM signal.

The pinout N1,N2,N3 and N4 can be used to control the forward and backward direction of motor.

as attached picture.

Step 2: Software Installation:


Step 1: Install latest Arduino IDE (If you have Arduino IDE version after 1.1.16, please skip this step)

Download Arduino IDE from https://www.arduino.cc/en/Main/Software?setlang=en , then installs the software.

Step 2: Download Lesson Two sample code from http://www.kookye.com/download/car/tank_robot_lesson3.zip , unzip the download zip file tank_robot_lesson3.zip, you will see a folder called tank_robot_lesson3.

Step 3: Connect UNO R3 board to PC with USB cable, Open Arduino IDE -> click file -> click Open -> choose code "tank_robot_lesson3.ino" in tank_robot_lesson3 folder, load the code into arduino.

Step 4: Choose the corresponding board and port for your project, upload the sketch to the board.

Step 3: Understanding the Code:

Part 1: Define the pinout as the table

#define IN1 8 //K1,K2 motor direction

#define IN2 9 //K1,K2 motor direction

#define IN3 10 //K3,K4 motor direction

#define IN4 12 //K3,K4 motor direction

#define ENA 5 //needs to be a PWM pin to be able to control motor speed ENA

#define ENB 6 //needs to be a PWM pin to be able to control motor speed ENB

Part 2: Understand the structure,value and function.

void go_ahead() //motor rotate clockwise -->robot go ahead

{

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

digitalWrite(IN3, LOW);

digitalWrite(IN4,HIGH);

}

void go_back() //motor rotate counterclockwise -->robot go back

{

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

digitalWrite(IN3, HIGH);

digitalWrite(IN4,LOW);

}

void go_stop() //motor brake -->robot stop

{

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4,LOW);

}

void turn_left() //left motor rotate counterclockwise and right motor rotate clockwise -->robot turn left

{

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4, HIGH);

}

void turn_right() //left motor rotate clockwise and right motor rotate counterclockwise -->robot turn right

{

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

}

/*set motor speed */ void set_motorspeed(int lspeed,int rspeed) //change motor speed

{

analogWrite(ENA,lspeed);//lspeed:0-255

analogWrite(ENB,rspeed);//rspeed:0-255

}

Part 3: Data initialization. We need to set the correct work mode to each pinouts(ENA,ENB,N1,N2,N3,N4) on driver board and call the above function in step 2.

void setup()

{

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

pinMode(IN3, OUTPUT);

pinMode(IN4, OUTPUT);

pinMode(ENA, OUTPUT);

pinMode(ENB, OUTPUT);

set_motorspeed(255,255);//maximum speed

go_ahead(),delay(5000),go_stop(); //robot forward 5s

go_back(),delay(5000),go_stop(); //robot go back 5s

turn_left(),delay(5000),go_stop();//robot turn left 5s

turn_right(),delay(5000),go_stop();//robot turn right 5s

go_stop();//stop

}

Step 4: Hardware Installation:

Step 1: Install Expansion Board on UNO R3 board.

Step 2: Move the wire connected to digit ports(D5,D6,D8,D9,D10,D12) in UNO R3 board to its counterpart digit pin in expansion board. Connected the port(GND,VCC,VT) in voltage meter and the port (GND,12V,VO) in drvier board.

Driver Board - Esp8266 wifi board

Driver Board - voltage meter

Step 3: Turn the switch of expansion board to "1" and "2" position, as the following photo shows.

(If you have finished the above steps on lesson one, please skip these step)

Step 4: Testing. the tank car will go forward for 5s,go back for 5s ,turn left for 5s and turn right for 5s in sequence.You also can change the example code to make the car movements as your need.