Monday, February 25, 2013

Water level monitor using transistor based sensor

Recently, I had built a water level monitoring setup at my home. I used transistors to sense the water level instead of using a mechanical sensor to avoid wear and tear due to prolonged usage. The main unit is based on a PIC16F886 microcontroller. The microcontroller is used to convert the analog signal from the sensor into a digital value to control the water pump. The percentage of water is then displayed through 7 segment displays.

COMPONENTS USED:

1) General purpose PCB
2) Transistors
     - BC547B x 24pcs
3) PIC16F886 microcontroller
4) IC 7447
5) IC 7805
6) 7 segment displays -> 3pcs
7) Resistors
     -  330 x 7pc
     - 4.7K x 1pc
     - 5.6K x 1pc
     - 10K x 2 pc
     - 22K x 3pcs 
     - 330K x 20pcs
8) Relay 250V 20A
9) Capacitor
     -10uF x 1pc
10) DC regulated power supply (Around 9V)
11) PVC pipe (to mount the sensor)
12) Casing for main unit
13) Power socket and plug
14) Lots of wires!

SENSOR:

This sensor measures water level in 20 discrete levels. Hence, 20 transistors are required, one for each level.

Each bolt is connected to the base of a sensor transistor. The voltage reference is connected to the bottommost bolt.
One single unit of sensor looks like this:
When the water level rises, an electric contact is made between the reference bolt and some of the bolts in the the sensor array, and turns on the corresponding transistors. The number of transistors which are switched on can be determined by measuring the sum of emitter currents.
The schematic of the complete sensor:


CONTROL UNIT:

The PIC16F886's ADC is used to convert the sensor's analog values to digital value. The percentage of water is displayed on three 7 segment displays. The motor is switched on when the water percentage drops below 35% and switched off when the water level rises above 95%.





Pictures of the control unit being mounted into its casing:



The values obtained from the sensor are not exactly linear because a precision summing circuit is not used. Due to this issue, the output has to be calibrated in code.
The assembly code for the microcontroller:


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                        ;;
;; AUTHOR : ARJUN M NAIR                  ;;
;;                                        ;;
;; DESCRIPTION : WATER LEVEL         ;;
;;                                        ;;
;;                                        ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 include 

  ORG 0000
  GOTO START
  
START    ;MAIN PROGRAM START
  ORG 0020
  BANKSEL OSCCON
  MOVLW 61h
  MOVWF OSCCON
; CLKSTABLE BTFSS OSCCON, 2
;  GOTO  CLKSTABLE
  
    ;PORT B CONFIG
  BANKSEL TRISB
  CLRF TRISB
  BANKSEL ANSELH
  CLRF ANSELH
  BANKSEL PORTB
  CLRF PORTB
    ;PORT A CONFIG  
  BANKSEL PORTA
  CLRF PORTA
  MOVLW 0FFh
  BANKSEL ANSEL
  MOVWF ANSEL
  BANKSEL TRISA
  MOVWF TRISA
  BANKSEL ADCON0
  MOVLW 0C1h
  MOVWF ADCON0
  BANKSEL ADCON1
  MOVLW 30h
  MOVWF ADCON1
  
    ;PORT C CONFIG
  BANKSEL TRISC
  CLRF TRISC
  BANKSEL PORTC
  CLRF PORTC
  BANKSEL PSTRCON
  CLRF PSTRCON
  
  
    ;APP INITIATE
  CLRF 73h
  
 STARTPROC   ;PROCESS WHEN APP STARTS
  BANKSEL ADCON0
  BTFSS ADCON0, 1
  CALL CONVERT  
  CALL DISPUPD
  GOTO STARTPROC
     

;; SUBROUTINES


  
DISDELAY    ;DISPLAY DELAY FOR BRIGHTNESS (APPROX 5ms AT 4MHz CLOCK)
  MOVLW d'15'
  MOVWF 70h
 LDSD2 MOVLW d'150'
  MOVWF 71h
  LDSD1 DECFSZ 71h, 1
  GOTO LDSD1
  DECFSZ 70h, 1
  GOTO LDSD2
  RETURN

DISPUPD    ;UPDATE THE DISPLAY (1 PASS)
  BCF STATUS, 5 ;FIRST CONVERT TO BCD
  BCF STATUS, 6
  MOVF 73h, 0
  CLRF 22h
  CLRF 78h
  MOVWF 20h
 LDISP1 MOVF 20h, 0
  MOVWF 21h
  MOVLW 0Ah
  INCF 22h
  SUBWF 20h, 1
  BTFSC STATUS, 0
  GOTO LDISP1
  DECF 22h
  MOVF 21h, 0
  MOVWF 77h
  MOVLW 0Ah
 LDISP2 MOVF 22h, 0
  MOVWF 21h
  MOVLW 0Ah
  INCF 78h
  SUBWF 22h,1
  BTFSC STATUS, 0
  GOTO LDISP2
  DECF 78h
  SWAPF 21h, 0
  IORWF 77h, 1
 
  MOVF 78h, 0
  ANDLW 0Fh
  IORLW 40h
  BANKSEL PORTC
  BSF PORTC, 7
  BANKSEL PORTB
  MOVWF PORTB
    
  CALL DISDELAY
  
  SWAPF 77h, 0
  ANDLW 0Fh
  IORLW 20h
  MOVWF PORTB
  CALL DISDELAY 
 
  MOVF 77h, 0
  ANDLW 0Fh
  IORLW 10h
  MOVWF PORTB
 
  CALL DISDELAY
 
  BANKSEL PORTC
  BCF PORTC, 7
  RETURN
  
CONVERT    ;RETRIEVE ANALOG INPUT
  BANKSEL ADRESH
  MOVF ADRESH, 0
  MOVWF 79h
  MOVLW d'7'
  CLRF 73h
 CONV1 INCF 73h
  SUBWF 79h, 1
  BTFSC STATUS, 0
  GOTO CONV1
  DECF 73h
  MOVF 73h, 0
  ADDWF 73h, 1 
  MOVF 73h, 0
  ADDWF 73h, 1
  MOVLW d'100'
  SUBWF 73h, 0
  BTFSC STATUS, 0
  SUBWF 73h, 1
  BANKSEL ADCON0
  BSF ADCON0, 1
    ;REL CONTROL
  BANKSEL PORTC
  BTFSS PORTC, 6
  GOTO CHKEMPTY

  MOVLW d'95'
  SUBWF 73h, 0
  BTFSC STATUS, 0
  GOTO RELCLR
  GOTO RELOVER
  
 CHKEMPTY MOVLW d'35'
  SUBWF 73h, 0
  BTFSS STATUS, 0
  GOTO RELSET
  GOTO RELOVER
  
 RELCLR
  BANKSEL PORTC
  BCF PORTC, 6
  GOTO RELOVER  
 RELSET 
  BANKSEL PORTC
  BSF PORTC, 6   
 RELOVER RETURN  
END