MCP4725: an example that runs
Yes. You can use the MCP4725 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (DAC, MCP4725). The example below, "MCP4725 volts, EEPROM and readback", runs on the Arduino Uno R3.
Built in on Mokxi: #include <MCP4725.h> and it builds, with nothing to install. Free, and no account needed.
What MCP4725 does
The MCP4725’s value, voltage and percentage, EEPROM, power-down modes and general call reset. That is what MCP4725, by Rob Tillaart, covers. The sketch below uses it on the Arduino Uno: “MCP4725 volts, EEPROM and readback”.
Rob Tillaart's MCP4725 library's interface (0.4.x), for the Microchip MCP4725 12-bit I2C DAC.
setValue() is the datasheet's fast write (Figure 6-1), two bytes with the power-down bits last set by writePowerDownMode(). writeDAC(value, EEPROM) is the three-byte write, 0x40 or 0x60 (Figure 6-2). readDAC(), readEEPROM() and the power-down reads read the chip's five status and data bytes (Figure 6-3); ready() is its RDY/BSY bit, low for the 25 ms an EEPROM write takes.
The calls follow release 0.4.3 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.
| Library Manager name | MCP4725 |
|---|---|
| Author | Rob Tillaart |
| Include | #include <MCP4725.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | DAC, MCP4725 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <MCP4725.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "MCP4725" by Rob Tillaart from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add MCP4725 to lib_deps in PlatformIO. Mokxi follows the calls of release 0.4.3, so a sketch written here builds there too.
What to know in the simulator: Call Wire.begin() before begin(), as the library asks. setVoltage() scales against setMaxVoltage(), 5 V unless set, which should be the board’s supply.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
MCP4725 MCP(0x60)
Makes MCP, the MCP4725 object the calls below are made on.
MCP.begin()
Starts it up and returns true if it started, false if it did not. Call it once in setup().
MCP.readDAC()
Returns the dac as a whole number.
MCP.setMaxVoltage(5.0)
Sets the max voltage to the value you pass in. Arguments: max volts.
MCP.setVoltage(3.75)
Sets the voltage to the value you pass in. Arguments: volts.
MCP.getValue()
Returns the value as a whole number.
Example: MCP4725 volts, EEPROM and readback
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Wire.h>
#include <MCP4725.h>
MCP4725 MCP(0x60);
void setup() {
Serial.begin(9600);
Wire.begin();
if (!MCP.begin()) {
Serial.println("MCP4725 not connected");
while (1) delay(10);
}
Serial.print("power-on value: ");
Serial.println(MCP.readDAC()); // the factory EEPROM: mid-scale, 2048
MCP.setMaxVoltage(5.0);
MCP.setVoltage(3.75);
Serial.print("set: ");
Serial.println(MCP.getValue());
Serial.print("chip: ");
Serial.println(MCP.readDAC());
// Store 1000 in the EEPROM, wait out the write, and reset to load it.
MCP.writeDAC(1000, true);
while (!MCP.ready()) delay(1);
MCP.setValue(0);
MCP.powerOnReset();
Serial.print("after reset: ");
Serial.println(MCP.readDAC());
}
void loop() {}Wiring
- Arduino Uno R3 A4 to DAC, MCP4725 SDA
- Arduino Uno R3 A5 to DAC, MCP4725 SCL
- Arduino Uno R3 5V to DAC, MCP4725 VCC
- Arduino Uno R3 GND to DAC, MCP4725 GND
Try MCP4725 without installing anything
Open the example, change the sketch, and keep your version in a free account.