Skip to main content

Arduino Examples

Now that we have the library installed, let's take a closer look at a few of the examples included in it.

Example 1 - Basic

The first example shows how to write the DAC to two voltages: 0 and 3.3V. The code initializes the MCP4725 on the I2C bus and then toggles the DAC between the two voltages every 500ms. Open the example by navigating to File > Examples > SparkFun MCP4725 Arduino Library > Example01_Basic. Select your Board and Port and click the Upload button. After the code finishes uploading, open the serial monitor with the baud set to 115200 and you should see some setup messages print out followed by messages every 500ms showing the DAC is set to either 0V or 3.3V.

Example 2 - Waveform

Example 2 creates a lookup table to provide waveforms generated by the DAC. The lookup table in the example generates sine waves and loops through it to generate a smooth waveform on the DAC output.

Example 3 - Triangle Wave

The third example is similar to Example 2 and uses a lookup table to generate a waveform on the DAC but run at the full 12-bit scale to generate a triangle wave. We use this example in the Quick Start Guide to drive an LED and visualize the waveform with the LED pulsing on and off.

Example 4 - Read

Example 4 shows how to write to both the DAC and EEPROM and then return the stored values over a serial printout. The example sets the EEPROM to 1024 (~0.825V with VDD @3.3V) and the DAC to 2048 (~1.65V with VDD @3.3V) and the main loop just prints out these values. Note, the values stored in the EEPROM will persist through power cycles but the DAC value will reset to the EEPROM value on reset.