Easier Approach to Electronic Projects

Controlling DC Motor using Arduino


This is actually controlling speed of DC motor with potentiometer using  Arduino, where DC motor is supplied by PWM output of Arduino.

You can select from any PWM outputs (3,5,6,9,10,11), it is denoted by “~”
One thing to be kept in mind, Never connect any thing above 5V to any digital output of  Arduino, you might damage the Arduino as it is not capable of feeding it. Because Arduino is made just for producing signals but not to bear any loads.

So for that you have to use a transistor as a switch and hook up the DC motor in it’s emitter and supply the external voltage to the Collector and PWM output is given to the base along with a Kohm resistor.
To be serious, this d313 transistor is not that good for carrying out a switching process for a DC motor, you better use 2N2222A transistor in place of this D313 transistor.

 

To begin with PWM, there is a example in Arduino interface on Examples>Basics>Fade. Well, First of all, to test your code, hookup simply LED to any PWM digital output and vary it’s brightness with potentiometer. In arduino the brightness of LED ranges from 0-255, at 0 the LED turns OFF. Here I have used 100k potentiometer, any value will work, since we set the working in the programming ;)

 

The Pin Configuration of D313 transistor is in alphabetic order :p B C E . thats cool, look at picture.
The configuration of this potentiometer is explained in my LED Chaser Using Arduino.
One thing is very important to notice while hooking up DC motor powered by external battery source and signalled by arduino, that the ground of External battery is connected to the Gnd of Arduino and offcource our DC motor’s one pin is connected to emiter and other is connected to the same Gnd. In this picture Arduino is not powered, so dont forget to power it before moving your potentiometer :p

While I was doing this, i tried two potentiometers but they didnt work properly. All small potentiometers like the one here are fine for LED, but I will recommend you to get a bigger potentiometer like the one shown on this link http://www.google.com.pk/imagepot

CODE:

I hope you must have expected a longer code ;) :p Well for both LED and DC motor the code is same.

int potentioValue = 0; //Value of Potentiometer

void setup()
{
pinMode(9, OUTPUT); // we select any PWM pin the ones with this sign “~”
}

void loop()
{
potentioValue = analogRead(0); //0 means Read Value from Analog A0
potentioValue = map(potentioValue, 0,1023,0,255); //as we know LED’s brightness ranges from 0 – 255
analogWrite(9, potentioValue); //Show output at 9 pin according to potentio’s value
}


 

Did it work? If it did, you can thank me back by donating whatsoever amount possible. My website is a personal initiative for education and development, and to provide a user-friendly experience I do not allow any sort of monetizing adds on my website. Your donation is very precious for the future development of Rookie Electronics. Thank you!

Click this donate button which will take you to safe PayPal window where you can donate Rookie Electronics.