pleasedontcode

# I2C Expander rev_01

Feb 5th, 2026
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # I2C Expander
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2026-02-05 14:51:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* add i2c gpio expander PCF8574 */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. // Create PCF8574 instance with default I2C address 0x20
  25. // Address range: 0x20 to 0x27 depending on A0, A1, A2 pins
  26. PCF8574 expander;
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. void setup(void)
  33. {
  34.     // Initialize Serial communication
  35.     Serial.begin(115200);
  36.     delay(100);
  37.    
  38.     Serial.println("\n\nPCF8574 I2C GPIO Expander Initialization...");
  39.    
  40.     // Initialize PCF8574 with I2C address 0x20
  41.     // Using default ESP8266 pins: SDA=4 (D2), SCL=5 (D1)
  42.     if (expander.begin()) {
  43.         Serial.println("PCF8574 initialized successfully!");
  44.         Serial.print("I2C Address: 0x");
  45.         Serial.println(0x20, HEX);
  46.        
  47.         // Initialize all pins to HIGH (default state)
  48.         expander.writePort(0xFF);
  49.         Serial.println("All pins set to HIGH");
  50.     } else {
  51.         Serial.println("Failed to initialize PCF8574!");
  52.         Serial.println("Please check I2C connections and address configuration");
  53.     }
  54.    
  55.     delay(500);
  56. }
  57.  
  58.  
  59. void loop(void)
  60. {
  61.     // Check if PCF8574 is still connected
  62.     if (expander.isConnected()) {
  63.         // Example: Set pins individually
  64.         // Set pin 0 to HIGH
  65.         expander.setPin(0);
  66.         delay(500);
  67.        
  68.         // Set pin 1 to HIGH
  69.         expander.setPin(1);
  70.         delay(500);
  71.        
  72.         // Clear pin 0 to LOW
  73.         expander.clearPin(0);
  74.         delay(500);
  75.        
  76.         // Clear pin 1 to LOW
  77.         expander.clearPin(1);
  78.         delay(500);
  79.        
  80.         // Toggle pin 2 multiple times
  81.         expander.togglePin(2);
  82.         delay(250);
  83.         expander.togglePin(2);
  84.         delay(250);
  85.        
  86.         // Read entire port state
  87.         uint8_t portState = expander.readPort();
  88.         Serial.print("Port state: 0x");
  89.         Serial.println(portState, HEX);
  90.        
  91.         // Read individual pin states
  92.         for (int i = 0; i < 8; i++) {
  93.             Serial.print("Pin ");
  94.             Serial.print(i);
  95.             Serial.print(": ");
  96.             Serial.println(expander.getPinState(i) ? "HIGH" : "LOW");
  97.         }
  98.        
  99.         Serial.println("---");
  100.         delay(2000);
  101.     } else {
  102.         Serial.println("PCF8574 device not connected!");
  103.         delay(2000);
  104.     }
  105. }
  106.  
  107. /* END CODE */
  108.  
Advertisement
Add Comment
Please, Sign In to add comment