How to control a DC motor speed with a Arduino Uno board and a potentiometer
Hi guys,
We started this week a small serie on the DC motor. It's purpose is to help you step by step
make more schematic and programming on how to control a DC motor with differents electronics components.
Today, the tutorial is focusing on how to control a DC motor speed with an arduino uno board, a transistor and a potentiometer. This is the same schematic as our last tutorial except that we are adding a potentiometer to control the motor's speed.
Hardware:
To start, we need to gather the following materiels:
one 220 ohm resistor
1 breadboard (prototyping board)
Wires
1 potentiometer (a variable resistor to change the motor's speed)
one 6V battery
1 Diode 1N4001 to protect our transistor against high voltage coming from the motor when the power is stopped.
6V DC motor
1 PNP 2N2222 transistor to amplify the current to control the motor
The potentiometer will be connected to the analog input A0 on our arduino uno board as following with or schematic.
Programming:
For the programming part, we will make a normal arduino loop.
The Input with the resistor will be A0. it's an analog input.
The output will be on the pin 8 with the DC motor.
The potentiometer is connected to our input A0 on the arduino Uno. Inside the the microcontroller chip is a analog to digital converter that convert the analog value to digital.
The Arduino works with the analogRead() function that converts the value analog value from 0 to 1024.
On the output side of the arduino uno board, to control the motor, we'll use the analogWrite() function to control the motor's speed. The function includes 2 values. The arduino pin and the value for the motor's speed. The value to control the motor's speed vary from 0 to 255. It is a PWM (pulse width modulation). At 255, the motor is at full speed and at 0, the motor is stopped.
Analogread working with value from 0 to 1024, it need to be divided by 4 to be used by the function analogWrite() that works with value for 0 to 255.
program
int potentiometer = A0;
#define motor_output 6
void setup()
{
Serial.begin(9600);
pinMode(potentiometer, OUTPUT);
}
void loop()
{
int control_motor = analogRead(potentiometer/4);
analogWrite(motor_output, control_motor);
}
Explanations:
The potentiometer will be wire to an analogIn pin that works with the function called ''Analogread().
Analogread() can read analog value on the analog pin depending of the max voltage (We chose A0). In our case, we'll plug our potentiometer to the arduino power supply (5V in our case: can be seen on the schematic). So the max value will be 5 volts which is 1024 as a value after the analog to digital converter (inside the arduino chip: an atmel microcontroller) makes it's job.
1024 is the max value that is display to our max voltage on the analog input. Its works for 6, 7, 3V, etc.
When we have 0 volt, the value is o. If the max value is 5V, the middle value 2.5 volts should be 512 (1024/2).
Another example may be for 1 volt with a max voltage of 5 volts should be 204 (1024/5) and so on.
To start playing with your new project. You need to go on arduino.cc to download the arduino IDE and upload the previous program to the arduino Uno. Then your can turn the potentiometer to change the motor's speed from to to the full speed.
Next steps:
Following this nice tutorial on how to control a DC motor with an arduino uno and a potentiometer, you can change or improve the schematic by adding more DC motors, create a robot, make a robot working with light sensors to control the motor, etc.
As I said before, this is a serie of tutorial made to learn how to control a DC motor. More tutorial are coming in a couple of days. Stay tuned to our last articles with our newsletter which located on the footer on our website "makersgeneration.net". And also on our social medias ( Facebook, Twitter, Google+, Instagram, Youtube).
Leave a thumb up or a comment below if you like the article or even share it on social medias with your friends.
See you soon for our next tutorial and at the same time wishing you an happy new year and best wishes for 2018.