Menu
CoddyTech

NeoPixel Brightness

Part of the Displays and Sensors section of Coddy's Arduino journey. Lesson 16 of 24.

NeoPixels are bright. ring.setBrightness(level) turns them all down at once: 0 is off, 255 is full.

It works by scaling the colours stored in the board's memory. At brightness 64, about a quarter, a red of 255 is stored as 64. The catch: the scaling throws detail away. Turn the brightness down to 0 and every stored colour becomes 0, so turning it back up has nothing left to scale, and the ring stays dark.

So whenever the brightness changes, set the colours again after it:

ring.setBrightness(level);
ring.fill(white);   // stored at the new brightness
ring.show();

Colours set after setBrightness() are scaled as they are stored.

challenge icon

Challenge

Medium

The knob on A0 is a dimmer for the ring:

  • turn the reading into a brightness from 0 to 255 with map()
  • set it with ring.setBrightness()
  • then fill the ring white and show it, on every pass of loop()

Press Run and turn the knob, then Check.

Try it yourself

#include <Adafruit_NeoPixel.h>

// 16 pixels, data on pin 6
Adafruit_NeoPixel ring(16, 6, NEO_GRB + NEO_KHZ800);

uint32_t white = ring.Color(255, 255, 255);

void setup() {
  ring.begin();
}

void loop() {
  int reading = analogRead(A0);
  // set the brightness from the knob, then light the whole ring white
  delay(50);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Displays and Sensors

Practice on your own: Arduino playground