2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/PCA9685_RT/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-12-23 05:18:10 -05:00
|
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/PCA9685_RT/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PCA9685_RT/actions/workflows/arduino-lint.yml)
|
|
|
|
|
[![JSON check](https://github.com/RobTillaart/PCA9685_RT/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PCA9685_RT/actions/workflows/jsoncheck.yml)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PCA9685_RT/blob/master/LICENSE)
|
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PCA9685_RT.svg?maxAge=3600)](https://github.com/RobTillaart/PCA9685_RT/releases)
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
# PCA9685_RT
|
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
Arduino library for PCA9685 I2C 12 bit PWM LED driver, 16 channel.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
## Description
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
This library is to control the I2C PCA9685 PWM extender.
|
|
|
|
|
The 16 channels are independently configurable in steps of 1/4096.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
This allows for better than 0.1% fine tuning of the duty-cycle
|
2021-01-29 06:31:58 -05:00
|
|
|
|
of the PWM signal.
|
|
|
|
|
|
|
|
|
|
The PWM's of the different channels have individual start and stop moments.
|
|
|
|
|
This can be used to distribute the power more evenly over multiple servo's
|
|
|
|
|
or give special effects when used in an RGB LED.
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
The frequency of the PWM can be set from 24 to 1526 according to the datasheet,
|
|
|
|
|
however in practice not all frequencies are set accurate.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
Lower frequencies do better than higher frequencies.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
### Constructor
|
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
- **PCA9685(uint8_t deviceAddress, TwoWire \* wire = &Wire)** Constructor with I2C device address,
|
|
|
|
|
and optional the Wire interface as parameter.
|
|
|
|
|
- **bool begin(uint8_t mode1_mask = PCA9685_MODE1_AUTOINCR | PCA9685_MODE1_ALLCALL, uint8_t mode2_mask = PCA9685_MODE2_TOTEMPOLE)**
|
|
|
|
|
initializes the library after startup. Optionally setting the MODE1 and MODE2 configuration registers.
|
|
|
|
|
See PCA9685.h and datasheet for settings possible.
|
|
|
|
|
- **bool begin(uint8_t sda, uint8_t scl, uint8_t mode1_mask = PCA9685_MODE1_AUTOINCR | PCA9685_MODE1_ALLCALL, uint8_t mode2_mask = PCA9685_MODE2_TOTEMPOLE)**
|
|
|
|
|
idem, ESP32 ESP8266 only.
|
|
|
|
|
- **void configure(uint8_t mode1_mask, uint8_t mode2_mask)**
|
|
|
|
|
To configure the library after startup one can set the MODE1 and MODE2 configuration registers.
|
|
|
|
|
See PCA9685.h and datasheet for settings possible.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **bool isConnected()** checks if address is available on I2C bus.
|
2022-01-06 14:24:55 -05:00
|
|
|
|
- **uint8_t channelCount()** returns the number of channels = 16.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
### Mode registers
|
|
|
|
|
|
|
|
|
|
Used to configure the PCA9685 general behaviour.
|
|
|
|
|
|
|
|
|
|
- **uint8_t writeMode(uint8_t reg, uint8_t value)** configuration of one of the two configuration registers.
|
|
|
|
|
Check datasheet for details.
|
|
|
|
|
- **uint8_t readMode(uint8_t reg)** reads back the configured mode,
|
|
|
|
|
useful to add or remove a single flag (bit masking).
|
|
|
|
|
- **uint8_t setMode1(uint8_t value)** convenience wrapper.
|
|
|
|
|
- **uint8_t setMode2(uint8_t value)** convenience wrapper.
|
|
|
|
|
- **uint8_t getMode1()** convenience wrapper.
|
|
|
|
|
- **uint8_t getMode2()** convenience wrapper.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### Constants for mode registers
|
|
|
|
|
|
|
|
|
|
(added 0.4.0)
|
|
|
|
|
|
|
|
|
|
| Name | Value | Description |
|
|
|
|
|
|:------------------------|:-----:|:-----------------------------------|
|
|
|
|
|
| PCA9685_MODE1_RESTART | 0x80 | 0 = disable 1 = enable |
|
|
|
|
|
| PCA9685_MODE1_EXTCLK | 0x40 | 0 = internal 1 = external |
|
|
|
|
|
| PCA9685_MODE1_AUTOINCR | 0x20 | 0 = disable 1 = enable |
|
|
|
|
|
| PCA9685_MODE1_SLEEP | 0x10 | 0 = normal 1 = sleep |
|
|
|
|
|
| PCA9685_MODE1_SUB1 | 0x08 | 0 = disable 1 = enable |
|
|
|
|
|
| PCA9685_MODE1_SUB2 | 0x04 | 0 = disable 1 = enable |
|
|
|
|
|
| PCA9685_MODE1_SUB3 | 0x02 | 0 = disable 1 = enable |
|
|
|
|
|
| PCA9685_MODE1_ALLCALL | 0x01 | 0 = disable 1 = enable |
|
|
|
|
|
| PCA9685_MODE1_NONE | 0x00 | |
|
|
|
|
|
| | | |
|
|
|
|
|
| PCA9685_MODE2_BLINK | 0x20 | 0 = dim 1 = blink |
|
|
|
|
|
| PCA9685_MODE2_INVERT | 0x10 | 0 = normal 1 = inverted |
|
|
|
|
|
| PCA9685_MODE2_STOP | 0x08 | 0 = on STOP 1 = on ACK |
|
|
|
|
|
| PCA9685_MODE2_TOTEMPOLE | 0x04 | 0 = open drain 1 = totem-pole |
|
|
|
|
|
| PCA9685_MODE2_OUTNE | 0x03 | check datasheet |
|
|
|
|
|
| PCA9685_MODE2_NONE | 0x00 | |
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
These constants makes it easier to set modes without using a non descriptive
|
|
|
|
|
bit mask. The constants can be merged by OR-ing them together, see snippet:
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
```cpp
|
|
|
|
|
ledArray.writeMode(PCA9685_MODE2, 0b00110100);
|
|
|
|
|
|
|
|
|
|
// would become
|
|
|
|
|
|
|
|
|
|
uint8_t mode2_mask = PCA9685_MODE2_BLINK | PCA9685_MODE2_INVERT | PCA9685_MODE2_TOTEMPOLE;
|
|
|
|
|
ledArray.writeMode(PCA9685_MODE2, mode2_mask);
|
|
|
|
|
|
|
|
|
|
// or even
|
|
|
|
|
|
|
|
|
|
ledArray.setMode2(PCA9685_MODE2_BLINK | PCA9685_MODE2_INVERT | PCA9685_MODE2_TOTEMPOLE);
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
### PWM
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **void setPWM(uint8_t channel, uint16_t onTime, uint16_t offTime)** The chip has 16 channels to do PWM.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
The signal is divided in 4096 steps, 0..4095.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
The pulse can begin **onTime** on any step and it can stop on any step **offTime**.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
This allows e.g. to distribute the power over the 16 channels, e.g. the
|
|
|
|
|
channels do not need to start at the same moment with HIGH.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **void setPWM(uint8_t channel, offTime)** simple PWM that always start on **onTime = 0**.
|
|
|
|
|
- **void getPWM(uint8_t channel, uint16_t \* onTime, uint16_t \* offTime)**
|
|
|
|
|
read back the configuration of the channel.
|
|
|
|
|
- **void allOFF()** switches all PWM channels OFF. **Experimental** in 0.3.0.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
To "undo" the allOFF one can call the **reset()** function and set all
|
|
|
|
|
PWM channels again.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **void digitalWrite(channel, mode)** mode = HIGH or LOW, just use the PCA9685 as
|
|
|
|
|
a digital pin.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
This single function replaces the setON() and setOFF() that will become
|
|
|
|
|
obsolete in the future.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Frequency
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **void setFrequency(uint16_t freq, int offset = 0)** set the update speed of the channels.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
This value is set the same for all channels at once.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **int getFrequency(bool cache = true)** get the current update frequency of the channels.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
This is same for all channels. If cache is false, the frequency is fetched and
|
2021-12-23 05:18:10 -05:00
|
|
|
|
calculated from the **preScaler** register and will probably differ from the
|
2021-01-29 06:31:58 -05:00
|
|
|
|
value set with **setFrequency()**.
|
|
|
|
|
|
|
|
|
|
The frequency is constrained to be between 24 and 1526 Hz.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
As the frequency is converted to an 8 bit **preScaler**,
|
2021-01-29 06:31:58 -05:00
|
|
|
|
the frequency set will seldom be exact.
|
|
|
|
|
After changing the frequency, one must set all channels (again),
|
|
|
|
|
so one should set the frequency in **setup()**
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
The parameter offset can be used to tune the **preScaler** to get a frequency
|
2021-01-29 06:31:58 -05:00
|
|
|
|
closer to the requested value. See **PCA9685_setFrequency_offset** example.
|
2021-12-23 05:18:10 -05:00
|
|
|
|
Default the offset = 0. As the **preScaler** is smaller at higher frequencies
|
2021-01-29 06:31:58 -05:00
|
|
|
|
higher frequencies are less accurate.
|
|
|
|
|
Making offset too large can result in very incorrect frequencies.
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
When using offset, the **getFrequency(false)** will return the adjusted **preScaler**.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
### Miscellaneous
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- **int lastError()** returns **PCA9685_OK = 0** if all is OK, and
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
| Error code | Value | Description |
|
|
|
|
|
|:--------------------|:-----:|:------------|
|
|
|
|
|
| PCA9685_OK | 0x00 | Everything went well
|
|
|
|
|
| PCA9685_ERROR | 0xFF | generic error
|
|
|
|
|
| PCA9685_ERR_CHANNEL | 0xFE | Channel out of range
|
|
|
|
|
| PCA9685_ERR_MODE | 0xFD | Invalid mode register chosen |
|
|
|
|
|
| PCA9685_ERR_I2C | 0xFC | PCA9685 I2C communication error |
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
2022-06-10 06:52:02 -04:00
|
|
|
|
### SUB CALL and ALL CALL
|
|
|
|
|
|
|
|
|
|
(new since 0.4.0)
|
|
|
|
|
|
|
|
|
|
Please read the datasheet to understand the working of **SUB CALL** and **ALL CALL**.
|
|
|
|
|
|
|
|
|
|
Since version 0.4.0 there is (experimental) support for the **SUB CALL** and **ALL CALL** functions.
|
|
|
|
|
It needs more testing and if there are issues, please report.
|
|
|
|
|
|
|
|
|
|
AllCall is automatically activated for each device on startup.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### Description
|
|
|
|
|
|
|
|
|
|
**SUB CALL** allows one to make groups of PCA9685 devices and control them on group level.
|
|
|
|
|
The number of groups one can make depends on free I2C addresses on one I2C bus.
|
|
|
|
|
Using multiple I2C buses or multiplexers will even increase the possible number.
|
|
|
|
|
Every PCA9685 device can be member of up to three of these groups.
|
|
|
|
|
To become member one needs to set the **setSubCallAddress(nr, address)** and enable
|
|
|
|
|
it with **enableSubCall(nr)**.
|
|
|
|
|
|
|
|
|
|
In the same way one can become member of an **ALL CALL** group.
|
|
|
|
|
Typically there is only one such group but one can configure more of them by applying different addresses.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### Interface
|
|
|
|
|
|
|
|
|
|
The functions to enable all/sub-addresses are straightforward:
|
|
|
|
|
|
|
|
|
|
- **bool enableSubCall(uint8_t nr)** nr = 1,2,3
|
|
|
|
|
- **bool disableSubCall(uint8_t nr)** nr = 1,2,3
|
|
|
|
|
- **bool isEnabledSubCall(uint8_t nr)** nr = 1,2,3
|
|
|
|
|
- **bool setSubCallAddress(uint8_t nr, uint8_t address)**
|
|
|
|
|
- **uint8_t getSubCallAddress(uint8_t nr)**
|
|
|
|
|
|
|
|
|
|
- **bool enableAllCall()**
|
|
|
|
|
- **bool disableAllCall()**
|
|
|
|
|
- **bool isEnabledAllCall()**
|
|
|
|
|
- **bool setAllCallAddress(uint8_t address)**
|
|
|
|
|
- **uint8_t getAllCallAddress()**
|
|
|
|
|
|
|
|
|
|
|
2023-03-14 12:37:50 -04:00
|
|
|
|
#### OutputEnable
|
|
|
|
|
|
|
|
|
|
Since 0.2.6 (experimental) support to control the OE (Output Enable) pin of the PCA9634.
|
|
|
|
|
This OE pin can control all LEDs simultaneously.
|
|
|
|
|
It also allows to control multiple PCA9634 modules by connecting the OE pins.
|
|
|
|
|
Think of simultaneous switching ON/OFF or get dimming with a high frequency PWM.
|
|
|
|
|
Or use 2 modules alternatively by placing an inverter in between.
|
|
|
|
|
|
|
|
|
|
See datasheet for the details
|
|
|
|
|
|
|
|
|
|
- **bool setOutputEnablePin(uint8_t pin = 255)** sets the IO pin to connect to the OE pin of the PCA9634.
|
|
|
|
|
A value of 255 indicates no pin set/selected.
|
|
|
|
|
Sets the OE pin to HIGH.
|
|
|
|
|
Returns true on success.
|
|
|
|
|
- **bool setOutputEnable(uint8_t value)** Sets the OE pin HIGH or LOW.
|
|
|
|
|
All non zero values are LOW.
|
|
|
|
|
Returns true on success.
|
|
|
|
|
- **uint8_t getOutputEnable()** get the current value of the OE pin.
|
|
|
|
|
If pin is not set/selected it will return HIGH.
|
|
|
|
|
|
|
|
|
|
Note: the OE is LOW active.
|
|
|
|
|
The user has to set the power on value by means of a PULL DOWN resistor.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### I2C Software reset
|
|
|
|
|
|
|
|
|
|
The goal of this function is to reset ALL PCA9685 devices on the bus.
|
|
|
|
|
When using the software reset, ALL devices attached to the bus are set to their hardware startup conditions.
|
|
|
|
|
Generally, there are multiple definitions of software resets by the I2C inventor NXP.
|
|
|
|
|
To accommodate this, two different modes for this function have been defined and tested (See PCA9634).
|
|
|
|
|
|
|
|
|
|
- Method 1 is a tested method which is specific to the PCA9634.
|
|
|
|
|
Since the number of different types of I2C chips is very large, side-effects on other chips might be possible.
|
|
|
|
|
Before using this method, consult the data sheets of all chips on the bus to mitigate potential undefined states.
|
|
|
|
|
- Method 0 is a somewhat <20>general<61> method which resets many chips on the I2C-bus.
|
|
|
|
|
However, this method DOES NOT reset the PCA9685 chip.
|
|
|
|
|
Therefore, consult the data sheet of all different chips on the bus to mitigate potential undefined states.
|
|
|
|
|
|
|
|
|
|
When only working with PCA9685 chips on a bus, only method 1 is required.
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
ledArray.I2C_SoftwareReset(1); // for method 1
|
|
|
|
|
ledArray.I2C_SoftwareReset(0); // for method 0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In case you experience issues with this function on your chips (non-PCA9685),
|
|
|
|
|
please give feedback, so the documentation can be improved.
|
|
|
|
|
|
|
|
|
|
For further details of the development, see - #10 (PCA9634 repo)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-12-23 05:18:10 -05:00
|
|
|
|
## Operation
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
See examples
|
2021-12-23 05:18:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Future
|
|
|
|
|
|
2022-11-19 11:04:25 -05:00
|
|
|
|
#### must
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- improve documentation
|
2022-11-19 11:04:25 -05:00
|
|
|
|
|
|
|
|
|
#### should
|
2021-12-23 05:18:10 -05:00
|
|
|
|
- add unit tests (if possible)
|
2022-11-19 11:04:25 -05:00
|
|
|
|
|
|
|
|
|
#### could
|
2022-06-10 06:52:02 -04:00
|
|
|
|
- investigate int vs uint16_t ?
|
|
|
|
|
- **setFrequency(), getFrequency(), \_freq**
|
|
|
|
|
- sync with PCA9634/35/85 where possible
|
|
|
|
|
- error handling?
|
2021-12-23 05:18:10 -05:00
|
|
|
|
|
|
|
|
|
|