Coding the Lights on my Christmas Tree

Introduction

A few years ago, my curiousity sparked from a YouTube video that showcased someone skillfully orchestrating a Christmas tree’s lights using Python, crafting a stunning three-dimensional spectacle (which is displayed below). My initial fascination lingered, and now, armed with knowledge from my first semester in a Master’s in Data Science program, I believe I have the skills to bring a similar vision to life.



Despite a solid foundation in math and coding, my foray into the world of computer hardware presented unforeseen challenges. An absence of comprehensive guidance on what to purchase, how to wire the lights, and much more quickly became a formidable task. I hope this documentation serves a dual purpose: a roadmap for those embarking on a similar venture and a narrative detailing my data science journey.

I want to express my sincere gratitude to my friend Kangheng Liu, who provided invaluable support throughout this project. Together, we faced challenges head-on, persevered through difficulties, and shared in the triumphs. You can explore Kang’s work on his website here.

Join me on this technical adventure, from mastering coding to navigating the intricacies of hardware challenges. Together, let’s not only illuminate Christmas trees but also explore the fascinating intersection of technology, creativity, and the spirit of celebration.

Materials

Disclaimer: Acknowledging my novice status in this domain, I recognize that more cost-effective alternatives may exist for these components. The choices outlined below are specific to my project, and prices are based on my purchases in the fall of 2023, subject to fluctuations.

  1. Raspberry Pi 4 Model B:
    • Purpose: The Raspberry Pi acts as the project’s brain, executing Python code to control the lights.
    • Link: Amazon - Raspberry Pi 4 Model B
    • Price: $65.42
  2. Micro SD Card for Raspberry Pi:
    • Purpose: This micro SD card stores the Raspberry Pi’s operating system and code.
    • Link: Amazon - Micro SD Card
    • Price: $8.99
  3. 500 Individually Addressable Christmas Lights (10 strands of 50):
  4. Power Supply for Raspberry Pi:
  5. Power Supply for Lights:
    • Purpose: Provides power to the Christmas lights, ensuring they illuminate as intended.
    • Link: Amazon - Lights Power Supply
    • Price: $23.19
    • Note: If using only one strand, a smaller 5V power supply might be sufficient.
  6. Level Shifter:
    • Purpose: Facilitates communication between the Raspberry Pi and individually addressable lights.
    • Link: Amazon - Level Shifter
    • Price: $5.15
  7. Breadboard:
    • Purpose: A platform for prototyping and connecting electronic components.
    • Link: Amazon - Breadboard
    • Price: $6.75
  8. Assorted Jumper Wires (M-M & M-F & F-F):

Total Cost: $224.87

Additional Tools (Extremely Helpful/Necessary):

  • Wire Cutters
  • Wire Strippers
  • Electrical Tape
  • Multimeter

Proof of Concept

Now that all the materials have been acquired, demonstrating the feasibility of this project is the next crucial step. This phase involves testing fundamental functionalities to ensure that the project can progress successfully.

Proof of concepts serves as a critical checkpoint in any project, offering a preliminary validation of its viability. By addressing key questions such as turning lights on/off, changing colors, or controlling individual lights, we lay the foundation for the broader implementation. This step helps identify potential challenges early on and ensures that subsequent development is built on a solid framework.

Flashing the Raspberry Pi

Before diving into the light control, the Raspberry Pi needs to be set up with an operating system. Use the provided micro SD card and adapter to flash the Raspberry Pi using the Raspberry Pi Imager. During the flashing process, input the Wi-Fi information for future SSH access.

SSHing into the Raspberry Pi

After flashing, insert the SD card into the Raspberry Pi and power it on. This step initiates the device and prepares it for further configuration.

Mapping the Lights in 3D Space

Code
import plotly.graph_objects as go

# Read data from the text file
with open('lights500.txt', 'r') as file:
    data = [eval(line.strip()) for line in file]

# Extract coordinates
x, y, z = zip(*data)

# Create a 3D scatter plot
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers',
                                   marker=dict(size=3, color='green'))])

# Set labels
fig.update_layout(scene=dict(xaxis_title='X Label', yaxis_title='Y Label', zaxis_title='Z Label'))

# Show the plot
fig.show()

the below code checks to see if there are any big changes that could cause problems with our data

Code
# Check for changes greater than or equal to 7
for i in range(1, len(data)):
    diff_x = abs(data[i][0] - data[i-1][0])
    diff_y = abs(data[i][1] - data[i-1][1])
    diff_z = abs(data[i][2] - data[i-1][2])

    if diff_x >= 7 or diff_y >= 7 or diff_z >= 7:
        print(f"Change detected at index {i}: {diff_x=}, {diff_y=}, {diff_z=}")
Change detected at index 200: diff_x=8, diff_y=2, diff_z=5
Change detected at index 250: diff_x=9, diff_y=2, diff_z=0
Change detected at index 300: diff_x=0, diff_y=3, diff_z=7

the above output is what we want. the only changes detected occured when one strip changed into another… there were not fauly measurements or inputs of data

Coding scripts

more to come…