Friday, July 04, 2014

Simple Motor Control using Arduino

We are first going to simply control the speed of a DC motor, in one direction, using a power transistor, diode, and external power supply to power the motor and a potentiometer to control the speed. Any suitable NPN power transistor designed for high current loads can replace the TIP120 transistor. However, I would highly recommend you use a power Darlington-type transistor. Make sure you choose a transistor that can handle the voltage and current your motor will draw. It may be necessary to fit a heat sink to the transistor if it is pulling more than about an amp.

The external power supply can be a set of batteries or a “wall wart”-style external DC power supply. The power source must have enough voltage and current to drive the motor. The voltage must not exceed that required by the motor. For my testing purposes I used a DC power supply that provided 5V at 500mA. This was enough for the 5V DC motor I was using. If you use a power supply with voltage higher than the motor can take, you may damage it permanently.

























Connect It Up

First, make sure your Arduino is powered off by unplugging it from the USB cable. Now, take the required parts and connect them up as in Figure. It is essential for this project that you check and double check all of your connections are as they should be before supplying power to the circuit, as failure to do so may result in damage to your components or even your Arduino.



Enter the Code

Open up your Arduino IDE and type in the code

int potPin = 0; // Analog in 0 connected to the potentiometer
int transistorPin = 9; // PWM Pin 9 connected to the base of the transistor
int potValue = 0; // value returned from the potentiometer

void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
}
void loop() {
// read the potentiometer, convert it to 0 - 255:
potValue = analogRead(potPin) / 4;
// use that to control the transistor:
analogWrite(transistorPin, potValue);
}

Before uploading your code, disconnect the external power supply to the motor and ensure the potentiometer is turned fully counterclockwise. Now upload the code to the Arduino. Once the code is uploaded, connect the external power supply. You can now turn the potentiometer to control the speed of the motor.

No comments:

Post a Comment