Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
bwachtel
 
Posts: 7
Joined: Thu Sep 11, 2014 11:56 am

Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by bwachtel »

I recently got this shield (well two of them) to fade and brighten 20 LEDS's (3W UV LED's). I figured it wouldn't be hard to find some sample code on the web, but all i can find is sample code for controlling servo motors. Could somebody post some basic code to 1. light Led's with 2 shields stacked (The second shield's address is 0x41) 2. brighten LED's from off to full brightness over say a half second. 3. dim LED's from full brightness to off. I eventually want to also have the LED's fade and brighten to music, but fist i want to get the basics of using pwm ports Via Adafruit's PWM Shield.

As always, any and all help is greatly appreciated. Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by adafruit_support_bill »

to fade and brighten 20 LEDS's (3W UV LED's).
3W leds are going to require some additional drive circuitry. The PWM board itself can only supply about 30mA.. How do you have these LEDs connected?

The PWMTest example sketch in the library shows how to ramp the PWM up and down for a bank of LEDs.
https://github.com/adafruit/Adafruit-PW ... wmtest.pde

User avatar
bwachtel
 
Posts: 7
Joined: Thu Sep 11, 2014 11:56 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by bwachtel »

My plan was to use a tip122 power transistor being controlled by the pwm, being powered by a lm317 acting as a constant current source. Then the tip122 would power the 3W led. I would have this combination for every single led and have them be in parallel. I still need to research to find what current i should use for my constant current source, & if i will need a resistor in series with the led, what my power supply should output. But in theory, does my plan look like it should work? (assuming i pick the right currents/voltages/resistances)

I looked at that code you posted and tested my pwm shields to make sure my soldering connections were solid and they were. I then started my own code pulling bits here and there out of the sample code. My new code is supposed to randomly select on and off for pins 0-16. It then brightens and dims the leds with corresponding 1's. its working fine, except that i would prefer the led's that are turning on to turn on simultaneously together instead of individually as they are now. I noticed on the sample code that the led's are fading in and out simultaneously. How would i modify my code to do that?

I also was confused about these bits of code and their purpose.

-pwm.setPWMFreq(1600); //When i changed this number to say 100, there was no obvious change in the led's lighting.
- uint8_t & uint16_t // i believe these are initializers like int, but i am unsure of their use.
-twbrbackup & TWBR = 12 // what are these used for?
- The last one is the include statement of Wire.h // what is Wire.h library used for with pwm?

Below is the code that i made using the sample code you linked me to as a reference.

And Thank You again for all of the help! I am really getting closer to putting this all together.

Code: Select all

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int led[16];

void setup() {

  pwm.begin();
  pwm.setPWMFreq(1600);
    
  // save I2C bitrate
  uint8_t twbrbackup = TWBR;
  TWBR = 12;
    
}

void loop() {
  
  randomgen();
  LED();
  
}

void randomgen() {
  
  for (int i=0; i<16; i++){
    led[i] = random(0,2);
  }
  
}

void LED(){
  
  for (int i=0; i<16; i++){
    
    if (led[i] == 1){
      for (int y=0; y<4096; y+=1){
        pwm.setPWM(i, 0, y);
      }
        
      for (int y=4095; y>-1; y-=1){
        pwm.setPWM(i, 0, y);
      }
    }
  }
}

User avatar
bwachtel
 
Posts: 7
Joined: Thu Sep 11, 2014 11:56 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by bwachtel »

well after some thinking, i fixed my code so it now does them simultaneously. But im sure there is a better way to do this than the way i did it.

Code: Select all

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int led[16];
int y = 0;

void setup() {

  pwm.begin();
  pwm.setPWMFreq(1600);
    
  // save I2C bitrate
  uint8_t twbrbackup = TWBR;
  TWBR = 12;
    
}

void loop() {
  
  randomgen();
  LED();
  
}

void randomgen() {
  
  for (int i=0; i<16; i++){
    led[i] = random(0,2);
  }
  
}

void LED(){
  
  for (int y=0; y<4096; y+=3){
      for (int i=0; i<16; i++){
        if (led[i] == 1){
          pwm.setPWM(i, 0, y);
        }
      }
    }
  
  for (int y=4095; y>-1; y-=3){
    for (int i=0; i<16; i++){
      if (led[i] == 1){
        pwm.setPWM(i, 0, y);
      }
    }
  }
}

User avatar
bwachtel
 
Posts: 7
Joined: Thu Sep 11, 2014 11:56 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by bwachtel »

This is what i came up with for my circuit that i was going to use. (still need to come up with voltage/current/capacitance/resistor values though.
LED Circuit.PNG
LED Circuit.PNG (25.05 KiB) Viewed 534 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by adafruit_support_bill »

But im sure there is a better way to do this than the way i did it.
Looks pretty straightforward to me!
This is what i came up with for my circuit
You show 4 connections to the TIP122. It is only a 3 pin device. Also, you have both emitter and collector (via the load) connected to GND.

User avatar
bwachtel
 
Posts: 7
Joined: Thu Sep 11, 2014 11:56 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by bwachtel »

Fixed my circuit and here is the update. My parts arrived and i tested just 1 LED on a breadboard and the LED flickers when it is being turned on and when it is being turned off. I am using the code that i posted earlier that randomly brightens and dims LEDs simultaneously. Any suggestions on how to fix this or whats the cause?
Attachments
UV LED - v2.PNG
UV LED - v2.PNG (24.54 KiB) Viewed 516 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by adafruit_support_bill »

the LED flickers when it is being turned on and when it is being turned off.
What exactly do you mean by flicker? The code you posted ramps up and down rather rapidly - and at random times. If you want to slow down the ramp rate, you could add some delays to your loops.

User avatar
bwachtel
 
Posts: 7
Joined: Thu Sep 11, 2014 11:56 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by bwachtel »

When the led first starts to turn on, it "flickers" (lumens are low, then jump to bright then go low) the flickering settles around the peak and then repeats the process as the arduino starts dimming the led. I would post a video of the "flickering" but I don't beleive I can post stuff like that here.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit 16-Channel 12-bit PWM/Servo Shield Help

Post by adafruit_support_bill »

Add some delays to your for loops.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”