# LEGO Set Lighting

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/126/333/medium800thumb/hacks_wintervillage01.jpg?1701217032)

https://youtu.be/eMm64RiZa1A

Light up your LEGO buildings, spacecraft, vehicles, and more with super-tiny LEDs. Sets such as the Winter Village and Star Wars lines are just begging to have lights added. You can add lights exactly where you want using pre-wired SMD LEDs. Then, you can animate them to blink, dim, and flicker exactly as you like using CircuitPython.

Or, you can keep it very simple using an on/off coin cell battery holder breakout.

![](https://cdn-learn.adafruit.com/assets/assets/000/126/334/medium800/hacks_legoled-5919.jpg?1701217068)

## Other Resources

If you want to mount your QT Py to LEGO bricks, check out this great [Ruiz Brothers guide](https://learn.adafruit.com/qt-py-lego).

And for other STEMMA QT board mounting, [this guide](https://learn.adafruit.com/stemma-lego-base-plates) has you covered.

While working on this guide, I found an excellent and very similar tutorial on BrickNerd! [Check it out](https://bricknerd.com/home/diy-lego-lighting-creating-custom-light-effects-11-9-23), especially for some more ideas on doing an amazing cyberpunk-style lighting setup.

## Parts
### Adafruit QT Py RP2040

[Adafruit QT Py RP2040](https://www.adafruit.com/product/4900)
What a cutie pie! Or is it... a QT Py?&nbsp;This diminutive dev board comes with one of our new favorite chip, the RP2040. It's been made famous in the new [Raspberry Pi Pico](https://www.adafruit.com/pico) _and_ our [Feather...](http://www.adafruit.com/product/4884)

In Stock
[Buy Now](https://www.adafruit.com/product/4900)
[Related Guides to the Product](https://learn.adafruit.com/products/4900/guides)
![Video of hand holding a QT Py PCB in their hand. An LED glows rainbow colors.](https://cdn-shop.adafruit.com/product-videos/640x480/4900-06.jpg)

### Adafruit AW9523 GPIO Expander and LED Driver Breakout

[Adafruit AW9523 GPIO Expander and LED Driver Breakout](https://www.adafruit.com/product/4886)
Expand your project possibilities, with the **Adafruit AW9523 GPIO Expander and LED Driver Breakout** - a cute and powerful I2C expander with a lot of tricks up its sleeve.

GPIO expanders work like this: you have a board with some number of GPIO but not enough for your...

In Stock
[Buy Now](https://www.adafruit.com/product/4886)
[Related Guides to the Product](https://learn.adafruit.com/products/4886/guides)
![Video of a Adafruit AW9523 GPIO Expander and LED Driver Breakout connected to a LED changing colors. ](https://cdn-shop.adafruit.com/product-videos/640x480/4886-05.jpg)

### Miniature Wired LEDs - 0805 SMT LED - Warm White - 5 pack

[Miniature Wired LEDs - 0805 SMT LED - Warm White - 5 pack](https://www.adafruit.com/product/5487)
These ultra-fine wired LEDs are designed for model-makers and crafters but they could also come in handy for folks who want to add tiny points of light to a design without needing SMT-level soldering skills.&nbsp;Use this five-pack bundle of **Miniature&nbsp;Wired 0805 LEDs** for...

In Stock
[Buy Now](https://www.adafruit.com/product/5487)
[Related Guides to the Product](https://learn.adafruit.com/products/5487/guides)
![Video of a five mini wired LEDs emitting warm white light against a white open palm.](https://cdn-shop.adafruit.com/product-videos/640x480/5487-03.jpg)

### Part: Miniature Wired LEDs - 0805 SMT LED - Green
quantity: 1
5 pack
[Miniature Wired LEDs - 0805 SMT LED - Green](https://www.adafruit.com/product/5489)

### Part: Miniature Wired LEDs - 0805 SMT LED - Blue
quantity: 1
5 pack
[Miniature Wired LEDs - 0805 SMT LED - Blue](https://www.adafruit.com/product/5490)

### Part: Miniature Wired LEDs - 0805 SMT LED Pink
quantity: 1
5 pack
[Miniature Wired LEDs - 0805 SMT LED Pink](https://www.adafruit.com/product/5488)

### Part: Miniature Wired LEDs - 0805 SMT LED - Purple
quantity: 1
5 pack
[Miniature Wired LEDs - 0805 SMT LED - Purple](https://www.adafruit.com/product/5491)

### Part: Lithium Coin Cell Battery
quantity: 1
CR2032
[Lithium Coin Cell Battery](https://www.adafruit.com/product/654)

### Part: STEMMA QT / Qwiic JST SH 4-pin Cable
quantity: 1
100mm Long
[STEMMA QT / Qwiic JST SH 4-pin Cable](https://www.adafruit.com/product/4210)

### Part: 1.25mm Pitch 2-pin Cable Matching Pair
quantity: 16
40cm long
[1.25mm Pitch 2-pin Cable Matching Pair](https://www.adafruit.com/product/4720)

### Part: Multi-Colored Heat Shrink Pack
quantity: 1
3/32" + 1/8" + 3/16" Diameters
[Multi-Colored Heat Shrink Pack](https://www.adafruit.com/product/1649)

### Simple Setup / Testing

A coin cell breakout it great for quickly testing LEDs or for doing a simple on/off lighting setup.

### 20mm Coin Cell Breakout w/On-Off Switch (CR2032)

[20mm Coin Cell Breakout w/On-Off Switch (CR2032)](https://www.adafruit.com/product/1871)
Simple but effective - this sewable breakout board has a CR2032 coin cell battery holder soldered on, an on/off switch and 0.1" pitch breakout pins for easy connecting. Great for powering very low-current projects, or adding a battery backup circuit to a real-time-clock or similar....

In Stock
[Buy Now](https://www.adafruit.com/product/1871)
[Related Guides to the Product](https://learn.adafruit.com/products/1871/guides)
![Angled shot of a 20mm Coin Cell Breakout Board w/ On-Off Switch.](https://cdn-shop.adafruit.com/640x480/1871-08.jpg)

## LEGO Set

Any LEGO creation you build that includes translucent plates and bricks can be lit up with these techniques.

If you want to start with an official model, the models in [Winter Village line](https://www.lego.com/en-us/categories/adults-welcome/every-set-in-lego-winter-village-collection) is are excellent candidates for cozy lighting, as are any of the [modular buildings](https://www.lego.com/en-us/product/jazz-club-10312).

The [Razor Crest](https://www.lego.com/en-us/product/the-razor-crest-75292) is one of the many, many, many [Star Wars](https://www.lego.com/en-us/themes/star-wars) ships that light up nicely.

![hacks_bakery.png](https://cdn-learn.adafruit.com/assets/assets/000/126/341/medium640/hacks_bakery.png?1701218874)

![hacks_jazzclub.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/342/medium640/hacks_jazzclub.jpg?1701218892)

![hacks_razor.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/344/medium640/hacks_razor.jpg?1701218972)

# LEGO Set Lighting

## Test Lighting with a Coin Cell

The wired SMD LEDs are designed to run off of 3VDC, so you can easily power them from a 3V coin cell battery.

The holder with switch make it simple to get started (and to test LEDs for color).

The **blue** wires connect to either **GND** pad, and the **red** wires connect to the **Sw** pad if you want to use the on/off switch. The **On** pad is always on regardless of switch position.

![hacks_legoled-5876.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/335/medium640/hacks_legoled-5876.jpg?1701217148)

![hacks_legoled-5877.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/336/medium640/hacks_legoled-5877.jpg?1701217159)

![hacks_legoled-5880.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/337/medium640/hacks_legoled-5880.jpg?1701217173)

## Semi-Permanent Solderless Wiring

While soldering would be ideal, you can get by pretty well by twisting together all of the blue wires to each other and all of the red wires to each other of multiple LEDs and then twisting these bundles in the coin cell breakout pads as shown here.

This is a parallel circuit, in which each LED draws the full power, so there's no need to worry about dimming as in a series circuit.

![hacks_legoled-5884.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/347/medium640/hacks_legoled-5884.jpg?1701219885)

![hacks_legoled-5885.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/348/medium640/hacks_legoled-5885.jpg?1701219911)

![hacks_legoled-5890.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/349/medium640/hacks_legoled-5890.jpg?1701220091)

![hacks_legoled-5889.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/350/medium640/hacks_legoled-5889.jpg?1701220109)

# LEGO Set Lighting

## Mounting LEDs in Bricks and Plates

![](https://cdn-learn.adafruit.com/assets/assets/000/126/338/medium800/hacks_legoled-5921.jpg?1701217224)

## Wire Insertion

The great thing about these wired LEDs is that the wires are so thin you can put the lights where you need and still stack plates and bricks right over the wires.

Here, the LEDs are fed through the open stud round plate though the bottom.

Once the LED is where you want it, press the round plate back onto the stud below.

Then, the red translucent round plate is placed over top.

![hacks_legoled-5891.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/351/medium640/hacks_legoled-5891.jpg?1701220152)

![hacks_legoled-5893.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/352/medium640/hacks_legoled-5893.jpg?1701220168)

![hacks_legoled-5894.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/353/medium640/hacks_legoled-5894.jpg?1701220183)

![hacks_legoled-5896.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/354/medium640/hacks_legoled-5896.jpg?1701220198)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/355/medium800/hacks_legoled-5898.jpg?1701220481)

## Battery Pack

Find a central location to hide the battery pack and switch -- I chose this spot under the removable escape pod.

![hacks_legoled-5923.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/360/medium640/hacks_legoled-5923.jpg?1701220740)

![hacks_legoled-5925.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/361/medium640/hacks_legoled-5925.jpg?1701220793)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/366/medium800/hacks_legoled-5927.jpg?1701221098 Is he hiding the battery in there?)

## Wiring Plan

I then roughly placed the lights in their desired location before feeding them through the model. This build has two red headlights (pink LEDs under translucent red round plates), two blue engine lights, and one green cockpit light.

![hacks_legoled-5905.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/383/medium640/hacks_legoled-5905.jpg?1701222126)

![hacks_legoled-5907.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/384/medium640/hacks_legoled-5907.jpg?1701222147)

## Part Fitting

Add LEDs to both of the front lights on the ship as shown here, then fit the assembly back onto the ship.

![hacks_legoled-5901.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/356/medium640/hacks_legoled-5901.jpg?1701220548)

![hacks_legoled-5902.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/357/medium640/hacks_legoled-5902.jpg?1701220601)

![hacks_legoled-5904.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/358/medium640/hacks_legoled-5904.jpg?1701220625)

![hacks_legoled-5908.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/359/medium640/hacks_legoled-5908.jpg?1701220637)

## Engine Lighting

With the battery in a central location, I lifted strategic plates to sneak the wiring through to the engines, then placed the plates back, being careful to route the wire between studs.

&nbsp;

![hacks_legoled-5911.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/368/medium640/hacks_legoled-5911.jpg?1701221703)

![hacks_legoled-5912.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/369/medium640/hacks_legoled-5912.jpg?1701221737)

![hacks_legoled-5913.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/370/medium640/hacks_legoled-5913.jpg?1701221747)

![hacks_legoled-5914.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/371/medium640/hacks_legoled-5914.jpg?1701221829)

Then, I covered the blue LEDs with a clear translucent square plate and affixed to a stud.

![hacks_legoled-5915.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/372/medium640/hacks_legoled-5915.jpg?1701221849)

![hacks_legoled-5916.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/373/medium640/hacks_legoled-5916.jpg?1701221856)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/380/medium800/hacks_legoled-5917.jpg?1701221914 That wire could be hidden more effectively...)

## Cockpit Light

The cockpit light is angled up to illuminate Baby Yoda.

![hacks_legoled-5920.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/382/medium640/hacks_legoled-5920.jpg?1701222070)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/399/medium800/hacks_legoled-5919.jpg?1701222685)

Next, let's look at creating an animated lighting display using CircuitPython and a constant current driver to control the LEDs.

# LEGO Set Lighting

## Build the Lighting Circuit

![](https://cdn-learn.adafruit.com/assets/assets/000/126/339/medium800/hacks_lego_light_fritz.jpg?1701217346)

You can gain lots of control over your LEGO lighting by using a microcontroller and constant current driver board. This will allow you to create individualized brightness levels and animated effects for 16 LEDs (more if you use additional driver boards, for a total of 64 LEDs per I2C port).

## Wiring Cables

You'll use the AW9523 driver breakout to control the LEDs. The board is conveniently designed with 16 pairs of pads for the connections to your LEDs. You can wire the LEDs directly ( **red** to **Vin** , blue to numbered GPIO current-source pin), or do some extra wiring prep to make detachable wiring cable assemblies.

![hacks_legoled-5767.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/407/medium640/hacks_legoled-5767.jpg?1701226251)

### Cable Assembly

Twist the wire pairs to make them more managable. You can do this by hand or chuck them into a drill for speed.

Then, add a short length of heat shrink tubing to each LED wire.

Solder the red LED wire to the red wire of the smaller female JST cable, and the blue LED wire to the black wire of the JST cable.&nbsp;

![hacks_legoled-5769.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/408/medium640/hacks_legoled-5769.jpg?1701226611)

![hacks_legoled-5771.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/409/medium640/hacks_legoled-5771.jpg?1701226797)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/413/medium800/hacks_legoled-5770.jpg?1701226840)

### Board Cable

Next, solder the larger male JST cable to the driver board. The **black** wire will go to pin **0** , the **red** to the associated **Vin** pad.

![hacks_legoled-5772.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/414/medium640/hacks_legoled-5772.jpg?1701226856)

![hacks_legoled-5773.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/415/medium640/hacks_legoled-5773.jpg?1701226918)

![hacks_legoled-5775.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/416/medium640/hacks_legoled-5775.jpg?1701226949)

Plug the cable ends together to connect.

![](https://cdn-learn.adafruit.com/assets/assets/000/126/417/medium800/hacks_legoled-5778.jpg?1701227060)

You can now create cable assemblies for all of the LEDs you want to use. It can be helpful to add color-coded heat shrink tubing to help differentiate the lights.

![hacks_legoled-5790.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/418/medium640/hacks_legoled-5790.jpg?1701227085)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/419/medium800/hacks_legoled-5795.jpg?1701227369)

## Lamp Lighting

To light up a typical Winter Village lamp, simply run the LED up through the base as shown.

The translucent yellow brick will look amazing when illuminated from within!

![hacks_legoled-5779.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/420/medium640/hacks_legoled-5779.jpg?1701227391)

![hacks_legoled-5780.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/421/medium640/hacks_legoled-5780.jpg?1701227285)

![hacks_legoled-5781.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/422/medium640/hacks_legoled-5781.jpg?1701227419)

![hacks_legoled-5782.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/423/medium640/hacks_legoled-5782.jpg?1701227419)

Here's what the lamp will look like when we plug the board into the QT Py and code it with CircuitPython in the next pages.

![](https://cdn-learn.adafruit.com/assets/assets/000/126/426/medium800/hacks_legoled-5786.jpg?1701289838)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/425/medium800/hacks_legoled-5788.jpg?1701289243)

Now you will connect the driver board to your QT Py via the STEMMA QT cable and code it.

# LEGO Set Lighting

## Easy Codeless Installation

If you want to get set up quickly with the same lighting patterns I used in my model, all you have to do is copy a single file onto the QT Py RP2040. You won't be able to customize this file, but it is as easy as drag-and-drop to get started.

To customize, check out the **CircuitPython** and **Code the Lighting Controller** pages.

[Download LEGO Lighting .uf2](https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/main/LEGO_Lighting/lego_lighting_01.uf2)
![](https://cdn-learn.adafruit.com/assets/assets/000/126/446/medium800/hacks_adafruit_products_QTRP_buttons.jpg?1701282721)

To enter the bootloader, hold down the&nbsp; **BOOT/**** BOOTSEL button&nbsp;**(highlighted in red above), and while continuing to hold it (don't let go!), press and release the&nbsp;**reset button&nbsp;**(highlighted in blue above).&nbsp;**Continue to hold the BOOT/BOOTSEL button until the RPI-RP2 drive appears!**

If the drive does not appear, release all the buttons, and then repeat the process above.

You can also start with your board unplugged from USB, press and hold the BOOTSEL button (highlighted in red above), continue to hold it while plugging it into USB, and wait for the drive to appear before releasing the button.

A lot of people end up using charge-only USB cables and it is very frustrating! **&nbsp;Make sure you have a USB cable you know is good for data sync.**

Warning: 

You will see a new disk drive appear called&nbsp; **RPI-RP2**.

Drag the&nbsp; **lego\_lighting\_01.uf2** &nbsp;file to&nbsp; **RPI-RP2.&nbsp;**

The&nbsp; **RPI-RP2** &nbsp;drive will disappear and a new disk drive called&nbsp; **CIRCUITPY** &nbsp;will appear.

That's it, you're done, the lights will automatically start running.

Primary: 

[Create All-in-One UF2's with CIRCUITPY Files on RP2040](https://adafruit-playground.com/u/danhalbert/pages/create-all-in-one-uf2-s-with-circuitpy-files-on-rp2040)
# LEGO Set Lighting

## CircuitPython

[CircuitPython](https://github.com/adafruit/circuitpython) is a derivative of [MicroPython](https://micropython.org) designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the **CIRCUITPY** drive to iterate.

## CircuitPython Quickstart

Follow this step-by-step to quickly get CircuitPython running on your board.

[Download the latest version of CircuitPython for this board via circuitpython.org](https://circuitpython.org/board/adafruit_qtpy_rp2040/)
 **Click the link above to download the latest CircuitPython UF2 file.**

Save it wherever is convenient for you.

![install_circuitpython_on_rp2040_RP2040_UF2_downloaded.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/655/medium640/install_circuitpython_on_rp2040_RP2040_UF2_downloaded.jpg?1618943202)

![](https://cdn-learn.adafruit.com/assets/assets/000/101/680/medium800/adafruit_products_QTRP_buttons.jpg?1618956837)

To enter the bootloader, hold down the **BOOT/**** BOOTSEL button**(highlighted in red above), and while continuing to hold it (don't let go!), press and release the**reset button**(highlighted in red or blue above).&nbsp;**Continue to hold the BOOT/BOOTSEL button until the RPI-RP2 drive appears!**

If the drive does not appear, release all the buttons, and then repeat the process above.

You can also start with your board unplugged from USB, press and hold the BOOTSEL button (highlighted in red above), continue to hold it while plugging it into USB, and wait for the drive to appear before releasing the button.

A lot of people end up using charge-only USB cables and it is very frustrating! **Make sure you have a USB cable you know is good for data sync.**

You will see a new disk drive appear called **RPI-RP2**.

&nbsp;

Drag the **adafruit\_circuitpython\_etc.uf2** file to **RPI-RP2.**

![install_circuitpython_on_rp2040_RP2040_bootloader_drive.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/656/medium640/install_circuitpython_on_rp2040_RP2040_bootloader_drive.jpg?1618943666)

![install_circuitpython_on_rp2040_RP2040_drag_UF2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/657/medium640/install_circuitpython_on_rp2040_RP2040_drag_UF2.jpg?1618943674)

The **RPI-RP2** drive will disappear and a new disk drive called **CIRCUITPY** will appear.

That's it, you're done! :)

![install_circuitpython_on_rp2040_RP2040_CIRCUITPY.jpg](https://cdn-learn.adafruit.com/assets/assets/000/101/658/medium640/install_circuitpython_on_rp2040_RP2040_CIRCUITPY.jpg?1618943864)

## Safe Mode

You want to edit your **code.py** or modify the files on your **CIRCUITPY** drive, but find that you can't. Perhaps your board has gotten into a state where **CIRCUITPY** is read-only. You may have turned off the **CIRCUITPY** drive altogether. Whatever the reason, safe mode can help.

Safe mode in CircuitPython does not run any user code on startup, and disables auto-reload. This means a few things. First, safe mode _bypasses any code in_ **boot.py** (where you can set **CIRCUITPY** read-only or turn it off completely). Second, _it does not run the code in_ **code.py**. And finally, _it does not automatically soft-reload when data is written to the_ **CIRCUITPY** _drive_.

Therefore, whatever you may have done to put your board in a non-interactive state, safe mode gives you the opportunity to correct it without losing all of the data on the **CIRCUITPY** drive.

### Entering Safe Mode
To enter safe mode when using CircuitPython, plug in your board or hit reset (highlighted in red above). Immediately after the board starts up or resets, it waits 1000ms. On some boards, the onboard status LED (highlighted in green above) will blink yellow during that time. If you press reset during that 1000ms, the board will start up in safe mode. It can be difficult to react to the yellow LED, so you may want to think of it simply as a slow double click of the reset button. (Remember, a fast double click of reset enters the bootloader.)

### In Safe Mode

If you successfully enter safe mode on CircuitPython, the LED will intermittently blink yellow three times.

If you connect to the serial console, you'll find the following message.

```terminal
Auto-reload is off.
Running in safe mode! Not running saved code.

CircuitPython is in safe mode because you pressed the reset button during boot. Press again to exit safe mode.

Press any key to enter the REPL. Use CTRL-D to reload.
```

You can now edit the contents of the **CIRCUITPY** drive. Remember, _your code will not run until you press the reset button, or unplug and plug in your board, to get out of safe mode._

## Flash Resetting UF2

If your board ever gets into a really _weird_ state and CIRCUITPY doesn't show up as a disk drive after installing CircuitPython, try loading this 'nuke' UF2 to RPI-RP2. which will do a 'deep clean' on your Flash Memory. **You will lose all the files on the board** , but at least you'll be able to revive it! After loading this UF2, follow the steps above to re-install CircuitPython.

[Download flash erasing "nuke" UF2](https://cdn-learn.adafruit.com/assets/assets/000/101/659/original/flash_nuke.uf2?1618945856)
# LEGO Set Lighting

## Code the Lighting Controller

## Text Editor

Adafruit recommends using the&nbsp; **Mu** &nbsp;editor for editing your CircuitPython code. You can get more info in&nbsp;[this guide](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor).

Alternatively, you can use any text editor that saves simple text files.

## Download the Project Bundle

Your project will use a specific set of CircuitPython libraries, and the&nbsp; **code.py** &nbsp;file. To get everything you need, click on the&nbsp; **Download Project Bundle** &nbsp;link below, and uncompress the .zip file.

Connect your computer to the board via a known good USB power+data cable. A new flash drive should show up as&nbsp; **CIRCUITPY**.

Drag the contents of the uncompressed bundle directory onto your board **CIRCUITPY** drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/LEGO_Lighting/code.py

## How it Works

### Imports

First the code imports the necessary library modules:

- `asyncio`: This module provides infrastructure for writing single-threaded concurrent code using coroutines
- `randint`, `uniform`: Functions for generating random numbers
- `busio`, `board`: for setting up the I2C communication bus
- `adafruit_aw9523`: library for the AW9523 LED driver board

```auto
import asyncio
from random import randint, uniform
import busio
import board
import adafruit_aw9523
```

### I2C and Driver Setup

- `busio.I2C` initializes one I2C bus on the STEMMA QT port pins
- `adafruit_aw9523.AW9523(i2c)` initializes the AW9523 driver using the I2C bus with the name `leddriver`
- Configure all of the `leddriver` pins as LED constant current mode with their direction set to output

```auto
i2c = busio.I2C(board.SCL1, board.SDA1)
leddriver = adafruit_aw9523.AW9523(i2c)
leddriver.LED_modes = 0xFFFF
leddriver.directions = 0xFFFF
```

### Light Sets

Four of the lights are part of the red/green string over the upper floor bakery window. These pins are added to a list named `window_set[]` with the proper order for running a left-right chase pattern.

Four other lights (lanterns, bakery shop display window) will always be on, so they are handled very simply just once. The `always_on_set[]` is a list of these four pins.

The `always_on_set_maxes[]` define the brightness level for the "always on" set of lights.

```auto
window_set = [8, 10, 9, 11]  # red/green string
always_on_set = [0, 1, 2, 4]  # window and lanterns
always_on_set_maxes = [100, 30, 30, 40]  # maximum brightness per light
```

### Always-On Set Activation

Next we'll turn on the LEDs that are always on at a constant current value.

```auto
for n in range(len(always_on_set)):
    leddriver.set_constant_current(always_on_set[n], always_on_set_maxes[n])
```

### async flicker() Function

We define a function that flickers lights with inputs for pin, minimum current, maximum current, and the timing interval.

Similar to a regular CircuitPython function, but these are `asyncio` functions, which means they can operate in a non-blocking way so their timing is independent of other async functions.

This is really helpful for creating multiple different lighting effects that happen simultaneously! In fact, this function will run concurrently on four different LEDs, lending them an organic look, since they operate independently of each other with their own randomized brightness levels.

```auto
async def flicker(pin, min_curr, max_curr, interval):
    while True:
        rand_max_curr = randint(min_curr, max_curr)
        for i in range(min_curr, rand_max_curr):
            leddriver.set_constant_current(pin, i)  # aw9523 pin, current out of 255
            await asyncio.sleep(0.07)
        await asyncio.sleep(uniform(0.0, interval))
```

### async string\_lights() Function

This function is a typical holiday string light effect where each light fades up in turn and then loops around to fade down in order.

```auto
async def string_lights(interval, max_curr):
    while True:
        for i in range(len(window_set)):
            # fade up
            for j in range(max_curr):
                leddriver.set_constant_current(window_set[i], j)
                print(j)
                await asyncio.sleep(interval)
        for i in range(len(window_set)):
            # fade down
            for j in range(max_curr):
                leddriver.set_constant_current(window_set[i], max_curr-j)
                print(j)
                await asyncio.sleep(interval)
```

### async main() Function
In the `main()` async function, create each task that is to be executed concurrently. These tasks call the previously defined async functions `flicker()` and `string_lights()`. In the case of `flicker()`, different pins, minimum and maximum current values and timing intervals per task can be specified.

The `asyncio.gather()` function runs the multiple co-routines concurrently, and awaits their completion before starting them again, respectively.

```auto
async def main():
    led0_task = asyncio.create_task(flicker(3, 3, 10, 0.7))  # music candle
    led1_task = asyncio.create_task(flicker(5, 6, 12, 0.7))  # music sconce a
    led2_task = asyncio.create_task(flicker(6, 6, 12, 0.7))  # music sconce b
    led3_task = asyncio.create_task(flicker(7, 3, 10, 0.7))  # music candle
    led4_task = asyncio.create_task(string_lights(0.03, 30))
    await asyncio.gather(led0_task, led1_task, led2_task, led3_task, led4_task)
```

### Run main() co-routine

Finally, to call the `main()` function using `asyncio.run()`.

```auto
asyncio.run(main())
```

# LEGO Set Lighting

## Light the Winter Village

![](https://cdn-learn.adafruit.com/assets/assets/000/126/436/medium800/hacks_legoled-5973a.jpg?1701270019)

## Rig the Lights

There are many different ways to run your lighting. I found convenient gaps in bricks and windows, and ran a light down into the wall sconce, behind the display window overhang, into the four string lights, inside a candle, and in the three lantern lights.

With some extra care you can hide the wiring even better than the examples here :)

![hacks_legoled-5929.jpg](https://cdn-learn.adafruit.com/assets/assets/000/126/427/medium640/hacks_legoled-5929.jpg?1701273386)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/428/medium800/hacks_legoled-5931.jpg?1701273496)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/430/medium800/hacks_legoled-5930.jpg?1701273901)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/431/medium800/hacks_legoled-5934.jpg?1701275156)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/432/medium800/hacks_legoled-5933.jpg?1701275106)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/434/medium800/hacks_legoled-5932.jpg?1701275548)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/435/medium800/hacks_legoled-5937.jpg?1701275731)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/437/medium800/hacks_legoled-5974.jpg?1701270019 No peeking backstage)

![](https://cdn-learn.adafruit.com/assets/assets/000/126/438/medium800/hacks_legoled-5980.jpg?1701270037)

https://youtu.be/eMm64RiZa1A


## Featured Products

### Adafruit QT Py RP2040

[Adafruit QT Py RP2040](https://www.adafruit.com/product/4900)
What a cutie pie! Or is it... a QT Py?&nbsp;This diminutive dev board comes with one of our new favorite chip, the RP2040. It's been made famous in the new [Raspberry Pi Pico](https://www.adafruit.com/pico) _and_ our [Feather...](http://www.adafruit.com/product/4884)

In Stock
[Buy Now](https://www.adafruit.com/product/4900)
[Related Guides to the Product](https://learn.adafruit.com/products/4900/guides)
### Adafruit AW9523 GPIO Expander and LED Driver Breakout

[Adafruit AW9523 GPIO Expander and LED Driver Breakout](https://www.adafruit.com/product/4886)
Expand your project possibilities, with the **Adafruit AW9523 GPIO Expander and LED Driver Breakout** - a cute and powerful I2C expander with a lot of tricks up its sleeve.

GPIO expanders work like this: you have a board with some number of GPIO but not enough for your...

In Stock
[Buy Now](https://www.adafruit.com/product/4886)
[Related Guides to the Product](https://learn.adafruit.com/products/4886/guides)
### STEMMA QT / Qwiic JST SH 4-pin Cable - 100mm Long

[STEMMA QT / Qwiic JST SH 4-pin Cable - 100mm Long](https://www.adafruit.com/product/4210)
This 4-wire cable is a little over 100mm / 4" long and fitted with JST-SH female 4-pin connectors on both ends. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert and remove.

<a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/4210)
[Related Guides to the Product](https://learn.adafruit.com/products/4210/guides)
### Miniature Wired LEDs - 0805 SMT LED - Warm White - 5 pack

[Miniature Wired LEDs - 0805 SMT LED - Warm White - 5 pack](https://www.adafruit.com/product/5487)
These ultra-fine wired LEDs are designed for model-makers and crafters but they could also come in handy for folks who want to add tiny points of light to a design without needing SMT-level soldering skills.&nbsp;Use this five-pack bundle of **Miniature&nbsp;Wired 0805 LEDs** for...

In Stock
[Buy Now](https://www.adafruit.com/product/5487)
[Related Guides to the Product](https://learn.adafruit.com/products/5487/guides)
### Miniature Wired LEDs - 0805 SMT LED - Pink - 5 pack

[Miniature Wired LEDs - 0805 SMT LED - Pink - 5 pack](https://www.adafruit.com/product/5488)
These ultra-fine wired LEDs are designed for model-makers and crafters but they could also come in handy for folks who want to add tiny points of light to a design without needing SMT-level soldering skills.&nbsp;Use this five-pack bundle of&nbsp; **Miniature&nbsp;Wired 0805...**

In Stock
[Buy Now](https://www.adafruit.com/product/5488)
[Related Guides to the Product](https://learn.adafruit.com/products/5488/guides)
### Miniature Wired LEDs - 0805 SMT LED - Pure Green - 5 pack

[Miniature Wired LEDs - 0805 SMT LED - Pure Green - 5 pack](https://www.adafruit.com/product/5489)
These ultra-fine wired LEDs are designed for model-makers and crafters but they could also come in handy for folks who want to add tiny points of light to a design without needing SMT-level soldering skills.&nbsp;Use this five-pack bundle of **Miniature&nbsp;Wired 0805 LEDs** for...

In Stock
[Buy Now](https://www.adafruit.com/product/5489)
[Related Guides to the Product](https://learn.adafruit.com/products/5489/guides)
### Miniature Wired LEDs - 0805 SMT LED - Blue - 5 pack

[Miniature Wired LEDs - 0805 SMT LED - Blue - 5 pack](https://www.adafruit.com/product/5490)
These ultra-fine wired LEDs are designed for model-makers and crafters but they could also come in handy for folks who want to add tiny points of light to a design without needing SMT-level soldering skills.&nbsp;Use this five-pack bundle of&nbsp; **Miniature&nbsp;Wired 0805...**

In Stock
[Buy Now](https://www.adafruit.com/product/5490)
[Related Guides to the Product](https://learn.adafruit.com/products/5490/guides)
### Miniature Wired LEDs - 0805 SMT LED - Purple - 5 pack

[Miniature Wired LEDs - 0805 SMT LED - Purple - 5 pack](https://www.adafruit.com/product/5491)
These ultra-fine wired LEDs are designed for model-makers and crafters but they could also come in handy for folks who want to add tiny points of light to a design without needing SMT-level soldering skills.&nbsp;Use this five-pack bundle of&nbsp; **Miniature&nbsp;Wired 0805...**

In Stock
[Buy Now](https://www.adafruit.com/product/5491)
[Related Guides to the Product](https://learn.adafruit.com/products/5491/guides)

## Related Guides

- [Adafruit AW9523 GPIO Expander and LED Driver](https://learn.adafruit.com/adafruit-aw9523-gpio-expander-and-led-driver.md)
- [Adafruit QT Py RP2040](https://learn.adafruit.com/adafruit-qt-py-2040.md)
- [Split Ortho Keyboard with TCA8418 Matrix Expanders](https://learn.adafruit.com/split-ortho-keyboard.md)
- [An Introduction to RP2040 PIO with CircuitPython](https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython.md)
- [MIDI Breath Controller](https://learn.adafruit.com/midi-breath-controller.md)
- [Animated Edge Lit Acrylic NeoPixel Lamp](https://learn.adafruit.com/acrylic-neopixel-lamp.md)
- [NeoPixel Menorah](https://learn.adafruit.com/neopixel-menorah.md)
- [Customizing USB Devices in CircuitPython](https://learn.adafruit.com/customizing-usb-devices-in-circuitpython.md)
- [SNES Mouse to USB HID with CircuitPython](https://learn.adafruit.com/snes-mouse-to-usb-hid-with-circuitpython.md)
- [LED Masquerade Masks with n00ds](https://learn.adafruit.com/led-masquerade-masks-with-n00ds.md)
- [Hexpad](https://learn.adafruit.com/hexpad.md)
- [QT Py RP2040 USB to Serial MIDI Friends](https://learn.adafruit.com/qt-py-rp2040-usb-to-serial-midi-friends.md)
- [NeXT Computer Keyboard to USB HID with CircuitPython](https://learn.adafruit.com/next-computer-keyboard-to-usb-hid-with-circuitpython.md)
- [NeoPixel Mini VU Meter](https://learn.adafruit.com/neopixel-mini-vu-meter.md)
- [Audio Synthesis with CircuitPython synthio](https://learn.adafruit.com/audio-synthesis-with-circuitpython-synthio.md)
- [NeoPixel Novelty Cufflinks with Scrolling Text](https://learn.adafruit.com/neopixel-novelty-cufflinks-with-scrolling-text.md)
