Jump to content

Experiment 5: Controlling DC Motor Using Transistor Driver Circuit

From cognifront
Revision as of 05:41, 10 October 2025 by Siraj (talk | contribs)

Experiment 5: Controlling DC Motor Using Transistor Driver Circuit

Objective

To control a DC motor's speed using PWM output from Arduino through a transistor driver circuit.

Task

  1. Connect DC motor through to Arduino.
  2. Use analogWrite() to vary motor speed.
  3. Implement a simple ramp-up or potentiometer-based speed control.

⚠️ Disconnect your Arduino from the computer while making or removing component connections.

⚠️ Important Note:
1. Connect only the Arduino Uno (without any component connections) to the computer via USB.
2. Upload the code to the Arduino.
3. Disconnect the Arduino from the computer and remove the USB cable.
4. Make all component connections using patch cords as per the circuit diagram.
5. Then connect the 9V adapter to provide external power supply to the Arduino.

Circuit Diagram (without LCD)

Circuit Diagram

Circuit Diagram (with LCD)

Circuit Diagram

Board Connections (without LCD)

Board Connections

Board Connections (with LCD)

Board Connections


Circuit Connections (without LCD)

Component/Pin Connect To (Arduino)
DC Motor (via driver) Output of Transistor Driver Circuit
Potentiometer (Vout) A0
Motor Input A1 D5
Motor Input A2 D6
Motor Enable (PWM) D9

Important

Before uploading your Arduino code, make sure you have installed the following libraries in the Arduino IDE: 1. Wire.h – For I²C communication between Arduino and peripheral devices. 2. LiquidCrystal_I2C.h – For controlling LCD displays using the I²C interface.

✅ Install via (ZIP Method):

Sketch → Include Library → Add .ZIP Library → Select Downloaded .zip File → Open → Wait for “Library Installed” message

✅ Verify :

Sketch → Include Library → The included library name should be present

Code

// © 2025, Cognifront. Member of The IoT Kits Family.

// Experiment 5: Controlling DC Motor Using Transistor Driver Circuit
// with Serial Monitor output for potentiometer and motor speed

// Define analog input pin connected to the potentiometer
const int Pot = A0;

// Define motor control pins
const int motorA1 = 5;   // Motor driver input A1 (direction control)
const int motorA2 = 6;   // Motor driver input A2 (direction control)
const int enableA = 9;   // Motor driver enable pin (PWM speed control)

void setup() {
  // Start Serial communication for debugging and monitoring
  Serial.begin(9600);

  // Configure potentiometer pin as input (optional for analogRead)
  pinMode(Pot, INPUT);

  // Configure motor control pins as outputs
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(enableA, OUTPUT);
}

void loop() {
  // Read potentiometer value (range 0–1023 from ADC)
  int potVal = analogRead(Pot);           
  
  // Map potentiometer range to motor speed range (0–255 for PWM)
  int speed = map(potVal, 0, 1023, 0, 255);  

  // Set motor direction: A1 HIGH, A2 LOW → rotate in one direction
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);

  // Control motor speed using PWM signal on enable pin
  analogWrite(enableA, speed);     

  // Display potentiometer value and motor speed on Serial Monitor
  Serial.print("Potentiometer Value: ");
  Serial.print(potVal);
  Serial.print("  |  Motor Speed (PWM): ");
  Serial.println(speed);

  // Small delay for readability in Serial Monitor
  delay(200);          
}

Circuit Connections (with LCD)

Component/Pin Connect To (Arduino)
DC Motor (via driver) Output of Transistor Driver Circuit
Potentiometer (Vout) A0
Motor Input A1 D5
Motor Input A2 D6
Motor Enable (PWM) D9
LCD VCC 5V
LCD GND GND
LCD SDA A4
LCD SCL A5

Code

// © 2025, Cognifront. Member of The IoT Kits Family.
// Experiment 5: Controlling DC Motor Using Transistor Driver Circuit
// Added: Display potentiometer value and motor speed on 16x2 I2C LCD

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// I2C address for many modules is 0x27. If nothing appears, try 0x3F.
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define analog input pin connected to the potentiometer
const int Pot = A0;

// Define motor control pins (using a transistor/H-bridge style driver)
const int motorA1 = 5;   // Motor driver input A1 (direction control)
const int motorA2 = 6;   // Motor driver input A2 (direction control)
const int enableA = 9;   // Motor driver enable pin (PWM speed control)

void setup() {
  // Start Serial communication for debugging and monitoring
  Serial.begin(9600);

  // Configure potentiometer pin as input (optional for analogRead)
  pinMode(Pot, INPUT);

  // Configure motor control pins as outputs
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(enableA, OUTPUT);

  // Initialize LCD
  lcd.init();       // initialize the lcd (some libraries use begin(), init() works for common LiquidCrystal_I2C)
  lcd.backlight();  // turn on backlight
  lcd.clear();

  // Initial display header
  lcd.setCursor(0, 0);
  lcd.print("Pot: ");
  lcd.setCursor(0, 1);
  lcd.print("Speed: ");
}

/*
  🔌 LCD CONNECTIONS (16x2 I2C Display with Arduino UNO)
  ------------------------------------------------------
  LCD Pin     →  Arduino Pin
  ----------------------------
  GND         →  GND
  VCC         →  5V
  SDA         →  A4   (I2C Data line on UNO)
  SCL         →  A5   (I2C Clock line on UNO)
*/

void loop() {
  // Read potentiometer value (range 0–1023 from ADC)
  int potVal = analogRead(Pot);

  // Map potentiometer range to motor speed range (0–255 for PWM)
  int speed = map(potVal, 0, 1023, 0, 255);

  // Set motor direction: A1 HIGH, A2 LOW → rotate in one direction
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);

  // Control motor speed using PWM signal on enable pin
  analogWrite(enableA, speed);

  // Display potentiometer value and motor speed on Serial Monitor
  Serial.print("Potentiometer Value: ");
  Serial.print(potVal);
  Serial.print("  |  Motor Speed (PWM): ");
  Serial.println(speed);

  // Display on LCD (update only necessary parts; pad with spaces to clear previous digits)
  lcd.setCursor(5, 0);      // after "Pot: "
  lcd.print(potVal);
  lcd.print("   ");         // pad to clear older digits

  lcd.setCursor(7, 1);      // after "Speed: "
  lcd.print(speed);
  lcd.print("   ");         // pad to clear older digits

  delay(200);               // Small delay for readability
}

Observation

  • As the potentiometer value increases, the PWM duty cycle on pin D9 increases.
  • The motor speed changes smoothly in proportion to the potentiometer position.
  • Direction remains constant because A1 is HIGH and A2 is LOW.

Conclusion

The DC motor speed can be controlled effectively using PWM output from Arduino and a transistor driver circuit. The potentiometer provides an analog input for real-time speed adjustment, demonstrating a simple method for variable motor speed control.

Real Life Example

Conveyor Belt Speed Control: Transistor-based motor drivers regulate the speed of conveyor motors used in material handling systems, ensuring smooth movement and controlled transfer of mechanical parts.