ledEsp32OnTheSecondI2C.ino 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * PCF8574 GPIO Port Expand
  3. * Blink all led
  4. * by Mischianti Renzo <http://www.mischianti.org>
  5. *
  6. * https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
  7. *
  8. *
  9. * PCF8574 ----- Esp32
  10. * A0 ----- GRD
  11. * A1 ----- GRD
  12. * A2 ----- GRD
  13. * VSS ----- GRD
  14. * VDD ----- 5V/3.3V
  15. * SDA ----- 21
  16. * SCL ----- 22
  17. *
  18. * P0 ----------------- LED0
  19. * P1 ----------------- LED1
  20. * P2 ----------------- LED2
  21. * P3 ----------------- LED3
  22. * P4 ----------------- LED4
  23. * P5 ----------------- LED5
  24. * P6 ----------------- LED6
  25. * P7 ----------------- LED7
  26. *
  27. */
  28. #include "Arduino.h"
  29. #include "PCF8574.h" // https://github.com/xreef/PCF8574_library
  30. // Instantiate Wire for generic use at 400kHz
  31. TwoWire I2Cone = TwoWire(0);
  32. // Instantiate Wire for generic use at 100kHz
  33. TwoWire I2Ctwo = TwoWire(1);
  34. // Set i2c address
  35. PCF8574 pcf8574(&I2Ctwo, 0x20);
  36. // PCF8574 pcf8574(&I2Ctwo, 0x20, 21, 22);
  37. // PCF8574(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
  38. // PCF8574(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
  39. void setup()
  40. {
  41. Serial.begin(112560);
  42. I2Cone.begin(16,17,400000U); // SDA pin 16, SCL pin 17, 400kHz frequency
  43. delay(1000);
  44. // Set pinMode to OUTPUT
  45. for(int i=0;i<8;i++) {
  46. pcf8574.pinMode(i, OUTPUT);
  47. }
  48. Serial.print("Init pcf8574...");
  49. if (pcf8574.begin()){
  50. Serial.println("OK");
  51. } else {
  52. Serial.println("KO");
  53. }
  54. }
  55. void loop()
  56. {
  57. static int pin = 0;
  58. pcf8574.digitalWrite(pin, HIGH);
  59. delay(400);
  60. pcf8574.digitalWrite(pin, LOW);
  61. delay(400);
  62. pin++;
  63. if (pin > 7) pin = 0;
  64. }