
Arduino Quick Start
The demo board provides four DAC analog output channels whose voltages can be configured in software. The module communicates over the I2C bus using the 7-bit device address 0x4C.
Modules that communicate over I2C can be connected to the M5Core default I2C pins, PIN21 (SDA) and PIN22 (SCL).
The program sets the four DAC outputs to approximately 1.25 V, 2.50 V, 3.75 V, and 5.00 V, and displays the theoretical output voltages on the screen.
#include <M5Unified.h>
constexpr uint8_t DAC_ADDR = 0x4C;
bool outVoltage(uint8_t ch, uint16_t v) {
uint8_t cmd = 0x10 | (ch << 1);
uint8_t d[2] = { uint8_t((v >> 2) & 0xFF), uint8_t((v << 6) & 0xFF) };
return M5.In_I2C.writeRegister(DAC_ADDR, cmd, d, 2, 100000);
}
void setup() {
M5.begin(M5.config());
Serial.begin(115200);
dacWrite(25, 0);
M5.Display.setBrightness(100);
M5.Display.setTextSize(2);
M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Display.fillScreen(TFT_BLACK);
M5.Display.drawString("DAC", 145, 10);
const uint16_t val[4] = {256, 512, 768, 1023};
bool ok = true;
for (int i = 0; i < 4; i++) {
ok &= outVoltage(i, val[i]);
uint32_t mv = (uint32_t)val[i] * 5000 / 1023;
M5.Display.setCursor(10, 80 + i * 30);
M5.Display.printf("CH%d:%4u.%02uV", i, mv / 1000, mv % 1000 / 10);
}
Serial.println(ok ? "DAC output configured" : "DAC I2C write failed");
}
void loop() { delay(1000); }After uploading the program, you should see the following result.
