MOTOR SPEED MEASUREMENT

motor speed

The code can be downloaded at : https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/DC%20motor%20speed.zip

int encoder_pin = 2; // pulse output from the module
int pulses;
unsigned long timeold;
int pinState = 0;
int lastpinState = 0;
int laju;
int turn;

void setup()
{
Serial.begin(9600);
pinMode(encoder_pin, INPUT);
attachInterrupt(0, counter, FALLING);
pulses = 0;
timeold = 0;
}
void loop()
{
if (millis() – timeold >= 500) {
detachInterrupt(0);
laju=(pulses)*(6.28/10000)*1000*60; //number pulses in 500ms with 20 hole disk
turn=(pulses)/10;
Serial.print(pulses);
Serial.print(” pulses/500ms “);
Serial.print(laju);
Serial.print(” rpm”);
Serial.print(” “);
Serial.print(turn);
Serial.println(” turn/second”);
timeold = millis();
pulses = 0;
attachInterrupt(0, counter, FALLING);

}
}
void counter()
{
pinState = digitalRead(encoder_pin);
if (pinState != lastpinState) {
if (pinState == HIGH) {
pulses++;
}
lastpinState = pinState;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s