Built-in project · Arduino Uno R3

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 About 15 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.

Dreidel spinner · Arduino Uno R3live0.000 s 0.00x
Press the button to spin the dreidel
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading a button with INPUT_PULLUP
  • Random numbers with random and randomSeed
  • Writing text to an I2C character LCD
  • Making an animation slow down with a growing delay
  • Sound from a piezo with tone
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

Press the button and the 16x2 LCD whirls through four names: Nun, Gimel, Hei and Shin. Each step makes a short piezo click, and the pauses grow longer so the spin slows down like a real top.

The sketch picks the landing letter at random before the spin starts, with each letter equally likely. It also picks how many full turns to take.

When the spin ends, the LCD shows what the letter does to the pot, and the serial monitor prints the new total. Gimel takes the pot, Hei takes half, Shin adds one, and Nun does nothing.

Parts list

5 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Kits that can build it

These 3 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 pinConnects to
Piezo speaker pin 1Arduino Uno R3 pin 8
Pushbutton pin 1aArduino Uno R3 pin 2
Character LCD with I2C backpack pin VCCArduino Uno R3 pin 5V
Pushbutton pin 2aArduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Character LCD with I2C backpack pin GNDArduino Uno R3 pin GND
Character LCD with I2C backpack pin SDAArduino Uno R3 pin A4
Character LCD with I2C backpack pin SCLArduino Uno R3 pin A5

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

face sketch.ino

Writes one letter name on the top row of the LCD. It pads the line with spaces so a short name fully covers a longer one from the last frame.

void face(int i) {
  lcd.setCursor(0, 0);
  lcd.print("   ");
  lcd.print(LETTERS[i]);
  lcd.print("        ");
}

setup sketch.ino

Starts serial, sets the button pin to INPUT_PULLUP, and starts the LCD with its backlight. It seeds the random generator from the floating pin A0, then shows the starting pot and a prompt.

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");
}

loop sketch.ino

Waits for a press and for the release. Then it picks land, plays the spin with the delay growing each step (wait * 17 / 16 + 2), and updates the pot. The last line shows the rule and plays a higher tone for gimel.

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]);
  // ... 6 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LCD stays blank This project
    Check SDA on A4 and SCL on A5. The code expects the I2C address 0x27, so a backpack with another address needs that number changed.
  • Pressing the button does nothing This project
    Wire the button between pin 2 and ground. The sketch waits for the pin to read LOW.
  • No clicks are heard This project
    Check that the piezo is between pin 8 and ground.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
    A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead.
  • Sound is very quiet or the pin seems overloaded. Piezo speaker
    Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.
  • The screen stays blank or shows only a row of blocks. Character LCD with I2C backpack
    The I2C address is usually 0x27 or 0x3F. Run an I2C scanner to find yours. Then turn the contrast trimmer on the backpack slowly until text appears.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.