Fading LED using I2C and PCA9865

Hi,
I’ m new in i2c protocol. I have got PCA9685 (16-channel, 12-bit PWM Fm+ I2C-bus LED controller) and I would like to start using it, by for example fadind single LED.
I read something about i2c, but I still cannot write code for control led using this module.
My connections look like these:

When I use i2c-scanner I get:

I2C Scanner
Scanning...
I2C device found at address 0x40  !
I2C device found at address 0x70  ! done

How code to fading this LED can looks like?

Hi. Looks like you have an Adafruit module. Have you checked their website for example code and tutorials?

Also, do you have another i2c device attached? The scanner is finding two devices.

Paul

Now scanner is finding only one device (at address 0x40 ) and that’s ok, earlier was my mistake.

Yes, my module is Adafruit PCA9685. I tried build some working code using tips from this thread: Interfacing with Adafruit I2C 16 Channel, 12 Bit, especially mentioned library but something went wrong with “BasicLEDtest” example.

There’s also tutorial about servos from adafruit: Overview | Adafruit PCA9685 16-Channel Servo Driver | Adafruit Learning System but I am interested in LEDs.
My knowledge about i2c is not too big, but I would like to have some working code which is flashing LED and see how it works, later I will go depper in that…

You need a resistor in line with that LED.

Post what you have done so far use the code tabs icon </>

The PCA9685 has more sink (25mA) than source (10 mA) capacity.
Maybe better to use your LED/resistor between VCC and PWM output.
Leo..

I used sketch "pwmtest" from Adafruit PCA9685 library with 0x40 adress and now LED is flashing. Moreover added resistor you were talking about.

Now I am trying to understand how it works and how control next diodes...

Did you also change the wiring to sink the current?

Now I am trying to understand how it works

You have two 16 bit counters. One defines when the LED is turned on, this can be set at zero, the other defines when it is turned off a value of 0 to 0xffff will control the brightness.

I am currently using this library.
https://github.com/brunnels/PCA9685 (the 2012 version).
It changes 12-bit from the chip into 8-bit perceived brightness steps with a lookup table.
(Google “CIE lighting standards”).
Some example code below to get you started.

The current version is a bit different.
I think you have to use board_0.setChannel8bit(0, 255); and board_0.setChannels8bit(0, 15, 255);
Not sure. Look through the library files.
Leo…

#include <I2C.h> // might not be needed with newer version 
#include <PCA9685.h> // 2012 version
PCA9685 board_0(0x5F); // I named the first board board_0 | I2C address is in the brackets
// PCA9685 board_1(0x5E); // second board, if used

void setup() {
  board_0.begin(); // starts the first board
  // board_1.begin(); // second board, if used
  board_0.setPWMFrequency(200); // boards default to 200hz PWM
  // board_1.setPWMFrequency(200);
}

void loop() {
  board_0.PWM(0, 15, 0); // turns all LEDs  (0-15) off
  board_0.PWM(0, 15, 255); // turns all LEDs on
  board_0.PWM(0, 3, 64); // sets the first four LEDs to 1/4 brightness
  board_0.PWM(7, 128); // sets LED 8 to 50% brightness
}

This library does not work

action_64:
This library does not work

What libiary?
In what way does it not work? - Compile, produce any output or produce a wrong looking output.

You will have to up your game if you want to post successfully here, so first read How to use this forum

The Brunnels library has been updated at some stage with new commands.
The examples that come with the library have not been updated yet.
So they don't work with the newer library.
Some info here.
Post your code if you want detailed help.
Leo..

At start of check produces an error:

C:\Users\D395~1\AppData\Local\Temp\arduino_modified_sketch_163006\sketch_may17d.ino:1:60: fatal error: I2C.h: No such file or directory

#include <I2C.h> // might not be needed with newer version

^

compilation terminated.

exit status 1

I said I was using the old library and that the current version has different commands.
Did you look at the link in post#10.

The sketch in there compiles if... you install the two libraries and... edit the Wire/Wire.h to Wire.h in the PCA9685.h library.
Let us know if you have problems with any of those tasks.
Leo..

Tried to do as you wrote. I downloaded: https://github.com/rambo/I2C, https://github.com/brunnels/PCA9685. Now, when checking, another error occurs:
Arduino: 1.8.2 Hourly Build 2017/03/01 11:33 (Windows 10), Плата:“Arduino/Genuino Uno”

In file included from C:\Arduino\sketch_may18a\sketch_may18a.ino:2:0:

D:\Arduino\arduino-nightly\libraries\PCA9685/PCA9685.h:12:23: fatal error: Wire/Wire.h: No such file or directory

#include <Wire/Wire.h>

^

compilation terminated.

exit status 1

How to solve this problem? Thank you very much for your help!!!

Did you edit the PCA9685.h library file, as explained in the other thread?
I use Notepad++ for that.

I have attached the edited file for your convenience.
Replace the existing PCA9685.h file with this one.

My installed library files are kept in Libraries>Documents>Arduino>sketchbook>libraries> PCA9685-master
Your library files might be in a different place.
Leo…

PCA9685.zip (3.46 KB)

Thank you very much Leo !! I am guilty, it was necessary to penetrate into the written. I will try not to make such simple mistakes in the future.

No problem. I assume you got it solved.
+1 for reporting back.
Leo…

Be aware, the Adafruit PWM board (and the various knock-offs of it) already have 220ohm resistors tied to the PWM pins.

Funny you should bring up this old thread.

I was just sorting through my eBay bins yesterday and came a cross a couple of these boards. :grinning:

Hi,
I am using PCA9685 for RGB LED. I want different colours by using different HEX code.

In my understanding:-
setPWM(LED1, 0, 4096) (means the pin is off and no light is there)
setPWM(LED1,4096,0) (means the pin os on and light will be there)

Now,
If I want red light then
setPWM(LED1,4096,0)
setPWM(LED2,0,4096)
setPWM(LED3,0,4096) because the HEX value is FF0000 of RGB

So I think if I need any colour with HEX code (A52A2A) in decimal (165,42,42) for brown colour
so should I do like this-
setPWM(LED1,4096 * 165/256,0)
setPWM(LED2,4096 * 42/256,0)
setPWM(LED3,4096 * 42/256,0)

I am not getting the proper result by using this. Can someone tell me how to generate any colour by using HEX code and setPWM function.

In the same way I came across a function:

esp_err_t fade_all_up(void)
{
//esp_err_t ret;
for (uint16_t i = 0; i < 128; i++)
{
for (uint8_t pin=0; pin<15; )//pin++)
{
// fade up
setPWM(LEDs[pin], 32+32i-1, 0);//pwmTable*);*
setPWM(LEDs[pin+1], 32+32i-1, 0);//4096, 0);
setPWM(LEDs[pin+2], 4096, 0);
pin+=3;
}
vTaskDelay(10/portTICK_PERIOD_MS);
}
return ESP_OK; //ret;
}
this function lights the LED in dark yellow coulor slowly. I want to know how can I use different HEX code to light up any colour in same manner and same intensity.Hi,
I am using PCA9685 for RGB LED. I want different colours by using different HEX code.
In my understanding:-
setPWM(LED1, 0, 4096) (means the pin is off and no light is there)
setPWM(LED1,4096,0) (means the pin os on and light will be there)
Now,
If I want red light then
setPWM(LED1,4096,0)
setPWM(LED2,0,4096)
setPWM(LED3,0,4096) because the HEX value is FF0000 of RGB
So I think if I need any colour with HEX code (A52A2A) in decimal (165,42,42) for brown colour
so should I do like this-
setPWM(LED1,4096 * 165/256,0)
setPWM(LED2,4096 * 42/256,0)
setPWM(LED3,4096 * 42/256,0)
I am not getting the proper result by using this. Can someone tell me how to generate any colour by using HEX code and setPWM function.
In the same way I came across a function:
esp_err_t fade_all_up(void)
{
//esp_err_t ret;
for (uint16_t i = 0; i < 128; i++)
{
for (uint8_t pin=0; pin<15; )//pin++)
{
// fade up
setPWM(LEDs[pin], 32+32i-1, 0);//pwmTable*);*
setPWM(LEDs[pin+1], 32+32i-1, 0);//4096, 0);
setPWM(LEDs[pin+2], 4096, 0);
pin+=3;
}
vTaskDelay(10/portTICK_PERIOD_MS);
}
return ESP_OK; //ret;
}
this function lights the LED in dark yellow coulor slowly. I want to know how can I use different HEX code to light up any colour in same manner and same intensity.