Speed of a DC motor using Arduino

Speed of a DC motor using Arduino

The objective of this project is to control the speed of a DC motor using Arduino. A potentiometer will be used to vary the speed by adjusting the PWM (Pulse Width Modulation) signal sent to the motor. This project will demonstrate how to use analogRead() to read input from a potentiometer and analogWrite() to control the motor speed.

Fundamental Programming Concepts

  • PWM (analogWrite()): Used to send a variable signal to the DC motor to adjust its speed.
  • Analog Input (analogRead()): Reads the value from the potentiometer and converts it to control the motor’s speed.
  • DC Motor Control: Adjust the motor’s speed by applying different levels of power using PWM.

Requirement Components

To complete this DC motor speed control using Arduino project, you will need:

  • Arduino Uno Board
  • DC Motor
  • Motor Driver (L298N or similar)
  • Potentiometer
  • Breadboard
  • Jumper Wires
  • Power Source (for the motor)
  • USB Cable (for connecting Arduino to your computer)

Circuit Diagram

Circuit Connection

Component Arduino Pin
Motor Driver (IN1, IN2) Digital Pin 9, 8
Potentiometer (Signal) A0 (Analog Input)
Motor Driver (ENB) PWM Pin 10
Motor Driver (Vcc, GND) 5V, GND
Motor Driver (Motor) DC Motor

How to Connect the Circuit

  1. Motor Driver: Connect the motor to the motor driver. The motor driver will receive signals from Arduino to control the motor’s speed.
  2. Potentiometer: Connect the middle pin of the potentiometer (signal pin) to A0. Connect the other two pins to 5V and GND.
  3. PWM Pin: The motor driver’s ENB pin should be connected to Pin 10 (PWM capable) to receive the PWM signal.
  4. Power: Ensure the motor driver is powered by the motor’s external power source, and that Arduino is powered via USB.

Explanation of Circuit

  • The potentiometer acts as a speed control knob, varying the analog input on A0.
  • The motor driver receives control signals from Arduino and drives the DC motor at varying speeds using PWM.
  • The motor’s speed will be controlled by adjusting the analogWrite() values based on the potentiometer input.

Programming Section for speed of a DC motor using Arduino

Arduino Syntax

Topic Name Syntax Explanation
analogRead() analogRead(pin) Reads the analog input value (0-1023) from a specified pin.
analogWrite() analogWrite(pin, value) Sends a PWM signal (0-255) to the motor driver to control the speed.
pinMode() pinMode(pin, mode) Sets the mode of the pin to OUTPUT or INPUT.

Arduino Code:

Here’s the Arduino code to control the speed of a DC motor using a potentiometer:

const int potPin = A0;    // Pin connected to the potentiometer
const int motorPin = 10;  // PWM pin connected to motor driver ENB
void setup() {
  pinMode(motorPin, OUTPUT);  // Set motor pin as output
}
void loop() {
  // Read the potentiometer value (0-1023)
  int potValue = analogRead(potPin);
  // Map the potentiometer value to PWM range (0-255)
  int motorSpeed = map(potValue, 0, 1023, 0, 255);
  // Set the motor speed using PWM
  analogWrite(motorPin, motorSpeed);
}

Steps to Upload Code:

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the provided code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino.
  5. Rotate the potentiometer to observe the DC motor’s speed changing according to the PWM signal.

Check Output

Once the code is uploaded, turning the potentiometer will adjust the speed of the DC motor. The motor speed will increase as the potentiometer’s resistance decreases and slow down as it increases.

Troubleshooting Tips

  • Motor not spinning? Check the connections from the motor to the motor driver and ensure the driver is properly powered.
  • Speed not changing? Verify that the potentiometer is connected to A0, and check the PWM pin connection for the motor driver.
  • Motor spins erratically? Ensure the external power supply for the motor driver is adequate and stable.

Further Exploration

  • Add Direction Control: Use additional motor driver inputs to control the direction of the motor (forward and reverse).
  • Multiple Motors: Extend the project to control more than one DC motor by adding additional motor drivers and potentiometers.
  • LCD Display: Display the motor speed on an LCD or monitor using Serial Monitor for better visualization.

Note

This project introduces the concept of controlling a DC motor using PWM signals. It demonstrates how analog input from a potentiometer can be used to vary the motor’s speed, a common requirement in robotics and automation projects.

FAQ

Q1: What is PWM, and how does it control the motor’s speed?
PWM (Pulse Width Modulation) controls the motor’s speed by varying the amount of power delivered to the motor. By adjusting the duty cycle, you can increase or decrease the motor’s speed.

Q2: How does analogRead() work in this project?
The analogRead() function reads the input value from the potentiometer, which is then mapped to a PWM value that controls the motor speed.

Q3: Can I control multiple DC motors in this project?
Yes, you can control multiple DC motors by adding additional motor drivers and potentiometers, and adjusting the code to manage each motor independently.

Q4: What is the purpose of the motor driver?
The motor driver acts as an interface between the Arduino and the DC motor, allowing the Arduino to safely control the motor’s speed and direction using PWM signals.

Q5: How can I reverse the motor’s direction?
To reverse the motor’s direction, you can adjust the motor driver’s IN1 and IN2 pins using digitalWrite() to change the motor’s rotation direction.