Adafruit 16 channel PWM breakout board

by walker on Wed Jul 24, 2013 10:47 pm

I'm using the PCA9685 breakout board (http://www.adafruit.com/products/815) to control a set of LEDs from an Arduino Mega. I also need to have a Raspberry pi on the i2c bus. I've been trying to use the pass-through connectors on the board to link the Arduino in with a bi-directional logic level shifter. So far I'm not having much luck. If the Mega isn't attached I can see the PWM board with no problem. Once I connect the Mega I can't see any devices if I do an i2cdetect from the Rpi if the Mega is powered down. If I turn the Mega on then i2cdetect reports devices on all addresses.

VCC on the PWM board is connected to the Mega 5v line. V+ is powered by a 5vdc wall wart. Again, the i2c chain is: Arduino Mega->PWM board->logic level shifter->Raspberry Pi. Any ideas about what could cause this? Would it be possible for me to connect the Mega 3.3v line to VCC and drop the logic level shifter?
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by walker on Thu Jul 25, 2013 3:50 am

Update: the problem seems to be with using the Adafruit_PWMServoDriver. If I strictly use Wire the Rpi can see the Arduino and the PWM board with i2cdetect.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by walker on Thu Jul 25, 2013 6:05 am

I'm guessing the PWM driver is setting the bus frequency to something other than the default for the Raspi (which I think is 100khz.) Do I need to set TWBR to something specific to match it? Sorry, I don't know much about this register.

Before introducing the Rpi to this setup I was using the example code that sets the bus to 400khz. Is it possible that TWBR retained that setting across boots? I've commented those lines out of the code now but the problem persists.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by walker on Thu Jul 25, 2013 7:53 am

Sorry to answer my own questions a lot overnight. I was working on this quite late and didn't expect answers at this time of night!

Am I correct that setting TWBR to 72 will reset the bus speed to 100khz? I'm away from the equipment right now but should be able to test this afternoon.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by adafruit_support_bill on Thu Jul 25, 2013 8:19 am

The PWMServoDriver does not change the i2c bus speed. It just uses the Wire library defaults. However, if you loaded a sketch on the Mega that did change that speed, it would persist through power downs and resets. The Atmega is an embedded processor. It comes up running whatever is in Flash.

As setting the bus speed explicitly, the wire library documentation has this to say:
The twi.init() call is internal to the Wire library. It performs the following tasks:
enables the TWI hardware to access the appropriate pins on the ATmega processor. In the case of the ATmega 328, these are analog pins 4 for SDA and 5 for the SCL line of the I2C bus.
sets the clock frequency that the TWI hardware will use if/when it is the master on the I2C bus. It is set in the source code to 100kHz, but theoretically at least you can reset this frequency by redefining TWBR prior to calling Wire.begin(), eg: TWBR=400000L should set it to 400kHz.


Note that not all bi-directional logic shifters will work with i2c signals - due to they way the pull-ups work on the bus. This one is designed specifically for i2c: http://www.adafruit.com/products/757
User avatar
adafruit_support_bill
 
Posts: 28185
Joined: Sat Feb 07, 2009 9:11 am

Re: Adafruit 16 channel PWM breakout board

by walker on Thu Jul 25, 2013 8:46 am

Thanks... that is indeed the exact logic level shifter that I am using. And as long as the Arduino is off it seems to work well.

So that's interesting to know that TWBR sets the register in flash. I suspect that is my problem. I'll report back when I get a chance to set it to 100khz. Alternatively, I suppose it might be possible to set the Rpi to a higher speed but I don't have time to search for that right now.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by adafruit_support_bill on Thu Jul 25, 2013 9:59 am

So that's interesting to know that TWBR sets the register in flash.

Not sure if the register itself is persistent. What I was trying to say was that if you had a sketch that set it to 400KHz, that sketch would come up running and set it again after a reset.
User avatar
adafruit_support_bill
 
Posts: 28185
Joined: Sat Feb 07, 2009 9:11 am

Re: Adafruit 16 channel PWM breakout board

by walker on Thu Jul 25, 2013 10:48 am

I see... that is troubling then. In previous iterations of my code I set TWBR to 12 and I am hoping that the problem I'm seeing is the result of the Arduino running at 400khz while the Rpi is running at 100khz. In the meantime I've removed the code from the sketch that set TWBR to 12 by commenting it out. However the problem is persisting.

I was hoping that register was persistent somehow in the sense that not setting the frequency took the speed that was last set. Then I would have the hope that I could explicitly set TWBR to 72 to get it back to the default of 100khz. (I guess it doesn't hurt to try.)

So, I may have another issue.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by walker on Thu Jul 25, 2013 3:34 pm

Okay, divide and conquer debug style now... I've commented all of the pwm library calls out of my code for now. I'm going through line-by-line to see what happens when I re-enable parts. I'm mostly interested in the setup() loop since this is likely where I'll find the problem.

Code: Select all | TOGGLE FULL SIZE
// Init the driver board for the Target LEDs
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
 
void setup()
{
  Serial.begin(9600);
  pinMode(TRIGGERPIN, INPUT_PULLUP);
  pinMode(STARTBUTTON, INPUT_PULLUP);
  gunServo.attach(SERVOPIN);
  gunServo.write(SERVOINDEXPOS);
   
  // save I2C bitrate
//  uint8_t twbrbackup = TWBR;
  // must be changed after calling Wire.begin() (inside pwm.begin())
//  TWBR = 12; // upgrage to 400KHz!
  pwm.begin();
  pwm.setPWMFreq(1600); 

 
  Wire.begin(I2CSLAVEADDR);
//  Wire.onReceive(receiveData);
//  Wire.onRequest(sendData);

  for (int thisTarget = 0; thisTarget < NUMTARGETS; thisTarget++) {
    pinMode( A0 + thisTarget, INPUT);
//    pwm.setPWM(thisTarget, 0, LEDOFF);
  }
}


I can see a potential issue right away. The pwm.begin() command is going to call Wire.begin() putting the Arduino into master mode. And my Wire.begin line is going to also put the Arduino into slave mode. The problem with the Raspi smbus library is that it doesn't have a slave mode (which I dearly wish it did!) I assume I need the Arduino to have a single Wire.begin() call and not be in the two different modes, right?

So I guess I need to write my i2c communications as a multi-master bus. That hadn't occurred to me yet but I wish it had.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by walker on Tue Jul 30, 2013 2:41 pm

Bill, I just wanted to drop a note thanking you for your help. I was able to display my project at the Detroit Maker Faire last weekend.

Unfortunately I never got the multiple-master i2c bus running correctly since I was running out of time. I kicked back to the simpler method of linking some GPIO ports to signal between the Arduino and Rpi. I set one pin high to indicate the game was in process and set it low when the game was over. And I used a second pin to signal a target hit to increment the score by 10. Frankly, I should have been doing it this way from the beginning! Much simpler and only took a few minutes to wire and code.

The Adafruit PWM board as well as the level shifter worked perfectly. My project also featured a Powerswitch Tail II from Adafruit which also worked flawlessly despite being integrated the night before the Faire!
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by adafruit_support_bill on Tue Jul 30, 2013 2:55 pm

Glad it all worked out for you. Did you get any pictures? :D
User avatar
adafruit_support_bill
 
Posts: 28185
Joined: Sat Feb 07, 2009 9:11 am

Re: Adafruit 16 channel PWM breakout board

by walker on Tue Jul 30, 2013 3:55 pm

Sure. I don't have any pics of the electronics at the show, but I could take some photos of the board if you want that as well.

The project was a pneumatic target range using recycled materials and some common hardware from local stores (mostly PVC pipe and fittings.) The gun uses a 12 amp motor from a discarded Bissell vacuum cleaner to generate the air pressure. The ammo is ping pong balls that have some additional weight added to them. We stuffed them with cotton to give them a little more punch and accuracy. (All of the cotton had fallen out by the end of the Faire though so accuracy got worse as the event progressed.) The balls are loaded into the gun via a venturi tube and they are collected into a bucket after they are shot. Another vacuum cleaner motor loads them into a hopper over the gun for re-use.

The targets are side-lit plexi panels with flourescent marker drawings on them. The target lights are controlled by an Arduino Mega and the Adafruit PWM breakout board. The breakout board supplies a PWM signal to a pair of Darlington Array chips (transistors) to provide the 12v current needed to light them. A large number of the LEDs used in the targets were recycled from bulbs that were inadvertently used in dimmers. (Don't ask me how I know.) The Arduino lights the target that you need to hit in order to score. The target hits are sensed using piezo sensors and a small envelope detector circuit that adafruit_support_mike provided in these forums.

The Arduino sends signals via GPIO to a Raspberry Pi computer which supplies video and audio feedback. The video is the scoreboard and the audio is a variety of musical scores by Kevin MacLeod who was kind enough to allow use of his muic under the Creative Commons license (www.incompetech.com) I have to say that the music adds a lot to the experience.

18988_3279854812253_752388642_n.jpg
18988_3279854812253_752388642_n.jpg (110.54 KiB) Viewed 961 times


533892_3279855772277_1396145910_n.jpg
533892_3279855772277_1396145910_n.jpg (132.53 KiB) Viewed 961 times


1016948_3279855652274_630999213_n.jpg
1016948_3279855652274_630999213_n.jpg (102.35 KiB) Viewed 961 times


This last pic gives a little bit of a look at the scoreboard. You have to peak around the tent support in the center. And you can see a little of the targets at the back of the tent.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by walker on Tue Jul 30, 2013 3:58 pm

Forgot to mention... the Arduino, Raspberry Pi, custom electronics and the LEDs were all run from solar power during most of the Faire. Sadly we had some cloudy weather on day 1 so had to resort to mains in the afternoon.
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am

Re: Adafruit 16 channel PWM breakout board

by adafruit_support_bill on Tue Jul 30, 2013 4:00 pm

Awesome! :D I'm sending this to Matt for the blog!
User avatar
adafruit_support_bill
 
Posts: 28185
Joined: Sat Feb 07, 2009 9:11 am

Re: Adafruit 16 channel PWM breakout board

by walker on Fri Aug 02, 2013 6:16 pm

Awesome! Here is a pic of the gun, and two of the electronics in case anyone wants to take a closer look:

IMG_0304_2.png
IMG_0304_2.png (607.63 KiB) Viewed 823 times


IMG_0308.png
IMG_0308.png (714.99 KiB) Viewed 823 times


IMG_0310.png
IMG_0310.png (773.29 KiB) Viewed 823 times
walker
 
Posts: 75
Joined: Sat Aug 04, 2012 7:40 am