LDR sensor

Status
Not open for further replies.
Can anyone help me find any mistake on the code and the circuit diagram. My aim is to light the LED if it is dark and turn it off if it is bright using LDR sensor. I have written the code and tested it on MPLAB and constructed the circuit diagram as shown on the attachment but the LED did not light, may be the circuit diagram is not complete or the code is wrong, i would be pleased if i can find solution. My code looks like this
Code:
;PROGRAM FUNCTION:light control for smart home

list	p=16f887	; list directive to define processor
		#include <p16f887.inc>	; processor specific variable definitions

		__config _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
		__config _CONFIG2, _WRT_OFF & _BOR21V


;Declarations:
     
NumH    equ     20h
NumL    equ     21h
     
      
	org		0x0000
	goto Start	

;Program Start:

Start
    call Init             


Main           
   ; Read_ADC
    bsf	ADCON0, GO_DONE		;initiate conversion
    btfsc   ADCON0, GO_DONE
    goto    $-1			;wait for ADC to finish
    movf    ADRESH,W
    andlw   0x03
    movwf   NumH
    BANKSEL  ADRESL
    movf    ADRESL,W
    BANKSEL ADRESH
   movwf	NumL			;return result in NumL and NumH

;Check if it is dark or bright
    btfss NumH,1       ;check the if output voltage exist 2.5 volts
    goto led_on
    bcf PORTB,2       ; turn 0ff the LED
    goto Main
led_on   
    bsf PORTB,2      ;turn on the LED
    goto Main  


;Subroutines:
     
Init

;Initializing porta
    BCF STATUS, RP0 ;
    BCF STATUS, RP1 ; Bank0
    CLRF PORTA      ; Initialize PORTA clearing output data latches
    BSF STATUS, RP0 ; Select Bank 1
    MOVLW 0x06      ; Configure all pinss 
    MOVWF ADCON1    ; as digital inputs
    MOVLW 0xCF      ; Value used to initialize data  direction
    MOVWF TRISA     ; Set RA<3:0> as inputs RA<5:4> as outputs TRISA<7:6>are always read as '0'

; Initializing portb
    clrw                    ; zerp
    movwf   PORTB           ; Ensure PORTB is zero before we enable it.
    bsf     STATUS,RP0      ; Select Bank 1
    movlw   0xF0            ; Set port B bits 0-3 as outputs
    movwf   TRISB           ; Set TRISB register.                                                         
    bcf     STATUS,RP0      ; Select Bank 0         

;initializing ADC
;Set ADCON0
   	    movlw   b'10000001'   ;FOSC/32, select AN0, A/D conversion not in progress, ADC is enabled
     	    movwf   ADCON0 
;Set ADCON1
            BANKSEL ADCON1
	    movlw   b'10000000'    ;Right justified, select VSS, select VDD 
	    movwf   ADCON1
	    BANKSEL ADCON0
            return

    END
 

Attachments

  • circuit.jpg
    18.7 KB · Views: 865


hi,
The LED works ok in the Oshonsoft simulator.
LED is off when AN0 > 2.5V and on when AN0 > 2.5V.
See the attachments.

Your circuit diagram is incomplete!, please post the full circuit diagram for the project.
 

Attachments

  • AAesp01.gif
    36.7 KB · Views: 413
  • AAesp02.gif
    36.8 KB · Views: 353
Hi, i have figured out how to construct a complete circuit diagram, look at the attachment. i want to replace LDR with Infrared photo detector (rs: 2017729). Could you please tell me what is the output voltage of infrared photo detector when it captures motion and when there is no motion. Its because with LDR, i know that its output voltage is less than 2.5v when it is dark and greater than 2.5v when it is bright.
 

Attachments

  • da.jpg
    48.5 KB · Views: 438
  • br.jpg
    65.9 KB · Views: 1,265
  • lm.jpg
    32.9 KB · Views: 1,496
hi,
You cannot just use a PIR 'movement' detector sensor without the fresnel lens system.

Look at these two pdf's for lenses and amplifiers for IR PIR detectors.
 

Attachments

  • PIR_D202X.pdf
    174.7 KB · Views: 451
  • PIRamp1.pdf
    9.4 KB · Views: 250
hi,
The LED works ok in the Oshonsoft simulator.
LED is off when AN0 > 2.5V and on when AN0 > 2.5V.
See the attachments.
Hello Sir, I have been trying to analyse the result of Oshonsoft simulator, could you please tell me about the yellow wave form, what does it show? and why is its amplitute between 2 and 3?. I can understant that pin 35(RB2) is on because it is where LED is connected and it shows that LED is turned on. I would say other pins are off because they are not connected, but I am suprised to see that pin 1(Vpp) is off. from there, I would like to know what does 400,420,0 and 0 on pins 2,3,4 and 5 respectively means. Also 0 and 0 on pin 35 and 37. I'm, I correct to say Vout of 1.954v is the output voltage of LDR sensor and 400 is the corresponding digital number converted from analog voltage of 1.954v. I would be very happy to get your valuable reply.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…