# MCP4725 Tutorial with Arduino and ESP-board (NodeMCU)

As We knew that we can’t just generate analog voltage using most microcontrollers. To generate variable output we usually use the PWM which stands for Pulse Width Modulation. Which works for most things, but of course not everything.
Sometimes we really need the true analog Voltage. Like when we have to give input to certain circuits. One of the solutions to do that is using DAC. So, In this video, we will learn how to generate true analog voltage using this MCP475 DAC module. And let’s see the result.

DAC stands for Digital to Analog Converter, this module has the ability to generate true analog Voltage. It has a 12-bit resolution. So you could generate 4096 steps voltage output.

If we use Arduino, the maximum voltage output would be 5V. Since the voltage reference is 5V. So 4096 would mean 5V and 0 would mean 0V. So, if we want to generate an output of 2.5V, for example, we need to find the middle value of this range. So it would be around 2048. And you can find any value using this simple formula

Value = V*4096/5;

This formula also applies to 3.3V if we use an ESP8266 board, which usually only has 3.3V power.

Value = V*4096/3.3;

## MCP4725 With Arduino Wiring Diagram

You can simply hook it up to Arduino. Since it has an I2C communication protocol, as usual, we just need to connect the SDA to A4 and SCL to A5. I used the 5V as VCC.

## The Code and Library

You can download the library from here. Or just download using the library manager in the Arduino IDE. Here’s the example if you want to generate the 1 V voltage. Just change the value as you wish.

#include <Wire.h>

// Set this value to 9, 8, 7, 6 or 5 to adjust the resolution
#define DAC_RESOLUTION    (9);

void setup() {
Serial.begin(9600);
Serial.println("Hello!");
dac.begin(0x60);
}

void loop() {
// put your main code here, to run repeatedly:
dac.setVoltage(1 * 4096 / 5, false); //Change the 1 value according to your desired voltage output
}

If you notice that the result is slightly inaccurate. This is caused by the voltage reference that is not 5V. When I measure my Arduino power voltage it is about 4.94V. So to make the result accurate, you can change the multiplier or the divider. Just remember, that the maximum result should be not more than 4096.

Value = V*4096/4.9;

## I2C Scanner

If for any reason, your module doesn’t work, maybe you have the wrong address. So to figure out your I2C address. You need to scan the device that is connected to your Arduino. Just upload the code below and open the serial monitor. And you should find the address there.

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>

void setup()
{
Wire.begin();

Serial.begin(9600);
while (!Serial);             // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}

void loop()
{
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
error = Wire.endTransmission();

if (error == 0)
{
Serial.print("I2C device found at address 0x");
Serial.print("0");
Serial.println("  !");

nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
Serial.print("0");
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");

delay(500);           // wait 5 seconds for next scan
}

## Generate Sinus and Triangle Wave

And more than that, using the library example we can also generate sinus waves just open the example in the library directory named Sinus Wave or Triangle Wave. Cool Right?

## MCP4725 DAC with ESP8266 or NodeMCU

Now, how to use this on an ESP board like nodeMCU. We can use the same way, just follow this wiring diagram and on the code, we should change the voltage reference to 3.3V. And this is the result. To use it with an ESP8266 board, you can just do it the same way as Arduino. It is able to run on 3.3V. You just need to remember that since it has a 3.3V power supply. The maximum voltage output would be 3.3V.

#include <Wire.h>

// Set this value to 9, 8, 7, 6 or 5 to adjust the resolution
#define DAC_RESOLUTION    (9);

void setup() {
Serial.begin(9600);
Serial.println("Hello!");
dac.begin(0x60);
}

void loop() {
// put your main code here, to run repeatedly:
dac.setVoltage(1 * 4096 / 3.3, false);
}

And Here’s the Complete tutorial on Video