In this tutorial, we will learn how to control a DC motor in one direction using an Arduino, a power transistor, a diode, an external power supply, and a potentiometer for speed control. We will use a TIP120 transistor, but any suitable NPN power transistor can replace it, especially a power Darlington-type transistor. Ensure your transistor can handle the voltage and current required by your motor, and use a heat sink if it pulls more than about an amp.
What We Will Learn in This Section- Connecting and controlling a DC motor with Arduino
- Using a power transistor and diode for motor control
- Adjusting motor speed with a potentiometer
- Safely powering a motor with an external power supply
Understanding how to control a DC motor is fundamental for many Arduino projects. It introduces you to interfacing external components with Arduino and managing higher current loads safely.
Components List- Arduino
- Potentiometer
- DC Motor
- TIP120 Transistor
- 1N4001 Diode
- Resistors
- Connecting wires
- Breadboard
- LiPo Battery or external DC power supply
Before beginning, ensure your Arduino is powered off by unplugging it from the USB cable. Connect the components as shown in the diagram, double-checking all connections to avoid damage to your components or Arduino. The diode is essential to protect the Arduino from back EMF.
Codeint 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);
}
Code Explanation
Variable Declaration:
potPin
is connected to the potentiometer.transistorPin
is connected to the base of the transistor.potValue
stores the value from the potentiometer.- Variable Declaration:
potPin
is connected to the potentiometer.transistorPin
is connected to the base of the transistor.potValue
stores the value from the potentiometer.
Setup Function:
pinMode(transistorPin, OUTPUT);
: Sets the transistor pin as output.- Setup Function:
pinMode(transistorPin, OUTPUT);
: Sets the transistor pin as output.
Loop Function:
potValue = analogRead(potPin) / 4;
: Reads the potentiometer value and scales it to 0-255.analogWrite(transistorPin, potValue);
: Controls the transistor based on the potentiometer value.- Loop Function:
potValue = analogRead(potPin) / 4;
: Reads the potentiometer value and scales it to 0-255.analogWrite(transistorPin, potValue);
: Controls the transistor based on the potentiometer value.
Before uploading the code, disconnect the external power supply to the motor and ensure the potentiometer is turned fully counterclockwise. After uploading the code to the Arduino, reconnect the power supply. You can now use the potentiometer to control the motor speed.
Further Learning: This book will help you gain more knowledge about Arduino: Beginning Arduino
Comments