Harry Potter Sorting Hat – Code Overview

Once all of the sensors were integrated into a single circuit, all of the separate test code for the sensors (among other things) needed to be written into a single cohesive program. The sensors and piezo buzzers were controlled by the Arduino microcontroller. As such, much of the code was written in the Arduino IDE.

Serial communication was first established between the Arduino and A3BU boards. The A3BU code would initialize and set itself up using the USART framework. The A3BU code had a continuous loop where it would wait to receive a byte (character) on its RxD pin on J1. If the Arduino sent a ‘G’, the A3BU would then write “Gryffindor” to its LCD screen using the GFX Monochrome library. If it received an ‘S’, it would write “Slytherin,” and etc. There were 4 characters for each of the 4 houses.

In the Arduino code, it first entered a while loop which would iterate until the IR distance sensor detected the hat was placed on a person’s head. Afterwards, the color sensor was triggered. It initialized itself and read in the RGB values (luminance was calculated based on these). It passes the calculated luminance value to a function, which returned a single character corresponding to the house (‘G’ = Gryffindor, ‘S’ = Slytherin, ‘R’ = Ravenclaw, ‘H’ = Hufflepuff). This character was sent to the A3BU board using the Serial.print() function. At this point, it would call a function to trigger the piezo buzzers for play the Harry Potter tune to indicate that the mighty hat had sorted the wearer into a house. The wearer would then take the hat off and read their house on the A3BU’s LCD screen (as well as enjoy that sweet tune played by the piezo buzzers).

A3BU Xplained Code: 

#include <asf.h>
#include <conf_usart_example.h>
#include <delay.h>
#include <gfx_mono.h>
#include <sysfont.h>

int main (void)

       // USART options.
       static usart_rs232_options_t USART_SERIAL_OPTIONS = {
              .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE,
              .charlength = USART_SERIAL_CHAR_LENGTH,
              .paritytype = USART_SERIAL_PARITY,
              .stopbits = USART_SERIAL_STOP_BIT

       // Initialize USART driver in RS232 mode


       uint8_t received_byte;
       char character;

              received_byte = usart_getchar(USART_SERIAL_EXAMPLE);
              character = (int)received_byte;


              if(character == 'G')
                     gfx_mono_draw_string("Gryffindor!", 0, 0, &sysfont);
              }else if(character == 'S')
                     gfx_mono_draw_string("Slytherin!", 0, 0, &sysfont);
              }else if(character == 'H')
                     gfx_mono_draw_string("Hufflepuff!", 0, 0, &sysfont);
              }else if(character == 'R')
                     gfx_mono_draw_string("Ravenclaw!", 0, 0, &sysfont);
                     gfx_mono_draw_string("Hat is confused...", 0, 0, &sysfont);

Sample Arduino Uno Code:

void setup() //setup is called automatically
    Serial.begin(9600); //Initialize baud rate to 9600
    pinMode(1, OUTPUT); //set TxD pin to output for A3BU communication

void loop() //Arduino calls this function automatically continuously (main function)
    //Until IR sensor detects the hat is put on, do not go further
    distanceReading = analogRead(2);
    while( distanceReading < 350 ) //350 is the distance from IR sensor to base of boxhat
         distanceReading = analogRead(2); //read value form analog pin 2
    Serial.println("Hat is put on...distance: " + distanceReading);

    Serial.println("Activating color sensor...");
    uint16_t luxValue = readColorSensor();    

    char houseCode = determineHouse(luxValue); 
    Serial.print(houseCode); //send the house code to the A3BU

    if(houseCode == 'G' || houseCode == 'S' || houseCode == 'H' || houseCode =='R')

uint16_t readColorSensor()
    if (tcs.begin()) 
      Serial.println("Found sensor");
    } else 
      Serial.println("No TCS34725 found ... check your connections");
      while (1);
    // Now we're ready to get readings!

    uint16_t r, g, b, c, colorTemp, lux;

    tcs.getRawData(&r, &g, &b, &c);
    colorTemp = tcs.calculateColorTemperature(r, g, b);
    lux = tcs.calculateLux(r, g, b);

    return lux;

Harry Potter Sorting Hat – Schematics/Hardware

The IR sensor’s function in our project was to detect when the hat is worn. Initially, it was implemented alone on a breadboard for testing. This particular sensor had a supply voltage of 4.5V-5.5V, so it could take power directly from the Arduino board. The sensor test program and serial monitor in the Arduino IDE showed the output values ranging from 0-1023, which indicated the detected distance.

Initially, we had the color sensor connected directly to the 5V port of the Arduino board. Once we realized that the color sensor has a max voltage setting of 3.8V, a voltage divider was implemented within the circuit to step down the 5V from the Arduino. The two resistors in voltage divider in series were a 220 Ohm resistor and a 330 Ohm resistor.

The 5V from the Arduino was inputted into the power line of the breadboard. From there, the voltage divider stepped it down for the devices that required it. The last item we needed to implement was a piezo buzzer, which was very simple in nature (when pin is high, buzz; when pin is low, stop buzzing). Delays and duty cycles are cleverly used to trick the piezo buzzer in playing decipherable simple music. A short tune of the Harry Potter theme music was found and implemented into our code.

Below are some of the pictures of to give a overview of how everything was wired up:

Harry Potter Sorting Hat – Project Idea

We embarked on building the Harry Potter sorting hat as our final project.  In the HP universe, all new students at Hogwarts School of Wizardry & Witchcraft are sorted into one of the four ancient houses based on the personality of the student by a magical sorting hat. The four houses are: Gryffindor, Ravenclaw, Slytherin, and Hufflepuff.

Related image

An IR sensor, color sensor, and piezo buzzers were used along with the Atmel software, an A3BU board, an Arduino Uno board, and Arduino IDE software. This combination of sensors and programming will allow the sorting of houses as per the lore of Harry Potter.

Here are some of the ideas the project utilized:

1)    An IR sensor for knowing if someone is wearing the hat.

2)    A color sensor to take in data from hair color.

3)    Piezo buzzers to play a short tune from the movie’s theme song. (The original plan was for the hat to shout the different house names as seen in the books/movies, but due to the time constraint, this was unfortunately not undertaken).

4)    All three devices (above) were to be integrated into a circuit with a voltage divider.

5)    An Arduino Uno board was to be used for the integrated sensor circuit.

6)    The A3BU would take in the data read from the Arduino and display the specific house onto its LCD screen.