C API for PCA9685 16x12bit PWM driver?

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
ctada
 
Posts: 7
Joined: Wed Jun 01, 2016 3:31 pm

C API for PCA9685 16x12bit PWM driver?

Post by ctada »

Hello
I have been successful getting this product to work with the python libraries but was wondering if there was a C library that I could use with this.
Thanks!

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

Re: C API for PCA9685 16x12bit PWM driver?

Post by adafruit_support_bill »

We do not have a C library for the Pi. We support Python on the Pi and C++ on the Arduino:
https://github.com/adafruit/Adafruit-PW ... er-Library

User avatar
ctada
 
Posts: 7
Joined: Wed Jun 01, 2016 3:31 pm

Re: C API for PCA9685 16x12bit PWM driver?

Post by ctada »

Ok, thank you.
I am pretty new to all of this - Pi's, Py'thons and Py'thagoras. Well that was just for alliteration. Please bear with me. I wonder if I could ask the following clarifications/questions.

First, in the 9685 library example, one sets the PWM fre

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

Re: C API for PCA9685 16x12bit PWM driver?

Post by adafruit_support_bill »

Not sure what the question is there. Setting the PWM frequency on the PCA9685 applies to all 16 PWM channels. The frequency is controlled by dividing down the master clock on the chip so there are a finite number of possible frequencies. The library will calculate the settings to achieve the closest possible frequency to what is requested.

User avatar
ctada
 
Posts: 7
Joined: Wed Jun 01, 2016 3:31 pm

Re: C API for PCA9685 16x12bit PWM driver?

Post by ctada »

Sorry - only half the question got posted and while I was typing it I was experimenting to see if I could figure out at least some of it.

You did answer that half though :). So after I pwm.set_pwm_freq(f) all LEDs are driven with power at frequency f until I make another call to set_pwm_freq(). I can't have sets at two different frequencies - I suppose I could add one board for each frequency I want.

As far as dividing down the master clock - can you tell me more about that? What frequencies are possible and what frequencies are not?

I am trying to determine the internal frame rate of a very high frame rate camera that only displays its output at 30 Hz - I am trying to do that by blinking LEDs at variable frequencies in order to cause a beat frequency with the camera sampling rate, hence the focus on exact frequencies.

The other part of the question is a clarification; if I set_pwm_freq(60) (say) power is sent to the LEDs with a 60 Hz modulation. Then the command
set_pwm(3, 1024, 4095)
would send 60Hz current to LED number 3, for 1024us, then off for (4095-1024)=3071us etc.?

Like in the attached sketch?
IMG_5338.jpg
IMG_5338.jpg (28.26 KiB) Viewed 341 times

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

Re: C API for PCA9685 16x12bit PWM driver?

Post by adafruit_support_bill »

As far as dividing down the master clock - can you tell me more about that? What frequencies are possible and what frequencies are not?
The internal clock operates at a nominal 25MHz. It can be divided by the pre-scale register register is described in section 7.3.5 of the datasheet. THe minimum frequency is 24Hz. The maximum is 1526 Hz. The internal clock is not a super-precise one, so the frequencies are approximate.
https://cdn-shop.adafruit.com/datasheets/PCA9685.pdf
The other part of the question is a clarification; if I set_pwm_freq(60) (say) power is sent to the LEDs with a 60 Hz modulation. Then the command
set_pwm(3, 1024, 4095)
would send 60Hz current to LED number 3, for 1024us, then off for (4095-1024)=3071us etc.?
No. The frequency determines the rate at which pulses are generated. So a 60Hz frequency will generate one pulse every 16.6667 milliseconds. The ON and OFF parameters the setPWM function determine how that 16.7ms period is divided up.

The chip is a 12bit resolution, so the period is divided into 4096 increments. So an 'on' time of 1024 will make the rising edge of the pulse occur at 16.7ms/4096 * 1024 or about 4.17 ms into the period. An 'off' time of 4096 will make the falling edge of the pulse occur at the end of the cycle. So you will have a 16.7ms - 4.17ms = 12.53 ms long pulse. That will give you a 60Hz signal with a 75% duty cycle.

User avatar
ctada
 
Posts: 7
Joined: Wed Jun 01, 2016 3:31 pm

Re: C API for PCA9685 16x12bit PWM driver?

Post by ctada »

Ok that is very clear - thanks very much.

User avatar
ctada
 
Posts: 7
Joined: Wed Jun 01, 2016 3:31 pm

Re: C API for PCA9685 16x12bit PWM driver?

Post by ctada »

Slight problem - somehow I must have deleted something somewhere (how's that for specifics) so that now the function

pwm.set_all_pwm(....)

no longer works - I get no error messages, no complaints, but the LEDs do not come on.

I am assuming reinstalling the device from scratch would probably fix this - is there a recommended way to do so? Do I uninstall it first? Or just follow the install instructions from the top?

Thanks!

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

Re: C API for PCA9685 16x12bit PWM driver?

Post by adafruit_support_bill »

I'm not a python expert, but I believe you need to uninstall it first.

User avatar
ctada
 
Posts: 7
Joined: Wed Jun 01, 2016 3:31 pm

Re: C API for PCA9685 16x12bit PWM driver?

Post by ctada »

Any thoughts on what that uninstall command might be?

sudo apt-UNget ...

haha

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

Re: C API for PCA9685 16x12bit PWM driver?

Post by adafruit_support_bill »

Try the 'remove' option documented here:

https://www.raspberrypi.org/documentati ... are/apt.md

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”