Example Code for Arduino - ESP32-S3 native I2C interface

Last revision 2026/02/05

Function Description

The DMX512 controller provides an I2C interface, which can be used to connect a large number of open-source hardware sensors and actuators, greatly expanding the controller's application scenarios.
Native GPIO pins of ESP32-S3

The mapping of I2C pins is as follows:

Pins Pins function
SDA ESP32-S3 IO2
SCL ESP32-S3 IO1

Example 1: Light Sensing LED

The program will demonstrate how to read the value from the LTR390 light sensor using the I2C interface. The sensor supports power supply from 3.3V to 5V, and it can communicate via UART and I2C. Additionally, there is an Arduino library available for direct use.
Product Link:Gravity: LTR390 UV Light Sensor
Download Arduino Library:DFRobot_LTR390UV Library

Wiring diagram

DFRobot_LTR390UV

Please switch the communication mode switch of the sensor to the I2C side before use.

Programming

Program Function: The following example demonstrates reading the values of the LTR390 light sensor using the I2C interface.

#include "DFRobot_LTR390UV.h"
#include "DFRobot_DMX512.h"

DFRobot_LTR390UV ltr390(/*addr = */LTR390UV_DEVICE_ADDR, /*pWire = */&Wire);
DFRobot_DMX512 dmx512;

void setup()
{
    Serial.begin(115200);  
    while(dmx512.begin() != 0){   //Initialize DMX512.
        Serial.println("init error");
        delay(1000);
    }
    Serial.println("init OK");

    while(ltr390.begin() != 0){
        Serial.println(" Sensor initialize failed!!");
        delay(1000);
    }
    Serial.println(" Sensor  initialize success!!");
    ltr390.setALSOrUVSMeasRate(ltr390.e18bit,ltr390.e100ms);//18-bit data, sampling time 100ms. 
    ltr390.setALSOrUVSGain(ltr390.eGain3);//3x gain
    ltr390.setMode(ltr390.eALSMode); //Set ambient light mode
}

void loop()
{
    float als = 0;
    als = ltr390.readALSTransformData();//Get ambient light data
    Serial.println(als);
    int dataals = map(als,0,14500,1,8);
    Serial.println(dataals);
    dmx512.RGBLight(1,dataals,0,0,255);
    delay(30);
    dmx512.RGBLight(dataals*3+1,8,0,0,0);
    delay(30);

}

Example 2: Voice Recognition Light

This example demonstrates controlling DMX512 lighting using offline voice recognition. You'll need to connect an I2C interface offline voice recognition module. Here is the product link:
https://www.dfrobot.com/product-2665.html
Arduino Library Download Link:
https://github.com/DFRobot/DFRobot_DF2301Q

Wiring diagram

Voice Recognition Light

When using, please switch the communication mode switch of the voice recognition module to the I2C side.

Programming

The program functions as follows:

Wake up: "Hello,Robot."
Say: "Turn on the lights" // lights turn on
Say: "Set to red" // lights set to red
Say: "Set to blue" // lights set to blue
Say: "Set to green" // lights set to green
Say: "Turn off the lights" // lights turn off

#include "DFRobot_DF2301Q.h"
#include "DFRobot_DMX512.h"

DFRobot_DMX512 dmx512;
DFRobot_DF2301Q_I2C asr;

uint8_t state = 0;

void setup() {
    Serial.begin(115200);  
    while(dmx512.begin() != 0){      //Initialize DMX512.
        Serial.println("init error");
        delay(1000);
    }
    Serial.println("init OK");

    while (!(asr.begin())) {
        Serial.println("Communication with device failed, please check connection");
        delay(3000);
    }
    Serial.println("Begin ok!");

    asr.setVolume(4);
    asr.setMuteMode(0);
    asr.setWakeTime(20);

    uint8_t wakeTime = 0;
    wakeTime = asr.getWakeTime();
    Serial.print("wakeTime = ");
    Serial.println(wakeTime);

}

void loop() {
    uint8_t CMDID = asr.getCMDID();
    switch (CMDID) {
        case 117:                                                  //Command 'Turn on the lights
            state = 1;
            dmx512.RGBLight(1,8,255,255,255);
            break;

        case 118:                                                  //Command 'Turn off the lights
            state = 0;
            dmx512.RGBLight(1,8,0,0,0);
            break;

        case 130:                                                  //Command 'Set to Red
            if(state == 1)
                dmx512.RGBLight(1,8,100,0,0);
            break;


        case 133:                                                  //Command 'Set to Green
            if(state == 1)
                dmx512.RGBLight(1,8,0,100,0);
            break;

        case 135:                                                  //Command 'Set to Blue                       
            if(state == 1)
                dmx512.RGBLight(1,8,0,0,100);
            break;

        default:
            if (CMDID != 0) {
                Serial.print("CMDID = ");  
                Serial.println(CMDID);
            }
    }
    delay(300);
}

Was this article helpful?

ON THIS PAGE

TOP