Advertisement
sijun

RelayTest with PCF8574

Feb 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2.  
  3. //  Address of PCF8574
  4. #define DEVICE_1 B0100000  
  5.  
  6. byte Relay = 0;
  7.  
  8. //  Write a byte to the IO expander
  9. void IOexpanderWrite(byte address, byte _data ) {
  10.   Wire.beginTransmission(address);
  11.   Wire.write(_data);
  12.   Wire.endTransmission();
  13. }
  14.  
  15. void setup() {
  16.   Serial.begin(115200);
  17.   Wire.begin(D4, D3);/* join i2c bus with SDA=D4 and SCL=D3 of NodeMCU */
  18. }
  19.  
  20. /*
  21.  * Instead of an integer value you could use a byte.
  22.  * With the byte you can easily set/clear every relay.
  23.  * You can use
  24.  * bitSet(ByteVariable, BitToSet);
  25.  * bitClear(ByteVariable, BitToClear);
  26.  */
  27.  
  28. void loop() {
  29.   while(i<255) {                  // count up
  30.     Serial.println(i);            // for debug
  31.     IOexpanderWrite(DEVICE_1, i); // send data to PCF
  32.     i++;                          // increase count value
  33.     delay(100);                   // a little delay
  34.   }
  35.  
  36.   while(i>1) {                    // count down
  37.     Serial.println(i);            // for debug
  38.     IOexpanderWrite(DEVICE_1, i); // send data to PCF
  39.     i--;                          // decrease count value
  40.     delay(100);                   // a little delay
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement