I need help with my tachometer

hi... i have around the forum with this but a still have many doubts about it.....
first i need help with the code i have one which its a modified version of RPM counter in the playgorund....
changes i would to make are:
if there a possible way to structure the script to be faster.
add some filters i think with low-pass filter will be ok.
also y attach a delay in the main loop for update every second of rpm but a wold like to create a memory or something like that to save the process in the loop for that second..... would be recommended???
here its the code i have now:

volatile byte rpmcount;
volatile int status;

unsigned int rpm;

unsigned long timeold;

 void rpm_fun()
 {
   //this function its activated once every spin... for now   //
   //
      rpmcount++;
      
 }
void setup()
  {
   Serial.begin(115200);
   //Interrupt 0 it is digital pin 2, 
   // (cambia de HIGH a LOW)
   attachInterrupt(0, rpm_fun, FALLING);
   


   rpmcount = 0;
   rpm = 0;
   timeold = 0;
   status = LOW;
 }

 void loop()
 {
  
   delay(1000);//update rpm every second........;
   detachInterrupt(0);
   //Note: this will be changed 60*1000/(millis() - timeold)*rpmcount for now its only one sample per rpm
   //for multiple samples per spin
   rpm = 60*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;
   
   //print rmp to the serial port
   Serial.println(rpm,DEC);
   
   //restart the interrupt process
   attachInterrupt(0, rpm_fun, FALLING);

  }

i tried the code but have many issues. my sensors are one optical taked from an old ball mouse, and an inductive sensor take from a car transmisson gear... i have connected the sensor direct to arduino to interrupt zero...... but the sensors never change to high so rpm always stayed at 0.....
maybe its because they are not digital sensor right??....

so interrupts pins are only for digital sensors???, is there anything i could use to connect these sensors to interrupt pin 0???

hope you could help me out....

already thanks

copachino:
changes i would to make are:
if there a possible way to structure the script to be faster.
add some filters i think with low-pass filter will be ok.
also y attach a delay in the main loop for update every second of rpm but a wold like to create a memory or something like that to save the process in the loop for that second..... would be recommended???

I suggest you're wasting your time trying to enhance the sketch until you can get the Arduino to detect your sensor inputs. This strikes me as a wiring/driver circuit issue. How are you connecting your inductive sensor to the Arduino? How are you connecting your optical sensor to the Arduino?

thanks for reply.....

in fact i believe its also a wiring/driver case..... optical sensor it connected direct from the mouse circuit.... i have tested in analog pins and works fine..... inductive sensor.... well i am connecting this sensor direct to arduino without any driver o something else..... also tested in analog pings and works fine

but for interrupt pins.... what kind of driver could be ok schmitt trigger??

What sort of voltage do you see on the analogue inputs? Is it sufficient to trigger a digital input?

the voltage its less than a volt

copachino:
the voltage its less than a volt

That's a "no", then. You need to amplify the signal to get something high enough for the Arduino to detect.

the voltage its less than a volt

It is the voltage signal from your sensors ? No good for a digital pin. If your sensor give a signal like 0 V to 1 V, well get a comparator ( LM339 ) Vcc of 5 V for the comparator, use / set a voltage divider of one of the op-amp input, and place the signal at the other op-amp input. The output of the comparator should get a signal of 0 V to 5 V. Or a simple transistor circuit in switching mode ( on /off ) might work to.

As for testing your code, use a 555 circuit in astable mode, set to a frequency represent the RPM and see if you have a proper reading / calculation / display. Use pin 3 of the 555 going to the interrupt pin.

Should it work A LM324 as comparator???

I try this part, LM324 is a quad op-amp. It did not work for me. This op-amp is design for a dual-power supply. But a LM339 is design for one power supply. If parts is hard to find/expensive, try a transistor circuit, it might work, a transistor version of a comparator. A simple NPN may work... ( I recomend to do simulations on that one )

Heh...a cheap idea using transitors...

in fact i do have a LM339 jejeje i haven't seen it(took a few from a pc power supply)..... i will try it see if it works.....

ummm about 555 i cant find any on electronic shop.... do you know some electronics devices who uses it??? may i can recycle one or two... a friend of mine have many electronic grabage most of it tv, microwaves and stuff like that

Techone:

the voltage its less than a volt

It is the voltage signal from your sensors ? No good for a digital pin. If your sensor give a signal like 0 V to 1 V, well get a comparator ( LM339 ) Vcc of 5 V for the comparator, use / set a voltage divider of one of the op-amp input, and place the signal at the other op-amp input. The output of the comparator should get a signal of 0 V to 5 V. Or a simple transistor circuit in switching mode ( on /off ) might work to.

As for testing your code, use a 555 circuit in astable mode, set to a frequency represent the RPM and see if you have a proper reading / calculation / display. Use pin 3 of the 555 going to the interrupt pin.


this i found in the datasheet of LM339 does it work??? which wolud be better with o without histieresis

ummm about 555 i cant find any on electronic shop....

What ? can find a 555...! It is a popular chip. Even Radio-Shack carry it. Most Electronics store may carry it. Oh well...
hum... yes you can still make a astable circuit using basic NPN transistor. Just use the RC to calculate the frequency. The frequency of the astable circuit for a RPM about 800 is 13 Hz. I already make a 555 version here at my place. I use it to test your code. I have to use my code to test and it work. But your code...sorry for the bad news... not working. I change the baud rate to 9600. Still not working. I only get this : 46589 36789 78459 Random big number.... <--- ??? Conclusion : code need to be re-design.

About the comparator, a simple voltage divider at each input pin will do.

in fact i do have a LM339 jejeje i haven't seen it(took a few from a pc power supply)..... i will try it see if it works.....

I hope the part is operational. I hope you have a few NPN, PNP general purpose transistor : like the 2N3904

I will post some schematics ( transistor astable circuit ) and a comparator circuit. Not now... I have to :sleeping:

Anyway, here my frequency code.

/*
 size = 2996
 Version 1.0
 
 To measure a frequency pulse and display in the serial monitor.
 
 Connect the pulse source to pin 2. 
 The source have to be a +5 V to Gnd signal.
 
 In this experiment, I use a second Arduino to generated
 a pulse.  A 555 in astable mode can be use.
 Just connect pin 3 of the 555 to pin 2 of the Arduino.
 
 Program by Serge J Desjardins aka techone
 Toronto, Ontario, Canada
 
 Compile and Tested 

*/

byte freqpin = 2;

unsigned long timeone;

unsigned int frequency;
unsigned int timediff;

unsigned int tcount=0;

// sample time of 1 second or 1000 ms.
unsigned int sampletime=1000;

volatile unsigned int count=0;

void setup()
{
 pinMode(freqpin, INPUT);
 attachInterrupt(0,freqinput,RISING);
 // set interrupt pin 2 and use rising ( from gnd to +5 ) 
  Serial.begin(9600); 
} 

void loop()
{
 /* Use while() loop and use millis(). 
    Calculate the time difference and compare the 
    value with the sample time. 
 */
 // activate the interrupt
 tcount=0;
 count=0;
 timediff=0;
 attachInterrupt(0,freqinput,RISING);

 timeone=millis();
 while(timediff<sampletime)
 {
  tcount=count;
  timediff=millis()-timeone; 
 }
 // de-activated the interrupt
 detachInterrupt(0);
 
 // Calculated frequency
frequency=tcount; 
 
 // Display frequency
 Serial.print("Test while - ");
 Serial.print("Frequency : ");
 Serial.println(frequency,DEC);
 delay (3000);
 
}  

// The interrupt routine
void freqinput ()
{
  count++;
}

To calculate the RPM using my code, just add this line

frequency=tcount;
frequency=frequency*60; 
 
 // Display frequency
 Serial.print("Test while - ");
 Serial.print("Frequency : ");
 Serial.println(frequency,DEC);
 delay (3000);

Bear in mind, my code simply sample a series of pulses for 1 second. RPM is simply * 60 second = Rotation per minute.

Example : @ 12 Hz = 720 rpm
@ 13 Hz = 780 rpm
@ 14 Hz = 840 rpm

So expect those results when using my code and use the new code line.

amazing.... i was about to make a frequencies oscillator with an op amp and this its perfect because with your code i can measure frecuency i the oscillation
and then use code to calculate RPM adding those lines......

my code have many errors when it makes maths it seems to be calculating period instead of frecuency...

one question?? in your code wouldn't be faster to declare in void setup this: tcount=count..... or it may carry some troubles...

one question?? in your code wouldn't be faster to declare in void setup this: tcount=count..... or it may carry some troubles...

I am not an expert here. But I do not recomend any modification. It may not work... But if you dare... do it and see what happen.

Since you do not have a 555, here a picture / schematic of an astable oscillator using transistor. That circuit is simulation tested and I include some formulas to calculate the R or C. The ln 2 is the constant so : ln 2 = 0.69314718. It is also in the 555 calculation.

thanks this will be good for testing code until i can wire my sensor to arduino digital interrupt.....
ps.did you get my PM???

Yes. and I reply to it.

Here a basic schematic and a pulse into a comparator circuit. I use the LM339.

the transistored based astable vibrator didnt work... i just simulated with livewire and give a always low...... then i make the circuit and plug into arduino and did the same always low......

but... good news i found a NE555 in a local store amazing..... im going to make a very simple astable timer.... for about 5khz

the transistored based astable vibrator didnt work... i just simulated with livewire and give a always low......

What frequency did you set ? The schematic I post is from Circuit Wizard. LiveWire is from them. Good you found a 555.

For R1 and R2 choose 15 K, the cap is 10 nF or 1.5 K for R1 and R2 and C is 0.1 uF. And a diode like 1N4148 parrall to R2, pointing toward the ground.