Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Controlled LED Matrix with 16-Channel Multiplexers and RS485 Communication

View and Edit Project In Cirkit DesignerImage of ESP32-Controlled LED Matrix with 16-Channel Multiplexers and RS485 Communication

Circuit Documentation

Summary

This circuit is designed to control LEDs connected to four 16-channel analog multiplexers using an ESP32 microcontroller. The ESP32 receives commands via an RS485 module to turn on specific LEDs. The command format is a single byte where the upper 2 bits represent the multiplexer index (0-3) and the lower 4 bits represent the LED index (0-15).

Component List

  1. 16 Channel Analog Multiplexer Bob

    • Description: A multiplexer used to control multiple LEDs.
    • Pins: GND, VCC, EN, S0, S1, S2, S3, SIG, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15
  2. ESP32 38 PINS

    • Description: A microcontroller used to control the multiplexers and communicate via RS485.
    • Pins: GND, G23, G22, TXD, RXD, G21, G19, G18, G5, G17, G16, G4, G0, G2, G15, SDI, SD0, CLK, 3V3, EN, SP, SN, G34, G35, G32, 33, G25, G26, G27, G14, G12, G13, SD2, SD3, 5V
  3. Resistor

    • Description: A resistor used to limit current to the LEDs.
    • Pins: pin1, pin2
    • Properties: Resistance: 1000 Ohms
  4. LED: Two Pin (red)

    • Description: A red LED.
    • Pins: cathode, anode
  5. RS-485 Module

    • Description: A module used for RS485 communication.
    • Pins: VCC, B, A, GND, RO, RE, DE, DI
  6. RS485 to USB Converter

    • Description: A converter used to interface RS485 with USB.
    • Pins: A/TX, B/RX

Wiring Details

16 Channel Analog Multiplexer Bob

ESP32 38 PINS

Resistor

LED: Two Pin (red)

RS-485 Module

RS485 to USB Converter

Code Documentation

/*
 * This Arduino Sketch controls LEDs connected to four 16-channel analog
 * multiplexers using an ESP32 microcontroller. The ESP32 receives commands
 * via an RS485 module to turn on specific LEDs. The command format is a
 * single byte where the upper 2 bits represent the multiplexer index (0-3)
 * and the lower 4 bits represent the LED index (0-15).
 */

// Multiplexer control pins
const int enPins[4] = {23, 22, 21, 19};
const int s0Pin = 18;
const int s1Pin = 5;
const int s2Pin = 4;
const int s3Pin = 2;

#define Rflag 15
#define RXD2 16
#define TXD2 17

void setup() {
  // Initialize serial communication for RS485
  Serial.begin(9600);  
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);

  // Initialize multiplexer control pins
  for (int i = 0; i < 4; i++) {
    pinMode(enPins[i], OUTPUT);
    digitalWrite(enPins[i], HIGH); // Disable all multiplexers initially
  }
  pinMode(s0Pin, OUTPUT);
  pinMode(s1Pin, OUTPUT);
  pinMode(s2Pin, OUTPUT);
  pinMode(s3Pin, OUTPUT);
  pinMode(Rflag, OUTPUT);
  digitalWrite(Rflag, LOW);
}

void loop() {
  if (Serial2.available()) {
    int command = Serial2.read();
    Serial.print("command:");
    Serial.println(command, BIN);
    int muxIndex = (command >> 4) & 0x03; // Extract multiplexer index (0-3)
    Serial.print("muxIndex:");
    Serial.println(muxIndex, BIN);
    int ledIndex = command & 0x0F; // Extract LED index (0-15)
    Serial.print("ledIndex:");
    Serial.println(ledIndex, BIN);
    
    if (bitRead(command, 6) == 1){
      // Set multiplexer address
      digitalWrite(s0Pin, ledIndex & 0x01);
      digitalWrite(s1Pin, (ledIndex >> 1) & 0x01);
      digitalWrite(s2Pin, (ledIndex >> 2) & 0x01);
      digitalWrite(s3Pin, (ledIndex >> 3) & 0x01);

      // Enable the selected multiplexer
      for (int i = 0; i < 4; i++) {
        digitalWrite(enPins[i], i == muxIndex ? LOW : HIGH);
      }
    } else {
      for (int i = 0; i < 4; i++) {
        digitalWrite(enPins[i], HIGH); // Disable all multiplexers initially
      }
    }
  }
}

This code initializes the ESP32 microcontroller to control the multiplexers and LEDs. It sets up the serial communication for RS485 and configures the multiplexer control pins. In the main loop, it reads commands from the RS485 module and controls the multiplexers and LEDs accordingly.