Neopixel mood lighting based on time of day

Neopixel mood lighting based on time of day

Main Diagram

Hardware Required

  • PICUNO Microcontroller board (built-in NeoPixel LED on GPIO 17)
  • USB cable (for power and programming)

Description

The user is prompted to enter the current hour (0-23) through the serial input. Based on the input:

• 6 AM to 12 PM → Yellow (Morning)
• 12 PM to 6 PM → White (Afternoon)
• 6 PM to 9 PM → Orange (Evening)
• 9 PM to 6 AM → Dim Blue (Night)

If the user enters an invalid hour (not between 0 and 23), the program displays an error message without updating the LED.

Circuit Diagram

[Fritzing image to be added here]

Circuit

  • Connect the PICUNO board to the computer using a USB cable.
  • No external wires or breadboard are required.
  • The built-in NeoPixel is already wired to GPIO 17.
  • The Arduino sketch uses the Adafruit NeoPixel library for control.
  • The Thonny IDE uses picuno library for control.

Schematic

No external wiring is required. The NeoPixel LED is internally connected to GPIO 17 on the PicUNO board.

Code - C

#include <Adafruit_NeoPixel.h>

#define PIN 17
#define NUMPIXELS 1

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

String inputString = "";

void setup() {
  pixels.begin();
  pixels.setBrightness(100);
  Serial.begin(9600);
  Serial.println("Enter current hour (0-23):");
}

void loop() {
  if (Serial.available()) {
    inputString = Serial.readStringUntil('\n');
    inputString.trim();
    int hour = inputString.toInt();

    if (hour >= 0 && hour < 24) {
      uint32_t color;

      if (hour >= 6 && hour < 12) {
        color = pixels.Color(255, 200, 0); // Morning - Yellow
      } else if (hour >= 12 && hour < 18) {
        color = pixels.Color(255, 255, 255); // Afternoon - White
      } else if (hour >= 18 && hour < 21) {
        color = pixels.Color(255, 100, 0); // Evening - Orange
      } else {
        color = pixels.Color(0, 0, 100); // Night - Dim Blue
      }

      pixels.setPixelColor(0, color);
      pixels.show();
      Serial.println("Mood color set for hour: " + String(hour));
    } else {
      Serial.println("Invalid input. Please enter hour between 0 and 23.");
    }
  }
}
Adafruit_NeoPixel pixels(...) - Initializes the NeoPixel object.

inputString = Serial.readStringUntil('\n') - Reads user input from serial monitor.

inputString.toInt() - Converts input to integer.

if (hour >= 0 && hour < 24) - Validates user input range.

setPixelColor() and show() - Apply the selected color.

Code - Micropython

from picuno import Neopixel
import time

pixels = Neopixel(1, 0, 17, "GRB")
pixels.brightness(100)

# Define RGB colors for each time slot
MORNING = (255, 200, 0)
AFTERNOON = (255, 255, 255)
EVENING = (255, 100, 0)
NIGHT = (0, 0, 100)

while True:
    try:
        hour = int(input("Enter current hour (0-23): ").strip())

        if 0 <= hour < 24:
            if 6 <= hour < 12:
                color = MORNING
            elif 12 <= hour < 18:
                color = AFTERNOON
            elif 18 <= hour < 21:
                color = EVENING
            else:
                color = NIGHT

            pixels.set_pixel(0, color)
            pixels.show()
            print("Mood color set for hour:", hour, "→", color)
        else:
            print("Invalid hour. Please enter a value from 0 to 23.")
    except:
        print("Invalid input. Please enter a number.")
pixels = Neopixel(1, 0, 17, "GRB") - Initializes the built-in NeoPixel on GPIO 17.

hour = int(input(...)) - Takes user input and converts it to an integer.

if 0 <= hour < 24 - Validates the range (0 to 23).

pixels.set_pixel(0, color) and .show() - Sets and displays the color.

except - Catches invalid or non-numeric input.