DC Motor with Arduino

65

In this tutorial, we will learn how to interface a DC motor with Arduino. We can make things move by controlling motors with Arduino. In this article, we will interface multiple motors and control their speeds.

DC Motors are also known as Brushed and Brushless Motors.

dc motor

Most low-cost direct current (DC) motors are simple devices with two leads connected to brushes (contacts) that control the magnetic field of the coil that drives a metallic core (armature).

The direction of rotation can be reversed by reversing the polarity of the voltage on the contacts.

DC motors are available in many different sizes, but even the smallest (such as vibration motors used in cell phones) require a transistor or other external control to provide adequate current.

The recipes that follow to show how to control motors using a transistor or an external control circuit called H-Bridge.

Must See:


Components Needed

  • Arduino.
  • DC Motors.
  • L293D H-Bridge IC.
  • Breadboard.
  • Jumper Wires.

L293D H-Bridge IC

An H-Bridge can control two dc motors. The below figure shows the connections for the L293D H-Bridge IC; we can also use the SN754410 which has the same pin layout.


Controlling the Direction of a DC Motor with an H-Bridge

We want to control the direction of a dc motor- for example, we want to cause a motor to rotate in one direction or the other form serial port commands.

Circuit Diagram

circuit diagram for dc motor with arduino

Code

The following sketch controls the direction of a DC motor with H-Bridge:

The table shows how the values on the H-Bridge input affect the motor. In the solution a single motor is controlled using the IN1 & IN2 pins; the EN pin is permanently HIGH because it is connected to +5V.

ENIN1IN2Function
HIGHLOWHIGHTurn Clockwise
HIGHHIGHLOWTurn Anticlockwise
HIGHLOWLOWMotor Stop
HIGHHIGHHIGHMotor Stop
LOWIgnoredIgnoredMotor Stop

Controlling the Direction of Two DC Motor with Arduino

Now connect one more motor in the same circuit.

Circuit Diagram

Code

The following sketch controls both motors together:


Controlling the direction and speed of a DC motor with an H-Bridge

We want to control the direction and speed of a dc motor. This extends the functionality by controlling both motor direction and speed through a command from the serial port.

Circuit Diagram

Code

This sketch uses commands from the Serial Monitor to control the speed and direction of the motor.

Sending “0” will stop the motor, and digits “1” through “9” will control the speed. Sending “+” and “-” will set the direction of motor:

Motor direction is controlled by the levels on the IN1 & IN2 pins. But in addition, speed is controlled by the analogWrite value on the EN pin.

Writing a value of 0 will stop the motor; wiring 255 will run the motor at full speed. The motor speed will vary in proportion to values within this range.


Using Sensor to control the Direction and Speed of DC Motors

We want to control the direction and speed of dc motors with feedback from sensors. For example, we want two photo sensors to control motor speed and direction to cause a robot to move toward a beam of light.

This solution uses two motor connection, with the addition of two light-dependent resistors, as shown in the figure.

Must See:

Circuit Diagram

Code

The sketch monitors the light level on the sensors and drives the motors towards the sensor detecting the brighter light level:

This sketch controls the speed of two motors in response to the amount of light detected by two photocells.

When an increase in light on one side will increase the speed of the motor on the other side.

This causes the robot to turn toward the side with the brighter light. Light shining equally on both cells makes the robot move forward in a straight line. Insufficient light causes the robot to stop and look around to see if there is a light source coming from any other direction.

Light is sensed through analog input 0 & 1. When the program starts, the ambient light is measured and this threshold is used to determine the minimum light level needed to move the robot. When the light drops below the threshold, the lookAround function is called to rotate the robot to search for more light.

Motor speed is controlled in the setSpeed function. Two pins are used to control the direction for each motor and with another pin to control speed. The pin number is held in the leftPins and rightPins arrays. The first pin in each array is the speed pin; the other two pins are for direction.


Some More Articles For You:

Our Youtube Channel

LEAVE A REPLY

Please enter your comment!
Please enter your name here