Help with controlling two LED strips using different GPIOs/data pins for each

Hello,
This is my first time posting here so I hope I do everything right. I am trying to get some help with some programming for an LED project I am working on. Disclaimer, I am brand new to coding and projects involving boards or MCUs. I have tried a lot of googling, but I end up with a lot of results that I can’t understand due to their more advanced language or programming.

I am trying to learn how to control more than one LED (WS2812B) strip using a single ESP8266 ESP12. My project is basically a 3d printed statue that has the LEDs running inside of it creating light effects. I have currently gotten one LED strip working with code (basically just altered the cylon example code until it worked for me).

What I want to learn is how I can control a second LED strip. The first strip is off of data pin 3, so if I had a second LED strip off of data pin 4 how would I program that? For simplicity’s sake, for the example we can just have them run the same code.

I have everything wired up the standard way, with a capacitor and resistor where they need to be. I know that I will need to get a stronger power supply when both are running, but I am just trying to figure the code out first (feasibility) before purchasing more.

Here is my code. If anything is unclear or there are issues please let me know.

[code]
#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 144

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 3
//#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(84);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(250);
  }
}

void loop() {
  static uint8_t hue = 0;
  Serial.print("x");
  // First slide the led in one direction
  for (int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
  }

}

[/code]

Hardware:

LEDs (WS2812B RGB 5050SMD Individual Addressable)
https://www.amazon.com/gp/product/B01CDTEGGO/ref=ppx_yo_dt_b_asin_title_o03_s01?ie=UTF8&psc=1

Board (ESP8266 ESP-12)
https://www.amazon.com/gp/product/B081PX9YFV/ref=ppx_yo_dt_b_asin_title_o03_s01?ie=UTF8&psc=1

Power Supply (5V 10A Power Supply Adapter)
https://www.amazon.com/gp/product/B0852HL336/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1

You need 2 data pins

#define DATA_PIN1   3
#define DATA_PIN2   4

You need an array to store the LED values for each strip controller.

// Define the array of leds
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];

Then set the 2 controllers up in setup()

  LEDS.addLeds<WS2812B, DATA_PIN1, RGB>(leds1, NUM_LEDS);
  LEDS.addLeds<WS2812B, DATA_PIN2, RGB>(leds2, NUM_LEDS);

To use

 leds1[5] = CHSV(hue++, 255, 255);
 leds2[6] = CRGB::Red;

More than 2 I would use arrays of LED controllers. Here i have 4 strip controllers (one for each digit).

const byte DATA_PIN_1 = 6;
const byte DATA_PIN_2 = 5;
const byte DATA_PIN_3 = 4;
const byte DATA_PIN_4 = 7;

Array of LED value arrays

CRGB leds[NUM_DIGITS][NUM_LEDS];

controller setups

 LEDS.addLeds<WS2812, DATA_PIN_1, GRB>(leds[0], NUM_LEDS);
  LEDS.addLeds<WS2812, DATA_PIN_2, GRB>(leds[1], NUM_LEDS);
  LEDS.addLeds<WS2812, DATA_PIN_3, GRB>(leds[2], NUM_LEDS);
  LEDS.addLeds<WS2812, DATA_PIN_4, GRB>(leds[3], NUM_LEDS);

Use
// color black for digit 2, pixel 4.

leds[2][4] = CRGB::Black;  // leds[digit][n]

You could use twos strips, then you would need two of everything involved with one strip

an i/o pin to control the strip
an array of CRGB entities to hold the colors for the strip
an instantiation of the strip object

so this

LEDS.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

would appear twice, once for each strip

LEDS.addLeds<WS2812B, DATA_PIN_A, RGB>(ledsA, NUM_A_LEDS);
LEDS.addLeds<WS2812B, DATA_PIN_B, RGB>(ledsB, NUM_B_LEDS);

and change the calls to reflect which strip you are talking about.

As @groundfungus has detailed.

Much simpler is to have one physical strip, one i/o pin and one array of CRHBs and one call to addLeds, then use the strip addressability and logic to use the pixel index to refer either to, say, the first half of the pixels or the second:

If the two halves had 62 each, e.g., then the ‘i’ pixel in the first half is

leds[i] = CHSV(hue++, 255, 255);

and the same pixel in the second half would just be an offset

leds[i + 62] = CHSV(hue++, 255, 255);

Below your code, modified (and fixed a bit) untested but you’ll see the idea.

EDIT: I have fixed a few thinkos and this example runs good. I happened to have a 64 pixel strip on hand, so.

HTH

a7

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 64

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 7
//#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(84);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(250);
  }
}

void loop() {
  static uint8_t hue = 0;
  Serial.print("x");
  // First slide the led in one direction
  for (int i = 0; i < (NUM_LEDS / 2); i++) {
    // Set the i'th led to red
    leds[i] = CHSV(hue, 255, 255);
    leds[i + (NUM_LEDS / 2)] = CHSV(hue, 255, 255);
    hue++;
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
     leds[i] = CRGB::Black;
     leds[i + (NUM_LEDS / 2)] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
  }

}

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