Arduino Tachometer using IR

Discussion in 'Embedded Systems and Microcontrollers' started by electro_2015, Feb 21, 2015.

  1. electro_2015

    Thread Starter New Member

    Jan 1, 2015
    Hello everybody,

    I am completely new to arduino and one of the first projects am working on is this tachometer from The link to the tutorial is given below:


    I followed all the steps exactly as described (almost blindly, I must say) and used the same exact components on an arduino UNO. But am not able to make the tachometer work properly.

    The values (in RPM) that I am obtaining on my LCD display are extremely erratic. The values fluctuate very greatly and never stabilize. Often negative values are obtained too.

    The sketch used is shown below:

    Code (Text):
    1. #include <LiquidCrystal.h>
    2. LiquidCrystal lcd(3, 5, 9, 10, 11, 12);
    4. volatile float time = 0;
    5. volatile float time_last = 0;
    6. volatile int rpm_array[5] = {0,0,0,0,0};
    8. void setup()
    9. {
    10.   //Digital Pin 2 Set As An Interrupt
    11.  attachInterrupt(0, fan_interrupt, FALLING);
    13.   // set up the LCD's number of columns and rows:
    14.   lcd.begin(16, 2);
    15.   // Print a message to the LCD.
    16.   lcd.print("Current RPM:");
    17. }
    19. //Main Loop To Calculate RPM and Update LCD Display
    20. void loop()
    21. {
    22.   int rpm = 0;
    23.   while(1){    
    25.      //Slow Down The LCD Display Updates
    26.   delay(400);
    27.   //Clear The Bottom Row
    28.   lcd.setCursor(0, 1);
    29.   lcd.print("                ");  
    30.   //Update The Rpm Count
    31.   lcd.setCursor(0, 1);
    32.   lcd.print(rpm);  
    34.   ////lcd.setCursor(4, 1);
    35.   ////lcd.print(time);  
    37.   //Update The RPM
    38.   if(time > 0)
    39.   {
    40.     //5 Sample Moving Average To Smooth Out The Data
    41.       rpm_array[0] = rpm_array[1];
    42.       rpm_array[1] = rpm_array[2];
    43.       rpm_array[2] = rpm_array[3];
    44.       rpm_array[3] = rpm_array[4];
    45.       rpm_array[4] = 60*(1000000/(time*7));    
    46.     //Last 5 Average RPM Counts Eqauls....
    47.       rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
    48.   }
    50.  }
    51. }
    53. //Capture The IR Break-Beam Interrupt
    54. void fan_interrupt()
    55. {
    56.    time = (micros() - time_last);
    57.    time_last = micros();
    58. }

    In my opinion, the error may be comes from the sketch. From the basic knowledge that I have acquired (may be i am completely wrong), I find that the function fan_interrupt() is not used anywhere in the LOOP section. Is this correct?

    Any help would be much appreciated.

  2. ScottWang


    Aug 23, 2012
    The hardware has some problems.
    The voltage of c of darlington maybe too high and it can't pull the base of T3 down to ground, it means that the T3 can't turn off, so the photo transistor won't affect anything.

    There are two ways to fix the problems.
    1. R2 change to 10K, in series with a 2.2K resistor between the base of T3 and c of darlington.
    2. R2 change to 10K,, remove the c of T1 from c of T2, and c of T1 in series with a 10K resistor connecting to +5V.
    try again.
    Last edited: Feb 21, 2015
  3. electro_2015

    Thread Starter New Member

    Jan 1, 2015
    I tried both solutions but none of them worked. The values on the LCD remain at 0 rpm when the IR beam is cut by the blades.
  4. ScottWang


    Aug 23, 2012
    You have to measure the transistor, if they are working ok, then the next step is to find the problem on software.
  5. bertus


    Apr 5, 2008

    What are the specs of the IR-led?
    With 10 Ohms on 5 Volts , the current will be around 380 mA.

  6. ScottWang


    Aug 23, 2012
    If that is the normal IR then it could be damaged.
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    First off do not be discouraged because you got your LCD display to display meaningful characters; if you search out this forum you will find hundreds and hundreds of posts of people struggling to do this very basic accomplishment.

    I quite agree with bertus and Scott that the LED is damaged/dead. It's hard to check out an IR LED so just stick a new one in there, but first change R1 to something around 500 ohms so you don't blow out another.

    Next, do you have a voltmeter? If you are serious about electronics you need one. Harbor Freight sells what is a decent digital voltmeter for $10, or $2 on sale. It even has a battery. EBay can be your friend here too.

    With your voltmeter you cam make measurements around the circuit. The most important one is pin 2 of the Arduino: that should be very low (say less than 0.2V) and very high (again 4.8V or more) when the light is blocked or not blocked between the LED and the phototransistor.

    I would not mess around with anything else in the circuit. R3 may be a little low but should work as is. If T1 is still on when in the dark add a 100K (or larger) resistor from base to emitter (if there is a base on that device).

    fan_interrupt() may not seem to be called but is truly is called all the time, but the call is not done in software, it is done by the hardware every time pin 2 changes. That is what an interrupt is: a break in the normal flow of code to some other tasks; when that task is complete the normal flow of code resumes. Try Googling "interrupt service routine".
  8. electro_2015

    Thread Starter New Member

    Jan 1, 2015
    The maximum allowable current of this IR LED is 150 mA according to RS.
    Of course, I have changed that 10 ohms resistor to a much higher value so that the current is now less than 20mA

    I regularly check that my IR LED is still OK by using my phone camera.
    Till now it is still OK
    ErnieM likes this.
  9. Shehwar Hussain

    New Member

    Oct 26, 2014
    Well, i designed a similar project detecting heart-beats using IR. Problem i faced was that the output-change in signal (input to controller) was very low (order of millis), so i used amplifiers to clean and amplify the change in the signal which work like a charm. Amplifier that i used was LM358 (rail-to-rail output) compatible with 5v logic.
    electro_2015 likes this.
  10. ScottWang


    Aug 23, 2012
    The general LM358 is not a rail-to-rail.
    If you means that LMV358-N, that is a low-voltage (2.7 V to 5.5 V) versions of LM358.