How to add a LED indicator to report LDR sensor error using PIC?
I want to add a simple LED indicator that will turn ON when the LDR is broken or something


Actually driving the LED indicator is the easy part, of course. Depending on the expected light output from the "LDR fault detected" LED, then a drive transistor might not be needed.

For detecting that the LDR is "broken or something", you need to decide exactly what means "broken or something", especially the "something". I have only seen LDRs fail open-circuit (due to fractured elements), but there might be other failure modes.

Here are 2 quick approaches, which you can investigate and modify to fit your overall design constraints:


Use an LDR whose dark resistance can still be easily detected as different from an "open-circuit". That way, you would still expect to get an ADC output below the maximum value, when used in that potential divider with R1.

For example, using an LDR with a 20MΩ dark resistance makes this much more difficult, especially using the 10-bit ADC in your PIC16F877A. Whereas an LDR with a 100kΩ dark resistance is much easier to distinguish from being an open-circuit, as you would not expect the ADC value to get close to its maximum value even when the LDR is in the dark.
Or, more complex, but more flexible, you can test the LDR yourself, at your chosen interval.


Don't react to changes in your LDR-controlled ADC output immediately. Introduce a delay, so that your code does not report "light" during the test below.
Add a small green LED, under MCU control, which can shine on your LDR (green is a colour to which LDRs are typically most sensitive, around 550nm).
When you want to test the LDR, read the LDR's ADC value, power-on the small green LED and read the LDR's ADC value again. I expect that you will detect an LDR response within tens of milliseconds (depending on your LDR). Does the LDR's measured ADC value drop (indicating that it detects light and its resistance drops)? Yes - then the LDR is working. Then immediately switch off the green LED.
There will be a short delay before the LDR has gone back into its "dark state", after it has been tested by that green LED.



In that second approach, there are several variables you need to consider and adjust in your code, but that will allow you to fulfil an objective to test that the LDR responds to light.