Go Down

Topic: Shift PWM - having trouble controlling individual 595 outputs (Read 211 times) previous topic - next topic


hello. i've started working with the Shift PWM library, written by elco jacobs... http://www.elcojacobs.com/using-shiftpwm-to-control-rgb-leds-with-arduino/

i have 2 rgb leds hooked up to one shift register through the 595's Q0-Q5 outputs...

i run the test code from the examples... and its supposed to flash all outputs quickly, then slowly, then cycle through every two outputs, then do individual outputs, then randomly flash colors on both leds...

what i would expect to see is a quick flash of both LEDS, followed by a slow flash, followed by slow pulses of red/green... then green/blue... then blue from the first LED, and red from the second, etc... and then  go through each color on each LED, and finish with random colors flashing on both...

instead, i only see the first two flashes, and the parts of code where it should be cycling through the other outputs, the LEDs are just dimly glowing all outputs... RGB all just kindof barely on...the only thing that is different is when its supposed to randomly fire the different colors, it just kindof flickers all RGB at the same time...

long story short the code isn't doing what i want it to do... or maybe its the way i've wired it...

not sure.

anyway, here is the code... and some pics of my wiring... hopefully someone has had some similar experiences or has an idea what might be causing this and can help me fix it...

thank you for your help!

Code: [Select]
 * ShiftPWM blocking RGB fades example, (c) Elco Jacobs, updated August 2012.
 * ShiftPWM blocking RGB fades example. This example uses simple delay loops to create fades.
 * If you want to change the fading mode based on inputs (sensors, buttons, serial), use the non-blocking example as a starting point.
 * Please go to www.elcojacobs.com/shiftpwm for documentation, fuction reference and schematics.
 * If you want to use ShiftPWM with LED strips or high power LED's, visit the shop for boards.
// ShiftPWM uses timer1 by default. To use a different timer, before '#include <ShiftPWM.h>', add
// #define SHIFTPWM_USE_TIMER2  // for Arduino Uno and earlier (Atmega328)
// #define SHIFTPWM_USE_TIMER3  // for Arduino Micro/Leonardo (Atmega32u4)

// Clock and data pins are pins from the hardware SPI, you cannot choose them yourself if you use the hardware SPI.
// Data pin is MOSI (Uno and earlier: 11, Leonardo: ICSP 4, Mega: 51, Teensy 2.0: 2, Teensy 2.0++: 22)
// Clock pin is SCK (Uno and earlier: 13, Leonardo: ICSP 3, Mega: 52, Teensy 2.0: 1, Teensy 2.0++: 21)

// You can choose the latch pin yourself.
const int ShiftPWM_latchPin=8;

// ** uncomment this part to NOT use the SPI port and change the pin numbers. This is 2.5x slower **
 const int ShiftPWM_dataPin = 4;
 const int ShiftPWM_clockPin = 7;

// If your LED's turn on if the pin is low, set this to true, otherwise set it to false.
const bool ShiftPWM_invertOutputs = true;

// You can enable the option below to shift the PWM phase of each shift register by 8 compared to the previous.
// This will slightly increase the interrupt load, but will prevent all PWM signals from becoming high at the same time.
// This will be a bit easier on your power supply, because the current peaks are distributed.
const bool ShiftPWM_balanceLoad = false;

#include <ShiftPWM.h>   // include ShiftPWM.h after setting the pins!

// Here you set the number of brightness levels, the update frequency and the number of shift registers.
// These values affect the load of ShiftPWM.
// Choose them wisely and use the PrintInterruptLoad() function to verify your load.
// There is a calculator on my website to estimate the load.

unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 75;
int numRegisters = 1;
int numRGBleds = numRegisters*8/3;

void setup(){

  // Sets the number of 8-bit registers that are used.

  // SetPinGrouping allows flexibility in LED setup.
  // If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4).
  ShiftPWM.SetPinGrouping(1); //This is the default, but I added here to demonstrate how to use the funtion

void loop()
  // Turn all LED's off.

  // Print information about the interrupt frequency, duration and load on your program

  // Fade in and fade out all outputs one by one fast. Usefull for testing your hardware. Use OneByOneSlow when this is going to fast.

  // Fade in all outputs
  for(int j=0;j<maxBrightness;j++){
  // Fade out all outputs
  for(int j=maxBrightness;j>=0;j--){

  // Fade in and out 2 outputs at a time
  for(int output=0;output<numRegisters*8-1;output++){
    for(int brightness=0;brightness<maxBrightness;brightness++){

  // Hue shift all LED's
  for(int hue = 0; hue<360; hue++){
    ShiftPWM.SetAllHSV(hue, 255, 255);

  // Alternate LED's in 6 different colors
  for(int shift=0;shift<6;shift++){
    for(int led=0; led<numRGBleds; led++){
      case 0:
        ShiftPWM.SetRGB(led,255,0,0);    // red
      case 1:
        ShiftPWM.SetRGB(led,0,255,0);    // green
      case 2:
        ShiftPWM.SetRGB(led,0,0,255);    // blue
      case 3:
        ShiftPWM.SetRGB(led,255,128,0);  // orange
      case 4:
        ShiftPWM.SetRGB(led,0,255,255);  // turqoise
      case 5:
        ShiftPWM.SetRGB(led,255,0,255);  // purple

  // Update random LED to random color. Funky!
  for(int i=0;i<1000;i++){

  // Immitate a VU meter
  int peak=0;
  int prevPeak=0;

  int currentLevel = 0;
  for(int i=0;i<40;i++){
    prevPeak = peak;
      peak =  random(numRGBleds); // pick a new peak value that differs at least 5 from previous peak
    // animate to new top
      for(int led=0;led<numRGBleds;led++){
          int hue = (numRGBleds-1-led)*120/numRGBleds; // From green to red
      delay((64/numRGBleds)*(numRGBleds-currentLevel)); // go slower near the top

  //  A moving rainbow for RGB leds:
  rgbLedRainbow(numRGBleds, 5, 3, numRegisters*8/3); // Fast, over all LED's
  rgbLedRainbow(numRGBleds, 10, 3, numRegisters*8/3*4); //slower, wider than the number of LED's

void rgbLedRainbow(int numRGBLeds, int delayVal, int numCycles, int rainbowWidth){
  // Displays a rainbow spread over a few LED's (numRGBLeds), which shifts in hue.
  // The rainbow can be wider then the real number of LED's.

  for(int cycle=0;cycle<numCycles;cycle++){ // loop through the hue shift a number of times (numCycles)
    for(int colorshift=0;colorshift<360;colorshift++){ // Shift over full color range (like the hue slider in photoshop)
      for(int led=0;led<numRGBLeds;led++){ // loop over all LED's
        int hue = ((led)*360/(rainbowWidth-1)+colorshift)%360; // Set hue from 0 to 360 from first to last led and shift the hue
        ShiftPWM.SetHSV(led, hue, 255, 255); // write the HSV values, with saturation and value at maximum
      delay(delayVal); // this delay value determines the speed of hue shift

also, thought i should mention... i've got an IR receiver hooked up to the board as well... digital pin 2 goes to the IR receiver output, digital pins 4,7, and 8 go to the 595's data, clock, and latch respectively...


or maybe its the way i've wired it...
Neat though the wiring is it is not a schematic so it is almost impossible to say if you have done it right. However I don't see any supply decoupling capacitors. Can you post your schematic.

Don't be so fancy, just get the basic shift out examples working first to make sure your hardware is working. Only when it is the move on to trying the shift PWM code to work.


i have 2 rgb leds hooked up to one shift register through the 595's Q0-Q5 outputs...
Did the 2nd one fall off the board?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.



if you are talking about the second LED, it is on a seperate small breadboard, and is connected to the one you see pictured above by the red green blue and white wires leading away from the board...


Sorry that is not a schematic it is a physical layout diagram and next to useless.

However it does show you have no decoupling capacitor on the power supply.

What simple code are you trying, please post it.


Yes, a schematic would definitely help, with signal names corresponding to how the Arduino pins are being used, and an RGB LED that made it clear whether the part was common anode or common cathode.

What's the block at the upper left connected to D2? Is that not part of the code yet?

Fritzing does schematics, doesn't it? Post one that is nice & neat like the other drawing.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


sorry about that...

yes, it is true, i have no decoupling capacitor on the power supply... also, D2 of the arduino goes to the output of the IR receiver, i will use this to get IR signals from a tv remote in a later phase of this project...

D4 goes to the data pin of the shift register, D7 goes to the clock, and D8 goes to the latch...

and i believe i have common anode LEDs

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131