Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Teensy 4.1 and ShiftPWM library

  1. #1

    Teensy 4.1 and ShiftPWM library

    Does shiftPWM work with the teensy 4.1?

    I found this thread but I'm not sure if its indicating that it works or not.

    Also will it work with a 3mm 'common anode' RGB led like this schematic?
    I'm using 6 74hc595BQ

    Here's the code I'm trying...
    Code:
    // You can choose the latch pin yourself.
    const int ShiftPWM_latchPin=9;
    
    #define SHIFTPWM_NOSPI // ** uncomment this part to NOT use the SPI port and change the pin numbers. This is 2.5x slower **
    const int ShiftPWM_dataPin = 25;
    const int ShiftPWM_clockPin = 10;
    
    // 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 = true;
    
    #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.
    unsigned char maxBrightness = 100;
    unsigned char pwmFrequency = 75;
    unsigned int numRegisters = 6;
    
    elapsedMillis randomTimer;
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
    
      // Sets the number of 8-bit registers that are used.
      ShiftPWM.SetAmountOfRegisters(numRegisters);
      // 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  
      ShiftPWM.Start(pwmFrequency,maxBrightness);
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      if (randomTimer >= 500){
        Serial.println("testTimer");
        int randomLed = random(0, 14);
        int randomR = random(0, 254);
        int randomG = random(0, 254);
        int randomB = random(0, 254);
        Serial.print("randomLed = ");
        Serial.println(randomLed);
        Serial.print("randomR = ");
        Serial.println(randomR);
        Serial.print("randomG = ");
        Serial.println(randomG);
        Serial.print("randomB = ");
        Serial.println(randomB);
        
        ShiftPWM.SetRGB(randomLed, randomR, randomG, randomB);
        randomTimer = 0;
      }
      
    }
    Also yesterday I swear changing "const bool ShiftPWM_invertOutputs = true;" between true and false and uploading would turn all the leds on or off but now its not doing anything.

    Thanks for any incite.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,238
    I tried ShiftPWM using its "ShiftPWM_RGB_Example" and the test hardware I built years ago with six 74HC595 chips and RGB LEDs.

    I had the edit the example like this to make it compile.

    Code:
     #define SHIFTPWM_NOSPI
     const int ShiftPWM_dataPin = 2;
     const int ShiftPWM_clockPin = 1;
    At first glance, it seems to be working.

    Click image for larger version. 

Name:	shiftpwm1.png 
Views:	4 
Size:	567.5 KB 
ID:	21177

    However, when I watch carefully I can see some slight "ghosting". It's difficult to photograph, but I was able to get this shot where you can see some light blue light coming from the last LED and some red and green from others, when only 1 LED is supposed to be on fully red.

    Click image for larger version. 

Name:	shiftpwm2.jpg 
Views:	4 
Size:	122.1 KB 
ID:	21178

    I tried running at different speeds. The problem seems to go away if I run Teensy 4.1 at only 396 MHz, and it gets much worse if I overclock to 720 MHz. So my guess is the delays we currently have in the code might not be quite right.

  3. #3
    Hey Paul thanks for the clarification. I got it working, ended up being a soldering issue. But now I'm having another problem that might be software related.
    Basically I cant turn on led 24 without turning on led 23.
    Code:
        ShiftPWM.SetRGB(8, 150, 150, 150); //turnes on 4 leds (23,24,25,26)
        ShiftPWM.SetRGB(8, 0, 150, 150); //turnes on 2 leds (25,26)
        ShiftPWM.SetOne(23,150); //turnes on 1 led (23)
        ShiftPWM.SetOne(24,150); //turnes on 2 leds (23,24)
    maybe this is related to the ghosting?

    Here's my full code in case its relevant.
    Code:
    #include <ShiftIn.h>
    
    // Init ShiftIn instance with 3 chips
    ShiftIn<2> shift;
    
    
    
    // You can choose the latch pin yourself.
    const int ShiftPWM_latchPin=9;
    
    #define SHIFTPWM_NOSPI // ** uncomment this part to NOT use the SPI port and change the pin numbers. This is 2.5x slower **
    const int ShiftPWM_dataPin = 25;
    const int ShiftPWM_clockPin = 10;
    
    // 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 = true;
    
    #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.
    unsigned char maxBrightness = 100;
    unsigned char pwmFrequency = 75;
    unsigned int numRegisters = 6;
    //int numRGBleds = numRegisters*8/3;
    
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      
      // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
      shift.begin(3, 4, 2, 5);
      
    
      // Sets the number of 8-bit registers that are used.
      ShiftPWM.SetAmountOfRegisters(numRegisters);
      // 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  
      ShiftPWM.Start(pwmFrequency,maxBrightness);
    
    }
    
    void displayValues() {
      // print out all 16 buttons
      for(int i = 0; i < shift.getDataWidth(); i++)
        Serial.print( shift.state(i) ); // get state of button i
      Serial.println();
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      if (shift.update()){
        // read in all values. returns true if any button has changed
        displayValues();
      }
      if (shift.state(11) == 0){ //button 7
        ShiftPWM.SetRGB(7, 150, 150, 150); 
      }
      else{
        ShiftPWM.SetRGB(7, 0, 0, 0); //button 7
      }
      if (shift.state(13) == 0){ //button Q
        ShiftPWM.SetRGB(8, 150, 150, 150); //turnes on 4 leds (23,24,25,26)
        //ShiftPWM.SetRGB(8, 0, 150, 150); //turnes on 2 leds (25,26)
        //ShiftPWM.SetOne(23,150); //turnes on 1 led (23)
        //ShiftPWM.SetOne(24,150); //turnes on 2 leds (23,24)
      }
      else{
        ShiftPWM.SetRGB(8, 0, 0, 0); //button Q
      } 
    
    
    }
    *edit* Actually having it at 396 MHZ seems to fix the issue
    Last edited by cfredisded; 07-31-2020 at 11:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •