RGB Lights

The StudioBot has 4 RGB lights under the display. We can use these lights to show colours and signal output to the user.

The following code will set the light colours to red, green, blue with 2 seconds between each change.

import time
import board
import neopixel

pixel_pin = board.NEOPIXEL
num_pixels = 4

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

pixels.fill(RED)
pixels.show()

time.sleep(2)

pixels.fill(GREEN)
pixels.show()

time.sleep(2)

pixels.fill(BLUE)
pixels.show()


To set a single pixel color

import time
import board
import neopixel

pixel_pin = board.NEOPIXEL
num_pixels = 4

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

pixels[0] = RED. #use the array index to set single pixel colours
pixels.show()

The following code will show how to make the RGB lights swap colours after a given delay.

import time
import board
import neopixel

pixel_pin = board.NEOPIXEL
num_pixels = 4

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)


RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

def color_chase(color, wait):
    for i in range(4):
        pixels[i] = color
        time.sleep(wait)
        pixels.show()
        time.sleep(wait)
        
color_chase(RED, 0.1)  # Increase the number to slow down the color chase
color_chase(YELLOW, 0.1)
color_chase(GREEN, 0.1)
color_chase(CYAN, 0.1)
color_chase(BLUE, 0.1)
color_chase(PURPLE, 0.1)

Last updated