PIC16F877A object counting device using LDR and Laser module. MikroC code. Industrial grade project.

Опубликовано: 29 Март 2026
на канале: Learning Microcontrollers
1,407
40

Guys, My lectures are free for everyone.
If you want to support my channel, then become a Youtube member by following link below:

   / @learningmicrocontrollers3561  

Seek knowledge from the cradle to the grave
/// /////////////////////////////////////////////////////**********************************///////////////////////////////// Hello guys,
Welcome to Learning Microcontrollers youtube Channel,

Guys I have also compiled course on Udemy as well. Where you will learn under my direct supervision in a more supervised way.
Here is the links to the courses I have on Udemy, By taking any of these courses you will be supporting my channel aswell.
This will help me to make more videos with better hardware in the future. I hope you look forward to it.
Courses Links:

1- https://www.udemy.com/course/pic16f87...
2- https://www.udemy.com/course/mikroc-f...
3- https://www.udemy.com/course/mikroc-f...
4- https://www.udemy.com/course/pic-micr...
5- https://www.udemy.com/course/learn-ar... ///////////////////////////////////////////////////// Hello Folks,
In this video I am gonna show you how to use some cheap modules to make something that worth's a lot. This video is about making an industrial grade project using easily available device like
an LDR sensor, a PIC microcontroller and a laser module or any source of laser.
we sue LDR as target and Laser as beam projector. Anything coming in-between the target and projector is an object. then we use our MikroC for PIC compiler to program this logic and a PIC Kit3 to burn this in the PIC16F877A microcontroller. you can use any PIC microcontroller for this project. You can adjust pin layouts as shown in programming. To display and calibrate the output this project uses a simple 16 x1 LCD with no backlight.

Become a Patreon and support my channel using link below:
https://patreon.com/user?u=81261678

In this project I am using LDR configuration which is separately explained my other video as:
   • Video  

And LCD 16x1 is also explained and initialized in my previous video for better understanding.
   • Video  


To setup your hardware follow the video as under:
   • Setting up a PIC16F877A/PIC16F887/PIC18F****  


NOTE: Youtube dont allow writing mathematical symbols of greater than and lesser than in the description.
So i will use GT = greater than
LT = lesser than
You replace them when you copy paste this code.
Final MikroC for PIC code for this project is as below:

// Lcd pinout settings
sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D7 at RD1_bit;
sbit LCD_D6 at RD0_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D4 at RC2_bit;

// Pin direction
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D7_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC2_bit;
float ldr;
int count=0;
char count1[4];
void main() {
TRISB.F7 = 0;
PORTB.F7 = 0;
Delay_ms(100);

LCD_init();
Delay_ms(1000);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(100);

ADC_init();
Delay_ms(1000);

PORTB.F7 = 1;
delay_ms(100);
count=0;
delay_ms(100);
LCD_out(1,1, " QTY = ");
delay_ms(100);
while(1)
{
ldr = ADC_Read(0);
delay_ms(100);

if ( ldr LT 700 )
{
count = count + 1;
delay_ms(50);

do
{
ldr = ADC_Read(0);
delay_ms(10);
if ( ldr GT 800 )
{
break;
}

}while(ldrLT700 );

}

inttostr(count,count1);
delay_ms(100);

LCD_out(2,1, count1);
delay_ms(100);




}
}