├── .gitignore ├── Makefile ├── README.md ├── components └── pca9685 │ ├── component.mk │ ├── pca9685.c │ └── pca9685.h └── main ├── component.mk └── pca9685Test.c /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | **/build/* 3 | sdkconfig* 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := pca9685_pwm_test 7 | 8 | EXTRA_CFLAGS += --save-temps 9 | 10 | include $(IDF_PATH)/make/project.mk 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32 PCA9685 PWM/Servo Driver Library 2 | 3 | --- 4 | 5 | This is a example for the 16-channel PWM & Servo driver PCA9585 6 | 7 | #### Connections 8 | | ESP32 pin | PCA9685 | Notes | 9 | | --------- | ------- | ----- | 10 | | Any output pin | SCL | currenty pin 4 is used | 11 | | Any output pin | SDA | currenty pin 5 is used | 12 | | GND | GND | Power supply ground | 13 | | 3.3V | Vcc | Power supply positive | 14 | 15 | --- 16 | 17 | #### How to build 18 | 19 | Configure your esp32 build environment as for **esp-idf examples** 20 | 21 | Clone the repository 22 | 23 | `git clone https://github.com/brainelectronics/esp32-pca9685` 24 | 25 | Execute menuconfig and configure your Serial flash config and other settings. Included *sdkconfig.defaults* sets some defaults to be used. 26 | 27 | `make menuconfig` 28 | 29 | Make and flash the example. 30 | 31 | `make all && make flash` 32 | 33 | --- 34 | -------------------------------------------------------------------------------- /components/pca9685/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Main Makefile. This is basically the same as a component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | COMPONENT_SRCDIRS := . 7 | COMPONENT_ADD_INCLUDEDIRS := . 8 | -------------------------------------------------------------------------------- /components/pca9685/pca9685.c: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is a library for the PCA9685 LED PWM Driver 3 | 4 | This chip is connected via I2C, 2 pins are required to interface. The PWM frequency is set for all pins, the PWM for each individually. The set PWM is active as long as the chip is powered. 5 | 6 | Written by Jonas Scharpf 7 | BSD license, all text above must be included in any redistribution 8 | ****************************************************/ 9 | 10 | #include "pca9685.h" 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include "esp_err.h" 20 | #include 21 | #include "esp_log.h" 22 | #include "esp_system.h" 23 | 24 | uint8_t PCA9685_ADDR = 0x0; 25 | const uint16_t pwmTable[256] = {0, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 21, 21, 22, 22, 23, 24, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 47, 48, 49, 51, 52, 54, 55, 57, 59, 60, 62, 64, 66, 68, 69, 71, 74, 76, 78, 80, 82, 85, 87, 90, 92, 95, 98, 100, 103, 106, 109, 112, 116, 119, 122, 126, 130, 133, 137, 141, 145, 149, 153, 158, 162, 167, 172, 177, 182, 187, 193, 198, 204, 210, 216, 222, 228, 235, 241, 248, 255, 263, 270, 278, 286, 294, 303, 311, 320, 330, 339, 349, 359, 369, 380, 391, 402, 413, 425, 437, 450, 463, 476, 490, 504, 518, 533, 549, 564, 581, 597, 614, 632, 650, 669, 688, 708, 728, 749, 771, 793, 816, 839, 863, 888, 913, 940, 967, 994, 1023, 1052, 1082, 1114, 1146, 1178, 1212, 1247, 1283, 1320, 1358, 1397, 1437, 1478, 1520, 1564, 1609, 1655, 1703, 1752, 1802, 1854, 1907, 1962, 2018, 2076, 2135, 2197, 2260, 2325, 2391, 2460, 2531, 2603, 2678, 2755, 2834, 2916, 2999, 3085, 3174, 3265, 3359, 3455, 3555, 3657, 3762, 3870, 3981, 4095}; 26 | 27 | /** 28 | * @brief Sets the adress where the PCA9685 is located 29 | * 30 | * @param[in] addr The address 31 | */ 32 | void set_pca9685_adress(uint8_t addr) 33 | { 34 | PCA9685_ADDR = addr; 35 | } 36 | 37 | /** 38 | * @brief Reset the PCA9685 39 | * 40 | * @return result of command 41 | */ 42 | esp_err_t resetPCA9685(void) 43 | { 44 | esp_err_t ret; 45 | 46 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 47 | i2c_master_start(cmd); 48 | i2c_master_write_byte(cmd, (PCA9685_ADDR << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN); 49 | i2c_master_write_byte(cmd, MODE1, ACK_CHECK_EN); // 0x0 = "Mode register 1" 50 | i2c_master_write_byte(cmd, 0x80, ACK_CHECK_EN); // 0x80 = "Reset" 51 | i2c_master_stop(cmd); 52 | ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS); 53 | i2c_cmd_link_delete(cmd); 54 | 55 | vTaskDelay(50 / portTICK_RATE_MS); 56 | 57 | return ret; 58 | } 59 | 60 | /** 61 | * @brief Write two 16 bit values to the same register on an i2c device 62 | * 63 | * @param[in] regaddr The register address 64 | * @param[in] valueOn The value on 65 | * @param[in] valueOff The value off 66 | * 67 | * @return result of command 68 | */ 69 | esp_err_t generic_write_i2c_register_two_words(uint8_t regaddr, uint16_t valueOn, uint16_t valueOff) 70 | { 71 | esp_err_t ret; 72 | 73 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 74 | i2c_master_start(cmd); 75 | i2c_master_write_byte(cmd, (PCA9685_ADDR << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN); 76 | i2c_master_write_byte(cmd, regaddr, ACK_CHECK_EN); 77 | i2c_master_write_byte(cmd, valueOn & 0xff, ACK_VAL); 78 | i2c_master_write_byte(cmd, valueOn >> 8, NACK_VAL); 79 | i2c_master_write_byte(cmd, valueOff & 0xff, ACK_VAL); 80 | i2c_master_write_byte(cmd, valueOff >> 8, NACK_VAL); 81 | i2c_master_stop(cmd); 82 | ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS); 83 | i2c_cmd_link_delete(cmd); 84 | 85 | return ret; 86 | } 87 | 88 | /** 89 | * @brief Write a 16 bit value to a register on an i2c device 90 | * 91 | * @param[in] regaddr The register address 92 | * @param[in] value The value 93 | * 94 | * @return result of command 95 | */ 96 | esp_err_t generic_write_i2c_register_word(uint8_t regaddr, uint16_t value) 97 | { 98 | esp_err_t ret; 99 | 100 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 101 | i2c_master_start(cmd); 102 | i2c_master_write_byte(cmd, (PCA9685_ADDR << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN); 103 | i2c_master_write_byte(cmd, regaddr, ACK_CHECK_EN); 104 | i2c_master_write_byte(cmd, value & 0xff, ACK_VAL); 105 | i2c_master_write_byte(cmd, value >> 8, NACK_VAL); 106 | i2c_master_stop(cmd); 107 | ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS); 108 | i2c_cmd_link_delete(cmd); 109 | 110 | return ret; 111 | } 112 | 113 | /** 114 | * @brief Write a 8 bit value to a register on an i2c device 115 | * 116 | * @param[in] regaddr The register address 117 | * @param[in] value The value 118 | * 119 | * @return result of command 120 | */ 121 | esp_err_t generic_write_i2c_register(uint8_t regaddr, uint8_t value) 122 | { 123 | esp_err_t ret; 124 | 125 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 126 | i2c_master_start(cmd); 127 | i2c_master_write_byte(cmd, (PCA9685_ADDR << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN); 128 | i2c_master_write_byte(cmd, regaddr, ACK_CHECK_EN); 129 | i2c_master_write_byte(cmd, value, NACK_VAL); 130 | i2c_master_stop(cmd); 131 | ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS); 132 | i2c_cmd_link_delete(cmd); 133 | 134 | return ret; 135 | } 136 | 137 | /** 138 | * @brief Read two 8 bit values from the same register on an i2c device 139 | * 140 | * @param[in] regaddr The register address 141 | * @param valueA The first value 142 | * @param valueB The second value 143 | * 144 | * @return result of command 145 | */ 146 | esp_err_t generic_read_two_i2c_register(uint8_t regaddr, uint8_t* valueA, uint8_t* valueB) 147 | { 148 | esp_err_t ret; 149 | 150 | i2c_cmd_handle_t cmd = i2c_cmd_link_create(); 151 | i2c_master_start(cmd); 152 | i2c_master_write_byte(cmd, (PCA9685_ADDR << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN); 153 | i2c_master_write_byte(cmd, regaddr, ACK_CHECK_EN); 154 | i2c_master_stop(cmd); 155 | ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS); 156 | i2c_cmd_link_delete(cmd); 157 | if (ret != ESP_OK) { 158 | return ret; 159 | } 160 | cmd = i2c_cmd_link_create(); 161 | i2c_master_start(cmd); 162 | i2c_master_write_byte(cmd, PCA9685_ADDR << 1 | I2C_MASTER_READ, ACK_CHECK_EN); 163 | i2c_master_read_byte(cmd, valueA, ACK_VAL); 164 | i2c_master_read_byte(cmd, valueB, NACK_VAL); 165 | i2c_master_stop(cmd); 166 | ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS); 167 | i2c_cmd_link_delete(cmd); 168 | 169 | return ret; 170 | } 171 | 172 | /** 173 | * @brief Read a 16 bit value from a register on an i2c decivde 174 | * 175 | * @param[in] regaddr The register address 176 | * @param value The value 177 | * 178 | * @return result of command 179 | */ 180 | esp_err_t generic_read_i2c_register_word(uint8_t regaddr, uint16_t* value) 181 | { 182 | esp_err_t ret; 183 | 184 | uint8_t valueA; 185 | uint8_t valueB; 186 | 187 | ret = generic_read_two_i2c_register(regaddr, &valueA, &valueB); 188 | if (ret != ESP_OK) { 189 | return ret; 190 | } 191 | 192 | *value = (valueB << 8) | valueA; 193 | 194 | return ret; 195 | } 196 | 197 | 198 | /** 199 | * @brief Sets the frequency of PCA9685 200 | * 201 | * @param[in] freq The frequency 202 | * 203 | * @return result of command 204 | */ 205 | esp_err_t setFrequencyPCA9685(uint16_t freq) 206 | { 207 | esp_err_t ret; 208 | 209 | // Send to sleep 210 | ret = generic_write_i2c_register(MODE1, 0x10); 211 | if (ret != ESP_OK) { 212 | return ret; 213 | } 214 | 215 | // Set prescaler 216 | // calculation on page 25 of datasheet 217 | uint8_t prescale_val = round((CLOCK_FREQ / 4096 / (0.9*freq)) - 1+0.5); 218 | ret = generic_write_i2c_register(PRE_SCALE, prescale_val); 219 | if (ret != ESP_OK) { 220 | return ret; 221 | } 222 | 223 | // reset again 224 | resetPCA9685(); 225 | 226 | // Send to sleep again 227 | ret = generic_write_i2c_register(MODE1, 0x10); 228 | if (ret != ESP_OK) { 229 | return ret; 230 | } 231 | 232 | // wait 233 | vTaskDelay(5/portTICK_PERIOD_MS); 234 | 235 | // Write 0xa0 for auto increment LED0_x after received cmd 236 | ret = generic_write_i2c_register(MODE1, 0xa0); 237 | if (ret != ESP_OK) { 238 | return ret; 239 | } 240 | 241 | // // Read back set data 242 | // cmd = i2c_cmd_link_create(); 243 | // i2c_master_start(cmd); 244 | // i2c_master_write_byte(cmd, (PCA9685_ADDR << 1) | I2C_MASTER_READ, 1); 245 | // i2c_master_read_byte(cmd, data, 0); 246 | // i2c_master_read_byte(cmd, data+1, 0); 247 | // i2c_master_read_byte(cmd, data+2, 1); 248 | // i2c_master_stop(cmd); 249 | // ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100/portTICK_PERIOD_MS); 250 | // i2c_cmd_link_delete(cmd); 251 | // if (ret != ESP_OK) { 252 | // return ret; 253 | // } 254 | 255 | return ret; 256 | } 257 | 258 | /** 259 | * @brief Sets the pwm of the pin 260 | * 261 | * @param[in] num The pin number 262 | * @param[in] on On time 263 | * @param[in] off Off time 264 | * 265 | * @return result of command 266 | */ 267 | esp_err_t setPWM(uint8_t num, uint16_t on, uint16_t off) 268 | { 269 | esp_err_t ret; 270 | 271 | uint8_t pinAddress = LED0_ON_L + LED_MULTIPLYER * num; 272 | ret = generic_write_i2c_register_two_words(pinAddress & 0xff, on, off); 273 | 274 | return ret; 275 | } 276 | 277 | /** 278 | * @brief Gets the pwm of a pin detail 279 | * 280 | * Have read each LED0_ON_L and LED0_OFF_L seperate 281 | * 282 | * @param[in] num The number 283 | * @param dataReadOn0 The data read on 0 284 | * @param dataReadOn1 The data read on 1 285 | * @param dataReadOff0 The data read off 0 286 | * @param dataReadOff1 The data read off 1 287 | * 288 | * @return result of command 289 | */ 290 | esp_err_t getPWMDetail(uint8_t num, uint8_t* dataReadOn0, uint8_t* dataReadOn1, uint8_t* dataReadOff0, uint8_t* dataReadOff1) 291 | { 292 | esp_err_t ret; 293 | 294 | uint8_t pinAddress = LED0_ON_L + LED_MULTIPLYER * num; 295 | 296 | ret = generic_read_two_i2c_register(pinAddress, dataReadOn0, dataReadOn1); 297 | if (ret != ESP_OK) { 298 | return ret; 299 | } 300 | 301 | pinAddress = LED0_OFF_L + LED_MULTIPLYER * num; 302 | ret = generic_read_two_i2c_register(pinAddress, dataReadOff0, dataReadOff1); 303 | 304 | return ret; 305 | } 306 | 307 | /** 308 | * @brief Gets the pwm of a pin 309 | * 310 | * @param[in] num The number 311 | * @param dataOn The data on 312 | * @param dataOff The data off 313 | * 314 | * @return result of command 315 | */ 316 | esp_err_t getPWM(uint8_t num, uint16_t* dataOn, uint16_t* dataOff) 317 | { 318 | esp_err_t ret; 319 | 320 | uint8_t readPWMValueOn0; 321 | uint8_t readPWMValueOn1; 322 | uint8_t readPWMValueOff0; 323 | uint8_t readPWMValueOff1; 324 | 325 | ret = getPWMDetail(num, &readPWMValueOn0, &readPWMValueOn1, &readPWMValueOff0, &readPWMValueOff1); 326 | 327 | *dataOn = (readPWMValueOn1 << 8) | readPWMValueOn0; 328 | *dataOff = (readPWMValueOff1 << 8) | readPWMValueOff0; 329 | 330 | return ret; 331 | } 332 | 333 | /** 334 | * @brief Turn all LEDs off 335 | * 336 | * @return result of command 337 | */ 338 | esp_err_t turnAllOff(void) 339 | { 340 | esp_err_t ret; 341 | 342 | uint16_t valueOn = 0; 343 | uint16_t valueOff = 4096; 344 | ret = generic_write_i2c_register_two_words(ALLLED_ON_L, valueOn, valueOff); 345 | 346 | return ret; 347 | } 348 | 349 | /** 350 | * @brief fade pin up to maximum and back down 351 | * 352 | * @param[in] pin The pin 353 | * 354 | * @return result of command 355 | */ 356 | esp_err_t fade_pin_up_down(uint8_t pin) 357 | { 358 | esp_err_t ret; 359 | 360 | for (uint8_t i = 0; i < 255; i++) 361 | { 362 | // fade up 363 | ret = setPWM(pin, 0, pwmTable[i]); 364 | if (ret != ESP_OK) { 365 | return ret; 366 | } 367 | 368 | vTaskDelay(50/portTICK_PERIOD_MS); 369 | } 370 | 371 | for (uint8_t i = 0; i < 255; i++) 372 | { 373 | // fade down 374 | ret = setPWM(pin, pwmTable[i], 0); 375 | if (ret != ESP_OK) { 376 | return ret; 377 | } 378 | 379 | vTaskDelay(50/portTICK_PERIOD_MS); 380 | } 381 | 382 | // ensure it's really off 383 | ret = setPWM(pin, 0, 4096); 384 | if (ret != ESP_OK) { 385 | return ret; 386 | } 387 | vTaskDelay(100/portTICK_PERIOD_MS); 388 | 389 | return ret; 390 | } 391 | 392 | /** 393 | * @brief fade each pin up to maximum and back down 394 | * 395 | * @return result of command 396 | */ 397 | esp_err_t fade_all_up_down(void) 398 | { 399 | esp_err_t ret; 400 | 401 | for (uint8_t pin = 0; pin < 16; pin++) 402 | { 403 | for (uint8_t i = 0; i < 255; i++) 404 | { 405 | // fade up 406 | ret = setPWM(pin, 0, pwmTable[i]); 407 | if (ret != ESP_OK) { 408 | return ret; 409 | } 410 | 411 | vTaskDelay(50/portTICK_PERIOD_MS); 412 | } 413 | 414 | for (uint8_t i = 0; i < 255; i++) 415 | { 416 | // fade down 417 | ret = setPWM(pin, pwmTable[i], 0); 418 | if (ret != ESP_OK) { 419 | return ret; 420 | } 421 | 422 | vTaskDelay(50/portTICK_PERIOD_MS); 423 | } 424 | 425 | // ensure it's really off 426 | ret = setPWM(pin, 0, 4096); 427 | if (ret != ESP_OK) { 428 | return ret; 429 | } 430 | vTaskDelay(100/portTICK_PERIOD_MS); 431 | } 432 | 433 | return ret; 434 | } 435 | 436 | /** 437 | * @brief test function to show buffer 438 | * 439 | * @param buf The buffer 440 | * @param[in] len The length 441 | */ 442 | void disp_buf(uint16_t* buf, uint8_t len) 443 | { 444 | uint8_t i; 445 | for (i = 0; i < len; i++) 446 | { 447 | printf("%02x ", buf[i]); 448 | if (( i + 1 ) % 16 == 0) 449 | { 450 | printf("\n"); 451 | } 452 | } 453 | printf("\n"); 454 | } 455 | 456 | -------------------------------------------------------------------------------- /components/pca9685/pca9685.h: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is a library for the PCA9685 LED PWM Driver 3 | 4 | This chip is connected via I2C, 2 pins are required to interface. The PWM frequency is set for all pins, the PWM for each individually. The set PWM is active as long as the chip is powered. 5 | 6 | Written by Jonas Scharpf 7 | BSD license, all text above must be included in any redistribution 8 | ****************************************************/ 9 | 10 | #ifndef PCA9685_DRIVER_H 11 | #define PCA9685_DRIVER_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include "esp_err.h" 17 | #include 18 | #include "esp_log.h" 19 | #include "esp_system.h" 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | // #define PCA9685_ADDR 0x50 /*!< slave address for PCA9685 chip */ 26 | 27 | #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave */ 28 | #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */ 29 | #define ACK_VAL 0x0 /*!< I2C ack value */ 30 | #define NACK_VAL 0x1 /*!< I2C nack value */ 31 | 32 | #define MODE1 0x00 /*!< Mode register 1 */ 33 | #define MODE2 0x01 /*!< Mode register 2 */ 34 | #define SUBADR1 0x02 /*!< I2C-bus subaddress 1 */ 35 | #define SUBADR2 0x03 /*!< I2C-bus subaddress 2 */ 36 | #define SUBADR3 0x04 /*!< I2C-bus subaddress 3 */ 37 | #define ALLCALLADR 0x05 /*!< LED All Call I2C-bus address */ 38 | #define LED0 0x6 /*!< LED0 start register */ 39 | #define LED0_ON_L 0x6 /*!< LED0 output and brightness control byte 0 */ 40 | #define LED0_ON_H 0x7 /*!< LED0 output and brightness control byte 1 */ 41 | #define LED0_OFF_L 0x8 /*!< LED0 output and brightness control byte 2 */ 42 | #define LED0_OFF_H 0x9 /*!< LED0 output and brightness control byte 3 */ 43 | #define LED_MULTIPLYER 4 /*!< For the other 15 channels */ 44 | #define ALLLED_ON_L 0xFA /*!< load all the LEDn_ON registers, byte 0 (turn 0-7 channels on) */ 45 | #define ALLLED_ON_H 0xFB /*!< load all the LEDn_ON registers, byte 1 (turn 8-15 channels on) */ 46 | #define ALLLED_OFF_L 0xFC /*!< load all the LEDn_OFF registers, byte 0 (turn 0-7 channels off) */ 47 | #define ALLLED_OFF_H 0xFD /*!< load all the LEDn_OFF registers, byte 1 (turn 8-15 channels off) */ 48 | #define PRE_SCALE 0xFE /*!< prescaler for output frequency */ 49 | #define CLOCK_FREQ 25000000.0 /*!< 25MHz default osc clock */ 50 | 51 | extern void set_pca9685_adress(uint8_t addr); 52 | extern esp_err_t resetPCA9685(void); 53 | extern esp_err_t setFrequencyPCA9685(uint16_t freq); 54 | extern esp_err_t turnAllOff(void); 55 | extern esp_err_t setPWM(uint8_t num, uint16_t on, uint16_t off); 56 | extern esp_err_t getPWMDetail(uint8_t num, uint8_t* dataReadOn0, uint8_t* dataReadOn1, uint8_t* dataReadOff0, uint8_t* dataReadOff1); 57 | // extern esp_err_t getPWM(uint8_t num, uint16_t* dataReadOn, uint16_t* dataReadOff); 58 | // extern esp_err_t getPWM(uint8_t num); 59 | extern esp_err_t getPWM(uint8_t num, uint16_t* dataOn, uint16_t* dataOff); 60 | extern esp_err_t fade_pin_up_down(uint8_t pin); 61 | extern esp_err_t fade_all_up_down(void); 62 | 63 | extern esp_err_t generic_write_i2c_register_two_words(uint8_t regaddr, uint16_t valueOn, uint16_t valueOff); 64 | extern esp_err_t generic_write_i2c_register_word(uint8_t regaddr, uint16_t value); 65 | extern esp_err_t generic_write_i2c_register(uint8_t regaddr, uint8_t value); 66 | extern esp_err_t generic_read_i2c_register_word(uint8_t regaddr, uint16_t* value); 67 | extern esp_err_t generic_read_two_i2c_register(uint8_t regaddr, uint8_t* valueA, uint8_t* valueB); 68 | extern void disp_buf(uint16_t* buf, uint8_t len); 69 | 70 | #endif /* PCA9685_DRIVER_H */ 71 | -------------------------------------------------------------------------------- /main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Main Makefile. This is basically the same as a component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | 7 | COMPONENT_SRCDIRS := . 8 | COMPONENT_ADD_INCLUDEDIRS := . 9 | 10 | -------------------------------------------------------------------------------- /main/pca9685Test.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file pca9685Test.c 3 | * 4 | * @author Jonas Scharpf 5 | * @date Januar 2018 6 | * @version 1.0 7 | * 8 | * @brief set PWM of outputs of PCA9685 slave chip 9 | * 10 | * Description: 11 | * I2C Slave device PCA9685 at adress 0x40 can be controlled 12 | * to set individual PWM to the outputs 13 | * The PWM frequency can only be set for all outputs to same value 14 | * 15 | * Circuit: 16 | * PCA9685 attached to pins 4, 5 17 | * I2C Connection: 18 | * Board SDA SCL 19 | * ESP32 any (5) any (4) 20 | * Mega 20 21 21 | * Tiny D0/pin 5 D2/pin 7 (Digispark, Digispark Pro) 22 | * Uno A4 A5 23 | * 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "pca9685.h" 32 | 33 | #include "sdkconfig.h" 34 | 35 | #define I2C_EXAMPLE_MASTER_SCL_IO 4 /*!< gpio number for I2C master clock */ 36 | #define I2C_EXAMPLE_MASTER_SDA_IO 5 /*!< gpio number for I2C master data */ 37 | #define I2C_EXAMPLE_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */ 38 | #define I2C_EXAMPLE_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */ 39 | #define I2C_EXAMPLE_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ 40 | #define I2C_EXAMPLE_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ 41 | 42 | 43 | #define I2C_ADDRESS 0x40 /*!< lave address for PCA9685 */ 44 | 45 | #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave */ 46 | #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */ 47 | #define ACK_VAL 0x0 /*!< I2C ack value */ 48 | #define NACK_VAL 0x1 /*!< I2C nack value */ 49 | 50 | static char tag[] = "PCA9685"; 51 | 52 | #undef ESP_ERROR_CHECK 53 | #define ESP_ERROR_CHECK(x) do { esp_err_t rc = (x); if (rc != ESP_OK) { ESP_LOGE("err", "esp_err_t = %d", rc); assert(0 && #x);} } while(0); 54 | 55 | static void i2c_example_master_init(void); 56 | 57 | /** 58 | * @brief i2c master initialization 59 | */ 60 | static void i2c_example_master_init(void) 61 | { 62 | ESP_LOGD(tag, ">> PCA9685"); 63 | i2c_config_t conf; 64 | conf.mode = I2C_MODE_MASTER; 65 | conf.sda_io_num = I2C_EXAMPLE_MASTER_SDA_IO; 66 | conf.scl_io_num = I2C_EXAMPLE_MASTER_SCL_IO; 67 | conf.sda_pullup_en = GPIO_PULLUP_ENABLE; 68 | conf.scl_pullup_en = GPIO_PULLUP_ENABLE; 69 | conf.master.clk_speed = I2C_EXAMPLE_MASTER_FREQ_HZ; 70 | 71 | int i2c_master_port = I2C_EXAMPLE_MASTER_NUM; 72 | ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf)); 73 | ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode, 74 | I2C_EXAMPLE_MASTER_RX_BUF_DISABLE, 75 | I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0)); 76 | } 77 | 78 | void task_PCA9685(void *ignore) 79 | { 80 | printf("Executing on core %d\n", xPortGetCoreID()); 81 | 82 | esp_err_t ret; 83 | 84 | i2c_example_master_init(); 85 | 86 | set_pca9685_adress(I2C_ADDRESS); 87 | resetPCA9685(); 88 | setFrequencyPCA9685(1000); // 1000 Hz 89 | turnAllOff(); 90 | 91 | printf("Finished setup, entering loop now\n"); 92 | 93 | while(1) 94 | { 95 | // fade up and down each pin with static logarithmic table 96 | // see Weber Fechner Law 97 | ret = fade_all_up_down(); 98 | 99 | if(ret == ESP_ERR_TIMEOUT) 100 | { 101 | printf("I2C timeout\n"); 102 | } 103 | else if(ret == ESP_OK) 104 | { 105 | // all good 106 | } 107 | else 108 | { 109 | printf("No ack, sensor not connected...skip...\n"); 110 | } 111 | 112 | vTaskDelay(1000 / portTICK_RATE_MS); 113 | 114 | printf("Blink all pins starting from 0\n"); 115 | 116 | for (uint8_t pin = 0; pin < 16; pin++) 117 | { 118 | printf("Turn LED %d on\n", pin); 119 | setPWM(pin, 4096, 0); 120 | 121 | if(ret == ESP_ERR_TIMEOUT) 122 | { 123 | printf("I2C timeout\n"); 124 | } 125 | else if(ret == ESP_OK) 126 | { 127 | // all good 128 | } 129 | else 130 | { 131 | printf("No ack, sensor not connected...skip...\n"); 132 | } 133 | 134 | vTaskDelay(100/portTICK_PERIOD_MS); 135 | 136 | printf("Turn LED %d off\n", pin); 137 | setPWM(pin, 0, 4096); 138 | } 139 | 140 | vTaskDelay(1000 / portTICK_RATE_MS); 141 | 142 | /* 143 | // led turn on and 100ms off, starting from pin 0...15 144 | // read back the set value of each pin 145 | for (uint8_t pin = 0; pin < 16; pin++) 146 | { 147 | printf("Turn LED %d on to H(%d) L(%d)\n", pin, 4096-pin, 0); 148 | setPWM(pin, 4096-pin, 0); 149 | 150 | uint8_t readPWMValueOn0; 151 | uint8_t readPWMValueOn1; 152 | uint8_t readPWMValueOff0; 153 | uint8_t readPWMValueOff1; 154 | uint16_t myDataOn; 155 | uint16_t myDataOff; 156 | 157 | ret = getPWMDetail(pin, &readPWMValueOn0, &readPWMValueOn1, &readPWMValueOff0, &readPWMValueOff1); 158 | ret = getPWM(pin, &myDataOn, &myDataOff); 159 | 160 | if(ret == ESP_ERR_TIMEOUT) 161 | { 162 | printf("I2C timeout\n"); 163 | } 164 | else if(ret == ESP_OK) 165 | { 166 | printf("Read back from device detail: H(%d %d), L(%d %d)\n", readPWMValueOn0, readPWMValueOn1, readPWMValueOff0, readPWMValueOff1); 167 | 168 | printf("Read back from device: H(%d) L(%d)\n", myDataOn, myDataOff); 169 | } 170 | else 171 | { 172 | printf("No ack, sensor not connected...skip...\n"); 173 | } 174 | 175 | vTaskDelay(500/portTICK_PERIOD_MS); 176 | 177 | printf("Turn LED %d off\n", pin); 178 | setPWM(pin, 0, 4096); 179 | } 180 | */ 181 | 182 | // write_i2c_register_two_words(LED0_ON_L, 4096, 0); 183 | // vTaskDelay(1000/portTICK_PERIOD_MS); 184 | // write_i2c_register_two_words(LED0_ON_L, 0, 4096); 185 | } 186 | 187 | vTaskDelete(NULL); 188 | } 189 | 190 | void app_main() 191 | { 192 | xTaskCreate(task_PCA9685, "task_PCA9685", 1024 * 2, (void* ) 0, 10, NULL); 193 | } 194 | 195 | --------------------------------------------------------------------------------