Arduino DC Motor Control Experiment

Arduino DC Motor Control with Transistor

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

Circuit Setup

  1. Connect the emitter of the NPN transistor to the ground (GND) of both the Arduino and the external power supply.
  2. Connect the collector of the transistor to one terminal of the DC motor.
  3. Connect the other terminal of the motor to the positive terminal of the external power supply.
  4. 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.
  5. 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:

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