Measuring DC Motor RPM Through Built-in Hall Sensor Encoder

INTRODUCTION

When we deal with controlling motor speed, normally we just provide PWM and the motor will rotate accordingly to the voltage reach at motor terminal. To have a better speed controlling, we can monitor RPM. Revolutions per minute (abbreviated rpm, RPM, rev/min, r/min) is the number of turns in one minute. It is a standard unit of rotational speed or the frequency of rotation around a fixed axis.

To monitor RPM, we need to have a feedback or sensor to sense shaft rotation. Luckily, nowadays, there have many DC motor in the market comes with built-in hall sensor encoder attach to the motor shaft. There have a few options offered by Cytron Technologies:

  1. SPG30 With Encoder (Spur Gear)
  2. IG32 With Encoder (Planetary Gear)
  3. IG42 With Encoder (Planetary Gear)

VIDEO

How it works? Please check on this video.

Hardware Preparation

This tutorial uses:

  1. Maker UNO
  2. Cytron 10Amp DC Motor Driver
  3. DC Motor IG42E-24K
  4. Planetary Motor Bracket (IG42E/IG45)
  5. LiPo Rechargeable Battery 11.1V 2200mAH
  6. 2-3 Cell LIPO Balance Charger

You can make hardware connection based on the table below.

Connection
MD10C: +Battery +ve
MD30C: –Battery -ve
MD30C: AMotor red/black wire
MD30C: BMotor red/black wire
MD30C: GNDMaker UNO: GND
MD30C: PWMMaker UNO: pin 10
MD30C: DIRMaker UNO: pin 12
Motor brown wire (Hall sensor VCC)Maker UNO: 5V
Motor green wire (Hall sensor GND)Maker UNO: GND
Motor blue wire (Hall sensor A)Maker UNO: pin 3
Motor purple wire (Hall sensor B)Not connected

SAMPLE CODE

This is sample code used in the video. Enjoy it!

/*
This example shows how to measure motor's RPM through built-in
hall sensor encoder.
Product page:
DC Motor IG42E-24K: https://www.cytron.io/c-84-dc-motor/c-91-dc-geared-motor/c-494-ig42-with-encoder-planetary-gear/p-ig42e-24k
Cytron 10Amp DC Motor Driver: https://www.cytron.io/p-md10c
Maker UNO: https://www.cytron.io/p-maker-uno
Created by:
2/07/18 Idris, Cytron Technologies
*/
// Please insert your motor encoder output pulse per rotation
#define ENCODEROUTPUT 120
#define BUTTON 2 // Maker UNO built-in button connected to pin 2
#define HALLSEN_A 3 // Hall sensor A connected to pin 3 (external interrupt)
#define BUZZER 8 // Maker UNO built-in buzzer connected to pin 8
#define PWM 10 // MD10C PWM connected to pin 10
#define DIR 12 // MD10C DIR connected to pin 12
// Note and frequencies
#define NOTE_G4 392
#define NOTE_C5 523
#define NOTE_E5 659
#define NOTE_G5 784
int melody[] = {
NOTE_G4, NOTE_C5, NOTE_E5, NOTE_G5, 0, NOTE_E5, NOTE_G5
};
int noteDurations[] = {
10, 10, 10, 10, 10, 10, 4
};
volatile long encoderValue = 0;
int interval = 1000;
long previousMillis = 0;
long currentMillis = 0;
int rpm = 0;
boolean measureRpm = false;
int motorPwm = 0;
void setup()
{
Serial.begin(9600); // Initialize serial with 9600 baudrate
pinMode(BUTTON, INPUT_PULLUP); // Set button as input pullup
pinMode(HALLSEN_A, INPUT_PULLUP); // Set hall sensor A as input pullup
pinMode(BUZZER, OUTPUT); // Set buzzer as output
pinMode(PWM, OUTPUT); // Set PWM pin as output
pinMode(DIR, OUTPUT); // Set DIR pin as output
// Attach interrupt at hall sensor A on each rising signal
attachInterrupt(digitalPinToInterrupt(HALLSEN_A), updateEncoder, RISING);
Serial.print("\n\n");
Serial.println("Measuring DC Motor's RPM");
Serial.println("Press Maker UNO button to begin.");
Serial.println("Press again to stop.");
}
void loop()
{
if (digitalRead(BUTTON) == LOW) { // If button is pressed...
measureRpm = !measureRpm;
if (measureRpm == true) { // 1st pressed, start moving the motor
Serial.println("\nStart...\n");
Serial.println("PWM\tRPM");
playMelody();
}
else if (measureRpm == false) { // 2nd pressed, stop the motor
Serial.println("\nStop...");
tone(BUZZER, NOTE_C5, 100);
}
delay(500);
encoderValue = 0;
previousMillis = millis();
}
// Update RPM value on every second
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (measureRpm == true &&
motorPwm < 250) {
motorPwm += 50;
analogWrite(PWM, motorPwm);
}
else if (measureRpm == false &&
motorPwm > 0) {
motorPwm -= 50;
analogWrite(PWM, motorPwm);
}
// Revolutions per minute (RPM) =
// (total encoder pulse in 1s / motor encoder output) x 60s
rpm = (float)(encoderValue * 60 / ENCODEROUTPUT);
// Only update display when there have readings
if (motorPwm > 0 || rpm > 0) {
Serial.print(motorPwm);
Serial.print('\t');
Serial.print(encoderValue);
Serial.print(" pulse / ");
Serial.print(ENCODEROUTPUT);
Serial.print(" pulse per rotation x 60 seconds = ");
Serial.print(rpm);
Serial.println(" RPM");
}
encoderValue = 0;
}
}
void updateEncoder()
{
// Add encoderValue by 1, each time it detects rising signal
// from hall sensor A
encoderValue++;
}
void playMelody()
{
for (int thisNote = 0; thisNote < 7; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(BUZZER, melody[thisNote], noteDuration);
delay(noteDuration);
noTone(BUZZER);
}
}

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.

2 thoughts on “Measuring DC Motor RPM Through Built-in Hall Sensor Encoder”

  1. when i put a PWM value from serial monitor (0 to 255) for motorPwm then the values from encoder reading is not correct anymore.Can u discuss about this please .thank u

  2. hi. how to calculate distance from starter point to where the robot goes? what item that i need to use?

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

DIY Digital Alarm Clock Using REKA:BIT With Micro:bit
Display Internet Time (NTP) on micro:bit
DIY Interactive Robot Using REKA:BIT With Micro:bit
BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
Tutorials of Cytron Technologies Scroll to Top