Skip to main content

Overview

AutoLight V3 provides a comprehensive LED control system for ESP32 with dynamic PCF8574 I2C expander support. This guide demonstrates basic LED control patterns using the BaseChannel class.
1

Hardware Setup

Connect your PCF8574 I2C expanders to the ESP32:
  • SDA: GPIO 21 (default)
  • SCL: GPIO 22 (default)
  • PCF8574 Addresses: 0x20-0x27 (up to 8 devices)
  • Power: 3.3V or 5V depending on your PCF8574 variant
Each PCF8574 provides 8 GPIO pins for LED control.
2

Include AutoLight V3

Enable AutoLight V3 in your sketch:
#define ENABLE_ADDONS_AUTOLIGHT_V3
#include "Kinematrix.h"
using namespace AutoLight;
3

Initialize the System

Create BaseChannel and BaseConfig instances:
BaseChannel led;
BaseConfig config;

void setup() {
    Serial.begin(115200);
    
    // Configure 12 LED channels across 2 PCF8574 devices
    config.setDynamicConfig(12, 2);
    
    // Attach configuration to LED controller
    led.attachConfig(config.getConfigData());
    
    // Initialize the system
    led.initialize();
}
4

Run the Main Loop

Call runAutoLight() in your main loop:
void loop() {
    led.runAutoLight();
    delay(10);
}

Complete Basic Example

Here’s a complete working example that sets up a 12-channel LED system:
#define ENABLE_ADDONS_AUTOLIGHT_V3
#include "Kinematrix.h"

using namespace AutoLight;

BaseChannel led;
BaseConfig config;

void setup() {
    Serial.begin(115200);
    Serial.println("AutoLight V3 - Basic LED Control");
    
    // Configure 12 channels across 2 PCF8574 devices
    // Default addresses: 0x20 and 0x21
    config.setDynamicConfig(12, 2);
    
    // Set initial sequence mode (2 = Blink All)
    led.setInitSequence(2);
    
    // Set delay time between LED updates (milliseconds)
    led.setInitDelay(50);
    
    // Attach configuration to LED controller
    led.attachConfig(config.getConfigData());
    
    // Initialize the LED system
    if (led.initialize()) {
        Serial.println("LED system initialized successfully");
    } else {
        Serial.println("LED initialization failed!");
    }
}

void loop() {
    // Run the AutoLight system
    led.runAutoLight();
    delay(10);
}

LED Sequence Modes

AutoLight V3 includes 16 built-in LED sequence modes:
ModeNameDescription
0OFFAll LEDs off
1ONAll LEDs on (static)
2Blink AllAll LEDs blink simultaneously
3Fill Two PointLEDs fill from center outward
4Fill RightLEDs fill from right to left
5Fill InLEDs fill from both ends inward
6Blink OneSingle LED blinks sequentially
7Blink Two FillTwo LEDs blink while filling
8SnakeSnake pattern with reverse
9RandomRandom LED activation
10WaveWave propagation effect
11-15ComplexAdvanced pattern combinations

Controlling Sequence Modes

Change Mode Programmatically

void setup() {
    // ... initialization code ...
    
    // Start with mode 5 (Fill In)
    led.setInitSequence(5);
    led.initialize();
}

void loop() {
    led.runAutoLight();
    
    // Change to next mode after 10 seconds
    static unsigned long lastChange = 0;
    if (millis() - lastChange > 10000) {
        led.nextMode();
        lastChange = millis();
        Serial.println("Mode: " + String(led.getSequenceIndex()));
    }
    
    delay(10);
}

Manual Mode Control

// Navigate through modes
led.nextMode();      // Go to next sequence
led.previousMode();  // Go to previous sequence

// Direct control
led.onMode();        // Turn all LEDs on (mode 1)
led.offMode();       // Turn all LEDs off (mode 0)

// Get current state
int currentMode = led.getSequenceIndex();
int currentDelay = led.getDelayTime();

Dynamic Delay Adjustment

Control the speed of LED sequences:
void setup() {
    // ... initialization ...
    
    // Set initial delay to 100ms
    led.setInitDelay(100);
    led.initialize();
}

void loop() {
    led.runAutoLight();
    
    // Adjust delay based on analog input (potentiometer)
    int potValue = analogRead(A0);
    int newDelay = map(potValue, 0, 4095, 30, 300);
    led.setInitDelay(newDelay);
    
    delay(10);
}
Delay values range from 30ms (fast) to 300ms (slow). Lower values create faster animations.

PCF8574 Distribution Strategies

AutoLight V3 supports multiple distribution strategies for LED channels:

Balanced Distribution

Evenly distribute channels across all PCF8574 devices:
config.setDistributionStrategy(DISTRIBUTE_BALANCED);
config.setDynamicConfig(24, 3);  // 24 channels → 8 per PCF

Sequential Distribution

Fill each PCF8574 completely before using the next:
config.setDistributionStrategy(DISTRIBUTE_SEQUENTIAL);
config.setDynamicConfig(24, 8);  // Uses first 3 PCFs fully

Optimized Distribution

Minimize the number of PCF8574 devices used:
config.setDistributionStrategy(DISTRIBUTE_OPTIMIZED);
config.setDynamicConfig(20, 8);  // Uses 3 PCFs (8+8+4)

Custom PCF8574 Addresses

Specify custom I2C addresses for your PCF8574 devices:
void setup() {
    Serial.begin(115200);
    
    // Define custom PCF8574 addresses
    uint8_t customAddresses[] = {0x27, 0x38, 0x45};
    
    config.setDynamicConfig(24, 3);
    config.setPCFAddresses(customAddresses, 3);
    
    led.attachConfig(config.getConfigData());
    led.initialize();
}

Troubleshooting

LEDs Not Responding

  1. Check I2C connections (SDA/SCL)
  2. Verify PCF8574 addresses using I2C scanner
  3. Ensure proper power supply to PCF8574 devices
  4. Check serial output for initialization errors

I2C Scanner Example

void scanI2C() {
    Serial.println("Scanning I2C bus...");
    
    for (uint8_t addr = 0x20; addr <= 0x27; addr++) {
        Wire.beginTransmission(addr);
        if (Wire.endTransmission() == 0) {
            Serial.printf("Found PCF8574 at 0x%02X\n", addr);
        }
    }
}

void setup() {
    Serial.begin(115200);
    Wire.begin();
    
    scanI2C();  // Run before initialization
    
    // Continue with normal setup...
}

Next Steps

Pattern Sequences

Learn how to create custom LED sequences and use advanced patterns

Web Integration

Add WiFi control and REST API to your AutoLight project

Build docs developers (and LLMs) love