Magician’s hat

 

 

This particular hat was already sitting in the attic for some time, it had served its purpose as costume addition. Until I found this bucket of little mirror ball mirrors at a crafts store, I immediately had this picture in mind of a mirror ball hat with its own LED lighting. So, I bought the bucket of mirrors, some strong but flexible glue and spent a whole night gluing little mirrors to the fabric of the hat.

The electronics

 

The LED’s are separate pixels, similar to the WS2812B. These pixels have a gnd, singal and 5v channel each and can be connected is series. The advantage of these pixels is that they can be used to make a flat circle, which is impossible with a led strip, the disadvantage is the extra soldering work.

The controller is a ESP32-s2 based Wemos login S2 mini. Though small, it is powerful and versatile, with two double pin headers so more than enough of the ESP32 pins are available.

To make it a bit more interesting, an MSGEQ7 sound analyser is added to the hat wit a small electet microphone module. The MSGEQ7 is an IC with 7 integrated bandpass filters, which outputs gains for the sound intensity of each of these band frequencies. Usually it is applied in amplifiers which have a graphic equalizer, but it’s great for all kinds of projects.

The system is powered with a 18650 Li-ion battery in a battery shield. Because the hat is a wearable and Li-ion batteries can explode when punctured or improperly used, the electronics are placed in a firm junction box mounted in the top of the head. The top will only be covered with a thin wooden sheet, covered in black fabric.

Hat control

 

The hat is controlled with a python user interface, which runs in the app pythonista. This app has a toolbox for creating interfaces which are linked to your python script. In this case it is used to:

 

  • Select a mode
  • Set R, G, B color intensity
  • Set W color intensity in case of RGBW ledstrip
  • Set velocity (effects parameter)

 

The code also registers the iPhone’s gravity vector which can be used to control the parameters for some effects. This input can for example be used for a game in which two users both connect to the hat and moving red vs green leds by shaking the iPhone in a specific direction.

 

Next the data is sent in an UDP message over wifi to the hat. This UDP message has a standardised Artnet datagram and UDP port. This is used in the industry so the code is compatible with many light controllers. Also there are Artnet libraries for boards like Arduino and raspberry pi which makes all your projects more compatible with existing projects.

 

When the hat cannot connect to wifi, the hat will play at random changing its effects (so mode, color and velocity) every 5 seconds making it a nice addition to the light show of any party. Although it might be a bit annoying for the people standing directly behind you.

 

The next features I want to add:

 

  • When the hat cannot connect to wifi, it will setup a wifi acces point so you can still control it with your phone even when you’re not at home (unless you bring your wifi router to every occasion).
  • Create more sound-light interfacing patterns

Beat detection

In the video below the hat is responding to music. The BPM detection code uses an input value of a low frequency band-pass filtered sound value. The current time in milliseconds is stored as long as it’s equal to zero (t_cur[0]). A moving average of 1,5 seconds is used to calculate a threshold value, when the input value exceeds the threshold, the current time (t_cur[i]) is stored. This process is repeated and a total of 10 time samples are saved.

The average difference of time samples added to the average time of the samples results is a pretty accurate prediction of the next beat. As soon a a detectable beat is hearable, it takes about 1 to 3 seconds for the hat to adjust.