ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # PCA9685-Arduino
  2. Arduino Library for the PCA9685 16-Channel PWM Driver Module.
  3. **PCA9685-Arduino - Version 1.2.9**
  4. Library to control a PCA9685 16-channel PWM driver module from an Arduino board.
  5. Licensed under the copy-left GNU GPL v3 license.
  6. Created by Kasper Skårhøj, August 3rd, 2012.
  7. Forked by Vitska, June 18th, 2016.
  8. Forked by NachtRaveVL, July 29th, 2016.
  9. This library allows communication with boards running a PCA6985 16-channel PWM driver module. It supports a wide range of available functionality, from setting the output PWM frequecy, allowing multi-device proxy addressing, and provides an assistant class for working with Servos.
  10. The datasheet for the IC is available from <http://www.nxp.com/documents/data_sheet/PCA9685.pdf>.
  11. ## Library Setup
  12. There are several defines inside of the library's header file that allows for more fine-tuned control.
  13. ```Arduino
  14. // Uncomment this define to enable use of the software i2c library (min 4MHz+ processor required).
  15. //#define PCA9685_ENABLE_SOFTWARE_I2C 1 // http://playground.arduino.cc/Main/SoftwareI2CLibrary
  16. // Uncomment this define if wanting to exclude extended functionality from compilation.
  17. //#define PCA9685_EXCLUDE_EXT_FUNC 1
  18. // Uncomment this define if wanting to exclude ServoEvaluator assistant from compilation.
  19. //#define PCA9685_EXCLUDE_SERVO_EVAL 1
  20. // Uncomment this define to enable debug output.
  21. //#define PCA9685_ENABLE_DEBUG_OUTPUT 1
  22. ```
  23. ## Servo Control Note
  24. Many 180 degree controlled digital servos run on a 20ms pulse width (50Hz update frequency) based duty cycle, and do not utilize the entire pulse width for their -90/+90 degree control. Typically, 2.5% of the 20ms pulse width (0.5ms) is considered -90 degrees, and 12.5% of the 20ms pulse width (2.5ms) is considered +90 degrees. This roughly translates to raw PCA9685 PWM values of 102 and 512 (out of the 4096 value range) for -90 to +90 degree control, but may need to be adjusted to fit your specific servo (e.g. some I've tested run ~130 to ~525 for their -90/+90 degree control). Also be aware that driving some servos past their -90/+90 degrees of movement can cause a little plastic limiter pin to break off and get stuck inside of the gearing, which could potentially cause the servo to become jammed. See the PCA9685_ServoEvaluator class to assist with calculating PWM values from Servo angle values.
  25. ## Example Usage
  26. Below are several examples of library usage.
  27. ### Simple Example
  28. ```Arduino
  29. #include <Wire.h>
  30. #include "PCA9685.h"
  31. PCA9685 pwmController; // Library using default Wire and default linear phase balancing scheme
  32. void setup() {
  33. Serial.begin(115200);
  34. Wire.begin(); // Wire must be started first
  35. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  36. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  37. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  38. pwmController.setPWMFrequency(100); // Default is 200Hz, supports 24Hz to 1526Hz
  39. pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
  40. Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
  41. }
  42. ```
  43. ### Batching Example
  44. In this example, we randomly select PWM frequencies on all 12 outputs and allow them to drive for 5 seconds before changing them.
  45. ```Arduino
  46. #include <Wire.h>
  47. #include "PCA9685.h"
  48. PCA9685 pwmController; // Library using default Wire and default linear phase balancing scheme
  49. void setup() {
  50. Serial.begin(115200);
  51. Wire.begin(); // Wire must be started first
  52. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  53. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  54. pwmController.init(B010101); // Address pins A5-A0 set to B010101
  55. pwmController.setPWMFrequency(500); // Default is 200Hz, supports 24Hz to 1526Hz
  56. randomSeed(analogRead(0)); // Use white noise for our randomness
  57. }
  58. void loop() {
  59. word pwms[12];
  60. pwms[0] = random(0, 4096);
  61. pwms[1] = random(0, 4096);
  62. pwms[2] = random(0, 4096);
  63. pwms[3] = random(0, 4096);
  64. pwms[4] = random(0, 4096);
  65. pwms[5] = random(0, 4096);
  66. pwms[6] = random(0, 4096);
  67. pwms[7] = random(0, 4096);
  68. pwms[8] = random(0, 4096);
  69. pwms[9] = random(0, 4096);
  70. pwms[10] = random(0, 4096);
  71. pwms[11] = random(0, 4096);
  72. pwmController.setChannelsPWM(0, 12, pwms);
  73. delay(5000);
  74. // Note: Only 7 channels can be written in one i2c transaction due to a
  75. // BUFFER_LENGTH limit of 32, so 12 channels will take two i2c transactions.
  76. }
  77. ```
  78. ### Multi-Device Proxy Example
  79. In this example, we use a special instance to control other modules attached to it via the ALL_CALL register.
  80. ```Arduino
  81. #include <Wire.h>
  82. #include "PCA9685.h"
  83. PCA9685 pwmController1; // Library using default Wire and default linear phase balancing scheme
  84. PCA9685 pwmController2; // Library using default Wire and default linear phase balancing scheme
  85. PCA9685 pwmControllerAll; // Not a real device, will act as a proxy to pwmController1 and pwmController2
  86. void setup() {
  87. Serial.begin(115200);
  88. Wire.begin(); // Wire must be started first
  89. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  90. pwmController1.resetDevices(); // Software resets all PCA9685 devices on Wire line (including pwmController2 in this case)
  91. pwmController1.init(B000000); // Address pins A5-A0 set to B000000
  92. pwmController2.init(B000001); // Address pins A5-A0 set to B000001
  93. pwmController1.setChannelOff(0); // Turn channel 0 off
  94. pwmController2.setChannelOff(0); // On both
  95. pwmController1.enableAllCallAddress(); // Default address of 0xE0
  96. pwmController2.enableAllCallAddress(); // Same default address
  97. pwmControllerAll.initAsProxyAddresser(); // Same default address of 0x0E as used in enable above
  98. pwmControllerAll.setChannelPWM(0, 4096); // Enables full on on both pwmController1 and pwmController2
  99. Serial.println(pwmController1.getChannelPWM(0)); // Should output 4096
  100. Serial.println(pwmController2.getChannelPWM(0)); // Should also output 4096
  101. // Note: Various parts of functionality of the proxy class instance are actually
  102. // disabled - typically anything that involves a read command being issued.
  103. }
  104. ```
  105. ### Servo Evaluator Example
  106. In this example, we utilize the ServoEvaluator class to assist with setting PWM frequencies when working with servos. We will be using Wire1, which is only available on boards with SDA1/SCL1 (Due, Zero, etc.) - change to Wire if Wire1 is unavailable.
  107. ```Arduino
  108. #include <Wire.h>
  109. #include "PCA9685.h"
  110. PCA9685 pwmController(Wire1, PCA9685_PhaseBalancer_Weaved); // Library using Wire1 and weaved phase balancing scheme
  111. // Linearly interpolates between standard 2.5%/12.5% phase length (102/512) for -90°/+90°
  112. PCA9685_ServoEvaluator pwmServo1;
  113. // Testing our second servo has found that -90° sits at 128, 0° at 324, and +90° at 526.
  114. // Since 324 isn't precisely in the middle, a cubic spline will be used to smoothly
  115. // interpolate PWM values, which will account for said discrepancy. Additionally, since
  116. // 324 is closer to 128 than 526, there is less resolution in the -90° to 0° range, and
  117. // more in the 0° to +90° range.
  118. PCA9685_ServoEvaluator pwmServo2(128,324,526);
  119. void setup() {
  120. Serial.begin(115200);
  121. Wire1.begin(); // Wire must be started first
  122. Wire1.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  123. pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
  124. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  125. pwmController.setPWMFrequency(50); // 50Hz provides 20ms standard servo phase length
  126. pwmController.setChannelPWM(0, pwmServo1.pwmForAngle(-90));
  127. Serial.println(pwmController.getChannelPWM(0)); // Should output 102 for -90°
  128. // Showing linearity for midpoint, 205 away from both -90° and 90°
  129. Serial.println(pwmServo1.pwmForAngle(0)); // Should output 307 for 0°
  130. pwmController.setChannelPWM(0, pwmServo1.pwmForAngle(90));
  131. Serial.println(pwmController.getChannelPWM(0)); // Should output 512 for +90°
  132. pwmController.setChannelPWM(1, pwmServo2.pwmForAngle(-90));
  133. Serial.println(pwmController.getChannelPWM(1)); // Should output 128 for -90°
  134. // Showing less resolution in the -90° to 0° range
  135. Serial.println(pwmServo2.pwmForAngle(-45)); // Should output 225 for -45°, 97 away from -90°
  136. pwmController.setChannelPWM(1, pwmServo2.pwmForAngle(0));
  137. Serial.println(pwmController.getChannelPWM(1)); // Should output 324 for 0°
  138. // Showing more resolution in the 0° to +90° range
  139. Serial.println(pwmServo2.pwmForAngle(45)); // Should output 424 for +45°, 102 away from +90°
  140. pwmController.setChannelPWM(1, pwmServo2.pwmForAngle(90));
  141. Serial.println(pwmController.getChannelPWM(1)); // Should output 526 for +90°
  142. }
  143. ```
  144. ### Software I2C Example
  145. In this example, we utilize the software I2C functionality for chips that do not have a hardware I2C bus. We must uncomment the PCA9685_ENABLE_SOFTWARE_I2C define in the libraries main header file for software I2C mode to be enabled.
  146. In PCA9685.h:
  147. ```Arduino
  148. // Uncomment this define to enable use of the software i2c library (min 4MHz+ processor required).
  149. #define PCA9685_ENABLE_SOFTWARE_I2C 1 // http://playground.arduino.cc/Main/SoftwareI2CLibrary
  150. ```
  151. In main sketch:
  152. ```Arduino
  153. #include "PCA9685.h"
  154. #define SCL_PIN 2 // Setup defines are written before library include
  155. #define SCL_PORT PORTD
  156. #define SDA_PIN 0
  157. #define SDA_PORT PORTC
  158. #if F_CPU >= 16000000
  159. #define I2C_FASTMODE 1 // Running a 16MHz processor allows us to use I2C fast mode
  160. #endif
  161. #include "SoftI2CMaster.h" // Include must come after setup defines
  162. PCA9685 pwmController; // Library using default linear phase balancing scheme
  163. void setup() {
  164. Serial.begin(115200);
  165. i2c_init(); // Software I2C must be started first
  166. pwmController.resetDevices(); // Software resets all PCA9685 devices on software I2C line
  167. pwmController.init(B000000); // Address pins A5-A0 set to B000000
  168. pwmController.setChannelPWM(0, 2048); // Should see a 50% duty cycle along the 5ms phase width
  169. }
  170. ```
  171. ## Module Info
  172. If one uncomments the PCA9685_ENABLE_DEBUG_OUTPUT define in the libraries main header file (thus enabling debug output) the printModuleInfo() method becomes available, which will display information about the module itself, including initalized states, register values, current settings, etc. All calls being made will display internal debug information about the structure of the call itself. An example of this output is shown here:
  173. In PCA9685.h:
  174. ```Arduino
  175. // Uncomment this define to enable debug output.
  176. #define PCA9685_ENABLE_DEBUG_OUTPUT 1
  177. ```
  178. In main sketch:
  179. ```Arduino
  180. #include <Wire.h>
  181. #include "PCA9685.h"
  182. PCA9685 pwmController;
  183. void setup() {
  184. Serial.begin(115200);
  185. Wire.begin(); // Wire must be started first
  186. Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
  187. pwmController.printModuleInfo();
  188. }
  189. ```
  190. In serial monitor:
  191. ```
  192. ~~~ PCA9685 Module Info ~~~
  193. i2c Address:
  194. 0x40
  195. Phase Balancer:
  196. PCA9685_PhaseBalancer_Weaved
  197. Proxy Addresser:
  198. false
  199. Mode1 Register:
  200. PCA9685::readRegister regAddress: 0x0
  201. PCA9685::readRegister retVal: 0x20
  202. 0x20, Bitset: PCA9685_MODE_AUTOINC
  203. Mode2 Register:
  204. PCA9685::readRegister regAddress: 0x1
  205. PCA9685::readRegister retVal: 0xC
  206. 0xC, Bitset: PCA9685_MODE_OUTPUT_ONACK PCA9685_MODE_OUTPUT_TPOLE
  207. SubAddress1 Register:
  208. PCA9685::readRegister regAddress: 0x2
  209. PCA9685::readRegister retVal: 0xE2
  210. 0xE2
  211. SubAddress2 Register:
  212. PCA9685::readRegister regAddress: 0x3
  213. PCA9685::readRegister retVal: 0xE4
  214. 0xE4
  215. SubAddress3 Register:
  216. PCA9685::readRegister regAddress: 0x4
  217. PCA9685::readRegister retVal: 0xE8
  218. 0xE8
  219. AllCall Register:
  220. PCA9685::readRegister regAddress: 0x5
  221. PCA9685::readRegister retVal: 0xE0
  222. 0xE0
  223. ```