Looking for some help with positive edge trigger (flip flop)

Hello everyone,

Essentially what I want to do is use a hall sensor (non latching) to make an RPM sensor.

Initially, what I was thinking of doing was measuring the time between rising edge pulses from the hall sensor and carrying out the appropriate calculations for RPM.

However, with this method, if the pulse widths are short, and I have other code to execute, I don't want to "miss" detecting a rising edge while executing other code.

Someone suggested that I use a positive edge trigger for more reliability? They said that this component would see a rising edge, and then output the digital HIGH signal for me until the next rising edge is detected, and then it will switch back to low. With this pulse sequencing, I could check for a HIGH signal, get the current time, then go about executing my other code, and keep checking back until a LOW is detected again, get the time and then proceed to find the difference in time. Although I may not detect the very instant that there is a switch from HIGH to LOW or LOW to HIGH, it essentially increases my pulse width greatly so that I will reliably detect a change.

OKAY! So everything sounds great, but I've looked them up and I'm confused. There is a Set, Reset, Q and Qnot. There are also many different types of flip flop logic possibilities. I tried running through the truth tables, but I'm a little confused as I'm still a bit new with this stuff.

Can somebody point me in the right direction with which one I might need? I would really appreciate it! Thank you!

I would point you to a much simpler method that would give you the same results but without using any additional external components. Use the interrupt feature available on either pins 2 or 3:


Your interrupt could trigger on raising signal and just have a short ISR routine that stores a millis() count ( or micros() depending on the speed of the speed signal) on the first raising pulse and on the next raising count interrupt, subtracts the new current millis() count with the previous saved count. That would give you the time in millsecs (or microseconds) of the period of the signal.

The advantages of using interrupts is that your main loop program can be doing other stuff without having to worry about testing or measuring your speed probe frequency. The interrupt routine could just update a global variable with the last valid period time measurement and your main code could use it anytime it needs to.


I really don't see the need for external hardware, especially as you are now not detecting the edge so the timing may be off.

Using an interrupt will do the same job and be more determanistic, ie the latency from edge to read is about the same all the time with an interrupt. With the FF you only read the IP when you happen to get around to it.

So ditch the FF, but for future reference what you describe is a toggle or count function for the FF, tie /Q back to D, that way every time it gets a pulse on CLK the output will change state.

Also make sure the clock input is clean (ie debounced) or you may get multiple toggle per revolution.


Thanks guys, I think I will try the interrupt feature before anything else. I wasn't aware that this was possible.

Still kinda new with Arduino, programming, etc.

Very helpful!

Thanks guys, I think I will try the interrupt feature before anything else. I wasn't aware that this was possible.

Still kinda new with Arduino, programming, etc.

Very helpful!

Here is something I threw together that might help you get started:

// frequency counter
// Uses pin 2 interupt to measure frequency of signal input
// Note micros() function has a 4 us resolution so 1000hz can be 996.02 or 1,000 at 1,000
// retrolefty 2/4/11

volatile unsigned long isrPeriod;
volatile unsigned long start_time;
volatile unsigned long timestamp;
volatile byte first = 1;
unsigned long speed;

void setup() {

  Serial.println ("Frequency counter starting");     // signal initalization done
  attachInterrupt(0, countP, RISING);

  }  // End of setup

void loop() {
  long period = isrPeriod;
  float freq = 1/(float(period) * .000001);
  speed = long(freq);
  Serial.print("Freq =  ");
  Serial.print("   ");
  Serial.println("  Hz.");
}  // end of loop

void countP() 
   timestamp = micros();
   if (first)
      start_time = timestamp;
      first = 0;
      isrPeriod = timestamp - start_time;
      first = 1;     


Hey thanks Lefty, that was going way above and beyond helpful!

I read up a bit more on the interrupt feature and your code was nice and easy to understand.

Big thanks!