web analytics
Arduino

DC Motor with Arduino

Controlling the DC motor using Arduino it’s an easy process we will study ways to control the DC motor. but it is necessary to know that the Arduino provides only 5 volts and 40mA so the motor will work but not efficiently!. so to solve this problem we use extra components, these components have two types one of the basic electronic elements like transistor and another type called by Driver, We will build projects based on both methods where we will build DC Motor Direct connection, DC Motor & Transistor connection and DC Motor & potentiometer.

DC Motor Direct connection

At first, we will learn how to connect the motor directly with the Arduino Board, in this project we need these components as shown in the following table:

Component Number
Arduino UNO R3 1
Brushed DC Motor 6V1
Mini breadboard 1
wires 7
Small button1

We will make a project to control the DC Motor, when you press the button the motor will move and when you press again the motor will stop, connect the power supply to the mini breadboard and connect DC motor with digital pin D3 PWM then connect button with  digital pin D2, Connections of Brushed DC Motor and Button with Arduino UNO R3 it will be as follows:

Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

#define MOTOR 3
#define BUTTON 2
int val = 0;
int old_val = 0;
int state = 0;

void setup() {
pinMode(MOTOR, OUTPUT);
pinMode(BUTTON, INPUT);
}

void loop() {
val = digitalRead(BUTTON);

if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
}

old_val = val;

if (state == 1) {
analogWrite(MOTOR, 255)
}
else {
analogWrite(MOTOR, 0);
}
}

DC Motor & Transistor connection

In this way we use the transistor as an amplifier to raise the current value now we will make the user control the speed using serial monitor but we need extra the following components:

Component Number
PN2222 Transistor1
1N4001 diode1
270 Ω Resistor1

Also, You can download the datasheet to Connections of PN2222 Transistor and  1N4001 diode. Connections of Brushed DC Motor and Transistor with Arduino UNO R3 it will be as follows:

Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

int motorPin = 5;

void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}

void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}

After uploading the code you can open the serial monitor and control the speed of the motor note that the speed is proportional to the values from 0 to 255 this is from constant numbers like 9600.


DC Motor & potentiometer

In the third project we will control the DC Motor using Arduino and potentiometer, we need to add the potentiometer to the components in the first table. But what is potentiometer?

Potentiometer it is variable resistance but its value can be changed manually using knob since the increase of resistance will reduce the current, this piece is used for example in the control of the volume in radio, in our project we use to control the degree of DC motor.

 

There are many types of them, some of which moves in a linear and some moves in a rotating, in our project, you can choose any type but it should be worth 10k ohm, However, we recommend that you use the circular type because it is more flexible and gives greater control accuracy, such as this type:

Circular Potentiometer
Circular Potentiometer

Connections of Brushed DC Motor and potentiometer with Arduino UNO R3:

Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

int potPin = A1;
int motorPin = 5;
int potValue = 0;
int motorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin);
motorValue = map(potValue, 0, 1023, 0, 255);
analogWrite(motorPin, motorValue);
Serial.print("potentiometer = " );
Serial.print(potValue);
Serial.print("t motor = ");
Serial.println(motorValue);
delay(2);
}

Now change the values of potentiometers (clockwise or counter clockwise) and watch the DC motor speed change, and you can modify the code to add more DC motors.

Note: If you are using a 5V DC motor, you do not need an external power unit, because the USB computer is capable of providing you with sufficient power, so don’t worry about power.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Close
Close

Adblock Detected

Please consider supporting us by disabling your ad blocker