Battery Charge Level Viewer is a microcontroller based small project which shows charge level of battery using 5 LEDs . Generally battery becomes full at 5volts . Remember that an ADC channel of pic18f2550 can count from 0 to 1023 . Because it's ADC channel register is 10bits. As we like to measure 5volt . We will read the value of adc channel and will convert it to voltage . After converting into voltage we get a understandable value and we shows it through 5 green LEDs. It's the basic concept .
ADC(Analog to Digital Converter ) :
We need a basic knowledge about ADC . Let's take a look at here :![]() |
ADCON1 Register |
Basically ADC is like as voltage divider . According to voltage It produce output .
bit 5 : VCFG0: Voltage Reference Configuration bit (VREF- source)
1 = VREF- (AN2)
0 = VSS or 0 volt
bit 4 :VCFG0: Voltage Reference Configuration bit (VREF+ source)
1 = VREF+ (AN3)
0 = VDD or 5volt
We will set VCFG0[bit 5]=0and VCFG0[bit4]=0 . So we will get highest value 5volt[1023] and lowest value 0volt[0].The ADCON1 is a 10 bit register that means 2 to the power 10 is it's highest counting capacity and result is 1024 . So this register can count from 0 to 1023 . When 0 volt , we get reading at RA0 pin 0 .When 5 volt , we get reading at RA0 pin 1023. It means 5volt equivalent to 1023 .
If 1023 reading value equal to 5 volt.
So 1 reading value equal to 5/1023 volt
So 'read_val' reading value equal to (5/1023)*read_val volt
Source Code :
void main() {
int source=0;
int read_val=0;
int read=0;
ADCON1=0x0E; // Configuring RA0 pin as input
CMCON=7;
TRISB=0x00;
ADC_Init(); // Initialize ADC
PORTB.F0=0;
PORTB.F1=0;
PORTB.F2=0;
PORTB.F3=0;
PORTB.F4=0;
while(1){
source=ADC_Read(0);
read_val=(source*5.50)/1023;
read=read_val;
if(read==1){
PORTB.F0=0;
PORTB.F1=0;
PORTB.F2=0;
PORTB.F3=0;
PORTB.F4=1;
}
if(read==2){
PORTB.F0=0;
PORTB.F1=0;
PORTB.F2=0;
PORTB.F3=1;
PORTB.F4=1;
}
if(read==3){
PORTB.F0=0;
PORTB.F1=0;
PORTB.F2=1;
PORTB.F3=1;
PORTB.F4=1;
}
if(read==4){
PORTB.F0=0;
PORTB.F1=1;
PORTB.F2=1;
PORTB.F3=1;
PORTB.F4=1;
}
if(read==5){
PORTB.F0=1;
PORTB.F1=1;
PORTB.F2=1;
PORTB.F3=1;
PORTB.F4=1;
}
}
}
Circuit :
![]() |
circuit |