Introduction
This experiment demonstrates how to control a DC motor with an Arduino using a transistor. The transistor acts as a switch, allowing the Arduino to turn the motor on and off based on programmed instructions.
Components Needed
- Arduino (e.g., Uno, Nano)
- DC motor
- NPN transistor (e.g., 2N2222 or TIP120)
- 1kΩ resistor
- Diode (e.g., 1N4007)
- External power supply (e.g., 9V battery) for the motor
- Breadboard and jumper wires
Circuit Setup
- Connect the emitter of the NPN transistor to the ground (GND) of both the Arduino and the external power supply.
- Connect the collector of the transistor to one terminal of the DC motor.
- Connect the other terminal of the motor to the positive terminal of the external power supply.
- Place a diode across the motor terminals, with the anode connected to the transistor side and the cathode (marked end) connected to the positive power supply terminal. This protects the transistor from back-emf.
- Connect a 1kΩ resistor between digital pin 9 on the Arduino and the base of the transistor.
Code for DC Motor Control
Upload the following code to your Arduino to control the motor:
// Define motor control pin
const int motorPin = 9;
void setup() {
// Set the motor control pin as output
pinMode(motorPin, OUTPUT);
}
void loop() {
// Turn the motor on
digitalWrite(motorPin, HIGH);
delay(2000); // Run motor for 2 seconds
// Turn the motor off
digitalWrite(motorPin, LOW);
delay(2000); // Pause for 2 seconds
}
Explanation
This code controls the motor as follows:
- The transistor acts as a switch, allowing current to flow through the motor when
motorPin
is set to HIGH. - Setting
motorPin
to LOW turns the transistor off, stopping current flow and turning the motor off. - The
delay
commands create a 2-second cycle where the motor alternates between running and stopping.
Note: The diode across the motor terminals prevents damage to the transistor from the reverse voltage generated by the motor when it turns off (back-emf).
Troubleshooting
- If the motor doesn’t turn on, check all connections and ensure that the external power supply is sufficient for the motor.
- Ensure the transistor is oriented correctly. The emitter, base, and collector pins must be connected properly.
- Verify the diode orientation; placing it backward can prevent the motor from running or cause damage.
- If the motor runs continuously or erratically, confirm that the motorPin is correctly set in the code and connected to the base of the transistor through the resistor.