John Bradnam
Published © GPL3+

WS2812B Tester

A test system for up to 255 WS2812B RGB LEDs. It can test rings, panels and strips built from WS2812B LEDs.

IntermediateFull instructions provided5 hours7,464
WS2812B Tester

Things used in this project

Hardware components

Arduino Pro Mini 328 - 5V/16MHz
SparkFun Arduino Pro Mini 328 - 5V/16MHz
×1
DC-DC Buck Step Down Module 3.3V 5V 9V 12V 3A Adjustable Voltage Regulator Power
×1
6mm x 6mm x 17mm Tactile switch
×3
0.56in Common Cathode 3-Digit 7 Segment Display
×1
10K Resistor 0805 SMD
×4
330R Resistor 0805 SMD
×7
0.01R 1% Resistor 1206 SMD
×1
100K Resistor 0805 SMD
×1
2K2 Resistor 0805 SMD
×1
1K Resitsor 0805 SMD
×4
330R Resistor 1/4W 5%
×1
2N3904 NPN Transistor SMD
×3
0.1uF Capacitor 0805 SMD
×3
DC Power Supply Jack Socket Female Panel Mount Connector 5.5 x 2.1mm
×1
Plastic Electronic Project Box Enclosure Instrument Case 100x60x25mm
×1
WS2812 Addressable LED Strip
Digilent WS2812 Addressable LED Strip
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

PCB

Eagle Files

Schematic and Board layout in Eagle format

Code

WS2812BTesterV4.ino

C/C++
//Author: jbrad2089@gmail.com
//V3: Added Brightness mode and all white mode
//V4: Added Current meter mode

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#include "Display.h"
#include "Button.h"

#define PIN_LED 12
#define PIN_HUNDRED 9
#define PIN_TEN 10
#define PIN_ONE 11
#define PIN_SEG_A 8
#define PIN_SEG_B 2
#define PIN_SEG_C 4
#define PIN_SEG_D 5
#define PIN_SEG_E 6
#define PIN_SEG_F 7
#define PIN_SEG_G 3
#define PIN_MODE 13
#define PIN_DOWN A0
#define PIN_UP A1
#define PIN_VOLT A2
#define PIN_AMP A3

//define modes to set time
enum ModeEnum { COUNT, BRIGHTNESS, PATTERN, CURRENT };
int currentMode;
bool modeButtonPressed = false;

// 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)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN_LED, NEO_GRB + NEO_KHZ800);
Display display = Display();
Button mode = Button(PIN_MODE);
Button down = Button(PIN_DOWN);
Button up = Button(PIN_UP);

//Storage for current values
int pixels = 1;
int red = 128;
int green = 128;
int blue = 128;
int brightness = 128;
int pattern = 1;
int maxCurrent = 0;

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() 
{
  Serial.begin(115200);
  
  //Pixel Strip
  Serial.println("Adafruit_NeoPixel");
  strip.begin();
  strip.setBrightness(brightness);
  strip.show(); // Initialize all pixels to 'off'

  //Volt and Amp Meter
  pinMode(PIN_VOLT,INPUT);
  pinMode(PIN_AMP,INPUT);

  //7 Seg display
  Serial.println("Display");
  display.Initialise(PIN_SEG_A, PIN_SEG_B, PIN_SEG_C, PIN_SEG_D, PIN_SEG_E, PIN_SEG_F, PIN_SEG_G, -1, 3, PIN_ONE, PIN_TEN, PIN_HUNDRED);
  currentMode = COUNT;
  display.ShowChar(_C2, 2);
  display.ShowValue(pixels, HEX, 2);

  //Button callbacks
  mode.Background(ButtonBackground);
  down.Repeat(DownButtonPressed);
  down.Background(ButtonBackground);
  up.Repeat(UpButtonPressed);
  up.Background(ButtonBackground);

  //Set ISR for pin change on MODE pin
  Button::PinChangeSetup(PIN_MODE);
}

void loop() 
{
  delay(5);
  display.Multiplex();

  //Handle polling of up and down repeating buttons
  up.Pressed();
  down.Pressed();

  if (mode.Pressed())
  {
    modeButtonPressed = false;
    switch (currentMode)
    {
      case CURRENT:
        currentMode = COUNT;
        clear();
        display.ShowChar(_C2, 2);
        display.ShowValue(pixels, HEX, 2);
        break;
      
      case COUNT:
        currentMode = BRIGHTNESS;
        strip.updateLength(pixels);
        display.ShowChar(_B, 2);
        display.ShowValue(brightness, HEX, 2);
        break;
      
      case BRIGHTNESS:
        strip.setBrightness(brightness);
        currentMode = PATTERN;
        display.ShowChar(_P, 2);
        display.ShowValue(pattern, HEX, 2);
        break;
      
      case PATTERN:
        maxCurrent = 0;
        display.ShowChar(_HYPHEN, 2);
        display.ShowChar(_HYPHEN, 1);
        display.ShowChar(_HYPHEN, 0);
        display.MultiplexAll();
        switch (pattern)
        {
          case 1: colorWipe(strip.Color(255, 0, 0), 50); break; // Red
          case 2: colorWipe(strip.Color(0, 255, 0), 50); break; // Green
          case 3: colorWipe(strip.Color(0, 0, 255), 50); break; // Blue
          case 4: colorWipe(strip.Color(255, 255, 255), 50); break; // White
          case 5: theaterChase(strip.Color(127, 127, 127), 50); break; // White
          case 6: theaterChase(strip.Color(0, 127, 0), 50); break; // Green                                                  
          case 7: rainbow(20); break;
          case 8: rainbowCycle(20); break;
          case 9: theaterChaseRainbow(50); break;
        }
        if (modeButtonPressed && mode.State() == LOW)
        {
          mode.Pressed();
        }

        //Switch to current mode
        currentMode = CURRENT;
        if (maxCurrent > 9999)
        {
          maxCurrent = 9999;
        }
        display.ShowValue(maxCurrent);
        break;
    }
      
  }
}

//Mode button interrupt to break out of loops etc
//PCINT1 handles pin changes for pins for D8 to D13
ISR (PCINT0_vect)
{
  modeButtonPressed = modeButtonPressed | (mode.State() == LOW);
}

void ButtonBackground(void)
{
  display.MultiplexAll();
}

//Invoked when DOWN button is pressed
void DownButtonPressed(void)
{
    switch (currentMode)
    {
      case COUNT:
        pixels = max(pixels - 1, 1); 
        display.ShowValue(pixels, HEX, 2); 
        break;
        
      case BRIGHTNESS:
        brightness = max(brightness - 1, 1); 
        display.ShowValue(brightness, HEX, 2); 
        break;
        
      case PATTERN:
        pattern = max(pattern - 1, 1); 
        display.ShowValue(pattern, HEX, 2); 
        break;
    }
}

void UpButtonPressed(void)
{
    switch (currentMode)
    {
      case COUNT: 
        pixels = min(pixels + 1, 255); 
        display.ShowValue(pixels, HEX, 2); 
        break;
      
      case BRIGHTNESS: 
        brightness = min(brightness + 1, 255); 
        display.ShowValue(brightness, HEX, 2); 
        break;
      
      case PATTERN:
        pattern = min(pattern + 1, 9); 
        display.ShowValue(pattern, HEX, 2); 
        break;
    }
}

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

// 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() && !modeButtonPressed; i++) 
  {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
  ReadCurrent();
}

void rainbow(uint8_t wait) 
{
  for(uint16_t j=0; j < 256 && !modeButtonPressed; j++) 
  {
    for(uint16_t i=0; i < strip.numPixels() && !modeButtonPressed; i++) 
	  {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
  ReadCurrent();
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) 
{
  for(uint16_t j=0; j < 256*5 && !modeButtonPressed; j++) 
  { // 5 cycles of all colors on wheel
    for(uint16_t i=0; i < strip.numPixels() && !modeButtonPressed; i++) 
	  {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
  ReadCurrent();
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) 
{
  for (int j=0; j < 10 && !modeButtonPressed; j++) 
  {  //do 10 cycles of chasing
    for (int q=0; q < 3 && !modeButtonPressed; q++) 
	{
      for (uint16_t i=0; i < strip.numPixels() && !modeButtonPressed; i=i+3) 
	  {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
      ReadCurrent();
      delay(wait);

      for (uint16_t i=0; i < strip.numPixels() && !modeButtonPressed; i=i+3) 
	    {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) 
{
  for (int j=0; j < 256 && !modeButtonPressed; j++) 
  {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3 && !modeButtonPressed; q++) 
    {
      for (uint16_t i=0; i < strip.numPixels() && !modeButtonPressed; i=i+3) 
	    {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();
      ReadCurrent();
      delay(wait);

      for (uint16_t i=0; i < strip.numPixels() && !modeButtonPressed; i=i+3) 
	    {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// 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) 
{
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) 
  {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) 
  {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

float ReadCurrent()
{
  // taking the average for stable readings
  float current = 0;
//float voltage = 0;
//float power = 0;

  for (int i=0; i<20; i++) 
  {
    current = current + analogRead(PIN_AMP);
    //voltage = voltage + analogRead(PIN_VOLT); 
  }
  current = (current / 20);
  Serial.print("Raw =  " + String(current));
  //RAW => MEASURED => FACTOR
  //20.5 => 152.4mA => 7.43
  //11.8 => 86.7mA =>  7.35
  //33.85 => 252mA =>  7.44
  //11.95 => 87.1 => 7.28
  //7.15 => 53.8 => 7.52
  //7.65 => 55.3 => 7.22
  //49.1 => 360 => 7.33
  current = current * 7.36; // calibration value,to be changed according to components used
  Serial.print(", Calc =  " + String(current));
  
  //voltage = (voltage / 20); 
  //voltage = voltage * 0.0508 * 5.0; // calibration value,to be changed according to components used
  //power = voltage * current;

  maxCurrent = max(maxCurrent, round(current));
  Serial.println(", Max = " + String(maxCurrent));
  return current;
}

Button.cpp

C/C++
#include "Button.h"

Button::Button(int pin)
{
	_pin = pin;
	pinMode(_pin, INPUT);
}

//Set function to invoke in a delay or repeat loop
void Button::Background(void (*pBackgroundFunction)())
{
  _backgroundCallback = pBackgroundFunction;
}

//Set function to invoke if repeat system required
void Button::Repeat(void (*pRepeatFunction)())
{
  _repeatCallback = pRepeatFunction;
}

void Button::PinChangeSetup(byte pin) 
{
  *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin));  // enable pin
  PCIFR  |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  PCICR  |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group 
}


//Tests if a button is pressed and released
//  returns true if the button was pressed and released
//	if repeat callback supplied, the callback is called while the key is pressed
bool Button::Pressed()
{
  bool pressed = false;
  if (digitalRead(_pin) == LOW)
  {
    unsigned long wait = millis() + DEBOUNCE_DELAY;
    while (millis() < wait)
    {
      if (_backgroundCallback != NULL)
      {
        _backgroundCallback();
      }
    }
    if (digitalRead(_pin) == LOW)
    {
  	  //Set up for repeat loop
  	  if (_repeatCallback != NULL)
  	  {
  	    _repeatCallback();
  	  }
  	  unsigned long speed = REPEAT_START_SPEED;
  	  unsigned long time = millis() + speed;
      while (digitalRead(_pin) == LOW)
      {
        if (_backgroundCallback != NULL)
        {
          _backgroundCallback();
        }
    		if (_repeatCallback != NULL && millis() >= time)
    		{
    		  _repeatCallback();
    		  unsigned long faster = speed - REPEAT_INCREASE_SPEED;
    		  if (faster >= REPEAT_MAX_SPEED)
    		  {
    			  speed = faster;
    		  }
    		  time = millis() + speed;
    		}
      }
      pressed = true;
    }
  }
  return pressed;
}

//Return current button state
int Button::State()
{
	return digitalRead(_pin);
}

Button.h

C Header File
/*
Class: Button
Author: John Bradnam (jbrad2089@gmail.com)
Purpose: Arduino library to handle buttons
*/
#pragma once
#include "Arduino.h"

#define DEBOUNCE_DELAY 5

//Repeat speed
#define REPEAT_START_SPEED 500
#define REPEAT_INCREASE_SPEED 50
#define REPEAT_MAX_SPEED 50

class Button
{
	public:
		//Simple constructor
		Button(int Pin);
    //Background function called when in a wait or repeat loop
    void Background(void (*pBackgroundFunction)());
		//Repeat function called when button is pressed
    void Repeat(void (*pRepeatFunction)());
		//Test whether button is pressed and released
		//Will call repeat function if one is provided
		bool Pressed();
		//Return button state (HIGH or LOW) - LOW = Pressed
		int State();
    //Pin Change Interrupt Setup
    //ISR (PCINT0_vect) pin change interrupt for D8 to D13 
    //ISR (PCINT1_vect) pin change interrupt for A0 to A5 
    //ISR (PCINT2_vect) pin change interrupt for D0 to D7
    static void PinChangeSetup(byte pin);

	private:
		int _pin;
		void (*_repeatCallback)(void);
    void (*_backgroundCallback)(void);

};

Display.cpp

C/C++
#include "Display.h"

Display::Display() 
{
}

void Display::Initialise(int aPin, int bPin, int cPin, int dPin, int ePin, int fPin, int gPin, int dpPin, int digits, ...) 
{
	_aPin = aPin;
	_bPin = bPin;
	_cPin = cPin;
	_dPin = dPin;
	_ePin = ePin;
	_fPin = fPin;
	_gPin = gPin;
	_dpPin = dpPin;

	pinMode(_aPin, OUTPUT);
	pinMode(_bPin, OUTPUT);
	pinMode(_cPin, OUTPUT);
	pinMode(_dPin, OUTPUT);
	pinMode(_ePin, OUTPUT);
	pinMode(_fPin, OUTPUT);
	pinMode(_gPin, OUTPUT);

	if (_dpPin != -1)
	{
		pinMode(_dpPin, OUTPUT);
	}

	_digits = digits;
	va_list valist;
	va_start(valist, digits);
	for (int i = 0; i < digits; i++) 
	{
		int pin = va_arg(valist, int);
		_digitPins[i] = pin;
		pinMode(pin, OUTPUT);
	}
	va_end(valist);
	_multiplex = 0;
}

void Display::Multiplex()
{
	if (_multiplex < _digits)
	{
		//Clear all other digits
		for (int i = 0; i < _digits; i++) 
		{
      digitalWrite(_digitPins[i], LOW);
		}

		//Set the segments
		unsigned char c = _digitBuffer[_multiplex];
		digitalWrite(_aPin, ((c & 0x01) > 0) ? HIGH : LOW);
		digitalWrite(_bPin, ((c & 0x02) > 0) ? HIGH : LOW);
		digitalWrite(_cPin, ((c & 0x04) > 0) ? HIGH : LOW);
		digitalWrite(_dPin, ((c & 0x08) > 0) ? HIGH : LOW);
		digitalWrite(_ePin, ((c & 0x10) > 0) ? HIGH : LOW);
		digitalWrite(_fPin, ((c & 0x20) > 0) ? HIGH : LOW);
		digitalWrite(_gPin, ((c & 0x40) > 0) ? HIGH : LOW);
		digitalWrite(_dpPin, ((c & 0x80) > 0) ? HIGH : LOW);

    digitalWrite(_digitPins[_multiplex], HIGH);
    

    //Serial.print("Digit: ");
    //Serial.print(_multiplex, DEC);
    //Serial.print(", Segments: ");
    //Serial.println(c, HEX);
   
		//Set up for next
		_multiplex++;
		if (_multiplex >= _digits)
		{
			_multiplex = 0;
		}
	}
}

void Display::MultiplexAll()
{
  for (_multiplex = 0; _multiplex < _digits; _multiplex++)
  {
    Multiplex();
  }
  _multiplex = 0;
}

void Display::ShowChar(unsigned char c, int digit)
{
	if (digit >= 0 && digit < _digits && c >= 0 && c < _LAST)
	{
		_digitBuffer[digit] = pgm_read_byte(CH + c);
	}
}

void Display::ShowValue(int value, int radix, int width)
{
	int index = 0;
	while (index < width && index < _digits)
	{
    int c = value % radix;
		_digitBuffer[index] = pgm_read_byte(CH + c);
		value = value / radix;
    index++;
	}
}

void Display::ShowValue(int value)
{
  bool dp = false;
  if (value > 999)
  {
    value = value / 100;
    dp = true;
  }
  for (int i = 0; i < 3; i++)
  {
    _digitBuffer[i] = pgm_read_byte(CH + (value % 10));
    value = value / 10;
  }
  if (dp)
  {
    _digitBuffer[2] = _digitBuffer[1];
    _digitBuffer[1] = pgm_read_byte(CH + _HYPHEN);
  }
}

Display.h

C Header File
/*
Class: Display
Author: John Bradnam (jbrad2089@gmail.com)
Purpose: Arduino library to handle directly connected 7 segment displays
*/
#pragma once
#include "Arduino.h"

enum CharEnum { _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _A, _B, _C, _D, _E, _F, _R, _G, _C2, _P, _HYPHEN, _LAST};


//dp g f e d c b a
//
//pgfedcba
const unsigned char CH[] PROGMEM = {
 B00111111, // 0
 B00000110, // 1
 B01011011, // 2
 B01001111, // 3
 B01100110, // 4
 B01101101, // 5
 B01111101, // 6
 B00100111, // 7
 B01111111, // 8
 B01100111, // 9
 B01110111, // a
 B01111100, // b
 B00111001, // c
 B01011110, // d
 B01111001, // e
 B01110001, // f
 B01010000, // r
 B01101111, // g
 B01011000, // c2
 B01110011, // P
 B01000000, // -
};

class Display
{
	public:
		Display();
		//aPin..dpPin - Pins that the anode segments are connected to (via current limiting resistors)
		//If you don't use dpPin, pass a pin value of -1
		//digits - The number of digits in the display
		//digit1..n - Pins that drive NPN transistors that connect the common cathodes to ground. (Least significant to most significant)
		void Initialise(int aPin, int bPin, int cPin, int dPin, int ePin, int fPin, int gPin, int dpPin, int digits, ...);
		void Multiplex();
    void MultiplexAll();
		void ShowChar(unsigned char c, int digit);
		void ShowValue(int value, int radix, int width);
    void ShowValue(int value);

	private:
		int _aPin;
		int _bPin;
		int _cPin;
		int _dPin;
		int _ePin;
		int _fPin;
		int _gPin;
		int _dpPin;
		int _digits;
		int _digitPins[16];
		unsigned char _digitBuffer[16];
		int _multiplex;
};

Credits

John Bradnam

John Bradnam

141 projects • 167 followers
Thanks to Utsav_25.

Comments