HARRY POTTER SORTING HAT

by carmenbaelisava in Circuits > Arduino

877 Views, 4 Favorites, 0 Comments

HARRY POTTER SORTING HAT

IMG-9807 (1).JPG
IMG-9804.JPG

This is for all Harry Potter fans! It is about the Sorting Hat itself brought to reality with an electronic circuit operation programmed by ARDUINO UNO. It contains: the melody of the movie activated thanks to the motion sensor, LEDS lights of the colors of the different houses and a mechanism that lifts the hat with the activation of a photosensor. It's a nice simple way to build your own Sorting Hat for Halloween and have fun with friends!



Supplies

arduino uno.jpg
buzzer.png
fotosensor.jpg
leds.png
sensor moviminento.jpg
servo.jpg
proto.jpeg

ELECTRONIC COMPONENTS:

  • Arduino Uno

https://store.arduino.cc/products/arduino-uno-rev3?_gl=1*1sml892*_ga*NjcxMjM3OTczLjE2NTQwOTk2NTM.*_ga_NEXN8H46L5*MTY2NzI0NDQ5Mi4yMy4xLjE2NjcyNDQ0OTYuMC4wLjA.


  • Protoboard

https://www.amazon.es/BOJACK-Breadboard-Soldadura-Flexibles-Protoboard/dp/B09ZKF666S/ref=sr_1_1_sspa?crid=W8YDHZVGERTU&keywords=protoboard&qid=1667244565&qu=eyJxc2MiOiI1LjA1IiwicXNhIjoiNS4wNSIsInFzcCI6IjQuODAifQ%3D%3D&sprefix=pro%2Caps%2C158&sr=8-1-spons&th=1


  • LEDS ( Yellow, Red, Blue, Green)

https://www.amazon.es/YIXISI-Ultrabrillante-Multicolor-Emisores-Amarillo/dp/B08S2W74K7/ref=sr_1_1_sspa?crid=CH8DDJ2E57HM&keywords=led+arduino&qid=1667244643&qu=eyJxc2MiOiI1LjI4IiwicXNhIjoiNC43OSIsInFzcCI6IjQuMjAifQ%3D%3D&sprefix=leds+ard%2Caps%2C155&sr=8-1-spons&psc=1


  • Resistance 220 ohms
  • Resistance 1k ohms

https://www.amazon.es/HUAREW-valores-resistencias-pel%C3%ADcula-carbono/dp/B09NVDC3MY/ref=sr_1_34_sspa?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=26PNJS9IGEPA3&keywords=resistance+1%2F6+W&qid=1667244988&sprefix=resistance+1%2F6+w%2Caps%2C165&sr=8-34-spons&psc=1


  • Photosensor

https://www.amazon.es/Electr%C3%B3nica-resistencia-dependiente-fotoel%C3%A9ctrica-fotoel%C3%A9ctrico/dp/B09T79YKXH/ref=sr_1_14?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=1Y2LM9JLJPWCS&keywords=photo+sensor+ldr+arduino&qid=1667245062&sprefix=photosensor+ldr+arduino%2Caps%2C180&sr=8-14


  • Buzzer

https://www.amazon.es/ARCELI-electr%C3%B3nica-Magn%C3%A9tica-Continua-Arduino/dp/B07RDHNT1P/ref=sr_1_4?crid=11ZN4P3FT21PL&keywords=buzzer+arduino&qid=1667245100&qu=eyJxc2MiOiIzLjg4IiwicXNhIjoiMy4yMCIsInFzcCI6IjMuMTEifQ%3D%3D&sprefix=buzzer+%2Caps%2C192&sr=8-4


  • Servo motor (180º)

https://www.amazon.es/AZDelivery-%E2%AD%90%E2%AD%90%E2%AD%90%E2%AD%90%E2%AD%90-Micro-Servo-hubchrauber/dp/B07CYZK379/ref=sr_1_1_sspa?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=3KVZS1M6PFAJC&keywords=servomotor%2Barduino&qid=1667245141&qu=eyJxc2MiOiI1LjAyIiwicXNhIjoiNC41MCIsInFzcCI6IjQuMDYifQ%3D%3D&sprefix=servomotor%2Barduino%2Caps%2C171&sr=8-1-spons&th=1


  • HC-SR04 Distance Sensor Service

https://www.amazon.es/Ultrasonidos-Arduino-Medidor-Distancia-Ultrasonico/dp/B075MDMDL2/ref=sr_1_8?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=JXK4CXASPDZD&keywords=HC-SR04+Distance+Sensor+Service&qid=1667245182&sprefix=hc-sr04+distance+sensor+service%2Caps%2C250&sr=8-8


OTHER COMPONENTS:

  • Wooden box
  • Wooden/plastic/cardboard board for the support surface
  • Witch hat for the imitation of the Sorting Hat
  • Circular cork board for the support surface of the hat (optional)
  • 2 Blocks of cork board for the support of the servo motor and the hat.
  • Wooden toothpicks to make an arm to conect the servo with the hat.

Assemble the Electronic Components

MicrosoftTeams-image (10).png

Follow the picture to connect all the components to the Arduino.

  • LED resistance: 220 Ohms
  • Photosensor resistance: 1k Ohms

Code

Copy the code to Arduino, connect the arduino uno to the computer try the code and modificate any values that you preffer (optional).


CODE:



//SERVO MOTOR

#include <Servo.h>  //Define de servo motor

int pos = 0;        //The start position

Servo myservo;      // create servo object to control a servo


// MOVE SENSOR PINS

#include <hcsr04.h>  //define movement sensor

#define TRIG_PIN 6

#define ECHO_PIN 7

int valor;       //valor for the distance in the movement sensor

float distance;  //distance that the movemente sensor it is goning to percieve

HCSR04 hcsr04(TRIG_PIN, ECHO_PIN, 20, 4000);

long tiempo;


// LED PINS

int blue = 2;

int red = 3;

int green = 4;

int yellow = 5;


bool randomGenerated = false;  // It generates a boolean. In this case it stars false. This helps for turning on the leds randomly.

int randomLED;


// PHOTOSENSOR PIN

int pinLDR = 0;

int valorLDR = A0;  // Variable where it keeps the data of the photosensor.


//BUZZER

int t;

int buzzer = 10;      //pin buzzer

unsigned long lastT;  //This helps so then the arduino cannot read negative numbers, so in our melody every negative number will be a silent.


//NOTES

int c[5] = { 131, 262, 523, 1046, 2093 };   // frecuencias 4 octavas de Do

int cs[5] = { 139, 277, 554, 1108, 2217 };  // Do#

int d[5] = { 147, 294, 587, 1175, 2349 };   // Re

int ds[5] = { 156, 311, 622, 1244, 2489 };  // Re#

int e[5] = { 165, 330, 659, 1319, 2637 };   // Mi

int f[5] = { 175, 349, 698, 1397, 2794 };   // Fa

int fs[5] = { 185, 370, 740, 1480, 2960 };  // Fa#

int g[5] = { 196, 392, 784, 1568, 3136 };   // Sol

int gs[5] = { 208, 415, 831, 1661, 3322 };  // Sol#

int a[5] = { 220, 440, 880, 1760, 3520 };   // La

int as[5] = { 233, 466, 932, 1866, 3729 };  // La#

int b[5] = { 247, 494, 988, 1976, 3951 };   // Si



//MELODY

int musica[][34] = { { 0, b[2], e[3], g[3], fs[3], e[3], b[3], a[3], fs[3], b[2], e[3], g[3], fs[3], d[3], e[3], b[2], -1, b[2], e[3], g[3], fs[3], e[3], b[3], d[4], cs[4], c[4], a[3], c[4], b[3], as[3], b[2], g[3], e[3], -1 },

                     { 0, 500, 1000, 250, 250, 1000, 500, 1250, 1000, 500, 1000, 250, 250, 1000, 500, 1000, 1000, 500, 1000, 250, 250, 1000, 500, 1000, 500, 1000, 500, 1000, 250, 250, 1000, 500, 1000, 2000 } };




void setup() {

  // Configurate the Serial Port so we can know what is happening.

  Serial.begin(9600);


  //Configurate the servoPIN

  myservo.attach(9);


  //Configurate the move sensor pins

  pinMode(7, INPUT);

  pinMode(6, OUTPUT);


  //Configurate the start mode of the leds

  digitalWrite(blue, HIGH);

  digitalWrite(red, HIGH);

  digitalWrite(green, HIGH);

  digitalWrite(yellow, HIGH);


  //Configurate the melody setup

  pinMode(10, OUTPUT);

  lastT = millis();

  t = 0;

}

//NOTE FUNTION

/*This will make easy to read the melody because it will

 read the frecuency and the duration of each note

 (the array that was at the begining)

 */

void nota(int frec, int t) {

  tone(buzzer, frec);

}


void loop() {


  //MOVE SENSOR

  Serial.println(hcsr04.distanceInMillimeters());  //Give us the distance in mm

  distance = hcsr04.distanceInMillimeters();       //Reads the distance in mm



  //PHOTOSENSOR

  /*

  We keep the value that the move sensor (ADC) read in a variable. This variable increases in direct proportion

 with respect to the light perceived by the photosensor (LDR).

  */


  valorLDR = analogRead(pinLDR);  // Return the value read to our serial monitor in the Arduino IDE


  Serial.print(valorLDR);           //Show us the value of the photosensor

  Serial.print(" - ");              //Makes a separation

  Serial.println(randomGenerated);  // Encender los leds apropiados de acuerdo al valor de ADC



  //MELODY WITH MOVE SENSOR

  /*

  The objetive is activates the melody once the move sensor detects something

  at a certain distance

  */


  if (distance < 100) {  //This distance can be change


    /*

This first part inside the "if (distance < 100)", is going to control when it is time for the next note to sound.

With the millis() is going to be able to read the time and to compare it the the last time (lastT). So if the current

time minus the last time that the ARDUINO read is bigger than the duration of the note then do the next "if" that is

going to make the melody.


This funtion is going to make a NON-BLOKING function where the music and the Leds/Servo can go separately.

*/

    if (millis() - lastT > musica[1][t]) {

      t++;  //It is going to increase the time one by one.

      //This is just to clarify each part of the array:

      int f = musica[0][t];    //The notes

      int dur = musica[1][t];  //The duration of each note

      lastT = millis();        //The last time read

      /*

      This next part is going to indicate us that if the Frecuency ( the fisrt components of the array)

      is bigger than 0, then keep reading the note and if not just stop until the next note

      */

      if (f > 0) {


        tone(buzzer, f);


      } else {

        noTone(buzzer);

      }

    }


  } else {


    noTone(buzzer);

    t = 0;  //The song starts from the begining

  }



  //LED RANDOM + SERVO


  /*

For this second part, we want to turn on just ONE led randomly and move the servo. To make the random led we are going

to use the randomGenerated that we create at the begining. With this variable we are going to evitate blinks in our leds.


The value of the "valorLDR" can be change. It is going to depend the light of the room.


BOX OPEN: The randomGenerated starts at false (as we declared at the beggining) so what we whant is that

once the LDR value is more tha 250 (in this case) and the randomGenerated iS false then do the next action:

change it to true, turn off all the leds except one and move the servo to the position "pos".


BOX CLOSE: If that doesn´t happen then it means that the randomGenerated is true so then:  change it to false, turn on all the LEDs

and return the servo to the start position

  */


  if ((valorLDR > 250) && (randomGenerated == false)) {

 

    randomGenerated = true;

    myservo.write(pos);

    digitalWrite(blue, LOW);

    digitalWrite(red, LOW);

    digitalWrite(green, LOW);

    digitalWrite(yellow, LOW);

    randomLED = int(random(2, 6));

    digitalWrite(randomLED, HIGH);


  } else if (valorLDR < 250 && randomGenerated == true) {

    randomGenerated = false;

    digitalWrite(blue, HIGH);

    digitalWrite(red, HIGH);

    digitalWrite(green, HIGH);

    digitalWrite(yellow, HIGH);

    myservo.write(90);

  }

}

Paint the Components

04.jpg
0.jpg
01.jpg

We paint the details in gold. The details were made in a laser cut machine. You can customize yours as you like!

The Arm Mechanisim

IMG-9810.JPG
IMG-9809.JPG
IMG-9808.jpg

For this part you need to be carefull because you are going to need sharpe tools to cut some of the cork blocks.


  • Cut a block of cork in a rectangular shape that can fit inside the hat.
  • Draw on top of the block the servo motor shape so then it can be secured.
  • Cut the shape and glue it to the servo.
  • With the sticks make a 90º arm and glue them to the servo.
  • With an other cork block make a cone shape (like the top of the hat)
  • Glue the other end that is not glue to the servo, to the cone cork.
  • Finally you will have an arm that can move the hat.

Join the Components

IMG-9804.JPG

Once you have everithing working you just need one final touch!

Under the hat you can put anything you want (sweets, prizes, cards...) In this case we have printed stikers with the logo of each house.

Have fun!