Simple Voting Machine using 8051 Microcontroller

voting-machine-using-8051-microcontroller-circuit-diagram

Voting or polling machines is one of the finest example for the application of a Microcontroller. This project brings you the design and programming of building a very simple, cost effective polling machines which can do great job for elections in schools, colleges or particular locality. The highlighting feature of this voting machine using 8051 microcontroller is that the results of the poll can be viewed instantly also can be monitored from time to time over the period of the poll.

This voting machine was built around 8051 Microcontroller which uses 8 push buttons Poll 1 to Poll 8 which was assigned for a total of eight candidates in this design. To avoid multiple votes by the pollers a poll control switch was added which should be controlled by the supervisor of the poll. A LCD was used to display which candidates they have voted. And at last Hyperterminal should be used with the microcontroller to see poll results using serial communication.

DESIGN:

The poll  Poll 1 to Poll 8 switches was scanned from the Microcontroller for casting vote to the respective candidates. But at first Poll control switch must be pressed to make the Poll switches available for voting. So every time a vote is casted the control switch must be pressed in order to allow the next candidate to vote. This avoids multiple votes from the pollers. The Poll control switch was monitored by means of external interrupts.

A LCD was used to display the status of the voting machine i.e whether it is ready to take the next vote or needs to be initialized by the poll control. Also it will display the candidate name once a vote is casted. The vote results can be obtained by using a hyperterminal in a computer connected with 8051, this was done by using the concept of serial interrupts. Any button press in the keyboard will create interrupt and the vote results will be send to the computer in order starting from candidate 1 to candidate 8.

 If you are not aware of programming interrupts in 8051 kindly read External interrupt programming in 8051″ & “Serial Interrupt programming in 8051”.

VOTING PROCESS:

In order to make this more clear here is the step by step working of this voting machine.

  • Poll Control switch must be initialized first, then the Microcontroller will be ready to take inputs from the input switches and “CAST YOUR VOTE” message will be displayed in the LCD.
  • Votes can be now casted using the Poll switches Poll 1 to Poll 8.
  • After voting a message ‘VOTED:CANDIDATE” will be displayed in the LCD.
  • Now the Poll control switch must be pressed again in order to allow next one to cast their vote, this allows the Supervisors to control the votes.
  • The results of the poll can be obtained in the computer screen with a single press of any key in the keyboard.

CODE:

This code was built using Keil uVision 4.

#include<reg52.h>
#include<stdio.h>
sbit rs=P3^6;
sbit en=P3^7;
int i,j,k;
unsigned char init_msg[]="Cast Your Vote";
unsigned int votes[8];

void delay()                            //Delay Subroutine
{
  int k;
  for(k=0;k<=1000;k++);
}

void lcd(char a,short int b)       //Subroutine for displaying characters in LCD
{
  P1=a;
  rs=b;
  en=1;
  delay();
  en=0;
}

void msg(char *x)             //Subroutine to display messages
{
  while(*x)
    {
       lcd(*x++,1);
     }
}

void vote()                     //Subroutine to scan the keypad for vote input
{
  while(1)
    {
       if(P2==0xfe)          
         {
           P2=0x00;           //Disabling the port after single press to avoid multiple votes 
           votes[0]++;       //Increment vote value for specified candidate
           lcd(0x01,0);
           lcd(0x80,0);
           msg("VOTED:CANDIDATE1");
         }
      if(P2==0xfd)
        {
          P2=0x00;
          votes[1]++;
          lcd(0x01,0);
          lcd(0x80,0);
          msg("VOTED:CANDIDATE2");
        }
     if(P2==0xfb)
       {
         P2=0x00;
         votes[2]++;
         lcd(0x01,0);
         lcd(0x80,0);
         msg("VOTED:CANDIDATE3");
       }
     if(P2==0xf7)
       {
         P2=0x00;
         votes[3]++;
         lcd(0x01,0);
         lcd(0x80,0);
         msg("VOTED:CANDIDATE4");
       }
     if(P2==0xef)
       {
         P2=0x00;
         votes[4]++;
         lcd(0x01,0);
         lcd(0x80,0);
         msg("VOTED:CANDIDATE5");
       }
     if(P2==0xdf)
       {
         P2=0x00;
         votes[5]++;
         lcd(0x01,0);
         lcd(0x80,0);
         msg("VOTED:CANDIDATE6");
       }
     if(P2==0xbf)
       {
         P2=0x00;
         votes[6]++;
         lcd(0x01,0);
         lcd(0x80,0);
         msg("VOTED:CANDIDATE7");
       }
    if(P2==0x7f)
      {  
        P2=0x00;
        votes[7]++;
        lcd(0x01,0);
        lcd(0x80,0);
        msg("VOTED:CANDIDATE8");
      }
   }
}

void serial(void) interrupt 4       //Subroutine for serial interrupt/ To obtain poll results
{ 
   short int i,j;
   char c[10];
   IE=0x00;                          //Disabling interrupts to avoid recursion
   for(i=0;i<=7;i++)            //Loop to send vote results one by one
     {
       sprintf(c,"%d",votes[i]);
       while(c[j]!='')
         {
           SBUF=c[j++];
           while(TI==0);
           TI=0;
         }
       SBUF=0x20;
       while(TI==0);
       TI=0;
       j=0;
     }
   SBUF=0x0D;
   while(TI==0);
   RI=TI=0;
   IE=0x94;
}

void isr_ex1(void) interrupt 2  //Subroutine for external interrupt/To activate poll switches
{
  short int y;
  P2=0xff;          //Activating port 2 for poll inputs
  lcd(0x01,0);
  lcd(0x38,0);
  lcd(0x0f,0);
  lcd(0x80,0);
  delay();
  for(y=0;y<=13;y++)
    {
      lcd(init_msg[y],1);
    }
}

void main()
{
  TMOD=0x20;      //Activating timers to generate the baud rate for serial comm
  TH1=0xFD;
  TL1=0x00;
  SCON=0x50;
  TR1=1;
  IE=0x94;        //Activating serial and external interrupts.
  P2=0x00;
  vote();
  while(1);
}

NOTE:

  • You can implement matrix keypads in case if you need to use the above system for more than 8 candidates.
  • You can get the vote results whenever you needed.

9 Comments

  1. sanjana

    do you proteus circuit?

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Yes it is proteus.

      Reply
  2. Vijay Vilas Patil

    Do you have a dedicated video on this project. I need to design this for 10 candidates

    Reply
  3. Jane

    can we know how to do for 10 candidates , program for the same

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Quite simple actually. Add two more push buttons, Increase the array size votes[] from 8 to 10. Then add two more. And add extra piece of code to poll the additional two buttons and register their votes in corresponding location of your array.

      Hope it helps.

      Reply
  4. abreham kedir

    is the source code full? please send for me if it is not

    Reply
    1. Frank DonaldFrank Donald (Post author)

      The source code given is complete

      Reply
  5. Anteneh Nebyu

    do you have the full presentation?

    Reply
    1. Frank Donald

      Anteneh,
      Presentation in the sense? Above we have given everything you need code, design and explanation. Kindly be specific with your request.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *