Lesson 7 DC Motor Control

Share for us

 Introduction

In this experiment, you will learn how to control the direction and speed of a small-sized direct current (DC) motor) by using the driver chip L293D and the SunFounder Uno board. In order to make it easier for beginners, we will let the DC motor rotate left and right, accelerate and decelerate automatically.

Components

– 1 * Small-sized DC motor

– 1 * L293D

– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * USB cable

– Jumper wires

Principle

L293D       

This is a very practical chip that can independently control two DC motors. In this experiment, just half of the chip is used. Since most pins on the right side of the chip are used to control the second motor, they will not be used here.

L293D has two pins (Vcc1 and Vcc2) for power supply. Vcc2 is used to supply power for the motor, while Vcc1 to supply for the chip. Since a small-sized DC motor is used here, connect both pins to +5V. If you use a high power motor, connect Vcc2 to an external power supply. At the same time, the GND of L293D should be connected to that of the SunFounder Uno board.

DC Motor Specifications

Voltage: 3-6V

Main Size: length 25mm, thickness 15mm, width 20mm

Motor Shaft Length: 9mm, Shaft Diameter 2mm

Rated Voltage: 3v

Reference Current: 0.35-0.4A

3v Rotating Speed: 13000 RPM

Experimental Procedures

Step 1: Build the circuit

The schematic diagram

Step 2: Program (please go to our official website www.sunfounder.com to download related code by clicking LEARN -> Get Tutorials)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

The blade of the DC motor will begin rotating left and right, in a speed that varies accordingly.

Code

//DC Motor Control//The DC motor will begin rotating left and right, and its speed will vary accordingly.
//Email:support@sunfounder.com
//Website:www.sunfounder.com
//2015.5.7
/***************************************/
const int motorIn1 = 9; // the one pin of the motor attach to pin 9
const int motorIn2 = 10; // the another pin of the motor attach to pin 10
/***************************************/
void setup()
{
pinMode(motorIn1,OUTPUT); //initialize the motorIn1 pin as output
pinMode(motorIn2,OUTPUT); //initialize the motorIn2 pin as output
}
/****************************************/
void loop()
{
clockwise(200); //rotate clockwise
delay(1000); //wait for a second
counterclockwise(200); //rotate counterclockwise
delay(1000); //wait for a second
}
/****************************************/
//The function to drive motor rotate clockwise
void clockwise(int Speed)
{
analogWrite(motorIn1,Speed); //set the speed of motor
analogWrite(motorIn2,0); //stop the motorIn2 pin of motor
}
//The function to drive motor rotate counterclockwise
void counterclockwise(int Speed)
{
analogWrite(motorIn1,0); //stop the motorIn1 pin of motor
analogWrite(motorIn2,Speed); //set the speed of motor
}
/****************************************/ 

Video