Single color for WS2812B

Hi there,

I'm in desperate need of help with a school project.

I'm pretty new to Arduino and have been stuck on this one issue for a while now and still have no idea how to fix this small issue.

I am using the WS2812B light strips (also known as neopixels) and none of the tutorials online (or none that I have found) have shown he how to achieve a single solid color throughout the strip without flashing, swirls or rainbow colors.

I just want a solid color without any extras.

I would seriously appreciate it if anyone could help me with where I can find a tutorial or code.

Welcome to the forum

Which library are you using to control the LEDs ?

Do you know how to turn on a single LED with a particular colour ? Do you know how to address each LED in the strip ? Has the library got a function to fill the strip with a single colour ?

Hi, @batten01
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

If you have any code you have been using, can you please post it?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

more specific information:

  • I have an Arduino Uno board
  • the WS2812B strips are 5 meters long and are 5 V
  • I have been using the Adafruit_NeoPixel library but I am open to exploring a new one
  • the end goal is for the color of the LEDs to change with a change in temperature (for this I am using the DHT11 temperature and humidity sensory and the Simple.DHT library). the code for the sensor seems to be working so far.

Hi there,

So far I have been using the Adafruit_NeoPixel library. from what I know - the library does not include a function to fill the strip with a single color (I wish it did because then my problem would be solved). I have got almost no clue how to switch on a single LED but I do sort of know how to change colors.

hi,

Thanks - will definitely present my problems better in the future :slight_smile:
At the moment I do not have any code because I'm not sure of where or how to start.

That would be preposterous notion for a well known library like Adafruit. Perhaps you should polish on your searching skills. Took me 30 sec

/*!
@brief Fill all or part of the NeoPixel strip with a color.
@param c 32-bit color value. Most significant byte is white (for
RGBW pixels) or ignored (for RGB pixels), next is red,
then green, and least significant byte is blue. If all
arguments are unspecified, this will be 0 (off).
@param first Index of first pixel to fill, starting from 0. Must be
in-bounds, no clipping is performed. 0 if unspecified.
@param count Number of pixels to fill, as a positive value. Passing
0 or leaving unspecified will fill to end of strip.
*/

The method is called, believe it or not, fill

That library is fine for what you want to do. You have obviously looked and and hopefully tried the examples so should be able to turn on a single LED with a colour of your choice.

In fact the example by the name of "simple" sets all the LEDS in a strand to the same colour

How are the LEDs powered and how many of them are there in the strip(s) ?

You are lucky; NeoPixels allows to set all Leds to same color in once, I modified their example a little bit to show you how to do it:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library

// Modified by ec2021 to set all "pixels" to the same color
// To show an effect, the color Red goes from zero to 250 in steps of 50
// And starts again, 

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN        6 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16 // Popular NeoPixel ring size

// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}

int Red = 0;

void loop() {
 // The clear can be done every loop() or just once in setup ....
  pixels.clear(); // Set all pixel colors to 'off'

  pixels.fill(pixels.Color(Red, 0, 0));
  /*
  // If you want to control each Led separately, you can do it this way
  // The first NeoPixel in a strand is #0, second is 1, all the way up
  // to the count of pixels minus one.
   for(int i=0; i<NUMPIXELS; i++) { // For each pixel...

    // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
    // Here we're using a changing Red color:
    pixels.setPixelColor(i, pixels.Color(Red, 0, 0));


  }
  */
    pixels.show();   // Send the updated pixel colors to the hardware.
    Red += 50;
    Red &= 255;
 // Pause before next pass through loop
  delay(DELAYVAL);
}

You may have to "adjust" the NUMPIXELS value to the number of Leds in your stripe (but ONLY if you want to control each Led separately!) and the color control to the RGB values you need.

Be aware that this is solely an example; delays in the loop() are not recommended for sketches that are intended for more interaction (e.g. with serial or buttons).

If you want to play around a little bit with this sketch to get acquainted to its use feel free to try this

https://wokwi.com/arduino/projects/323112464806838867

1 Like

oh no, will look better in future. I have tried it before and it didn't seem to work - maybe something to do with my board. Will try again though. thank you :slight_smile:

The example sketch from my post works in the simulator, at least ... Maybe you have to check the initialization parameters?!?

In the current strip that I am practicing on there are 60 LEDs. The problem with the example you suggested called "simple" is that the further down the strip you go the LEDs start flashing violently. I am not sure ho to prevent this from happening.

Would you mind posting your actual code?

And telling us how the LEDs are wired and powered

1 Like

Are you sure that this matches the strip you are using?

In particular, weird unwanted color effects can be fixed by changing this part to agree with the strip you have:

NEO_GRB

Also, as far as filling the strip, fill works, but you could also code your own loop to do the same thing. Take a look at the loops that do the fancy stuff.

a7

Correct: That's my question from post # 11 (initialization params).

Correct: It is in my example in post # 9 (as a comment in loop() ).

P.S.: As I do not have access to NeoPixels I used their example configuration that worked well with Wokwi. Not the best thing to do, but at least the sketch can be compiled and tested ...

Yes, sry. I just wanted to aim the OP at the one that always bytes me.

As for code in comments, I never read comments. Probably a bad idea for code you write, but most of time comments are less then useful. :wink:

a7

Well known issue, especially when the comments just repeat what everybody can read a line later or below .

@batten01: I recon that @alto777 is in a much better shape to assist you with NeoPixels as I have not been bitten by them so far :wink: So go for the more practical knowledge!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.