Answer ball on an Arduino Uno
Ask a question and tip the tilt switch: the 16x2 LCD shows one of eight answers, picked with random(). The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
What you'll learn
- Reading a tilt switch with INPUT_PULLUP
- Detecting a change rather than a level
- Picking from a table with random()
- Seeding the random generator from a floating pin
- Writing text on a 16x2 LCD
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
The LCD shows Ask me a question. A tilt switch on pin 6 connects to ground and uses the Uno's internal pull-up, so it reads LOW when closed. When you tip the ball, the switch opens and the pin reads HIGH.
That change picks one of eight answers at random with random(8) and shows it under The ball says. The answers run from Yes and Certainly to Doubtful and No. To make the random picks differ each time, the sketch seeds the generator from an unconnected analog pin, A5.
Parts list
4 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Tilt switch
- 1 × Character LCD, 16x2
Kits that can build it
These 6 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Character LCD, 16x2 pin RS | Arduino Uno R3 pin 12 |
| Character LCD, 16x2 pin E | Arduino Uno R3 pin 11 |
| Tilt switch pin 2 | Arduino Uno R3 pin 6 |
| Character LCD, 16x2 pin D4 | Arduino Uno R3 pin 5 |
| Character LCD, 16x2 pin D5 | Arduino Uno R3 pin 4 |
| Character LCD, 16x2 pin D6 | Arduino Uno R3 pin 3 |
| Character LCD, 16x2 pin D7 | Arduino Uno R3 pin 2 |
| Character LCD, 16x2 pin VDD | Arduino Uno R3 pin 5V |
| Character LCD, 16x2 pin A | Arduino Uno R3 pin 5V |
| Tilt switch pin 1 | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin VSS | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin V0 | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin RW | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin K | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Answer ball: ask a yes-or-no question, tip the tilt switch, and the LCD
// shows an answer picked at random.
//
// LCD RS -> pin 12, E -> pin 11, D4 to D7 -> pins 5, 4, 3, 2
// VSS, RW, K -> GND; VDD, A -> 5V; V0 -> GND for full contrast
// pin 6 the tilt switch, its other leg to GND (internal pull-up)
//
// random(8) picks a number from 0 to 7, and that number picks a line from
// the table. The numbers come from a generator that would start the same way
// every time, so setup() seeds it from analogRead of a pin with nothing on it,
// which wanders a little.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int TILT = 6;
const char *ANSWERS[8] = {
"Yes", "Most likely", "Certainly", "Outlook good",
"Unsure", "Ask again", "Doubtful", "No",
};
int lastTilt = LOW;
void setup() {
pinMode(TILT, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.print("Ask me a");
lcd.setCursor(0, 1);
lcd.print("question");
randomSeed(analogRead(A5));
Serial.begin(9600);
Serial.println("Mokxi Uno: answer ball. Tip it to ask.");
lastTilt = digitalRead(TILT);
}
void loop() {
int tilt = digitalRead(TILT);
// Tipped over: the switch opens and the pull-up reads HIGH.
if (tilt != lastTilt && tilt == HIGH) {
const char *answer = ANSWERS[random(8)];
lcd.clear();
lcd.print("The ball says:");
lcd.setCursor(0, 1);
lcd.print(answer);
Serial.print("answer: ");
Serial.println(answer);
}
lastTilt = tilt;
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Turns on the pull-up for the tilt pin, starts the LCD and writes the prompt on its two lines. randomSeed(analogRead(A5)) uses the noise on an unconnected pin to start the random numbers differently each run. It also remembers the starting tilt state.
void setup() {
pinMode(TILT, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.print("Ask me a");
lcd.setCursor(0, 1);
lcd.print("question");
randomSeed(analogRead(A5));
Serial.begin(9600);
Serial.println("Mokxi Uno: answer ball. Tip it to ask.");
lastTilt = digitalRead(TILT);
}loop sketch.ino
Reads the tilt pin. If it just changed to HIGH, the ball was tipped, so it picks ANSWERS[random(8)], clears the LCD and shows the answer on the second line. It saves the state each time and waits 20 ms.
void loop() {
int tilt = digitalRead(TILT);
// Tipped over: the switch opens and the pull-up reads HIGH.
if (tilt != lastTilt && tilt == HIGH) {
const char *answer = ANSWERS[random(8)];
lcd.clear();
lcd.print("The ball says:");
lcd.setCursor(0, 1);
lcd.print(answer);
Serial.print("answer: ");
Serial.println(answer);
}
lastTilt = tilt;
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LCD shows nothing or only boxes This project
V0 goes to ground for full contrast. Check RS on pin 12, E on 11 and D4 to D7 on pins 5, 4, 3, 2. - Tipping gives no answer This project
The tilt switch needs one leg on pin 6 and the other on ground. - The reading flickers when the switch is moved. Tilt switch
The ball bounces on the contacts, giving many fast changes. Debounce in software by waiting 20 to 50 ms before accepting a new state, or average a few readings. - The input floats and never gives a clean reading. Tilt switch
A tilt switch is just a switch, so the pin floats when it is open. Use INPUT_PULLUP with the switch between the pin and GND, or add a 10 k pull-down resistor. - The screen lights up but shows only a row of solid boxes, or nothing at all. Character LCD
Contrast pin V0 needs a voltage. Connect the middle pin of a 10 k pot to V0, with its ends on 5 V and GND. Also confirm that the code calls lcd.begin(16, 2). - Garbled characters or a blank display even with good contrast. Character LCD
In 4-bit mode only D4 to D7 are used, and D0 to D3 stay unconnected. Tie RW to GND. Match the pins in LiquidCrystal(rs, en, d4, d5, d6, d7) exactly to your wiring. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.