Maker.io main logo

Minimalist Arduino-Based IoT RGB Mood Light

48

2022-04-28 | By Maker.io Staff

License: See Original Project

Colorful LED lights can easily brighten up a room and change its entire atmosphere. Dim, red ‎light is perfect for relaxing, and blue-ish lights often help us focus on mentally demanding tasks, ‎such as studying or working. This article discusses an IoT mood light, a simple, beginner-friendly ‎Arduino-based IoT project that lets users change the atmosphere in just about any room. In ‎addition, you can give others access to the online dashboard so that they can change the color to ‎remind you of appointments or to let you know they are thinking about you.‎

finish_1 

This image shows the finished device.‎

Building the Hardware‎

This project is very beginner-friendly: it requires only a handful of components and is incredibly ‎easy to assemble. An Arduino Nano 33 IoT development board forms the base of this project. It ‎establishes a connection to the Arduino IoT cloud and synchronizes its local variables with the ‎online service. This service also enables users to change the mood light’s color and toggle an ‎automatic color-fading mode. The Arduino then communicates with a WS2812-based LED strip ‎to display the color chosen by the user. Luckily, communicating with the LEDs requires only ‎three wires:‎

 

This image shows the schematic diagram of this project. This build only contains two ‎components, and the device's firmware does most of the heavy lifting.

Two of the three wires supply the LEDs with power, and the Arduino uses the third connection to ‎send the color and brightness data to the individual LEDs on the strip.‎

Lastly, the project also contains a minimalistic, modern 3D-printed enclosure that neatly houses ‎all the electronic components. I used two colors to print the case, and I printed the top part of the ‎enclosure using semi-translucent white PLA. This semi-transparent material acts as a diffuser ‎that evenly distributes the light emitted by the LEDs. In summary, the build contains the following ‎parts:‎

build_2

The build consists of four 3D-printed parts, two short LED strip pieces, and an Arduino Nano 33 ‎IoT development board.‎

To follow along with what I did step by step, you’ll need access to a 3D printer. However, you ‎can always employ a professional-grade 3D-printing service such as the one offered by Digi-Key. ‎You can download the 3D files at the end of this article.‎

Assembling the Device‎

The 3D-printed enclosure consists of four parts. The base part houses the Arduino board, and it ‎exposes the USB port of the Arduino so that you can plug in the device to power it. Make sure ‎you close the small solder jumper on the bottom side of the Arduino before you start assembling ‎the case:‎

board_3

Bridge the two PCB jumper pads using a small blob of solder, as shown in this image.‎

Closing this jumper will provide the +5V GPIO pin of the Arduino with power from the USB port. ‎If you don’t close this jumper, the LED strip won’t get any power, and the build won’t function as ‎intended.‎

Next, remove the adhesive backing from the LED strip parts and attach the two small pieces to ‎the center part of the plastic case. Then, add some wires to the flexible PCBs. Make sure to pay ‎close attention to the arrows on the LED strip pieces before you attach them to the case part. ‎The arrows indicate the direction of the data flow coming from the Arduino:‎

connect_4 

Connect the LED strips as shown in this image.‎

Attach the wires that will go to the Arduino to the side of the LED strip that contains a DI pad. ‎Then, connect the first LED strip's DO pin to the second row's DI pin. The figure above shows ‎that the arrows point from left to right on the top LED strip and in the opposite direction on the ‎second strip. Once you’re done, connect the three remaining wires to the Arduino board, as ‎shown in the schematic diagram above. Then, place the Arduino in the bottom half of the case ‎before adding the piece that contains the LED strips:‎

case_5

Place the Arduino in the bottom half of the case. Then, gently but firmly press the second plastic ‎piece in place. It should be a tight fit, but it shouldn’t be too difficult to insert the part that the LED ‎strips are attached to.‎

You can attach a USB cable to the Arduino to keep the board in place while pushing the second ‎plastic piece in place. All the parts in the case snap together and hold each other in place. Next, ‎you can add the second thin plastic piece to keep the LED strips in place if they don’t have an ‎adhesive backing. However, I omitted that piece in my build, as my strips came with a sticky ‎back layer. Lastly, push on the semi-translucent top part. Make sure that the four pegs align with ‎the four holes in the bottom half of the enclosure. You should feel some resistance when you ‎snap together the parts. However, if the resistance is too strong, check whether any cables are ‎in the way or whether you need to drill out the holes due to material shrinking.‎

The Firmware‎

Similar to the hardware, the software component of this project is straightforward and suitable for ‎beginners, as the Arduino IoT Cloud service does most of the heavy lifting. You can read more ‎about getting started with the Arduino IoT Cloud here if you need a refresher. Start by creating a ‎new thing in the IoT cloud. Then, add five variables, as shown in the following screenshot:‎

screen_5

Create a new thing and add five variables, as shown in this image.‎

As you can see, the IoT sketch contains five synchronized variables: one integer for each color ‎value, another one for the brightness, and a boolean value that toggles the color fading animation. ‎Next, build a dashboard so that users can set the mood light's color, change the brightness, and ‎toggle the animation:‎

dashboard_6 

This image shows an example of how the project’s dashboard may look. However, you can ‎arrange the dashboard as you like.‎

Then, edit the thing’s sketch so that it contains the following code:‎

Copy Code
#include "thingProperties.h"
#include <Adafruit_NeoPixel.h>

#define LED_DATA_PIN 3
#define NUM_LEDS 4
#define ANIMATION_DELAY 50

unsigned long lastAnimationStep = 0UL;
unsigned animationMode = 0;

Adafruit_NeoPixel leds(NUM_LEDS, LED_DATA_PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
Serial.begin(9600);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

leds.begin();
updateBrightness();
updateColor();
}

void updateColor(void)
{
leds.clear();

for(int i = 0; i < NUM_LEDS; i++)
leds.setPixelColor(i, leds.Color(r, g, b));

leds.show();
}

void updateBrightness(void)
{
leds.setBrightness(brightness);
leds.show();
}

void loop()
{
ArduinoCloud.update();

if(animate)
{
unsigned long currentMillis = millis();

if(currentMillis - lastAnimationStep > ANIMATION_DELAY)
{
animateValues();
updateColor();
lastAnimationStep = currentMillis;
}
}
}

void animateValues(void)
{
switch(animationMode)
{
case 0:
g++;
r--;
if(r < 0 || g > 255)
switchAnimationMode();
break;

case 1:
b++;
g--;
if(g < 0 || b > 255)
switchAnimationMode();
break;

case 2:
r++;
b--;
if(b < 0 || r > 255)
switchAnimationMode();
break;
break;
}
}

void switchAnimationMode(void)
{
if(animationMode == 0)
{
r = 0;
g = 255;
b = 0;
animationMode = 1;
}
else if(animationMode == 1)
{
r = 0;
g = 0;
b = 255;
animationMode = 2;
}
else
{
r = 255;
g = 0;
b = 0;
animationMode = 0;
}
}

void onRChange()
{
updateColor();
}

void onGChange()
{
updateColor();
}

void onBChange()
{
updateColor();
}

void onAnimateChange()
{ }

void onBrightnessChange()
{
updateBrightness();
}

Let’s break down what this code is doing. First, I import the libraries for communicating with the ‎IoT cloud service and the LED strip. The setup function then initializes the connection with the ‎Arduino IoT cloud before setting the initial color and brightness of the LEDs using two helper ‎functions. The updateColor helper method iterates over all the LEDs on the strip (four in this case) ‎and assigns them with the color value stored in the r, g, and b variables. Similarly, the ‎updateBrightness helper function calls the LED library’s setBrightness function using the value of ‎the synchronized brightness function.‎

Whenever the user changes one of the synchronized variables using the IoT cloud dashboard, ‎the IoT cloud library on the Arduino calls the appropriate onChange() function towards the end of ‎the script. For example, all the color value update callback functions call the updateColor helper. ‎Similarly, the onBrightnessChange function calls the updateBrightness helper.‎

The product would function in this state. However, the loop method implements a simple color ‎fading animation. First, the loop method checks whether the animate variable is true. If it is, it ‎checks whether enough time has elapsed since it last updated the animation. If that’s the case, ‎the loop method calls the animateValues helper and the updateColor function to display the ‎changed color. Depending on the current animation mode, the animateValues helper function ‎increments one of the color channels while simultaneously decrementing a second channel. If ‎either of the updated colors reaches one of the extreme values, the helper function switches to ‎the next mode. During a mode switch, the firmware resets the values so that one color channel ‎is at full brightness while all others are completely off. Then, the cycle repeats for two other ‎channels.‎

Download the Case Design Files‎

You can download the case design files here.‎

Summary‎

finish_7

This IoT mood light project might be simple to assemble and understand, but it still yields an ‎impressive-looking decoration item that lets you change the atmosphere in an entire room. The ‎build consists of only a handful of readily available low-cost components. An Arduino Nano 33 ‎IoT does most of the heavy lifting. It establishes a connection to the Arduino IoT cloud service, ‎listens for changes in the synchronized variables, and updates the LED values accordingly. A ‎custom 3D-printed enclosure houses all the electronic components.‎

The software part of this project mainly deals with synchronizing the variables with the IoT cloud ‎and reacting to changes made by a user. Whenever somebody changes one of the ‎synchronized color variables, the Arduino detects this change and propagates it to the LED strip. ‎Additionally, the firmware also implements a simple color-fade animation. The program ‎decreases one color channel’s value while simultaneously increasing another channel, ‎depending on its internal state.

Objednací č. Mfr ABX00027
ARDUINO NANO 33 IOT
Arduino
497,84 Kč
View More Details
Objednací č. Mfr 104990000
ADDRESS LED STRIP SERIAL RGB 1M
Seeed Technology Co., Ltd
206,22 Kč
View More Details
Objednací č. Mfr KT-PR0047NA
LULZBOT MINI 2 NORTH AMERICA
LulzBot
26 974,85 Kč
View More Details
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.