Build it step by step
0 of 8 doneWire the knob
Outer legs to 5V and GND, wiper to A0.
Wire the button
D2 to GND.
Wire the display
SDA to A4, SCL to A5, 5V and GND.
Install the libraries
Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The guess number and tries appear.
Turn the knob
The guess changes from 1 to 100.
Press to guess
Higher, Lower or Correct.
Press again after winning
A new game starts.
How do you build a higher or lower game?
A hidden number from 1 to 100 is guessed with a knob and a button, and the display says higher or lower until you find it. You need Arduino Nano, SSD1306 OLED Display, Potentiometer 10 kΩ, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Pot outer legs | Nano 5V and GND | Knob across the supply |
| Pot wiper (middle) | Nano A0 | Your guess, 1 to 100 |
| Guess button | Nano D2 and GND | Press to submit the guess |
| OLED SDA | Nano A4 | I2C data |
| OLED SCL | Nano A5 | I2C clock |
| OLED VCC and GND | Nano 5V and GND | Power (check your OLED accepts 5 V) |
Code
A guessing game is a good way to learn the idea of a binary search. If you always guess the middle of the remaining range, each answer of higher or lower halves the possibilities, so any number from 1 to 100 can be found in at most seven guesses. The sketch hides a random number, shows your current guess on the display as you turn the knob, and tells you higher or lower after each press. The knob is read on an analog pin and mapped to the numbers 1 to 100. The display shows the number of tries and the best score so far, and after a correct guess a press starts a new game. The random number is seeded from an unconnected analog pin so that every game is different. Try to beat seven by luck, or play it as a computer would, by always guessing the middle. The same method is used by programs searching sorted lists, and it is a nice way to see why computers can find things so quickly.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int KNOB = A0, BTN = 2;
int secret = 0, tries = 0, bestTries = 0;
bool gameOver = false, wasPressed = false;
unsigned long lastPress = 0;
const char *hint = "Guess a number";
int readGuess() {
return constrain(map(analogRead(KNOB), 0, 1023, 1, 100), 1, 100);
}
void newGame() {
secret = random(1, 101);
tries = 0;
gameOver = false;
hint = "Guess a number";
}
void draw(int guess) {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("Tries "); oled.print(tries);
oled.setCursor(70, 0); oled.print("Best "); oled.print(bestTries);
oled.setTextSize(3);
oled.setCursor(40, 14); oled.print(guess);
oled.setTextSize(2);
oled.setCursor(0, 46); oled.print(hint);
oled.display();
}
void setup() {
pinMode(BTN, INPUT_PULLUP);
randomSeed(analogRead(A1));
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
newGame();
}
void loop() {
int g = readGuess();
bool pressed = (digitalRead(BTN) == LOW);
if (pressed && !wasPressed && millis() - lastPress > 40) {
lastPress = millis();
if (gameOver) {
newGame();
} else {
tries++;
if (g == secret) {
hint = "Correct!";
gameOver = true;
if (bestTries == 0 || tries < bestTries) bestTries = tries;
} else if (g < secret) hint = "Higher";
else hint = "Lower";
}
}
wasPressed = pressed;
draw(g);
delay(40);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | A guess number and Tries 0 |
| Knob fully left | Guess 1 |
| Knob fully right | Guess 100 |
| Wrong guess | Higher or Lower and Tries goes up |
| Correct guess | Correct and the best score updates |
If it does not work
Blank screen
Check A4 and A5 and the OLED address.
Guess jumps around
Turn the knob slowly, the pot may be noisy.
Same secret every game
Check the random seed line.
Button counts twice
Increase the 40 millisecond lockout.
Cannot reach 100
Check the pot ends are on 5V and GND.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the higher or lower game?
Arduino Nano, SSD1306 OLED Display, Potentiometer 10 kΩ, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the higher or lower game take to build?
About 90 minutes for a beginner maker, in 8 steps.
How do I know the higher or lower game works?
Power on: A guess number and Tries 0. Knob fully left: Guess 1. Knob fully right: Guess 100.
Blank screen: what should I check?
Check A4 and A5 and the OLED address.
Guess jumps around: what should I check?
Turn the knob slowly, the pot may be noisy.
Same secret every game: what should I check?
Check the random seed line.
Button counts twice: what should I check?
Increase the 40 millisecond lockout.
