Animated LEGO Diorama Made With Arduino Uno

by dekaaspro in Circuits > Arduino

1733 Views, 6 Favorites, 0 Comments

Animated LEGO Diorama Made With Arduino Uno

ittt_front_active_1.jpg
ittt_front of case1.jpg
ITTT Arduino Living Lego Diorama 1
ITTT Arduino living lego diorama 2
itt_kwart_view case1.jpg
ittt_front_windowzoom1.jpg

[This Instructable was made for a project at HKU by Floris Koelemaij]


This project showcases how you can make your own living LEGO diorama using an Arduino Uno!

For this project, i wanted to create an interactive display piece that could be hung on the wall, or put somewhere in the livingroom for example. This interactive living Lego diorama comes to life with just the press of a button! The Lego diorama works when plugged-in to a power source and activates with the press of a button on the side of the box. Upon pressing the button, the Lego will come to life!

The case of this Lego diorama is made from a glass pannel and plates MDF-wood using a lasercutter. Besides that, the diorama itself is made from Lego. Supplies for your diorama and casing will bepend on what you are building, but the concept and steps for integrading an Arduino Uno into a project like this will stay the same.

Here are some photos and videos of the finished product:

Supplies

In addition to the Lego and MDF-wood used, you will need the following components for the Arduino:

  • 1x Ardunio Uno
  • 1x Breadboard for testing
  • 1x Breadboard for final soldering
  • 1x Pushbutton
  • 1x Pushbutton cap
  • 1x LED (color up to you, i used a blue LED)
  • 1x 10kOhm resistor
  • 1x 330Ohm resistor
  • 3x Mini Servo
  • (?) Jumper cables to connect your compontents (size and amount may change based on the scale of your diorama)

Sketch Out Your Design

ittt_concept_sketch1.jpg

When starting a project like this, it's important to know and plan out exactly what you want, before you starting building and buying parts. That's why we start with sketching out our Lego diorama, our casing and noting down which parts of the diorama we want to animate using arduino components.

The image here features sketches of my concept as an example:

Test-building Your Lego Diorama

ittt_testdio1.jpg
ittt_testdio2.jpg
ittt_testdio3.jpg
ittt_testdio6.jpg
ittt_testdio4.jpg
ittt_testdio5.jpg

After you have a clear picture of how your diorama will look, what you want to connect to the Arduino and what the size of your case will be, it's time to start with a test build! Before we go ahead and buy all of the needed parts (Lego, Casing, Arduino components), we'll start by building a 1:1 scale test build of our diorama. In this we we will be able to test if everything will be the correct size and figure out how and where we want to integrate our individual components (Servo motors, LEDs, Buzzers, etc).

Ordering Parts

ittt_startbuilding1.jpg

Once you've test-build your diorama and you've made a list off all the parts (Lego, casing, Arduino components, etc) you'll need for your project, it's time to order them.

You can always order some extra parts to make sure you have enough if you make some changes during final assembly.

Drawing and Testbuilding Your Arduino Circut

arduino_schema_ITTT_lego_diorama_werkend_final1.PNG
ittt_arduino_testbuild1.jpg

Before you start building your final Lego diorama, it's important to make sure that your Arduino circut works as intended. You won't need to write all your coding as of yet (but that's also possible if you prefer), but with this step we just want to make sure that all our components are working the way we want them to.

This step features images of my Arduino circut and my test build on the breadboard. For my diorama is used 1 button, 1 LED and 3 mini-servos to bring my Lego diorama to life.

Building the Final Lego Diorama

ittt_testfitparts1.jpg

Once you've tested your Arduino circut, it's time to finally build your Lego diorama. When building, make sure that all your wires and Arduino components fit where they should, which you can now test with the circut you have build in the previous step.

Here is an image of my final build: (You can see in the picture that i added some extra hight to fit all of the components in my build).

Soldering Your Arduino Circut

ittt_soldered_arduino1.jpg

Before putting the your Arduino and all of its components into your Lego diorama, it's a good idea to solder them on a breadboard.

Here is a picture of my soldered Arduino circut, ready to be put into the Lego diorama!

Integrating the Arduino Into Your Diorama

ittt_finaldio1.jpg
ittt_finaldioback.jpg
ittt_finaldio_bot.jpg

When your diorama is build and you've test-fit and soldered your arduino components, it's time to finally put them together!

It's a good idea to first test fit everything in place using some sticky-tac before you glue certrain parts together. Now is also the time to double check that all your parts are still working as they should. Once you've glued or attached your components, you're ready to move to the next step.

Here are some pictures of how i integrated my Arduino components into my diorama:

Programming

Once you've got your Arduino all integrated into your Lego diorama, it's finally time to make it come to life! The amount of coding and what exactly you want to code, will depend on your Lego diorama.

For my diorama, i wanted 2 moving Lego characters who are fighting eachother (a Ninja and a skeleton), 1 Cave door that opens up during the fight, with some LED light in the cave that is shining on the golden weapon. Below you can find the code i have written for this scene to happen.


Below is the code for my project:

//libiraries
#include <Servo.h>


//pin connections to the board
const int ledPin = 8;
const int buttonPin = 4;
const int ninjaPin = 9;
const int enemyPin = 6;
const int doorPin = 10;


//timed events
float ninjaActivateTime = 2000.0; //delay before the ninja is activated
float enemyActivateTime = 3200.0; //delay before the enemy is activated
float doorActivateTime = 4000.0; //delay before the door is activated

//button variables
int buttonState = 0;
int buttonActive = 0; //bool checking if the diorama should be running or not
float savedMillis;
int runningShow = 0;


//servo variables
Servo servoNinja;//servo connected to the lego ninja
int servoNinjaPos = 0;
float ninjaSpeed = 1.0f;
Servo servoEnemy; //servo connected to the lego skeleton enemy
int servoEnemyPos = 0;
float enemySpeed = 1.0f;
Servo servoDoor; //servo connected to the lego cave door
int servoDoorPos = 0;
int doorTurn = 120;
float doorSpeed = 1.0f;

void setup() {


  Serial.begin(9600);


  //Pin Connections
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  servoNinja.attach(ninjaPin);
  servoEnemy.attach(enemyPin);
  servoDoor.attach(doorPin);


 //setup starting positions
  int servoNinjaPos = 0;
  int servoEnemyPos = 0;
  int servoDoorPos = 0;
  int runningShow = 0;
 
  buttonActive = 0;


}


void loop() {
  buttonState = digitalRead(buttonPin);
  //Serial.println("reading button pin");


  if(buttonState == HIGH) { //check if the button is pressed
  Serial.println("Buttonstate = HIGH");
    if(buttonActive == 0) {
      buttonActive = 1;
      savedMillis = millis();
      Serial.println("Button Pressed");


      runningShow = 1;


    }
  }

  if(runningShow == 1) { //if the button has been pressed, run the following code:


    digitalWrite(ledPin, HIGH); //turn on the LED


//fighting animations
 if(millis() >= (savedMillis + ninjaActivateTime)) {
NinjaFights(70);
 }


 if(millis() >= (savedMillis + enemyActivateTime)) {
EnemyFights(90);
 }


 if(millis() >= (savedMillis + ninjaActivateTime)) {
NinjaFights(100);
 }


  if(millis() >= (savedMillis + enemyActivateTime)) {
EnemyFights(110);
 }


  if(millis() >= (savedMillis + ninjaActivateTime)) {
NinjaFights(60);
 }

    //door servo activation
  if(millis() >= (savedMillis + doorActivateTime)) {


         for (servoDoorPos = 0; servoDoorPos <= doorTurn; servoDoorPos += doorSpeed) { // goes from 0 degrees to max degrees
    // in steps of 1 degree
    servoDoor.write(servoDoorPos);              // tell servo to go to position
    delay(85);                       // waits for the servo to reach position
  }
  delay(1500);
  for (servoDoorPos = doorTurn; servoDoorPos >= 0; servoDoorPos -= doorSpeed) { // goes from max degrees to 0 degrees
    servoDoor.write(servoDoorPos);              // tell servo to go to position
    delay(85);                       // waits for the servo to reach position
  }
  runningShow = 0;
    buttonActive = 0;


  }


  } else {
    digitalWrite(ledPin, LOW); //turn off the LED if the button has not been pressed
  }


  }

void NinjaFights(int ninjaTurn) {  //ninja servo activation


         for (servoNinjaPos = 0; servoNinjaPos <= ninjaTurn; servoNinjaPos += ninjaSpeed) { // goes from 0 degrees to max degrees
    // in steps of 1 degree
    servoNinja.write(servoNinjaPos);              // tell servo to go to position
    delay(15);                       // waits for the servo to reach position
  }
  for (servoNinjaPos = ninjaTurn; servoNinjaPos >= 0; servoNinjaPos -= ninjaSpeed) { // goes from max degrees to 0 degrees
    servoNinja.write(servoNinjaPos);              // tell servo to go to position
    delay(15);                       // waits for the servo to reach position
  }


}


void EnemyFights(int enemyTurn) {   //enemy sero activation


    Serial.println("Enemy Servo Active");


         for (servoEnemyPos = enemyTurn; servoEnemyPos >= 0; servoEnemyPos -= enemySpeed) { // goes from 0 degrees to max degrees
    // in steps of 1 degree
    servoEnemy.write(servoEnemyPos);              // tell servo to go to position
    delay(15);                       // waits for the servo to reach position
  }
  for (servoEnemyPos = 0; servoEnemyPos <= enemyTurn; servoEnemyPos += enemySpeed) { // goes from max degrees to 0 degrees
    servoEnemy.write(servoEnemyPos);              // tell servo to go to position
    delay(15);                       // waits for the servo to reach position
 


}


}

Putting It in a Case

ittt_casing_unbuild1.jpg
ittt_opencase_back.jpg
ittt_case_button.jpg
ittt_case_glass.jpg
ittt_opencase_front.jpg
ittt_opencase_top.jpg

After your project is all up and running, it's time to put it in a nice and protective case. To make my case, i used MDF-wood and a lasercutter, aswel as a small glass panel i took out of an old picture frame. Something to keep in mind when designing your case, is that you should have a way to fit your button as well as the plugin for your power source on your case. I also made some holes in the back of the case, so that it can be hung on a wall.

Here you can find my dfx files for lasercutting the case and pictures of the construction:

The Last Step

ittt_case_front_turned1.jpg
ittt_case_front1.jpg
ittt_case_back.jpg
ittt_case_side_full.jpg

After you've put everthing in your case, check to make sure that everything is still working and your diorama is securely glued to your case, so that it won't more around when it is picked up. Then it's time to glue your case shut, and enjoy your own living Lego diorama!

Here are some pictures of the final product: