Project 6.01 Changing Color with the Potentiometer

In this project, you'll harness the power of the potentiometer to change the color of the onboard NeoPixels using the HSV (Hue, Saturation, Value) color model!

This project will demonstrate the use of a potentiometer to control the color of the Neopixels. As the potentiometer rotates, the color of the Neopixels will change.

Project Code:

				
					/////////////////////////////////////////////////////
// 6.01 - Changing Color with Potentiometer

#include <Adafruit_NeoPixel.h>

byte value = 255;
byte saturation = 255;

byte dataPin = 10;
byte numberOfPixels = 2;
byte brightness = 10;

byte potPin = A0;

Adafruit_NeoPixel pixels(numberOfPixels, dataPin, NEO_GRB + NEO_KHZ800);
void setup() {
  Serial.begin(9600);

  pinMode(potPin, INPUT);

  pixels.begin();
  pixels.setBrightness(brightness);
}

void loop() {

  int potValue = analogRead(potPin);
  long colorValue = map(potValue, 0, 1023, 0, 65535);

  pixels.setPixelColor(0, pixels.ColorHSV(colorValue, saturation, value));
  pixels.setPixelColor(1, pixels.ColorHSV(colorValue, saturation, value));
  pixels.show();

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

*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.

After declaring variables, in the setup() function Serial is initialized, the pinMode of the potPin is set, the Neopixels are initialized and their brightness is set.

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

  pinMode(potPin, INPUT);

  pixels.begin();
  pixels.setBrightness(brightness);
}
				
			

In the loop() function, an int variable called potValue is created to hold the analog value read from the pot.

				
					int potValue = analogRead(potPin);
				
			

At this point we have a problem. We have a potentiometer value somewhere between 0 and 1023 (10-bit) but our color range goes from 0 – 65535 (16-bit). This is where the map function comes in. The map function allows us to map two value ranges together linearly. This is much less scary than it sounds. Essentially, it will make 0 on the potentiometer equal 0 on the color range, and 1023 on the potentiometer equal 65535 on the color range. For example, if the potentiometer value read was 543 the map function would return a value of 34785.

We can check this by using the equation:

  1. (potValue * MaxHueValue) / MaxPotValue = MappedValue
  2. (543 * 65535)/1023 = 34,785

The map function takes five parameters. It takes the value to map, the min value of that range, the max value of that range, the min value of the range to be mapped to, and the max value of the range to be mapped to. See the table below:

Parameters Value
Value to map analogValue read from pot
Min value of that range 0
Max value of that range 1023
Min value of the range to be mapped to 0
Max value of the range to be mapped to 65535
				
					long colorValue = map(potValue, 0, 1023, 0, 65535);
				
			

Lastly, the value that was mapped is written to the Neopixels and shown.

				
					pixels.setPixelColor(0, pixels.ColorHSV(colorValue, saturation, value));
pixels.setPixelColor(1, pixels.ColorHSV(colorValue, saturation, value));
pixels.show();
				
			

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.