ESP32 Tutorial 36/55 - Guessing Number Game | SunFounder's ESP32 IoT Learning kit

ESP32 Tutorial 36/55 - Guessing Number Game | SunFounder's ESP32 IoT Learning kit

In this tutorial, we will create a fun guessing number game using the ESP32 microcontroller along with an infrared remote and an LCD display. The player will attempt to guess a randomly generated number between 0 and 99, with the game providing hints about whether the guess is too high or too low. Through this project, you will learn how to use the infrared receiver, display values on an LCD, and manage user input from a remote control. You can refer to the video for additional clarity (in video at 00:00).

ESP32_36_guessing_numbe-1
ESP32_36_guessing_numbe-2

Hardware Explained

The primary components used in this project include the ESP32 microcontroller, an infrared remote control, an infrared receiver, and an LCD display. The ESP32 serves as the main processing unit and is capable of handling wireless communication, making it a versatile choice for IoT projects. The infrared remote allows users to input guesses without requiring physical interaction with the board, while the LCD displays the game status and prompts.

The infrared receiver detects signals from the remote control and decodes them for use in the game. Each button press on the remote corresponds to a specific value that the ESP32 can interpret. The LCD display provides a visual interface for the user, showing the current guess range and whether the guess was correct.

Datasheet Details

Manufacturer SunFounder
Part number ESP32
Logic/IO voltage 3.3 V
Supply voltage 5 V (via USB)
Output current (per channel) 12 mA (max)
PWM frequency guidance 1 kHz
Input logic thresholds 0.3 * Vcc to 0.7 * Vcc
Voltage drop / RDS(on) / saturation 0.2 V
Thermal limits Operating temperature: -40 to 85 °C
Package WROOM-32 module
Notes / variants Supports Wi-Fi and Bluetooth

 

  • Ensure all components are rated for 3.3 V and 5 V where applicable.
  • Use proper pull-up resistors for the IR receiver to avoid floating inputs.
  • Consider using a heat sink if running at maximum output current for extended periods.
  • When using PWM, maintain a frequency of around 1 kHz for optimal performance.
  • Be cautious with wiring; ensure connections are secure to avoid intermittent failures.

Wiring Instructions

ESP32_36_guessing_number_wiring

To set up the wiring for the guessing number game, start by connecting the infrared receiver. Connect the red wire from the right pin of the receiver to the 3.3 V power supply on the ESP32. The black wire should go to the ground, while the left pin of the infrared receiver connects to pin 14 on the ESP32.

ESP32_36_guessing_number_schematic

Next, wire the LCD display. Connect the VCC pin of the LCD to the 5 V supply on the ESP32 and the ground pin to ground. The SDA and SCL pins of the LCD should be connected to pins 21 and 22 respectively. Ensure that there are two empty spaces between the SDA and SCL connections to avoid pin conflicts. Finally, make sure to remove any plastic covers from the battery before powering the board.

Code Examples & Walkthrough

The following code snippets illustrate essential parts of the program used for the guessing number game. We start by including necessary libraries and defining key identifiers.

#include 
#include 
#include 
#include 

const uint16_t IR_RECEIVE_PIN = 14;
IRrecv irrecv(IR_RECEIVE_PIN);
decode_results results;

In this excerpt, we include libraries for handling the LCD and IR receiver functionality. The pin for the infrared receiver is defined as 14, and we create instances of the necessary objects to manage input and output.

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  irrecv.enableIRIn();
  initNewValue();
}

This snippet shows the setup function where we initialize the LCD, start serial communication, and enable the IR receiver. The function initNewValue() is called to generate a new random number for the player to guess.

bool detectPoint() {
  if (count > pointValue) {
    if (count < upper) upper = count;
  } else if (count < pointValue) {
    if (count > lower) lower = count;
  } else if (count == pointValue) {
    count = 0;
    return 1;
  }
  count = 0;
  return 0;
}

This function checks the player's guess against the randomly generated number, adjusting the upper and lower limits accordingly. If the guess is correct, it resets the count and returns true.

The full code is loaded below the article for your reference.

Demonstration / What to Expect

Once everything is wired and the code is uploaded, the game will prompt you to press any number on the remote control. The game will then provide feedback on your guesses, updating the range of possible numbers until you correctly guess the target number. If you press the POWER button, the game resets and starts over (in video at 02:30).

Common pitfalls include ensuring that the infrared receiver is properly oriented and that all connections are secure. If the game does not respond, check the power supply and verify that the correct board and port are selected in the Arduino IDE.

Video Timestamps

  • 00:00 Start
  • 2:17 Introduction to the game project
  • 4:37 Wiring
  • 6:15 Arduino Code explained
  • 12:32 Selecting ESP32 Board and COM Port in Arduino IDE
  • 16:16 Playing Guessing number game

Images

ESP32_36_guessing_number_schematic
ESP32_36_guessing_number_schematic
ESP32_36_guessing_number_wiring
ESP32_36_guessing_number_wiring
ESP32_36_guessing_numbe-1
ESP32_36_guessing_numbe-1
ESP32_36_guessing_numbe-2
ESP32_36_guessing_numbe-2
837-ESP32 Tutorial 36/55- Arduino code for guessing number
Language: C++
Copied!

Resources & references

Files📁

No files available.