reading encoder from arduino

hi there, i am very very new to arduino and other programming languages, like for 1 week. i have a project for graduating, and i have to fix my problem in 3 days.

i have yamamoto 24V, 80 pulse DC motor. Its encoder is h9700. (https://i.stack.imgur.com/PqaKy.png) what should i do to know rpm of my motor with using arduino? i'd be so so happy if someone tell me how can i connect wires correctly. my proffesor told us, we can use arduino to read encoder directly, but i am not sure. i believe this community will save me.

thanks.

This is some simple code that calculates RPM

#define interruptPin 2
volatile int Counts = 400; // count per revolution
volatile unsigned long timeX = 1;
volatile unsigned long LastTime;
volatile int PulseCtrX;
unsigned long MicroS_in_One_Minute = 60000000;

void TachometerInterruptSetup(){
    static int Ctr;
    unsigned long Time;
    Ctr++;
    if (Ctr >= Counts) { // decrease the number pulses used.
      Time = micros();
      timeX += (Time - LastTime); // this time is accumulative ovrer those "Counts" readings
      LastTime = Time;
      PulseCtrX ++; // will usually be 1 unless something else delays the sample
      Ctr = 0;
    }
  }

void setup() {
  Serial.begin(115200);
  // attach interrupt to encoder with 400 pulses per revolution
  attachInterrupt(digitalPinToInterrupt(interruptPin), TachometerInterruptSetup, FALLING);
}

void loop() {
  // Other code to handle tachometer input
  if (PulseCtrX) {
    static unsigned long SpamTimer;
    if ((millis() - SpamTimer) >= (100)) {
      SpamTimer = millis();
      Serial.print(MicroS_in_One_Minute / timeX);
      Serial.println("RPM");
    }
    PulseCtrX = 0;
  }
}

use VCC and GND and attach pin2 to channel A forget channel b unless you need to measure distance.
change: volatile int Counts = 400; // count per revolution
to match how many pulses per revolution your encoder generates on channel A

done :slight_smile:

Z