Build an ATmega16 electronic voting machine with four candidate buttons, an officer enable key, a 16×2 LCD result screen and vote counts that survive power cuts in EEPROM.
Need it ready-made? Buy Ready Project
This ATmega16 electronic voting machine is a small, self-contained ballot unit for school elections, club polls and classroom demos. Four big buttons stand for candidates A, B, C and D. A presiding officer presses ENABLE before each voter, the voter presses one button, and the vote is counted and saved straight into the chip’s EEPROM.
Because every vote is written to EEPROM the moment it is cast, a power cut or a flat battery never loses the count. The officer can see the totals and the leading candidate on the 16×2 LCD at any time, and can erase all votes only by holding two buttons together for three seconds.

How It Works
- One vote per enable: the candidate buttons are ignored until the officer presses ENABLE. The green LED then lights and the LCD says Cast your vote. After one valid press the machine locks again, so a voter cannot press twice.
- Clean key reading: the four candidate buttons sit on PB0 to PB3 with the chip’s internal pull-ups. The code debounces each press for 30 ms and rejects two buttons pressed together, so a single press always means a single vote.
- Power-safe storage: each candidate has a 16-bit counter in EEPROM.
eeprom_update_word()writes only when the value changes, which saves wear on the EEPROM cells. A magic byte tells the code whether the EEPROM has been set up yet. - Results: RESULT shows all four counts as
A:012 B:003andC:000 D:007, then the leading candidate, or TIE if the top two are level. - Protected clear: holding RESULT and CLEAR together for 3 seconds wipes the counts, with a long beep to confirm. Letting go early cancels it.
- Clock and fuses: the chip runs on its internal 8 MHz oscillator, so no crystal is needed. JTAG is switched off and EESAVE is set, so re-flashing the program keeps the stored votes.
Components Required
- ATmega16 (40-pin DIP) microcontroller – 1
- 16×2 character LCD (HD44780) – 1
- 10 kΩ potentiometer (LCD contrast) – 1
- Large tactile push buttons for candidates A, B, C, D – 4
- Small tactile push buttons for ENABLE, RESULT, CLEAR – 3
- Green LED (ready) and red LED (vote recorded) – 1 each
- 220 Ω resistors for the LEDs – 2
- 5V active buzzer – 1
- 10 kΩ resistor (RESET pull-up) and 100 nF capacitors (decoupling) – 1 + 2
- USBasp programmer and 5V regulated supply (USB power module or 7805) – 1 each
- Breadboard or perfboard and jumper wires – 1 set
Wiring Table
| Part / pin | ATmega16 pin | Notes |
|---|---|---|
| LCD RS | PA0 (pin 40) | LCD RW to GND |
| LCD EN | PA1 (pin 39) | |
| LCD D4, D5, D6, D7 | PA4, PA5, PA6, PA7 (pins 36 to 33) | 4-bit mode, LCD D0 to D3 not connected |
| LCD V0 | 10 kΩ pot wiper | Pot ends to 5V and GND, sets the contrast |
| Candidate A, B, C, D | PB0, PB1, PB2, PB3 (pins 1 to 4) | Other leg of each button to GND |
| ENABLE button | PD2 (pin 16) | Other leg to GND |
| RESULT button | PD3 (pin 17) | Other leg to GND |
| CLEAR button | PD4 (pin 18) | Other leg to GND |
| Green LED (ready) | PD5 (pin 19) through 220 Ω | Cathode to GND |
| Red LED (recorded) | PD6 (pin 20) through 220 Ω | Cathode to GND |
| Active buzzer + | PD7 (pin 21) | Buzzer – to GND |
| VCC / AVCC | Pins 10 and 30 | 5V, with a 100 nF capacitor to GND at each |
| GND | Pins 11 and 31 | Common ground |
| RESET | Pin 9 | 10 kΩ to 5V, also goes to the USBasp RST line |
Connection Diagram
The illustration above shows the build on a breadboard. It is a layout guide only (the crystal shown there is not needed because the code uses the internal 8 MHz clock). Follow the pin-accurate drawing below, which matches the wiring table exactly.
AVR C Code
Save the code as atmega16_voting.c. It was compiled with avr-gcc for the ATmega16 and uses about 1.8 KB of flash. Build and flash it with a USBasp using these commands (Microchip Studio works too):
avr-gcc -mmcu=atmega16 -Os -o vote.elf atmega16_voting.c
avr-objcopy -O ihex -R .eeprom vote.elf vote.hex
avrdude -c usbasp -p m16 -U lfuse:w:0xE4:m -U hfuse:w:0xD1:m
avrdude -c usbasp -p m16 -U flash:w:vote.hex:iThe fuse values select the internal 8 MHz oscillator (lfuse 0xE4), turn off JTAG and keep the EEPROM when you re-flash (hfuse 0xD1). Set the fuses once, before flashing the program.
/*
* ATmega16 Electronic Voting Machine with 16x2 LCD & 4 Candidate Buttons
* ardiunoprojecthub.online
*
* MCU : ATmega16 @ 8 MHz internal oscillator
* Fuses : lfuse=0xE4 (internal 8 MHz), hfuse=0xD1 (JTAG off, EESAVE on)
* EESAVE keeps the stored votes when you re-flash the chip.
*
* LCD 16x2 (4-bit): RS=PA0, EN=PA1, D4=PA4, D5=PA5, D6=PA6, D7=PA7, RW=GND
* Candidate buttons: A=PB0, B=PB1, C=PB2, D=PB3 (to GND, internal pull-ups)
* Officer buttons : ENABLE=PD2, RESULT=PD3, CLEAR=PD4 (to GND, pull-ups)
* LEDs : green READY=PD5, red RECORDED=PD6 (each via 220 ohm)
* Buzzer (active) : PD7
*
* How to use:
* 1. Officer presses ENABLE -> green LED, "Cast your vote".
* 2. Voter presses ONE candidate button -> vote saved in EEPROM, beep,
* red LED, machine locks until ENABLE is pressed again.
* 3. RESULT shows all four counts, then the leader (or a tie).
* 4. Hold RESULT + CLEAR together for 3 seconds to erase all votes.
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdint.h>
#define NUM_CAND 4
#define EE_MAGIC 0x5A
uint8_t EEMEM ee_magic;
uint16_t EEMEM ee_votes[NUM_CAND];
static uint16_t votes[NUM_CAND];
static const char cand_name[NUM_CAND] = {'A', 'B', 'C', 'D'};
/* ---------------- LCD (HD44780, 4-bit mode on PORTA) ---------------- */
#define LCD_RS PA0
#define LCD_EN PA1
static void lcd_pulse(void) {
PORTA |= (1 << LCD_EN); _delay_us(1);
PORTA &= ~(1 << LCD_EN); _delay_us(50);
}
static void lcd_nibble(uint8_t n) { /* high nibble in bits 7..4 */
PORTA = (PORTA & 0x0F) | (n & 0xF0);
lcd_pulse();
}
static void lcd_send(uint8_t v, uint8_t rs) {
if (rs) PORTA |= (1 << LCD_RS); else PORTA &= ~(1 << LCD_RS);
lcd_nibble(v);
lcd_nibble(v << 4);
}
static void lcd_cmd(uint8_t c) { lcd_send(c, 0); if (c < 4) _delay_ms(2); }
static void lcd_char(char c) { lcd_send(c, 1); }
static void lcd_print(const char *s) { while (*s) lcd_char(*s++); }
static void lcd_goto(uint8_t col, uint8_t row) { lcd_cmd(0x80 | (row ? 0x40 : 0x00) | col); }
static void lcd_init(void) {
DDRA |= 0xF3; /* PA0, PA1, PA4..PA7 outputs */
_delay_ms(50);
PORTA &= ~(1 << LCD_RS);
lcd_nibble(0x30); _delay_ms(5); /* HD44780 wake-up sequence */
lcd_nibble(0x30); _delay_us(150);
lcd_nibble(0x30);
lcd_nibble(0x20); /* 4-bit mode */
lcd_cmd(0x28); /* 2 lines, 5x8 font */
lcd_cmd(0x0C); /* display on, cursor off */
lcd_cmd(0x06); /* auto-increment */
lcd_cmd(0x01);
}
static void lcd_two(const char *l1, const char *l2) {
lcd_cmd(0x01);
lcd_goto(0, 0); lcd_print(l1);
lcd_goto(0, 1); lcd_print(l2);
}
static void lcd_num3(uint16_t n) { /* prints 000..999 */
if (n > 999) n = 999;
lcd_char('0' + n / 100);
lcd_char('0' + (n / 10) % 10);
lcd_char('0' + n % 10);
}
/* ---------------- buttons, LEDs, buzzer ---------------- */
#define BTN_ENABLE PD2
#define BTN_RESULT PD3
#define BTN_CLEAR PD4
#define LED_READY PD5
#define LED_REC PD6
#define BUZZER PD7
static uint8_t pressed_d(uint8_t pin) { return !(PIND & (1 << pin)); }
static void beep(uint16_t ms) {
PORTD |= (1 << BUZZER);
while (ms--) _delay_ms(1);
PORTD &= ~(1 << BUZZER);
}
/* Wait until every button is released, then debounce */
static void wait_release(void) {
while ((PINB & 0x0F) != 0x0F || (PIND & 0x1C) != 0x1C) ;
_delay_ms(40);
}
/* Returns 0..3 for a candidate button, or 0xFF if none / more than one */
static uint8_t read_candidate(void) {
uint8_t b = ~PINB & 0x0F;
if (b == 0) return 0xFF;
_delay_ms(30); /* debounce */
if ((~PINB & 0x0F) != b) return 0xFF;
switch (b) { /* reject two keys at once */
case 0x01: return 0;
case 0x02: return 1;
case 0x04: return 2;
case 0x08: return 3;
default: return 0xFF;
}
}
/* ---------------- EEPROM storage ---------------- */
static void votes_load(void) {
if (eeprom_read_byte(&ee_magic) != EE_MAGIC) { /* first power-up */
for (uint8_t i = 0; i < NUM_CAND; i++) eeprom_update_word(&ee_votes[i], 0);
eeprom_update_byte(&ee_magic, EE_MAGIC);
}
for (uint8_t i = 0; i < NUM_CAND; i++) votes[i] = eeprom_read_word(&ee_votes[i]);
}
static void votes_save(uint8_t i) { eeprom_update_word(&ee_votes[i], votes[i]); }
/* ---------------- screens ---------------- */
static void show_idle(void) {
uint16_t total = 0;
for (uint8_t i = 0; i < NUM_CAND; i++) total += votes[i];
lcd_two("Voting Machine", "Total votes:");
lcd_goto(13, 1); lcd_num3(total);
}
static void show_result(void) {
lcd_cmd(0x01);
for (uint8_t i = 0; i < NUM_CAND; i++) {
lcd_goto((i % 2) ? 8 : 0, i / 2); /* A B on row 0, C D on row 1 */
lcd_char(cand_name[i]); lcd_char(':'); lcd_num3(votes[i]);
}
_delay_ms(4000);
uint8_t lead = 0, tie = 0;
for (uint8_t i = 1; i < NUM_CAND; i++) {
if (votes[i] > votes[lead]) { lead = i; tie = 0; }
else if (votes[i] == votes[lead]) tie = 1;
}
if (votes[lead] == 0) lcd_two("No votes yet", "");
else if (tie) lcd_two("Result: TIE", "Check counts");
else {
lcd_two("Leading:", "Candidate ");
lcd_goto(10, 1); lcd_char(cand_name[lead]);
lcd_goto(9, 0); lcd_num3(votes[lead]); lcd_print(" vt");
}
_delay_ms(3000);
}
static void try_clear(void) {
lcd_two("Hold 3 s to", "clear all votes");
for (uint8_t t = 0; t < 30; t++) { /* 30 x 100 ms = 3 s */
if (!(pressed_d(BTN_RESULT) && pressed_d(BTN_CLEAR))) { show_idle(); return; }
_delay_ms(100);
}
for (uint8_t i = 0; i < NUM_CAND; i++) { votes[i] = 0; votes_save(i); }
beep(600);
lcd_two("All votes", "cleared");
_delay_ms(1500);
wait_release();
show_idle();
}
int main(void) {
/* JTAG off in code as well (write JTD twice within 4 cycles) */
MCUCSR |= (1 << JTD); MCUCSR |= (1 << JTD);
DDRB &= 0xF0; PORTB |= 0x0F; /* candidate buttons */
DDRD |= (1 << LED_READY) | (1 << LED_REC) | (1 << BUZZER);
DDRD &= ~((1 << BTN_ENABLE) | (1 << BTN_RESULT) | (1 << BTN_CLEAR));
PORTD |= (1 << BTN_ENABLE) | (1 << BTN_RESULT) | (1 << BTN_CLEAR);
lcd_init();
votes_load();
beep(100);
show_idle();
while (1) {
if (pressed_d(BTN_RESULT) && pressed_d(BTN_CLEAR)) { try_clear(); continue; }
if (pressed_d(BTN_RESULT)) {
_delay_ms(30);
if (pressed_d(BTN_CLEAR)) continue; /* user is going for clear */
wait_release();
show_result();
show_idle();
}
if (pressed_d(BTN_ENABLE)) {
wait_release();
PORTD |= (1 << LED_READY);
PORTD &= ~(1 << LED_REC);
lcd_two("Cast your vote", "Press A B C or D");
beep(80);
uint8_t c;
while ((c = read_candidate()) == 0xFF) ; /* one vote only */
votes[c]++;
votes_save(c); /* safe on power cut */
PORTD &= ~(1 << LED_READY);
PORTD |= (1 << LED_REC);
lcd_two("Vote recorded", "Thank you!");
beep(400);
wait_release(); /* ignore extra presses */
_delay_ms(1500);
PORTD &= ~(1 << LED_REC);
show_idle();
}
}
}
Setting Up and Testing
- Power the board. You should hear a short beep and see Voting Machine and the total vote count. If the LCD shows only blocks, turn the contrast pot.
- Press ENABLE. The green LED lights and the LCD asks for a vote.
- Press candidate B. The red LED lights, the buzzer beeps and the LCD says Vote recorded. Pressing B again does nothing until ENABLE is pressed.
- Cast a few more votes, then press RESULT to see the four counts and the leader.
- Unplug the power, plug it back in and press RESULT. The counts are still there.
- Hold RESULT and CLEAR for 3 seconds to reset everything before a real election.
Tip: mount the four candidate buttons on a separate panel for the voter and keep ENABLE, RESULT and CLEAR on the officer’s side, joined by a short ribbon cable.
Troubleshooting
| Problem | Fix |
|---|---|
| LCD is blank or shows a row of blocks | Adjust the 10 kΩ contrast pot and check RW is tied to GND and D4 to D7 go to PA4 to PA7. |
| Everything is very slow and the LCD shows junk | The chip is still on its 1 MHz factory clock. Write lfuse 0xE4 with avrdude. |
| Counts reset after re-flashing | EESAVE is off. Use hfuse 0xD1, then the EEPROM is kept when you upload new code. |
| A vote is counted twice | A button is bouncing badly. Raise the 30 ms debounce in read_candidate() to 60 ms or swap the button. |
| Nothing happens after pressing ENABLE | Check the ENABLE button goes to PD2 and GND, using legs on opposite sides of the button. |
| Counts look like 999 or random numbers on first power-up | Hold RESULT and CLEAR for 3 seconds once to zero the EEPROM. |
Ideas to Extend
- Add more candidates on PB4 to PB7 and show the names on the LCD instead of letters.
- Send the final result to a PC or phone through an HC-05 Bluetooth module on the UART pins.
- Add a 4×4 keypad officer PIN so only the officer can see results or clear the votes.
1 thought on “ATmega16 Electronic Voting Machine with 16×2 LCD & EEPROM Storage”