encoder reading (speed measurement)

hello,

I have an engine which is provided with an encoder, this encoder should give a number of pulses per rotation (only speed measurement, no position measurement), the encoder provides four pulses per rotation.

Does anyone have a simple program to read these pulses, and debouncer in it? Later I need the number of pulses required for a PID controller in the program. Do I need to read those pulses with interrupt?

thx

Interrupt routine will mean you can get good accurate timings without having to worry about checking the pin in the rest of the code. How fast might the pulses come in? What is the encoder?

hello,

The encoder provides 4 pulses per rotation, and the maximum rpm of the motor is 1500RPM = 25 Hz. So the pulses come in at up to 100Hz

do anyone have a simple example, for the reading of these pulses, and counting them?

thx

Since I am a beginner myself don't take this response as known good. Nonetheless, below is some code that may work for you. Check out interrupts in the Reference section of this site.

const int Encoder_Pin = 2; // output of encoder to interrupt #0, pin 2 of Arduino
int Encoder_Count = 0;

void setup(){
  attachInterrupt(0, Count_Pulses, FALLING); // Attach interrupt to encoder pin
}

//ISR//
void Count_Pulses() {
  // This is the function that runs every time pin 2
  // switches from high to low.
  Encoder_Count++;
}

void loop(){
  /*zero Encoder_Count 
   use millis to set up a time interval
   at end of time interval turn interrupt off
   get Encoder_Count
   turn interrupt on
   do the math to get PRM*/
}
  • Scotty

use millis to set up a time interval at end of time interval turn interrupt off get Encoder_Count turn interrupt on do the math to get PRM*/

How do I do that?

thx for reply

All those instructions are pretty basic code for the Arduino. Those are all things you ought to learn on your own rather than have the code written for you which is about what it would amount to if someone where to answer your question ‘How do I do that?’. As I mentioned before the interrupt stuff is on the Reference Page of this site and that source will contain the answers to many, if not all, of your other questions. You can learn the code on your own and for stuff you just can’t grasp or for stuff that just doesn’t work this forum is the place to go. Have fun discovering how C++ code works and what you learn you can use in the future. - Scotty

okay thx for your answer ;) I will try to learn more on my own :D

thx