infra red based counter

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,580
Helped
122
Reputation
244
Reaction score
114
Trophy points
1,353
Location
India
Activity points
10,382
I am tried to make a rotation counter (for measuaring the rotation of a wheel) by IR TX and RX based. I am using 8051 controller using keil c51 compiler

My problem some time the measuring or counting is not detecting . Some times shows 3 roation for seeing one rotation

I am using to display in LCD

please correct me . the code and proteus file is following.
Code:
#include <REGX51.H>	 
#include<stdio.h> 
#include "lcd.h"


sbit RX=P1^0;
sbit TX=P1^1;








void main(){
unsigned char buffer[8];
unsigned int count = 0 ;
P1 =0x00;
lcd_init();
string("Lcd Testing ");


lcd_cmd(0x01);
 string(" Counter  ");
 
while(1)  
     { 
        
        if (RX == 1)	   // detected 
           {
	        count++;
		        while(RX==1); // relased
            } 
			
	   else 
	   {
	    LINE2
	    string("rotated : ");
    	sprintf(buffer,"%d",count); 
	    string(buffer); 
     	}


    }
}
 

hello

What RPM speed ? or pulses per second ?
You don't use debounce of RX detection ?
so you can get bad detections
RX must stay enough time to 1 , to valid this information
maybe recheck after a litle delay of any µSec...

if Rx==0 you spend a lot of time with all instructions to do !
so you can miss some information

Best way is to use interrupt treatment !
RB0 as RX pin ? and interrupt on RB0 ..
and deboucing by hard or soft.

What about mechanical design ?
and ambiant light ?
 
Actually It havent correct RPM because the fan rotated by hand (not a fan a shaft manually roatating) . want to sense the what is the rotation completed .
I am not using PIC I use the basic 89s52 controller .
I have a development board with IRX and IRf so doing on it .
But am testing it in proteus as switch instead of IRX .
 
Last edited:

Hi,

If you want it to be reliable: then use the hardware counter.

Klaus
 
The problem was the mechanical switch debouching , it cleared and working properly in proteus simulation thanks
the corrected code as follws
Code:
      if (RX == 1)       // detected 
        {
            count++;
               if(count==20); // debounce Checking 20 time
                         { num++;
                          LINE2
                string("rotated : ");
                sprintf(buffer,"%d",num); 
                string(buffer); 
                 count = 0;
Also i have write a post for switch denounce for my reference http://circuits-collection.blogspot.in/2016/08/switch-
bounce.html
 

hello,

reference

this could works fine only if your PIC input is trigger type ... because of slow rampe of voltage
Software filterring is maybe a better solution..
Other advises ?
 

hello,

reference

this could works fine only if your PIC input is trigger type ... because of slow rampe of voltage
Software filterring is maybe a better solution..
Other advises ?

I a taking the input from infrared sensor


But the infrared sensor has very pure sensibility. only read when the reflector placed at near the sensor
 

I changed the infrared module ,And now working properly the module .
But the i fell the counting is wrong . Because, If I increase the speed of shaft (the rotating shaft to count ) the counting is not up the corresponding increase .
I thinks It takes some times to detect the next detection .
Why so .

Code C++ (Qt) - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <REGX51.H>  
#include<stdio.h> 
#include "lcd.h"
 
sbit RX=P1^0;
sbit TX=P1^1;
 
 
unsigned char buffer[8];
unsigned int count = 0 ;
unsigned int num = 0;
 
void main(){
 
P1 =0x00;
lcd_init();
string("Lcd Testing ");
 
lcd_cmd(0x01);
 string("Rotation Counter ");
 
while(1)  
  { 
        
      if (RX == 1)     // detected 
        {
            //count++;
              // if(count==1); // debounce Checking 20 time
                    //   
                        num++;
                          LINE2
                string("rotated : ");
                sprintf(buffer,"%d",num); 
                string(buffer); 
                count = 0;
                             while(RX==1);               
            
       
           }
       }
}






 

hello,



your pluse duration when Rx=1 must be less than the duration of code execution time
else you miss the following pulses

Code:
     num[COLOR=#006E28]++;[/COLOR]
                  LINE2
                string[COLOR=#006E28]([/COLOR][COLOR=#BF0303]"rotated : "[/COLOR][COLOR=#006E28])[/COLOR][COLOR=#006E28];[/COLOR]
                [COLOR=#2B74C7]sprintf[/COLOR][COLOR=#006E28]([/COLOR]buffer[COLOR=#006E28],[/COLOR][COLOR=#BF0303]"%d"[/COLOR][COLOR=#006E28],[/COLOR]num[COLOR=#006E28])[/COLOR][COLOR=#006E28];[/COLOR] 
                string[COLOR=#006E28]([/COLOR]buffer[COLOR=#006E28])[/COLOR][COLOR=#006E28];[/COLOR]  [COLOR=#333555]                count [/COLOR][COLOR=#006E28]=[/COLOR][COLOR=#333555] [/COLOR][COLOR=#B08000]0[/COLOR][COLOR=#006E28];
[/COLOR]

to know the limit , give more informations
- max speed in RPM ?
- width of the detecteur or detection angle

example : 600 Tr/mn => 60 rotation per sec -> 60 hz
periode=1/60= 0,016667 sec or 16.66mS
Detection Angle 30° => 60/360= 1/6
Pulse duration = 166*1/16= 10,3mS

compare to the code duration ?
don't forget , driving LCD functions include some internal delay .in mS delay!

I don't know the MCU 8051 ..
but look at interrupt possibility ... Timer uses as counter ... interruptible input ?
 
max speed in RPM may be up to 300 Rotation /minute. 5 rotation per second so 5 Hz.
Period is 1/5 = 20milli second per rotation
Detection range I didnt know how to understand it .

The following module am using
Is the library function sprint() take more time ? do i need to change that ?
Am updating the LCD data each detection ,Is that any problem
 
Last edited:

Is the library function sprint() take more time ? do i need to change that ?
Am updating the LCD data each detection ,Is that any problem

Yes, i think so..
The only way to solve that,is to use interrupt ...

Count the pulse into the interrupt treatment, outside the main program .
In the main program , display the result, without any constraint of execution time


Maybe you can try to enlarge the area detection to 50% .. 180°



 
Is the library function sprint() take more time ?
Another alternative sometimes useful is managing by ourselves the array outflow by putc(), each byte a time.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…