Lesson 11 DC Motor Control

Share for us

Introduction

In this experiment, we will learn how to control the direction and speed of a small-sized DC motor by a driver chip L293D. Making simple experiments, we will just make the motor rotate left and right, and accelerate or decelerate automatically.

Components

– 1 * Small-sized DC motor

– 1 * L293D

– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * USB cable

– Jumper wires

Principle

The maximum current of an Arduino I/O port is 20mA but the drive current of a motor is at least 70mA. Therefore, we cannot directly use the I/O port to drive the current; instead, we can use an L293D to drive the motor.

L293D

L293D is designed to provide bidirectional drive currents of up to 600mA at voltages from 4.5V to 36V. It’s used to drive inductive loads such as relays, solenoids, DC and bipolar stepping motors, as well as other high-current/high-voltage loads in positive-supply applications.

See the figure of pins below. L293D has two pins (Vcc1 and Vcc2) for power supply. Vcc2 is used to supply power for the motor, while Vcc1, for the chip. Since a small-sized DC motor is used here, connect both pins to +5V. If you use a higher power motor, you need to connect Vcc2 to an external power supply. 

Pin EN is an enabling pin and works with High level. A stands for input and Y for output. When pin EN is High level, if A is High, Y outputs High level; if A is Low, Y outputs Low level. When pin EN is Low level, the L293D does not work. It just needs to drive one motor in this experiment, so here use one side of the L293D.

DC Motor

This is a 5V DC motor. Give the two terminals of the copper sheet one high and one low level, and the motor will rotate. For convenient purposes, you can weld the pins to it.

Size: 25*20*15MM                                                   Free-run speed (3V): 13000RPM 

Stall current (3V): 800 mA;                                  Shaft diameter: 2 mm

Operation Voltage: 1-6V                                      Free-run current (3V): 70 mA 

The schematic diagram

Principle: The Enable pin 1,2EN of the L293D are connected to 5V already, so L293D is always in the working state. Connect pin 1A and 2A to pin 9 and 10 of the control board respectively. The two pins of the motor are connected to pin 1Y and 2Y respectively. When pin 10 is set as High level and pin 9 as Low, the motor will start to rotate towards one direction. When the pin 10 is Low and pin 9 is High, it rotates in the opposite direction.

Experimental Procedures

Step 1: Build the circuit

Step 2: Open the code file

Step 3: Select correct Board and Port

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.

Experiment Summary

In this experiment, you can also control the motor to rotate or not. Just connect pin 1, 2EN of the L293D to an I/O port of the control board. Set 1, 2EN as High level, and the motor will start rotating; set it as Low level, it will stop the rotating. 

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; //attach to one of the pin of the motor const int motorIn2 = 10; //attach to another pin of the motor /***************************************/ 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 } /****************************************/