DC Motor Speed Detection

Introduction

This experiment demonstrates how to measure the speed of a DC motor using an encoder and an Arduino.

DC Motor

Components Needed

Circuit Setup

  1. Connect the motor's encoder output to a digital pin on the Arduino.
  2. Connect the motor's power and ground pins to the appropriate power supply and the GND pin on the Arduino.

The encoder generates pulses with each revolution of the motor, which the Arduino counts to determine the motor's speed.

DC Motor Circuit Setup

Code for DC Motor Speed Detection

Upload the following code to your Arduino to measure the motor's speed:


const int motorPin = 2;
volatile int pulseCount = 0;
float motorSpeed;

void setup() {
  pinMode(motorPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(motorPin), countPulse, RISING);
  Serial.begin(9600);
}

void loop() {
  motorSpeed = pulseCount * 60.0 / 1000.0;
  Serial.print("Motor Speed: ");
  Serial.println(motorSpeed);
  pulseCount = 0;
  delay(1000);
}

void countPulse() {
  pulseCount++;
}
            

Explanation

The encoder generates pulses as the motor shaft rotates. The Arduino counts these pulses to calculate the motor's speed.

Troubleshooting