Project 5.02 Using ColorHSV

In this project, you'll learn how to gracefully transition between a spectrum of colors using the onboard NeoPixels with the HSV (Hue, Saturation, Value) color model!

There are multiple ways to set the color of the Neopixels. Until now we have used the Color function to set the RGB color of the Neopixel. We can also use the function ColorHSV to set the color a different way. HSV stands for Hue, Saturation, and Value. Hue refers to the color of the Neopixel, Saturation refers to how white the Neopixel is, and Value refers to how bright the Neopixel is. Hue in this case is a 16-bit value (0 – 65535), while Saturation and Value are 8-bit values (0 – 255). Which color method you use is totally up to you and mostly comes down to preference and application.

Note: Using ColorHSV does make it easier to loop through the entire color spectrum.

Project Code:

				
					////////////////////////////////////////////////////////
// 5.02 - Using ColorHSV

#include <Adafruit_NeoPixel.h>
byte saturation = 255;
byte value = 255;

byte dataPin = 10;
byte numberOfLEDs = 2;
byte brightness = 10;

Adafruit_NeoPixel pixels(numberOfLEDs, dataPin, NEO_GRB + NEO_KHZ800);
void setup() {
  Serial.begin(9600);
  pixels.begin();
  pixels.setBrightness(brightness);
}

void loop() {
  for (long hue = 0; hue < 65536; hue++) {
    pixels.setPixelColor(0, pixels.ColorHSV(hue, saturation, value));
    pixels.setPixelColor(1, pixels.ColorHSV(hue, saturation, value));
    pixels.show();
    delayMicroseconds(3); 
  }

}
////////////////////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

Two variables are used to set the saturation and value parameters. These values are not changed throughout the course of the program. Feel free to change these values and see how it effects the program!

				
					byte saturation = 255;
byte value = 255;
				
			

In the loop section of the program, all possible hue values are looped through and displayed. Remember, hue is represented by a 16-bit value (0 – 65535).

				
					void loop() {
  for (long hue = 0; hue < 65536; hue++) {
    pixels.setPixelColor(0, pixels.ColorHSV(hue, saturation, value));
    pixels.setPixelColor(1, pixels.ColorHSV(hue, saturation, value));   
				
			

Every time the hue value is changed, the Neopixels are updated to provide a smooth transition between colors.

				
					pixels.show();
				
			

A small delay is incorporated to slow down the color cycling. This delay function uses microseconds to delay instead of milliseconds. 1 second is 1000 milliseconds and 1 millisecond is 1000 microseconds. That means that we are delaying 3 millionths of a second here! We use microseconds because we want the color to transition smoothly. There are 65,536 colors being displayed in the for-loop. Using the regular delay function between each one would make it look choppy and make the full color cycle take too long. Change the delayMicroseconds to delay and see what happens!

				
					    delayMicroseconds(3); 
  }
}
////////////////////////////////////////////////////////
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.