Pick A Number 1 4: Simple Project, Tricky Fairness
Pick a Number 1 to 4 with Buttons and Arduino Logic
To pick a number from 1 to 4 using buttons and Arduino logic, wire one pushbutton to an input pin, seed Arduino's pseudo-random generator once in setup, and generate a value with random(1, 5) each time the button is pressed so the result stays in the 1-to-4 range. Arduino's own documentation states that random(max) and random(min, max) return pseudo-random values, so the key is to use the correct upper bound and debounce the button so one press produces one number.
How the project works
This project is a simple input-output system: the button press acts as the trigger, the Arduino detects the press, and the code prints or displays a number from 1 to 4. In practice, mechanical buttons bounce, which can create multiple false triggers unless you add a short delay or state check; Arduino forum guidance commonly recommends debouncing and using pull-up wiring so the input stays stable.
For a beginner build, this is a useful mini-lesson in probability, digital input handling, and control logic. A standard approach is to use the internal pull-up resistor, read the button as LOW when pressed, and choose the number only when the press is confirmed.
Parts list
- 1 Arduino Uno or compatible board.
- 1 momentary pushbutton.
- 1 breadboard.
- 2 jumper wires.
- USB cable for programming and power.
Wiring guide
The simplest wiring method is to connect one side of the button to Arduino digital pin 2 and the other side to GND, then enable INPUT_PULLUP in code. That arrangement is widely used because the input reads cleanly without an external resistor, and the button becomes active-low, meaning pressed = LOW.
| Component | Connection | Purpose |
|---|---|---|
| Pushbutton side 1 | Arduino D2 | Digital input |
| Pushbutton side 2 | Arduino GND | Reference ground |
| Internal pull-up | Enabled in code | Keeps input HIGH until pressed |
| Serial Monitor | USB connection | Shows the chosen number |
Arduino code
This sketch prints one random number from 1 to 4 every time the button is pressed, with a short debounce delay to prevent duplicate readings. The seed uses an analog noise source, which is a common Arduino technique for changing the pseudo-random sequence between resets.
const int buttonPin = 2; bool lastState = HIGH; void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin; randomSeed(analogRead(A0)); } void loop() { bool currentState = digitalRead(buttonPin); if (lastState == HIGH && currentState == LOW) { delay; if (digitalRead(buttonPin) == LOW) { int number = random; Serial.println(number); while (digitalRead(buttonPin) == LOW) { } delay; } } lastState = currentState; }
Step-by-step build
- Connect the pushbutton between D2 and GND.
- Set the pin mode to
INPUT_PULLUPso no external resistor is needed. - Open the Serial Monitor at 9600 baud.
- Seed the random generator with a floating analog pin such as A0.
- Read the button state and detect the transition from unpressed to pressed.
- Add a short debounce delay of about 20 milliseconds.
- Generate
random(1, 5)and print the result. - Wait for release so the same press does not trigger multiple numbers.
Why the range matters
Arduino's random(min, max) function includes the minimum value but excludes the maximum, so random(1, 5) returns 1, 2, 3, or 4. This is a common source of errors in beginner projects, and it is the reason a request for "1 to 4" should use 5 as the upper limit.
In educator terms, this is a clean example of bounded randomness: the system does not "guess" outside the allowed range, it maps a random output into a fixed set of outcomes. That makes it ideal for classroom games, team pickers, lab groups, and simple decision-making tools.
Testing and troubleshooting
If the button seems to fire twice, the problem is usually bounce rather than code logic. The most reliable fixes are a short debounce delay, waiting for release before accepting another press, and using the internal pull-up resistor so the input is not left floating.
- If you always get the same sequence after reset, add
randomSeed(analogRead(A0))insetup. - If numbers repeat when you hold the button, wait for button release before the next read.
- If the input seems noisy, verify the button is wired to GND and the code uses
INPUT_PULLUP.
Classroom applications
The random picker pattern is useful in STEM lessons because it teaches input handling, conditionals, and randomness in one short activity. Teachers can use it to choose question numbers, group members, challenge levels, or game turns, and students can easily extend the project with LEDs, buzzers, or a 7-segment display.
For an easy extension, map the output to four LEDs, one for each number, or display the result on a serial LCD. That turns the same logic into a visible engineering demo that reinforces how digital systems translate a button press into a controlled output.
Why use INPUT_PULLUP?
INPUT_PULLUP lets the Arduino hold the input HIGH internally until the button connects it to GND, which simplifies wiring and reduces floating-input problems. This is one of the most common beginner-friendly button circuits in Arduino practice.
Everything you need to know about Pick A Number 1 4 Simple Project Tricky Fairness
How do I make Arduino pick 1 to 4?
Use random(1, 5) after seeding the generator in setup, because Arduino random values exclude the upper bound. Add button debouncing so one physical press produces one result.
Why is my number repeating?
Repeating output usually means either the random generator was never seeded properly or the button is bouncing. Seeding with analog noise and confirming the press after a short delay usually solves both issues.
Can I show the result with LEDs?
Yes, you can light one of four LEDs based on the selected number or use the number to trigger different outputs. That is a natural next step after printing the value to the Serial Monitor.