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
Digital Voltmeter measure voltage between two point in circuit . Now we are going to make a digital voltmeter which can measure 50 volt maximum . We will use pic18f2550 microcontroller and it's ADC pin . 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 50 volt , we need to apply a simple technique . So we have to convert 50 volt to 5 volt and we get reading 1023 at adc channel for 50 volt .
For converting we will connect 68k ohm, 22k ohm and 10k ohm resistors in serial so that during applying 50 volt it makes 5 volt drop at 10k ohm resistor . It's the main theme .
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 50 volt.
So 1 reading value equal to 50/1023 volt
So 'read_val' reading value equal to (50/1023)*read_val .
Source Code :
sbit LCD_RS at LATB7_bit;
sbit LCD_EN at LATB6_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB4_bit;
sbit LCD_D6 at LATB3_bit;
sbit LCD_D7 at LATB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
char volt[]=" ";
char mv[]="v";
void main() {
double source=0;
float calv=0;
float sum=0;
float mult=0;
ADCON1=0x0E; // Configure RA) pin as input
CMCON=7;
ADC_Init(); // Initialize ADC
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(2,15,mv);
while(1){
source=ADC_Read(0);
calv=(source*50.50)/1023;
floattostr(calv,volt);
Lcd_Out(1,1,"Volt Meter");
Lcd_Out(2,1,"V=");
Lcd_Out(2,4,volt);
}
}
For home security , we can build " Thief Detector " using PIC 18f2550 microcontroller and PIR Motion Sensor . When something will be moving front to the sensor , the thief detector system will give an alarm to us . That's the basic theme of this project .
PIR Motion Sensor :
Actually this sensor is continuously transmitting an Infrared Ray signal and receiving the reflected signal . According to the information of receiving signal it changes it's output signal . You can see the pin out of PIR Motion Sensor .
Generally when something moving in the front of it , the sensor produce 3.3 volt to it's out pin . Otherwise the out pin will be 0 volt . By applying this technique we can get information or status of out doors .
We will get the PIR Sensor connected with ADC(Analog to Digital Converter ) pin of PIC 18F2550 . We will use RA0 as input .
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 5 volt equal reading 1023 .
So 1 volt equal reading 1023/5 [When something detect PIR Sensor provide 3.3 volt at Output Pin]
So 3.3 volt equal reading (1023/5)*3.3 =675.8 . When we get reading 675 at ADC channel , we understand that sensor detects something . So it make PORTB.F6 pin high and Buzzer turns on .
Digital Voting Machine is one kind of microcontroller based device which can perform in election. Once I had made Digital Voting Machine when I was in 6th semester. Today I am going to share an upgrade version of it. In the upgrade version I used EEPROM of PIC 18f2550 so that data could be saved during electricity failure.
Now take a look on the picture :
Digital Voting Machine Circuit
Description :
This Voting Machine conains 6 buttons for operation and two LEDs for notification.
Candidate Category Button :
Button_RA0 : Candidate_1 .
Button_RA1 : Candidate_2 .
Button_RA2 : Candidate_3 .
Control Button :
Button_RA4 : Control Button.
Status Viewer Button :
Button_RC2 : Status View Button.
Result Button :
Button_RA3 : Final Result Button.
***Control Button :
This is the main controller of all other buttons. Without having permission of this button, any button can't perform. Without permission, reading instruction will not be taken. This will be placed in the presiding officer's room. Once he press the button and only one of these buttons can be permitted to perform only for one time. If any key or button is pressed, it works for one time and the system becomes disable automatically for voting until the presiding officer press control button again. So, one person will be able to vote for one time & voter will not be able to do that without permission of PO. Watch this video to understand completely. Video of Digital Voting Machine in Proteus :
digital voting machine
***Status Viewer Button :
For security reason we are hiding the voting status from the voter and Only the PO(presiding officer) can check this. At first PO have to press "Control Button " and LED 1 turns on when he can see the voting status by pressing "View Status" button. Current status will be shown only for a little time before getting back to the previous stage with turning on LED2.
*** Result Button :
When OP decide to publish who is winner , he just need to press "control button" and after that need to press Final Result Button . After Showing result , it will be reset and all data will be erased .
***Candidate Category Button :
When control button gives permission , only one of "candidate button category" buttons can perform, only for one time . After performing, this will disable the system . Generally these buttons are incremented by one during each action .
***LED 1 & LED 2 :
When LED1 remains on , others buttons can perform . When LED2 remains on, all buttons are disabled.
Source Code :
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections
short dat_can1=0,dat_can2=0,dat_can3=0,con=4;
char txt[]=" ";
char txt1[]="htp://pic18fmicrocontroller.blogspot.com";
char txt2[]=" Digital Voting Machine";
int i=0,j=0,chk=10;
char c1i='0',c1j='0',c1k='0';
char c2i,c2j,c2k;
char c3i,c3j,c3k;
int can_1adrs =15; // Keeping memory address for Candidate 1
int can_2adrs =19; // Keeping memory address for Candidate 2
int can_3adrs = 29; // Keeping memory address for Candidate 3
int chkk = 33,aq=0,b=0,aa=0,bb=0,cc=0;
char thirdchar(short dk){ ////find third Char of Short Data
aq=dk/100;
aa=aq*100;
aa=dk-aa;
if(aq==0)
{ aa=dk;
return '0'; }
if(aq==1) return '1';
if(aq==2) return '2';
if(aq==3) return '3';
if(aq==4) return '4';
if(aq==5) return '5';
if(aq==6) return '6';
if(aq==7) return '7';
if(aq==8) return '8';
if(aq==9) return '9';
}
char secondchar(short dk){ ////find Second Char of Short Data
b=aa/10;
bb=b*10;
bb=aa-bb;
if(b==0)
{ bb=dk;
return '0'; }
if(b==1) return '1';
if(b==2) return '2';
if(b==3) return '3';
if(b==4) return '4';
if(b==5) return '5';
if(b==6) return '6';
if(b==7) return '7';
if(b==8) return '8';
if(b==9) return '9';
}
char firstchar(short dk){ ////find first Char of Short Data
if(bb==0) return '0';
if(bb==1) return '1';
if(bb==2) return '2';
if(bb==3) return '3';
if(bb==4) return '4';
if(bb==5) return '5';
if(bb==6) return '6';
if(bb==7) return '7';
if(bb==8) return '8';
if(bb==9) return '9';
}
void main() {
ADCON1=0x0F;
CMCON=7;
TRISA.F0=1;
TRISA.F1=1;
TRISA.F2=1;
TRISA.F3=1;
TRISA.F4=1;
TRISC.F0=0;
TRISC.F1=0;
TRISC.F2=1;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
/////////////////////// Lcd Scroling Display Start
for(i=0;i<19;i++){
Lcd_Out(1,1,txt1);
Lcd_Out(2,1,txt2);
Lcd_Cmd(_LCD_SHIFT_LEFT);
delay_ms(200);
}
Lcd_Cmd(_LCD_CLEAR);
/////////////////////// Lcd Scroling Display End
for(j=0;j<16;j++){
Lcd_Cmd(_LCD_CLEAR);
txt[j]='.';
Lcd_Out(1,1," Starting...");
Lcd_Out(2,1,txt);
delay_ms(300);
}
dat_can1 = EEPROM_Read(can_1adrs); // reading previous data if avail able for Candidate 1
c1i=thirdchar(dat_can1); ///third
c1j=secondchar(dat_can1); //second
c1k=firstchar(dat_can1); //first
//// these functions should be called with this structure
/// I used this , because MikroC's short to str conversion didn't work .
dat_can2=EEPROM_Read(can_2adrs); // reading previous data if avail able for Candidate 2
c2i=thirdchar(dat_can2);
c2j=secondchar(dat_can1);
c2k=firstchar(dat_can1);
dat_can3=EEPROM_Read(can_3adrs); // reading previous data if avail able for Candidate 2
c3i=thirdchar(dat_can3);
c3j=secondchar(dat_can1);
c3k=firstchar(dat_can1);
Lcd_Cmd(_LCD_CLEAR);
if(dat_can3<=0){ // check if it runs for the first time , then set data null or 0
EEPROM_Write(can_3adrs,0);
dat_can3=EEPROM_Read(can_3adrs);
}
if(dat_can2<=0){ // check if it runs for the first time , then set data null or 0
EEPROM_Write(can_2adrs,0);
dat_can2=EEPROM_Read(can_2adrs);
}
if(dat_can1<=0){ // check if it runs for the first time , then set data null or 0
EEPROM_Write(can_1adrs,0);
dat_can1=EEPROM_Read(can_1adrs);
}
while(1){
c1i=thirdchar(dat_can1); // It is taking the last update data for Candidate 1
c1j=secondchar(dat_can1);
c1k=firstchar(dat_can1);
c2i=thirdchar(dat_can2); // It is taking the last update data for Candidate 2
c2j=secondchar(dat_can2);
c2k=firstchar(dat_can2);
c3i=thirdchar(dat_can3); // It is taking the last update data for Candidate 3
c3j=secondchar(dat_can3);
c3k=firstchar(dat_can3);
Lcd_Out(1,1,"C_1 C_2 C_3");
Lcd_Chr(2,1,'*');
Lcd_Chr(2,2,'*');
Lcd_Chr(2,3,'*');
Lcd_Chr(2,6,'*');
Lcd_Chr(2,7,'*');
Lcd_Chr(2,8,'*');
Lcd_Chr(2,11,'*');
Lcd_Chr(2,12,'*');
Lcd_Chr(2,13,'*');
if(PORTA.F4==0){ // if control button is pressed , it enables voting.
con=3;
}
if(con!=3){ // if control button is not pressed ,naturally it disables voting.
Lcd_Out(2,15,"DV");
PORTC.F0=0;
PORTC.F1=1; //LED2 is on
}
while(con==3){ // if control button is pressed , it enables voting.
Lcd_Out(2,15,"EV");
PORTC.F0=1; //LED1 is on
PORTC.F1=0;
if(PORTC.F2==0){ // when view status button is pressed
Lcd_Chr(2,1,c1i);
Lcd_Chr(2,2,c1j);
Lcd_Chr(2,3,c1k);
Lcd_Chr(2,6,c2i);
Lcd_Chr(2,7,c2j);
Lcd_Chr(2,8,c2k);
Lcd_Chr(2,11,c3i);
Lcd_Chr(2,12,c3j);
Lcd_Chr(2,13,c3k);
delay_ms(4000);
con=5; // con=5 makes disable voting
}
if(PORTA.F0==0){
dat_can1=dat_can1+1; // Candidate 1 variable is incrementing .
EEPROM_Write(can_1adrs,dat_can1); // writing the incremented value on EEPROM for Candidate 1
con=5; // con=5 makes disable voting
}
if(PORTA.F1==0){
dat_can2=dat_can2+1; // Candidate 2 variable is incrementing .
EEPROM_Write(can_2adrs,dat_can2); // writing the incremented value on EEPROM for Candidate 2
con=5; // con=5 makes disable voting
}
if(PORTA.F2==0){
dat_can3=dat_can3+1; // Candidate 3 variable is incrementing .
EEPROM_Write(can_3adrs,dat_can3); // writing the incremented value on EEPROM for Candidate 3
con=5; // con=5 makes disable voting
}
if(PORTA.F3==0){ // If Result Button is pressed .
Lcd_Cmd(_LCD_CLEAR);
for(j=1;j<17;j++){
Lcd_Out(1,1," Calculating...");
Lcd_Out(2,j,".");
delay_ms(200);
}
if(dat_can2>dat_can1&&dat_can2>dat_can3){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Winner is C_2");
Lcd_Out(2,1,"Congratulation!!");
delay_ms(5000);
Lcd_Out(1,1,"Winner is C_2");
Lcd_Out(2,1,"He got =");
delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
dat_can2=0;
//Erasing All Datas
dat_can1=0;
dat_can3=0;
EEPROM_Write(can_1adrs,0);
EEPROM_Write(can_2adrs,0);
EEPROM_Write(can_3adrs,0);
}
else if(dat_can1>dat_can2&&dat_can1>dat_can3){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Winner is C_1");
Lcd_Out(2,1,"Congratulation!!");
delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
dat_can2=0;
dat_can1=0;
dat_can3=0;
EEPROM_Write(can_1adrs,0);
EEPROM_Write(can_2adrs,0);
EEPROM_Write(can_3adrs,0);
}
else if(dat_can3>dat_can1&&dat_can3>dat_can2){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Winner is C_3");
Lcd_Out(2,1,"Congratulation!!");
delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
dat_can2=0;
dat_can1=0;
dat_can3=0;
EEPROM_Write(can_1adrs,0);
EEPROM_Write(can_2adrs,0);
EEPROM_Write(can_3adrs,0);
}
else{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Something is");
Lcd_Out(2,1,"Worng!!!");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
}
con=5;
}
}
}
}