# LEGO Head Lamp with Audio

## Overview

https://youtu.be/-XaAH0k4D68

## Interactive Lamp Projects

Build a light up LEGO head with Adafruit's Circuit Playground Express! 3D print the parts and make an interactive lamp that plays sound clips from the LEGO movie. It uses the built-in accelerometer to do shake detect to&nbsp;play audio wave files and animate NeoPixels.

![3d_printing_cpx-lego-head-1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/286/medium640/3d_printing_cpx-lego-head-1.jpg?1569181965)

## CircuitPython

Use Adafruit's&nbsp;CircuitPython to program the NeoPixels and play audio files. You can use the code to create your own sketches with flash animations with shake-to-play audio wave files.

![3d_printing_mu-editor.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/287/medium640/3d_printing_mu-editor.jpg?1569182327)

## STEMMA Speaker for Circuit Playground

Use the STEMMA speaker to easily add audio to the Circuit Playground Express. With the 3-wire JST alligator cable, you can quickly connect and no soldering is necessary!

![3d_printing_cpx-stemma.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/274/medium640/3d_printing_cpx-stemma.jpg?1569075472)

## Parts
All you need to build this project is a Circuit Playground Express, STEMMA Speaker, 3-wire JST alligator cable and a microUSB cable. We think this makes a great kit for a classroom setting!

![3d_printing_parts-angle.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/288/medium640/3d_printing_parts-angle.jpg?1569182642)

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

### Adafruit STEMMA Speaker - Plug and Play Audio Amplifier

[Adafruit STEMMA Speaker - Plug and Play Audio Amplifier](https://www.adafruit.com/product/3885)
Hey, have you heard the good news? With Adafruit STEMMA boards you can easily and safely plug sensors and devices together, like this **Adafruit STEMMA Speaker - Plug and Play Audio Amplifier**. Like the name implies, it's got a class D audio amplifier on board, and our...

In Stock
[Buy Now](https://www.adafruit.com/product/3885)
[Related Guides to the Product](https://learn.adafruit.com/products/3885/guides)
![Top view of a black speaker breakout board connected to a round microcontroller via alligator clips. A music note animation emits from the speaker.](https://cdn-shop.adafruit.com/product-videos/640x480/3885-05.jpg)

### JST PH 2mm 3-pin Plug to Color Coded Alligator Clips Cable

[JST PH 2mm 3-pin Plug to Color Coded Alligator Clips Cable](https://www.adafruit.com/product/4030)
This cable will let you turn a JST PH 3-pin cable port into 3 individual wires with grippy mini alligator clips. We're carrying these to match up with any of our boards or breakouts with 3-pin 'STEMMA' connectors on them such as the&nbsp;<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4030)
[Related Guides to the Product](https://learn.adafruit.com/products/4030/guides)
![Angled shot of JST PH 3-pin Plug to Color Coded Alligator Clips Cable.](https://cdn-shop.adafruit.com/640x480/4030-04.jpg)

### Fully Reversible Pink/Purple USB A to micro B Cable - 1m long

[Fully Reversible Pink/Purple USB A to micro B Cable - 1m long](https://www.adafruit.com/product/4111)
This cable is not only super-fashionable, with a woven pink and purple Blinka-like pattern, it's also fully reversible! That's right, you will save _seconds_ a day by not having to flip the cable around.

First let's talk about the cover and over-molding. We got these...

In Stock
[Buy Now](https://www.adafruit.com/product/4111)
[Related Guides to the Product](https://learn.adafruit.com/products/4111/guides)
![Fully Reversible Pink/Purple USB A to micro B Cable](https://cdn-shop.adafruit.com/640x480/4111-02.jpg)

### Filament for 3D Printers in Various Colors and Types

[Filament for 3D Printers in Various Colors and Types](https://www.adafruit.com/product/2080)
Having a 3D printer without filament is sort of like having a regular printer without paper or ink. &nbsp;And while a lot of printers come with some filament there's a good chance you've been printing up a storm and need something new. &nbsp;That's why we've started carrying a...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2080)
[Related Guides to the Product](https://learn.adafruit.com/products/2080/guides)
![Filament for 3D printers in various colors and types stacked together.](https://cdn-shop.adafruit.com/640x480/2080-07.jpg)

# LEGO Head Lamp with Audio

## Circuit Diagram

## Circuit&nbsp;Diagram

This provides a visual reference for wiring of the components. They aren't true to scale, just meant to be used as reference. This diagrams was created using [Fritzing software](http://fritzing.org/download/).

## Adafruit Library for Fritzing

Use our Fritzing parts library to create circuit diagrams for your projects. Download the library or just grab the individual parts. Get library and parts from [GitHub Adafruit Fritzing Parts](https://github.com/adafruit/Fritzing-Library/tree/master/parts).

![](https://cdn-learn.adafruit.com/assets/assets/000/081/149/medium800/3d_printing_circuit-diagram-stemma-speaker.jpg?1568999097)

## Wired Connections

Plug in the 3-wire JST-PH cable to the port on the STEMMA Speaker. Reference the circuit diagram and follow the colored wired to properly connect the STEMMA speaker to the CPX.

- **Black wire** from STEMMA Speaker to **GND** on CPX
- **White wire** from STEMMA Speaker to **A0** on CPX
- **Red wire** from STEMMA Speaker to **VOUT** on CPX

## USB Power

The Circuit Playground Express can stay connected via USB power from either a 5V wall adapter or computer's hub. This allows the project to stay on as long as you want (nightlight mode anyone?). The project has a cutaway to accommodate for a USB cable.

# LEGO Head Lamp with Audio

## Code

![](https://cdn-learn.adafruit.com/assets/assets/000/081/279/medium800/3d_printing_mu-banner.jpg?1569091674)

## Setup Circuit Playground Express with CircuitPython

We'll need to get our board setup so we can run CircuitPython code. Let's walk through these steps to get the latest version of CircuitPython onto your board.&nbsp;

## **Quick Start**

- Download the CircuitPython UF2 for Circuit Playground Express
- Connect Circuit Playground Express to your computer over USB and press the Reset button
- Drag-n-drop the CircuitPython **UF2** onto the&nbsp; **CPLAYBOOT** drive - the drive will vanish and a new **CIRCUITPY** drive should appear.
- Copy code and library files to the&nbsp; **CIRCUITPY** drive

![3d_printing_circuitpython-org.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/281/medium640/3d_printing_circuitpython-org.jpg?1569091829)

[Download Latest Circuit Python for Circuit Playground Express](https://circuitpython.org/board/circuitplayground_express/)
# Download Adafruit CircuitPython Library Bundle

In order to run the code, we'll need to download a library. The download linked below will contain all the libraries available for CircuitPython. To run the code for this project, we only need the Adafruit Circuit Playground library. Unzip the library bundle and search for the library. Drag and drop it onto a folder named **lib** on the **CIRCUITPY** drive (create the folder if it is not already on the Circuit Playground Express).

## Required Library&nbsp;

- Adafruit Circuit Playground –&nbsp; **adafruit\_circuitplayground**

[Download Latest Circuit Python Library Bundle](https://circuitpython.org/libraries)
## Upload Code

Click on the download link below to grab the main code directly from GitHub. Rename the file to **code.py** &nbsp;and drop it onto the **CIRCUITPY** &nbsp;main (root) directory. Create a new folder on the **CIRCUITPY** drive and name it **sounds**. Upload the audio files to that folder. The code will run properly when all of the files have been uploaded.

Use any text editor or favorite IDE to modify the code. We suggest using the Mu Python Editor. See below for more on Mu.

## Audio Files

The sounds used in this project&nbsp;were sampled from the LEGO movie. This pack contains sounds that are already in the supported audio format.

Adafruit CircuitPython supports **16-bit, Mono, 22.050kHz .wav** audio format. [See this guide](https://learn.adafruit.com/microcontroller-compatible-audio-file-conversion) to help format any audio files you might want to use in this project besides the files provided.

## Mu Python Editor

Mu is a simple Python editor that works with Adafruit CircuitPython hardware.&nbsp;It's written in Python and works on Windows, MacOS, Linux and Raspberry Pi. The serial console is built right in so you get immediate feedback from your board's serial output!

[Installing and Using the Mu Editor](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor)
[Download Project Zip from GitHub](https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/LEGO_Head_CPX_Lamp/)
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/LEGO_Head_CPX_Lamp/code.py

## Loop and Play

The `play_wav` function handles the location of the wav files. In this project, the audio wav files reside in a folder named **sounds**. The `party_flash` and `led_flash`&nbsp;animations use `cpx.pixels.fill` to quickly flash between two colors.

In the loop, we set the shake sensitivity and print a statement to let us know when a shake is detected. A variable is setup and starts a counter which increments each time a shake has been detected. This way we can play different audio files each time we shake it. In this project, we'll play up to five different wave files.

- On the first shake, we play **haha.wav** and turn all NeoPixels white for one second.&nbsp;
- One second shake, we play **awe-a.wav** and run&nbsp;`party_flash`&nbsp;3 times, 0.4 seconds each flash.
- On third shake, we play **awe-b.wav** and run `part_flash`&nbsp;3 times, 0.4 seconds each flash.
- On fourth sake, we play **awe-c.wav** &nbsp;and run the `part_flash`&nbsp;3 times, 0.4 seconds each flash.
- On fifth sake, we set the counter to 0, play **untz.wav** and run `led_flash` 3 times, 0.18 seconds each flash.

## Customization Options

Minor adjustments can be made to customize the functionality. Change the colors of the NeoPixels by adding your own values, just look for the RGB numbers in the code. If you want the lights to always stay on, look for the last line that uses `cpx.pixels.fill` and adjust the RGB values. Rearrange the animations and adjust the timing to match your audio files.

# LEGO Head Lamp with Audio

## 3D Printing

## 3D Parts

STL files for 3D printing are oriented to print "as-is" on FDM style machines. Original design source may be downloaded using the links below.

![3d_printing_3d-parts.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/273/medium640/3d_printing_3d-parts.jpg?1569075418)

## Slicing Parts

We 3D Printed the head with dual-extrusion so the eyes and mouth are dark when the lights are on. The model has a thin shell and it can be printed without any support material.

- PLA filament 220c extruder
- 0.2 layer height
- 10% gyroid infill
- 60mm/s print speed
- 60c heated bed

![3d_printing_cura-dual.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/289/medium640/3d_printing_cura-dual.jpg?1569189539)

## Snap Fit Mount

The Circuit Playground Express PCB snap fits into the case. Insert the PCB at an angle to fit under the snap. Line up the stand offs with the mounting holes and press down to snap fit.

![3d_printing_cura-cpx.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/290/medium640/3d_printing_cura-cpx.jpg?1569190482)

## Threaded Parts

The top and bottom parts are designed to screw into the LEGO head.&nbsp; The threads are mostly angled surfaces that 3D print without support material.

![3d_printing_cura-stud-thread.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/292/medium640/3d_printing_cura-stud-thread.jpg?1569190861)

## Design Source Files

The project assembly was designed in Fusion 360.&nbsp;This can be downloaded in different formats like STEP, SAT and more. Electronic components like Adafruit's board, displays, connectors and more can be downloaded from our&nbsp;[Adafruit CAD parts GitHub Repo](https://github.com/adafruit/Adafruit_CAD_Parts).

![3d_printing_stemma-speaker.gif](https://cdn-learn.adafruit.com/assets/assets/000/081/315/medium640thumb/3d_printing_stemma-speaker.jpg?1569266366)

## CAD Assembly

The components were modeled in the cad assembly. This animation shows where the electronics are mounted and how the top and bottom screw into the base.

![3d_printing_lego-head-cad-explode.gif](https://cdn-learn.adafruit.com/assets/assets/000/081/325/medium640thumb/3d_printing_lego-head-cad-explode.jpg?1569330920)

[Download CAD files from GitHub](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/LEGO_Head_CPX_Lamp)
[Adafruit CAD Parts on GitHub](https://github.com/adafruit/Adafruit_CAD_Parts)
[Fusion 360 Project Share Link](https://a360.co/2kCcZfD)
## Speaker Holder

The speaker holder will need to be super glued to the top stud. The speaker holder should be centered with the speaker grill on the top stud.&nbsp;

![3d_printing_stud-holder-placement.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/275/medium640/3d_printing_stud-holder-placement.jpg?1569086845)

## Press Fit Speaker

The speaker is press fitted into the holder and has a snug fit. The sticky adhesive is not necessary so you can leave the protective backing on.

![3d_printing_stud-speaker-installed.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/278/medium640/3d_printing_stud-speaker-installed.jpg?1569086922)

## Installed Speaker Holder

A small notch in the speaker holders lip accommodates for clearances for the pico connector and wiring.

![3d_printing_stud-speaker-press-fit.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/276/medium640/3d_printing_stud-speaker-press-fit.jpg?1569086869)

## Speaker Placement

The placement of the speaker sounds best when it's facing up, pressed up against the speaker grill. When the top is installed the LEGO head sound is amplified and much louder.

![3d_printing_stud-speaker-top.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/277/medium640/3d_printing_stud-speaker-top.jpg?1569086900)

## Install CPX to Mount

Orient the USB port with the opening and line up the mounting holes. Insert the PCB at an angle to get underneath the snaps. Firmly press down to snap fit the PCB into place.&nbsp;

![3d_printing_cpx-mount-install.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/293/medium640/3d_printing_cpx-mount-install.jpg?1569194568)

## Connect the Alligator Clips to CPX

Grab the JST cable and connect the alligator clips to pads. **Red** to **3.3V** pad, white to **A0** pad and **black** to **GND** pad. Be sure the alligator clips have a sturdy grip on the Circuit Playground Express.

![3d_printing_cpx-gators.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/294/medium640/3d_printing_cpx-gators.jpg?1569194588)

## Install Case to Head

Insert the JST cable into the bottom of the LEGO head and screw in the case.&nbsp;

![3d_printing_bottom-installing.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/295/medium640/3d_printing_bottom-installing.jpg?1569194619)

## Connect STEMMA Speaker

Grab the JST cable from top of LEGO head and plug it into the JST port on the STEMMA Speaker.

![3d_printing_jst-stemma-plugin.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/296/medium640/3d_printing_jst-stemma-plugin.jpg?1569194659)

## Install Top to Head

Twist the top stud onto the LEGO head to install it.

![3d_printing_top-installed.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/297/medium640/3d_printing_top-installed.jpg?1569194735)

## USB Power

Use a microUSB cable with a data line. You can leave it plugged into a 5V USB wall adapter or computer's USB hub.

![3d_printing_usb-power-plugin.jpg](https://cdn-learn.adafruit.com/assets/assets/000/081/298/medium640/3d_printing_usb-power-plugin.jpg?1569194782)


## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Adafruit STEMMA Speaker - Plug and Play Audio Amplifier

[Adafruit STEMMA Speaker - Plug and Play Audio Amplifier](https://www.adafruit.com/product/3885)
Hey, have you heard the good news? With Adafruit STEMMA boards you can easily and safely plug sensors and devices together, like this **Adafruit STEMMA Speaker - Plug and Play Audio Amplifier**. Like the name implies, it's got a class D audio amplifier on board, and our...

In Stock
[Buy Now](https://www.adafruit.com/product/3885)
[Related Guides to the Product](https://learn.adafruit.com/products/3885/guides)
### JST PH 2mm 3-pin Plug to Color Coded Alligator Clips Cable

[JST PH 2mm 3-pin Plug to Color Coded Alligator Clips Cable](https://www.adafruit.com/product/4030)
This cable will let you turn a JST PH 3-pin cable port into 3 individual wires with grippy mini alligator clips. We're carrying these to match up with any of our boards or breakouts with 3-pin 'STEMMA' connectors on them such as the&nbsp;<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4030)
[Related Guides to the Product](https://learn.adafruit.com/products/4030/guides)
### Fully Reversible Pink/Purple USB A to micro B Cable - 1m long

[Fully Reversible Pink/Purple USB A to micro B Cable - 1m long](https://www.adafruit.com/product/4111)
This cable is not only super-fashionable, with a woven pink and purple Blinka-like pattern, it's also fully reversible! That's right, you will save _seconds_ a day by not having to flip the cable around.

First let's talk about the cover and over-molding. We got these...

In Stock
[Buy Now](https://www.adafruit.com/product/4111)
[Related Guides to the Product](https://learn.adafruit.com/products/4111/guides)
### Filament for 3D Printers in Various Colors and Types

[Filament for 3D Printers in Various Colors and Types](https://www.adafruit.com/product/2080)
Having a 3D printer without filament is sort of like having a regular printer without paper or ink. &nbsp;And while a lot of printers come with some filament there's a good chance you've been printing up a storm and need something new. &nbsp;That's why we've started carrying a...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2080)
[Related Guides to the Product](https://learn.adafruit.com/products/2080/guides)
### Monoprice Inventor II 3D Printer with Touchscreen and WiFi

[Monoprice Inventor II 3D Printer with Touchscreen and WiFi](https://www.adafruit.com/product/3897)
The **Monoprice Inventor II 3D Printer Touchscreen with WiFi** is a perfect entry-level 3D printer with small footprint and reliable performance. It comes equipped with WiFi so you don't need a cable tether and a lovely 3.5" color touchscreen for settings & feedback....

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3897)
[Related Guides to the Product](https://learn.adafruit.com/products/3897/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Adafruit STEMMA Speaker](https://learn.adafruit.com/adafruit-stemma-speaker.md)
- [Welcome to CircuitPython!](https://learn.adafruit.com/welcome-to-circuitpython.md)
- [Drone Claw](https://learn.adafruit.com/drone-claw.md)
- [HalloWing Light Paintstick](https://learn.adafruit.com/hallowing-light-paintstick.md)
- [Cam Follower Automaton](https://learn.adafruit.com/cam-follower-automaton.md)
- [CircuitPython 101: Basic Builtin Data Structures](https://learn.adafruit.com/basic-datastructures-in-circuitpython.md)
- [Labo RC Car Action Lights](https://learn.adafruit.com/labo-rc-car-underglow.md)
- [Edit CircuitPython Code on iOS with Runestone](https://learn.adafruit.com/editing-circuitpython-on-ios-with-runestone.md)
- [Unicorn Christmas Stocking with Rainbow Lights & Sound](https://learn.adafruit.com/unicorn-christmas-stocking-with-lights-sound.md)
- [Circuit Playground Morse Code Flasher](https://learn.adafruit.com/circuitplayground-morse-code-flasher-makecode-circuit-python.md)
- [Glowing Slime Lunchbox](https://learn.adafruit.com/glowing-slime-lunchbox.md)
- [Animatronic Glowing Medusa Headdress](https://learn.adafruit.com/animatronic-glowing-medusa-headdress.md)
- [UFO Flying Saucer with Circuit Playground Express](https://learn.adafruit.com/ufo-circuit-playground-express.md)
- [Mu Keyboard Shortcut Cheat Sheets](https://learn.adafruit.com/mu-keyboard-shortcut-cheat-sheets.md)
- [How to Use Video Greenscreen in MakeCode](https://learn.adafruit.com/how-to-use-video-greenscreen-in-makecode.md)
- [Using the Android CircuitPython Editor](https://learn.adafruit.com/using-the-android-circuitpython-editor.md)
