top of page
Writer's pictureSanskruti Ashtikar

Electronic Voting Machine Using PIC Microcontroller

Updated: 7 days ago

Introduction


Electronic Voting Machines (EVMs) have revolutionized the electoral process, offering a quick, secure, and efficient method for casting and counting votes. In this project, we'll design a simple yet functional EVM using a PIC microcontroller. This project will introduce you to interfacing with input devices like buttons, a display, and managing user input securely to ensure a robust voting system.





System Overview


The Electronic Voting Machine will have several buttons, each representing a different candidate or option. Voters can cast their votes by pressing the button corresponding to their choice. The system will record the vote securely and display the current status (e.g., "Vote Casted") on an LCD. After the voting process is completed, the results can be viewed by authorized personnel by pressing a specific button combination.


Components Required


  1. PIC Microcontroller (e.g., PIC16F877A) - The main controller for managing votes and interfacing with input/output devices.

  2. 16x2 LCD Display - Displays instructions, status messages, and results.

  3. Push Buttons - For selecting candidates and controlling the voting process.

  4. EEPROM (optional, internal to PIC) - To store votes securely in case of power loss.

  5. Resistors and Capacitors - For interfacing and circuit stability.

  6. Breadboard and Connecting Wires - For assembling the circuit.

  7. Power Supply - To power the system.


Circuit Diagram


Below is a basic circuit diagram showing how the components are connected to the PIC microcontroller:


[Insert a circuit diagram here showing connections:
- Push buttons connected to digital input pins of the PIC microcontroller.
- LCD connected to PORTD of the PIC microcontroller for displaying messages and results.
- Power supply connections to the PIC, buttons, and LCD module.]

Working Principle


The EVM works by capturing input from voters through buttons. Each button corresponds to a different candidate. When a button is pressed, the microcontroller records the vote by incrementing the counter associated with that candidate and displays a confirmation message on the LCD. The voting process is secured by disabling further voting after a vote has been cast until a reset button is pressed by an administrator. Votes are stored in non-volatile memory (EEPROM) to prevent data loss in case of a power failure.


Step-by-Step Implementation


Step 1: Setting Up the Development Environment


Before starting with the hardware, ensure you have the necessary software tools installed:

  • MPLAB X IDE - Integrated Development Environment for writing and debugging code for PIC microcontrollers.

  • XC8 Compiler - A compiler for converting C code into a hex file that the PIC microcontroller can understand.


Step 2: Writing the Code


Begin by initializing the PIC microcontroller, setting up the button inputs, LCD, and EEPROM.


Code Outline:


#include <xc.h>
#include "lcd.h"  // Include LCD library
#include <stdio.h>
#define _XTAL_FREQ 20000000  // Define the crystal frequency (20MHz)
#define CANDIDATE1_BUTTON RB0
#define CANDIDATE2_BUTTON RB1
#define CANDIDATE3_BUTTON RB2
#define CANDIDATE4_BUTTON RB3
#define RESULT_BUTTON RB4
#define RESET_BUTTON RB5
unsigned int candidate1_votes = 0;
unsigned int candidate2_votes = 0;
unsigned int candidate3_votes = 0;
unsigned int candidate4_votes = 0;
void EEPROM_Write(unsigned char address, unsigned char data) {
    EEADR = address;
    EEDATA = data;
    EECON1bits.EEPGD = 0; // Access data EEPROM memory
    EECON1bits.WREN = 1;  // Enable write
    GIE = 0; // Disable global interrupts
    EECON2 = 0x55;  // Required sequence
    EECON2 = 0xAA;
    EECON1bits.WR = 1;   // Start writing
    GIE = 1; // Enable global interrupts
    while (EECON1bits.WR);  // Wait until write is complete
    EECON1bits.WREN = 0; // Disable write
}


unsigned char EEPROM_Read(unsigned char address) {
    EEADR = address;
    EECON1bits.EEPGD = 0; // Access data EEPROM memory
    EECON1bits.RD = 1;   // Start reading
    return EEDATA;
}
void update_vote_count() {
    EEPROM_Write(0, candidate1_votes);
    EEPROM_Write(1, candidate2_votes);
    EEPROM_Write(2, candidate3_votes);
    EEPROM_Write(3, candidate4_votes);
}
void load_vote_count() {
    candidate1_votes = EEPROM_Read(0);
    candidate2_votes = EEPROM_Read(1);
    candidate3_votes = EEPROM_Read(2);
    candidate4_votes = EEPROM_Read(3);
}
void display_results() {
    char buffer[16];
    Lcd_Clear();
    Lcd_Set_Cursor(1, 1);
    sprintf(buffer, "C1: %u", candidate1_votes);
    Lcd_Write_String(buffer);
    Lcd_Set_Cursor(2, 1);
    sprintf(buffer, "C2: %u", candidate2_votes);
    Lcd_Write_String(buffer);
    __delay_ms(2000);
    Lcd_Clear();
    Lcd_Set_Cursor(1, 1);
    sprintf(buffer, "C3: %u", candidate3_votes);
    Lcd_Write_String(buffer);
    Lcd_Set_Cursor(2, 1);
    sprintf(buffer, "C4: %u", candidate4_votes);
    Lcd_Write_String(buffer);
    __delay_ms(2000);
}
void main(void) {
    TRISB = 0xFF;  // Configure PORTB as input for buttons
    TRISD = 0x00;  // Configure PORTD as output for LCD
    Lcd_Init();    // Initialize LCD
    Lcd_Clear();


    load_vote_count();  // Load vote counts from EEPROM
    
    while(1) {
        if (CANDIDATE1_BUTTON == 0) {
            __delay_ms(50);  // Debounce delay
            if (CANDIDATE1_BUTTON == 0) {
                candidate1_votes++;
                update_vote_count();
                Lcd_Clear();
                Lcd_Set_Cursor(1, 1);
                Lcd_Write_String("Vote Casted");
                __delay_ms(1000);
            }
        }
        if (CANDIDATE2_BUTTON == 0) {
            __delay_ms(50);  // Debounce delay
            if (CANDIDATE2_BUTTON == 0) {
                candidate2_votes++;
                update_vote_count();
                Lcd_Clear();
                Lcd_Set_Cursor(1, 1);
                Lcd_Write_String("Vote Casted");
                __delay_ms(1000);
            }
        }
        if (CANDIDATE3_BUTTON == 0) {
            __delay_ms(50);  // Debounce delay
            if (CANDIDATE3_BUTTON == 0) {
                candidate3_votes++;
                update_vote_count();
                Lcd_Clear();
                Lcd_Set_Cursor(1, 1);
                Lcd_Write_String("Vote Casted");
                __delay_ms(1000);
            }
        }
        if (CANDIDATE4_BUTTON == 0) {
            __delay_ms(50);  // Debounce delay
            if (CANDIDATE4_BUTTON == 0) {
                candidate4_votes++;
                update_vote_count();
                Lcd_Clear();
                Lcd_Set_Cursor(1, 1);
                Lcd_Write_String("Vote Casted");
                __delay_ms(1000);
            }
        }
        if (RESULT_BUTTON == 0) {
            __delay_ms(50);  // Debounce delay
            if (RESULT_BUTTON == 0) {
                display_results();
            }
        }
        if (RESET_BUTTON == 0) {
            __delay_ms(50);  // Debounce delay
            if (RESET_BUTTON == 0) {
                candidate1_votes = 0;
                candidate2_votes = 0;
                candidate3_votes = 0;
                candidate4_votes = 0;
                update_vote_count();
                Lcd_Clear();
                Lcd_Set_Cursor(1, 1);
                Lcd_Write_String("System Reset");
                __delay_ms(1000);
            }
        }
    }
}




Step 3: Interfacing the Buttons


  • Connect push buttons to the digital input pins of the PIC microcontroller (e.g., RB0-RB5).

  • Use pull-down resistors to ensure the pins are at a known low state when the buttons are not pressed.


Step 4: Interfacing the LCD


  • Connect the LCD data pins (D4-D7) to the microcontroller’s PORTD.

  • Connect the RS, RW, and E control pins to the appropriate digital I/O pins on the PIC (e.g., RD0, RD1, and RD2).

  • Use a potentiometer to adjust the contrast of the LCD.


Step 5: EEPROM Configuration


  • The internal EEPROM of the PIC microcontroller is used to store the vote counts.

  • Ensure the EEPROM read/write functions are correctly implemented to prevent data corruption.


Step 6: Compiling and Uploading the Code


  • Write and compile the code in MPLAB X IDE using the XC8 compiler to generate a hex file.

  • Upload the hex file to the PIC microcontroller using a programmer (e.g., PICkit 3).


Step 7: Testing and Running the System


  • Power up the system and test the voting process by pressing the buttons.

  • Verify that votes are counted correctly, displayed on the LCD, and stored securely in EEPROM.

  • Test the reset functionality to ensure the system can be cleared for a new election.


Applications


  • Election Systems: Used in schools, small communities, or organizations for quick and efficient elections.

  • Opinion Polling: Used in meetings or gatherings to gauge opinions quickly.

  • Educational Projects: Demonstrates the application of microcontrollers in secure data processing and user interaction.


Conclusion


This project demonstrates how to build a basic yet functional Electronic Voting Machine using a PIC microcontroller. By implementing vote counting, secure data storage in EEPROM, and displaying results on an LCD, you can create a system that can be expanded with additional features such as voter authentication, remote result viewing, or even wireless communication for real-time data transmission.

This EVM project is an excellent way to learn about digital input processing, interfacing with displays, and working with non-volatile memory, all of which are essential skills in embedded systems design.


Want us to guide you through your project or make the project for you ?






Create Various Projects

Check out our Free Arduino Projects Playlist - Arduino Projects 

Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects 

Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects 

Check out our Free IoT Projects Playlist - IoT Projects 

Check out our Free Home Automation Projects Playlist - Home Automation Projects 

Check out our Free NodeMCu Projects Playlist - NodeMCu Projects 



0 views0 comments

Related Posts

See All

Comments