Arduino Uno Quiz Game Using U8G2 OLED and Keypad

by ruemc2 in Circuits > Arduino

271 Views, 3 Favorites, 0 Comments

Arduino Uno Quiz Game Using U8G2 OLED and Keypad

WIN_20250513_16_23_31_Pro.jpg

This project creates a fun and educational 5-question multiple-choice quiz using an Arduino Uno, a 4x4 keypad, and an OLED screen driven by the U8G2 library. It's a great project to learn how to read input from a keypad and display dynamic content on an OLED screen.

Supplies

  1. Arduino Uno
  2. SSD1306 128x64 OLED Display
  3. 4x4 Keypad
  4. Jumper Wires
  5. USB Cable
  6. Breadboard

Creating the Circuit

Screenshot_15-5-2025_163647_wokwi.com (1).jpeg

Keypad Wiring:

  1. Connect keypad rows to pins 2, 3, 4, 5 (Arduino)
  2. Connect keypad columns to pins 6, 7, 8, 9 (Arduino)

OLED Display Wiring (I2C):

  1. GND → GND
  2. VCC → 5V
  3. SCL → A5
  4. SDA → A4

Arduino IDE


1. Open the Arduino IDE.

2. Go to Tools → Board and select Arduino Uno.

3. Go to Tools → Port and select the correct COM port.

4. Install required libraries:


U8g2: for OLED display

Keypad: for reading the 4x4 matrix

Use Sketch → Include Library → Manage Libraries...to search and install.

Explaining the Code

#include <Keypad.h>
#include <Wire.h>
#include <U8g2lib.h>


U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);


int x = 5;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

char lastKey = '\0'; // Store last key to prevent repeats


void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);


}


void displayMessage(const char* message) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print(message);
u8g2.sendBuffer();


}



void loop() {
if (x == 5) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("Whatchapterdidgojodie?");
u8g2.setCursor(0, 20);
u8g2.print("A-236");
u8g2.setCursor(0, 30);
u8g2.print("B-84");
u8g2.setCursor(0, 40);
u8g2.print("C-147");
u8g2.setCursor(0, 50);
u8g2.print("D-64");
u8g2.sendBuffer();
delay(5000);
}
char key = keypad.getKey();

if (key && key != lastKey) { // Ensure a new key was pressed
displayMessage("You pressed:");
u8g2.setCursor(0, 30);
u8g2.print(key);
u8g2.sendBuffer();
delay(1000);


if (key == 'A') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
delay(1000);


lastKey = key; // Update last key to prevent repeats
} else if (!key) {
lastKey = '\0'; // Reset when no key is pressed
}


if (x == 6) {
displayMessage("Next question!");
delay(1000);
u8g2.clearBuffer()


u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("What does TNT stand for?");
u8g2.setCursor(0, 20);
u8g2.print("A-Trinitrotoluene");
u8g2.setCursor(0, 30);
u8g2.print("B-Thermonuclear Test");
u8g2.setCursor(0, 40);
u8g2.print("C-Minecraft");
u8g2.setCursor(0, 50);
u8g2.print("D-Tom Not Tim");
u8g2.sendBuffer();
delay(5000);


key = keypad.getKey();
if (key && key != lastKey) {
displayMessage("You pressed:");
u8g2.setCursor(0, 30);
u8g2.print(key);
u8g2.sendBuffer();
delay(1000);


if (key == 'A') {

displayMessage("Correct!");
x++;
} else {

displayMessage("Wrong!");
}
delay(1000);
}


lastKey = key;
}

if (x == 7) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("What'stheCapitalofFrance?");
u8g2.setCursor(0, 20);
u8g2.print("A-Cayenne");
u8g2.setCursor(0, 30);
u8g2.print("B-Lyon");
u8g2.setCursor(0, 40);
u8g2.print("C-Paris");
u8g2.setCursor(0, 50);
u8g2.print("D-RueDeRivoli");
u8g2.sendBuffer();
delay(5000);

if (key == 'C') {

displayMessage("Correct!");
x++;
} else {

displayMessage("Wrong!");
}

if (!key) {
lastKey = '\0';
}
}

if (x == 8) {
displayMessage("Next question!");
delay(2000);




u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("WholedtheVoyagerProject?");
u8g2.setCursor(0, 20);
u8g2.print("A-Neumann");
u8g2.setCursor(0, 30);
u8g2.print("B-Stone");
u8g2.setCursor(0, 40);
u8g2.print("C-Feynman");
u8g2.setCursor(0, 50);
u8g2.sendBuffer();
delay(5000);

if (key == 'B') {

displayMessage("Correct!");
x++;
} else {

displayMessage("Wrong!");
}

if (!key); {
lastKey = '\0';
}
if (x == 9) {
displayMessage("Next question!");
delay(2000);




u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("LargestCityinGuyane?");
u8g2.setCursor(0, 20);
u8g2.print("A-SaintgGeorges");
u8g2.setCursor(0, 30);
u8g2.print("B-Cayenne");
u8g2.setCursor(0, 40);
u8g2.print("C-Rueon");
u8g2.setCursor(0, 50);
u8g2.sendBuffer();
delay(5000);

if (key == 'B') {

displayMessage("Correct!");
x++;
} else {

displayMessage("Wrong!");
}

(!key); {
lastKey = '\0';
}

}
}
}


Explaining the Code

#include <Keypad.h>
#include <Wire.h>
#include <U8g2lib.h>

This Code essentially says to have these libraries included in the project (Library - Imagine a book but instead of chapters it is different functions for the arduino to do)

int x = 5;

* `x` is a variable used to track the current question number.

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'}
};

This code creates the array for the 4*4 keypad

byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

This piece says where all of the keypad' wires are plugged in

char lastKey = '\0';

This code prevents repeats in the answers

void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);

This code lets the u8g2 library begin and chooses the font it uses

void displayMessage(const char* message) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print(message);
u8g2.sendBuffer();

* `u8g2.print()` and related functions update the display.

* A helper function `displayMessage()` is used to show short messages cleanly.

void loop() {
if (x == 5) {

This code makes it so that this loops forever and if the variable is 5 the next piece of code activates

u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("Whatchapterdidgojodie?");
u8g2.setCursor(0, 20);
u8g2.print("A-236");
u8g2.setCursor(0, 30);
u8g2.print("B-84");
u8g2.setCursor(0, 40);
u8g2.print("C-147");
u8g2.setCursor(0, 50);
u8g2.print("D-64");
u8g2.sendBuffer();
delay(5000);
}

This code shows the questions and the choices for answers

char key = keypad.getKey();

* `keypad.getKey()` is used to read the user's input.

if (key && key != lastKey) {
displayMessage("You pressed:");
u8g2.setCursor(0, 30);
u8g2.print(key);
u8g2.sendBuffer();
delay(1000);

This piece of code not only makes sure a new key other than the last question was pressed but it also announces on the display monitor what key was pressed

if (key == 'A') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
delay(1000);

This block of code displays what answer is correct to press.

lastKey = key;
} else if (!key) {
lastKey = '\0';

The sketch presents questions one at a time. If the correct answer is pressed (e.g., 'A'), it displays "Correct!" and proceeds to the next question. Wrong answers display "Wrong!". The game progresses until all questions are answered.

Project Code

Project Video

Once it begins a short demonstration of the first question being answered and the second beginning.