Dreidel spinner on an Arduino Uno
Press the button to spin a dreidel on a 16x2 LCD: the letters whirl and slow with a piezo click each, land on nun, gimel, hei or shin, and the sketch keeps score of the pot. The whole build, an Arduino Uno and 3 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 Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Dreidel: press the button to spin, and the LCD whirls through the four
// letters, slowing down with a click for each one, until it lands. Then it
// says what the letter means and keeps score of the pot.
//
// LCD 16x2 with an I2C backpack: SDA -> A4, SCL -> A5
// button pin 2 -> button -> GND (INPUT_PULLUP): spin
// piezo pin 8 -> piezo -> GND
//
// The dreidel is a four-sided top played with at Hanukkah. Its sides carry
// the Hebrew letters nun, gimel, hei and shin (pe in Israel), for "a great
// miracle happened there". In the game everyone puts a token in the pot, and
// each spin says what to do:
//
// nun nothing happens
// gimel take the whole pot
// hei take half the pot
// shin put one token in
//
// The LCD's font has no Hebrew letters, so the screen spells the names. The
// spin slows the way a real top does: the wait between letters grows a little
// each time, and the letter it stops on is picked at random before the spin
// starts, each one equally likely.
//
// Things to try. Press the button to spin. Start the pot bigger with POT.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int BUTTON_PIN = 2;
const int PIEZO_PIN = 8;
const int POT = 10;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char *LETTERS[4] = {"Nun", "Gimel", "Hei", "Shin"};
const char *RULES[4] = {"Nothing happens", "Take the pot!", "Take half", "Put one in"};
int pot = POT;
int spins = 0;
void face(int i) {
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.print(LETTERS[i]);
lcd.print(" ");
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
randomSeed(analogRead(A0));
lcd.print("Dreidel! Pot ");
lcd.print(pot);
lcd.setCursor(0, 1);
lcd.print("Press to spin");
Serial.println("Happy Hanukkah! Press the button to spin the dreidel");
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) return;
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
spins++;
int land = random(0, 4);
int turns = 16 + random(0, 4) * 4 + land; // whole turns, then the letter
lcd.clear();
Serial.print("Spin ");
Serial.print(spins);
Serial.println("...");
unsigned long wait = 30;
for (int t = 0; t <= turns; t++) {
face(t % 4);
tone(PIEZO_PIN, 900, 8);
delay(wait);
wait = wait * 17 / 16 + 2; // slowing down
}
// What the letter does to the pot. If it empties, both players put one in.
if (land == 1) pot = 0;
else if (land == 2) pot -= (pot + 1) / 2;
else if (land == 3) pot++;
if (pot == 0) pot = 2;
lcd.setCursor(0, 1);
lcd.print(RULES[land]);
Serial.print(LETTERS[land]);
Serial.print(": ");
Serial.print(RULES[land]);
Serial.print(" The pot has ");
Serial.println(pot);
tone(PIEZO_PIN, land == 1 ? 1320 : 660, 200);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 8; Piezo speaker pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 1a
- Arduino Uno R3 pin 5V; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Pushbutton pin 2a; Piezo speaker pin 2; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.