How to control brightness with potentiometer

I am very new to arduino coding and am having trouble writing an appropriate code for my application.

All I need to do is set the 12-light NeoPixel ring to a specified color set to be constant (no loop or chase) and use a potentiometer to adust the brightness level.

I am using the Adafruit Flora with a 12-light NeoPixel ring. I have the potentiometer wired up to GND, 3.3v, and pin #12.

I have used the following code with some success. I can at least see that the potentiometer is working by adjusting the color.

#include <Adafruit_NeoPixel.h>

#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

int sensorPin = 12; // select the input pin for the potentiometer (analog 1 is digital 2)
int sensorValue = 0; // variable to store the value coming from the sensor
int colorValue = 0;

void setup() {
// Set internal pullup resistor for sensor pin (analog 1 is digital 2)
pinMode(12, INPUT_PULLUP);
strip.begin();
strip.setBrightness(40); //adjust brightness here
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
colorValue = map(sensorValue, 0, 1024, 0, 255); //map sensor values from 0-124 to 0-255
for (int i = 0; i<strip.numPixels(); i++){
strip.setPixelColor(i, Wheel(colorValue)); //use Wheel function to set color
}
strip.show();
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

Any help would be greatly appreciated.

int sensorPin = 12;    // select the input pin for the potentiometer (analog 1 is digital 2)

Explain, please.

It was the only pinout on Flora that actually did anything.

Frankly at this point I'm looking for an entirely new code. I posted that to show where I am but I'm still not even close

If the color is changing then the pot is working. It seems like you just need to swap the code where you set the brightness with the code where you set the color, like this:

void setup() {
  // Set internal pullup resistor for sensor pin (analog 1 is digital 2)
  pinMode(12, INPUT_PULLUP);
  strip.begin();
  
  colorValue = 127; 
  for (int i = 0; i<strip.numPixels(); i++){
    strip.setPixelColor(i, Wheel(colorValue)); //use Wheel function to set color
  }

  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  strip.setBrightness( map(sensorValue, 0, 1024, 0, 255)); //adjust brightness here
  strip.show();               
}

I put in 127 for colorValue, you would change it as desired.

Thanks for the reply!

So, now it doesn't light up at all. Heres what I have now:

#include <Adafruit_NeoPixel.h>

#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

int sensorPin = 12;    // select the input pin for the potentiometer (analog 1 is digital 2)
int sensorValue = 0;  // variable to store the value coming from the sensor
int colorValue = 0;



void setup() {
  // Set internal pullup resistor for sensor pin (analog 1 is digital 2)
  pinMode(12, INPUT_PULLUP);
  strip.begin();
  
  colorValue = 127; 

  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  strip.setBrightness( map(sensorValue, 0, 1024, 0, 255)); //adjust brightness here
  strip.show();               
}

moderator edit: CODE TAGS

Okay, so I got it to work (Kinda) by changing the color value.... and even then only certain colors will dim (green being about the only one).. But when I dim all the way down it won't come back up.

The color red just comes on and immediately goes out.

#include <Adafruit_NeoPixel.h>

#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

int sensorPin = 12; // select the input pin for the potentiometer (analog 1 is digital 2)
int sensorValue = 0; // variable to store the value coming from the sensor
int colorValue = 0;

void setup() {
// Set internal pullup resistor for sensor pin (analog 1 is digital 2)
pinMode(12, INPUT_PULLUP);
strip.begin();

colorValue = 255;
for (int i = 0; i<strip.numPixels(); i++){
strip.setPixelColor(i, Wheel(colorValue)); //use Wheel function to set color
}

strip.show(); // Initialize all pixels to 'off'
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
strip.setBrightness( map(sensorValue, 0, 1024, 0, 255)); //adjust brightness here
strip.show();
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

Heres what I have now.

Most colors don't function properly and they all act different.. For instance 255 (green) starts on, then dims a little before turning off

There's a note on the Adafruit library guide that says that fast, repeated calls to setBrightness() give unpredictable results. That's what you're seeing here I think.
Try replacing your loop() with this:

void loop() {

//set the colour you want with these three variables (there are other ways)
byte red = 20;
byte green = 200;
byte blue = 255;

  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

//turn the sensor value into a scaling factor for the colours
float brightness = sensorValue / 1024.0;

    for (int i = 0; i<strip.numPixels(); i++){
    strip.setPixelColor(i, red * brightness, green * brightness, blue * brightness); 
  }
  strip.show();               
}

Forgive me a few mistakes here - I don't have the IDE on this computer.

After posting I did see a post on the Adafruit forum saying that you should always reset the color when you are resetting the brightness, which I guess is what the comment "For a non-destructive change, you'll need to re-render the full strip data" in the library code is getting at.

Hey there,
i read so many posts about dimming a WS2812b with a potentiometer but I can´t solve my problem.
So i switch the colors by a button (works!) but if Itry do dim up or down it takes effect at the next color at least.
So, first color is 1, while it shows me this color i dim it down, nothing happens, but if i switch to color 2, it is dimmed down... while I have the live-color, my potentiometer doesn´t have any effect...
Maybe somebody can see my newbie - mistake(s) in my code?

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    9   // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 12   // number of neopixel (change this accordingly)



// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
#include <Wire.h> // Wire Bibliothek einbinden
#include <LiquidCrystal_I2C.h> // Vorher hinzugefügte LiquidCrystal_I2C Bibliothek einbinden
LiquidCrystal_I2C lcd(0x27, 16, 2);

bool oldState = HIGH;
int showType = 0;
int sensorPin = A0;    // select the input pin for the potentiometer (analog 1 is digital 2)
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  lcd.init(); //Im Setup wird der LCD gestartet 
  lcd.backlight();
  pinMode(A0, INPUT_PULLUP);
  Serial.begin(9600);
  pinMode(A0, INPUT_PULLUP);
}

void loop() {
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability 
  sensorValue = analogRead(sensorPin);
  strip.setBrightness( map(sensorValue, 0, 1024, 0, 255)); //adjust brightness here
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 4)
        showType=0;
      startShow(showType);
}
  }
  // Set the last button state to the old state.
  oldState = newState;

}

void startShow(int i) {
  switch(i){
    case 0: tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone ( x , y )" ein Ton abgegeben.
            delay(1000); // mit einer Dauer von 1 Sekunde
            noTone(8); // Der Ton wird abgeschaltet
            colorWipe(strip.Color(0, 0, 0), 50); // Black/off
            lcd.setCursor(0, 0);//Hier wird die Position des ersten Zeichens festgelegt. In diesem Fall bedeutet (0,0) das erste Zeichen in der ersten Zeile. 
            lcd.print("Die LED leuchtet"); 
            lcd.setCursor(0, 1);// In diesem Fall bedeutet (0,1) das erste Zeichen in der zweiten Zeile. 
            lcd.print("nicht"); 
            
            break;
    case 1: tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone ( x , y )" ein Ton abgegeben.
            delay(1000); // mit einer Dauer von 1 Sekunde
            noTone(8); // Der Ton wird abgeschaltet
            colorWipe(strip.Color(255, 0, 0), 50);  // Red
            lcd.setCursor(0, 0);//Hier wird die Position des ersten Zeichens festgelegt. In diesem Fall bedeutet (0,0) das erste Zeichen in der ersten Zeile. 
            lcd.print("Die LED leuchtet"); 
            lcd.setCursor(0, 1);// In diesem Fall bedeutet (0,1) das erste Zeichen in der zweiten Zeile. 
            lcd.print("rot  ");
            break;
    case 2: tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone ( x , y )" ein Ton abgegeben.
            delay(1000); // mit einer Dauer von 1 Sekunde
            noTone(8); // Der Ton wird abgeschaltet
            colorWipe(strip.Color(0, 255, 0), 50);  // Green
            lcd.setCursor(0, 0);//Hier wird die Position des ersten Zeichens festgelegt. In diesem Fall bedeutet (0,0) das erste Zeichen in der ersten Zeile. 
            lcd.print("Die LED leuchtet"); 
            lcd.setCursor(0, 1);// In diesem Fall bedeutet (0,1) das erste Zeichen in der zweiten Zeile. 
            lcd.print("gruen");
            break;
    case 3: tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone ( x , y )" ein Ton abgegeben.
            delay(1000); // mit einer Dauer von 1 Sekunde
            noTone(8); // Der Ton wird abgeschaltet
            colorWipe(strip.Color(0, 0, 255), 50);  // Blue
            lcd.setCursor(0, 0);//Hier wird die Position des ersten Zeichens festgelegt. In diesem Fall bedeutet (0,0) das erste Zeichen in der ersten Zeile. 
            lcd.print("Die LED leuchtet"); 
            lcd.setCursor(0, 1);// In diesem Fall bedeutet (0,1) das erste Zeichen in der zweiten Zeile. 
            lcd.print("blau ");
            break;
    case 4: tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone ( x , y )" ein Ton abgegeben.
            delay(1000); // mit einer Dauer von 1 Sekunde
            noTone(8); // Der Ton wird abgeschaltet
            colorWipe(strip.Color(127, 127, 127), 50); // White
            lcd.setCursor(0, 0);//Hier wird die Position des ersten Zeichens festgelegt. In diesem Fall bedeutet (0,0) das erste Zeichen in der ersten Zeile. 
            lcd.print("Die LED leuchtet"); 
            lcd.setCursor(0, 1);// In diesem Fall bedeutet (0,1) das erste Zeichen in der zweiten Zeile. 
            lcd.print("weiss");
            break;
    
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

Thanks for using code tags. Please don't reply to threads that are 4 years old, just because they are on the same subject. It is highly unlikely that either your problem or the solution has much in common with the old thread. Please start your own thread.

OK aarg, I will open a new thread, thank you!