Built-in project · Arduino Uno R3

Lion dance head on an Arduino Uno

A lion dance head opens its servo eyelids, flashes its LED eyes to a piezo drum beat set by a knob, and goes wild when you press the button to feed it the greens. The whole build, an Arduino Uno and 8 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.

Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $36. On Mokxi they're free.

Lion dance head · Arduino Uno R3live0.000 s 0.00x
Turn the knob for the tempo, and press the button to feed the lion
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

  • Playing a rhythm from a pattern array
  • Non-blocking timing with millis
  • Mapping a knob to tempo
  • PWM brightness on LEDs
  • Moving a hobby servo
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Arduino Uno R3
  • Parts: 10

How it works

On startup the lion has its eyes shut, then the servo eyelids open slowly while the two red LEDs brighten. After that, an eight step drum pattern plays on the piezo at pin 8. The potentiometer on A0 sets the tempo from 80 to 200 beats a minute.

The eyes flash on every boom, and the eyelids blink every few seconds. Hold the button on pin 2 and the lion plucks the greens: a fast drum roll with flashing eyes, then it settles back into its dance.

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
Hobby servo pin PWMArduino Uno R3 pin 9
Piezo speaker pin 1Arduino Uno R3 pin 8
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 5
Pushbutton pin 1aArduino Uno R3 pin 2
Potentiometer pin 3Arduino Uno R3 pin 5V
Hobby servo pin VCCArduino Uno R3 pin 5V
Potentiometer pin 1Arduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin GND
LED, red (1) pin CArduino Uno R3 pin GND
LED, red (2) pin CArduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Potentiometer pin 2Arduino Uno R3 pin A0
LED, red (1) pin AResistor, 220 Ω (1) pin 2
LED, red (2) pin AResistor, 220 Ω (2) pin 2

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
// Lion dance head: a lion that wakes up, blinks its big eyes and dances to a
// drum beat, and goes wild when you feed it the greens.
//
//   eyes      pins 5 and 6 -> 220 ohms -> LED -> GND, one each
//   eyelids   servo PWM -> pin 9, VCC -> 5 V, GND -> GND
//   drum      pin 8 -> piezo -> GND
//   tempo     potentiometer wiper -> A0, ends to 5 V and GND
//   greens    pin 2 -> button -> GND        (INPUT_PULLUP)
//
// Lion dances bring good luck at Lunar New Year, to the beat of a drum,
// cymbals and a gong. Before a new lion dances for the first time, its eyes
// are dotted to wake it, so this one starts with its eyes shut and opens them
// slowly.
//
// The drum plays an eight-step pattern: a low boom on the strong beats and a
// higher tap on the others. The eyes flash on every boom and the eyelids
// blink now and then. The knob sets the tempo.
//
// The button is the greens. In the custom called cai qing, plucking the
// greens, the lion reaches for lettuce hung up high, often with a red
// envelope tied to it. Press it and the drum rolls, the eyes flash fast, and
// then the lion settles back into its dance.
//
// Things to try. Turn the knob. Press the button. Write your own rhythm into
// PATTERN: 2 is a boom, 1 a tap, 0 a rest.

#include <Servo.h>

const int EYE_L = 5, EYE_R = 6;
const int LID_PIN = 9;
const int DRUM_PIN = 8;
const int GREENS_PIN = 2;
const int OPEN = 20, SHUT = 100;

const uint8_t PATTERN[8] = {2, 0, 1, 1, 2, 0, 1, 0};
Servo lids;

int step = 0;
unsigned long nextStep = 0;
unsigned long nextBlink = 3000;

void eyes(int level) {
  analogWrite(EYE_L, level);
  analogWrite(EYE_R, level);
}

void wake() {
  // Eyes shut, then opening slowly as they light.
  for (int a = SHUT; a >= OPEN; a -= 2) {
    lids.write(a);
    eyes(map(a, SHUT, OPEN, 0, 120));
    delay(30);
  }
  Serial.println("The lion is awake");
}

void greens() {
  Serial.println("Cai qing: the lion plucks the greens!");
  for (int i = 0; i < 24; i++) {
    tone(DRUM_PIN, i % 2 ? 150 : 110, 40);
    eyes(i % 2 ? 255 : 30);
    lids.write(i % 4 < 2 ? OPEN : OPEN + 30);
    delay(60);
  }
  eyes(120);
  lids.write(OPEN);
  Serial.println("Back to the dance");
}

void setup() {
  Serial.begin(9600);
  pinMode(EYE_L, OUTPUT);
  pinMode(EYE_R, OUTPUT);
  pinMode(GREENS_PIN, INPUT_PULLUP);
  lids.attach(LID_PIN);
  lids.write(SHUT);
  wake();
}

void loop() {
  unsigned long now = millis();
  if (digitalRead(GREENS_PIN) == LOW) {
    greens();
    while (digitalRead(GREENS_PIN) == LOW) delay(10);
    return;
  }

  if (now >= nextStep) {
    // 80 to 200 beats a minute, two steps to a beat.
    int bpm = map(analogRead(A0), 0, 1023, 80, 200);
    nextStep = now + 30000UL / bpm;
    uint8_t hit = PATTERN[step];
    if (hit == 2) {
      tone(DRUM_PIN, 110, 70);
      eyes(255);
    } else if (hit == 1) {
      tone(DRUM_PIN, 220, 30);
      eyes(160);
    } else {
      eyes(90);
    }
    if (step == 0) {
      Serial.print("Drum at ");
      Serial.print(bpm);
      Serial.println(" beats a minute");
    }
    step = (step + 1) % 8;
  }

  // A blink every few seconds: shut and open again.
  if (now >= nextBlink) {
    lids.write(SHUT);
    delay(150);
    lids.write(OPEN);
    nextBlink = now + random(2500, 6000);
  }
}

Code walkthrough

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

wake sketch.ino

Sweeps the servo from SHUT to OPEN in steps of 2 degrees. At each step, map raises the eye brightness from 0 to 120. The slow opening takes about 1.2 seconds.

void wake() {
  // Eyes shut, then opening slowly as they light.
  for (int a = SHUT; a >= OPEN; a -= 2) {
    lids.write(a);
    eyes(map(a, SHUT, OPEN, 0, 120));
    delay(30);
  }
  Serial.println("The lion is awake");
}

eyes sketch.ino

Sets both eye LEDs to the same PWM level with analogWrite. The rest of the sketch uses it to flash on booms and dim on rests.

void eyes(int level) {
  analogWrite(EYE_L, level);
  analogWrite(EYE_R, level);
}

greens sketch.ino

Runs a 24 step burst. The piezo alternates between two pitches, the eyes alternate between 255 and 30, and the lids shift. Then it restores the normal eye level and lid position.

void greens() {
  Serial.println("Cai qing: the lion plucks the greens!");
  for (int i = 0; i < 24; i++) {
    tone(DRUM_PIN, i % 2 ? 150 : 110, 40);
    eyes(i % 2 ? 255 : 30);
    lids.write(i % 4 < 2 ? OPEN : OPEN + 30);
    delay(60);
  }
  eyes(120);
  lids.write(OPEN);
  Serial.println("Back to the dance");
}

Troubleshooting

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

  • The tempo does not change. This project
    Check the potentiometer's middle pin goes to A0, with the ends on 5 V and GND.
  • The servo jitters. This project
    Check the servo's PWM wire is on pin 9 and its power is 5 V and GND.
  • No drum sound. This project
    The piezo goes from pin 8 to GND.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • 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.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.

Change it and keep it

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