Contactless Tachometer on AVR. Part 2. Firmware

Ibrahim Kamal (IKALOGIC)

Part 1. Schematic

Measurement algorithm.
The whole is done with that simple algorithm, which is executed on each new ADC (Analog to Digital Converter) end of conversion:

//Global variables used :
// ir_value : contains the value of the intensity of IR reflection
// ain_th_high : High threshold (rising hysteresis)
// ain_th_low : Low threshold (falling hysteresis)
// t_capture & t_postscale : reading of the time elapsed since last detected cycle
// ir_history_b and ir_history_a: hold the last two values of ir_value
// last_state : used to remember which hysteresis level to use
ir_value = ADC>>2;
if ((ir_value > ain_th_high) && (last_state==0)) //a cycle is detected
last_state = 1; //for next edge detection, use the falling hysteresis
// for next detection
t_capture=TCNT1;TCNT1 = 0;
t_postscale=post_scaller; post_scaller=0;

if ((ir_value < ain_th_low) && (last_state==1)) //a low level is detected
last_state = 0; //for next edge detection, use the rising hysteresis for
//next detection

if (delta(ir_history_a,ir_value) > 10)
{ //the values having more than 10 adc
//quantums will be used to determine
ir_history_b = ir_history_a; //average and hysteresis levels
ir_history_a = ir_value;
ir_avg = (ir_history_a+ir_history_b)/2;
ain_th_high = ir_avg + 5;
ain_th_low = ir_avg - 5;

A button at the top right corner allow you to activate the IR LED and start measuring RPM data. Once released, the last data is held on screen.

Contactless Tachometer on AVR

Contactless Tachometer on AVR

Contactless Tachometer on AVR

Some tips and tricks for the road

Besides from showing you how to measure RPM of a shaft with IR LED, this project also show you how to do many other cool things like:

- Using an alpha numeric LCD without the need of a potentiometer for the contrast. A simply algorithm that you can find in the source code will adapt the contrast of the LCD according to the batter level, to make sure you alwyas have the brightest possible display (see AVR: Monitor power supply voltage, for free!)
- Using LCD is 4 bit mode to save some wires
- Implementing a hysteresis to enhance noise immunity of the system


Source Code - download

You may have to register before you can post comments and get full access to forum.
User Name
10% Off for PCB and PCB Assembly Order