Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Quiz Game with 16x2 I2C LCD and Pushbuttons

View and Edit Project In Cirkit DesignerImage of Arduino UNO Quiz Game with 16x2 I2C LCD and Pushbuttons

Circuit Documentation

Summary

This circuit is a simple quiz game using an Arduino UNO microcontroller, a 16x2 I2C LCD display, and three pushbuttons. The LCD displays questions, and the user can answer by pressing the "True" or "False" buttons. The "Retry" button allows the user to restart the game. The circuit also includes resistors for proper button operation.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. 16x2 I2C LCD

    • Description: 16x2 character LCD display with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  3. Red Pushbutton

    • Description: Pushbutton switch.
    • Pins: Pin 1, Pin 2, Pin 3, Pin 4
  4. Pushbutton

    • Description: Pushbutton switch.
    • Pins: Pin 1, Pin 2, Pin 3, Pin 4
  5. Pushbutton

    • Description: Pushbutton switch.
    • Pins: Pin 1, Pin 2, Pin 3, Pin 4
  6. Resistor (10 Ohms)

    • Description: Resistor with a resistance of 10 Ohms.
    • Pins: pin1, pin2
  7. Resistor (10 Ohms)

    • Description: Resistor with a resistance of 10 Ohms.
    • Pins: pin1, pin2
  8. Resistor (10 Ohms)

    • Description: Resistor with a resistance of 10 Ohms.
    • Pins: pin1, pin2

Wiring Details

Arduino UNO

16x2 I2C LCD

Red Pushbutton

Pushbutton (First)

Pushbutton (Second)

Resistors

Code Documentation

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Initialize the LCD (16x2) with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Pin definitions for buttons
const int buttonTrue = 2;    // "True" button
const int buttonFalse = 3;   // "False" button
const int buttonRetry = 4;   // "Retry" button (optional)

// Quiz data
String questions[] = {"Is Earth round?", "Sky is green?", "2+3=7?", "3+8=11?"};
bool answers[] = {true, false, false, true};
int totalQuestions = sizeof(questions) / sizeof(questions[0]);

// Variables to track state
int currentQuestion = 0;
int score = 0;

void setup() {
  // Initialize LCD and buttons
  lcd.init();
  lcd.backlight();
  
  pinMode(buttonTrue, INPUT_PULLUP);
  pinMode(buttonFalse, INPUT_PULLUP);
  pinMode(buttonRetry, INPUT_PULLUP);

  // Welcome message
  lcd.setCursor(0, 0);
  lcd.print("Quiz Game!");
  delay(2000);
  lcd.clear();

  displayQuestion();
}

void loop() {
  // Check for button presses
  if (digitalRead(buttonTrue) == LOW) {
    checkAnswer(true);
  } else if (digitalRead(buttonFalse) == LOW) {
    checkAnswer(false);
  } else if (digitalRead(buttonRetry) == LOW) {
    resetGame();
  }
}

void displayQuestion() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Q");
  lcd.print(currentQuestion + 1);
  lcd.print(": ");
  lcd.setCursor(0, 1);
  lcd.print(questions[currentQuestion]);
}

void checkAnswer(bool userAnswer) {
  if (userAnswer == answers[currentQuestion]) {
    score++;
    lcd.clear();
    lcd.print("Correct!");
  } else {
    lcd.clear();
    lcd.print("Wrong!");
  }
  delay(2000);
  
  currentQuestion++;
  if (currentQuestion < totalQuestions) {
    displayQuestion();
  } else {
    displayScore();
  }
}

void displayScore() {
  lcd.clear();
  lcd.print("Score:");
  lcd.setCursor(0, 1);
  lcd.print(score);
  lcd.print("/");
  lcd.print(totalQuestions);
  delay(5000);
  lcd.clear();
  lcd.print("Press Retry");
  lcd.setCursor(0, 1);
  lcd.print("to play again");
}

void resetGame() {
  currentQuestion = 0;
  score = 0;
  displayQuestion();
}

This code initializes the LCD and buttons, displays a welcome message, and then enters a loop where it waits for button presses to answer quiz questions or restart the game. The displayQuestion, checkAnswer, displayScore, and resetGame functions handle the quiz logic and display updates.