MohammadReza Sharifi
Published © MIT

PS5 Temperature-Reactive RGB Lighting

Ambient Immersion with Raspberry Pi Pico 2 and LM35.

BeginnerFull instructions provided4 hours12
PS5 Temperature-Reactive RGB Lighting

Things used in this project

Hardware components

Raspberry Pi Pico 2
Raspberry Pi Pico 2
×1
NeoPixel Ring: WS2812 5050 RGB LED
Adafruit NeoPixel Ring: WS2812 5050 RGB LED
×1
Temperature Sensor
Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

MicroPython
MicroPython

Story

Read more

Schematics

Circuit Diagram

Code

MainApp

MicroPython
upload this code on Raspberry Pi Pico 2 Microcontroller
import machine
import utime
import neopixel
import math


NUM_LEDS = 16       
PIN = 0             
np = neopixel.NeoPixel(machine.Pin(PIN), NUM_LEDS)


adc = machine.ADC(26)   
VREF = 3.3
CONVERSION = VREF / 65535


def read_temp():
    raw = adc.read_u16()
    voltage = raw * CONVERSION
    temp_c = voltage * 100
    return temp_c


def set_color(r, g, b):
    for i in range(NUM_LEDS):
        np[i] = (r, g, b)
    np.write()


def wheel(pos):
    if pos < 85:
        return (pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return (255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return (0, pos * 3, 255 - pos * 3)

def rainbow(wait=20):
    for j in range(256):
        for i in range(NUM_LEDS):
            idx = (i * 256 // NUM_LEDS) + j
            np[i] = wheel(idx & 255)
        np.write()
        utime.sleep_ms(wait)


def pulse(color=(255,0,0), steps=40, delay=20):
    r, g, b = color
    for i in range(steps):
        factor = (math.sin(i / steps * math.pi) ** 2)
        set_color(int(r*factor), int(g*factor), int(b*factor))
        utime.sleep_ms(delay)


def dance(delay=20):
    colors = [(255,0,0), (0,255,0), (0,0,255), (255,255,0)]  
    steps = 50
    for c in range(len(colors)):
        r1,g1,b1 = colors[c]
        r2,g2,b2 = colors[(c+1) % len(colors)]
        for i in range(steps):
            r = int(r1 + (r2-r1) * i/steps)
            g = int(g1 + (g2-g1) * i/steps)
            b = int(b1 + (b2-b1) * i/steps)
            set_color(r,g,b)
            utime.sleep_ms(delay)


while True:
    temp = read_temp()
    print("Temp:", temp)

    if temp < 25:
        set_color(0, 0, 255)   

    elif 25 <= temp < 30:
        dance(delay=30)       

    elif 30 <= temp < 40:
        #pulse(color=(255,0,0)) 
        rainbow(wait=10)

    elif 40 <= temp < 55:
        pulse(color=(255,0,0)) 
        #rainbow(wait=10)
    else:
        rainbow(wait=10)       

Credits

MohammadReza Sharifi
14 projects • 9 followers
I'm an Electrical Engineer and Maker.

Comments