Arduino library for MCP_DAC MCP48xx and MCP49xx series SPI-DAC
Arduino library for Microchip SPI DAC's: 8, 10, 12 bits, 1, 2 channel.
The MCP_DAC is a library for DAC's from Microchip in the MCP48xx en MCP49xx series. The library is not tested with all different devices however stable.
Please post an issue if there are problems with a specific device.
| Type | Channels | Bits | MaxValue | Voltage reference |
|---|---|---|---|---|
| MCP4801 | 1 | 8 | 255 | internal 2.048 V |
| MCP4802 | 2 | 8 | 255 | internal 2.048 V |
| MCP4811 | 1 | 10 | 1023 | internal 2.048 V |
| MCP4812 | 2 | 10 | 1023 | internal 2.048 V |
| MCP4821 | 1 | 12 | 4095 | internal 2.048 V |
| MCP4822 | 2 | 12 | 4095 | internal 2.048 V |
| MCP4901 | 1 | 8 | 255 | external |
| MCP4902 | 2 | 8 | 255 | external |
| MCP4911 | 1 | 10 | 1023 | external |
| MCP4912 | 2 | 10 | 1023 | external |
| MCP4921 | 1 | 12 | 4095 | external |
| MCP4922 | 2 | 12 | 4095 | external |
The output voltage of the MCP_DAC depends on the voltage supplied, which is in the range of 2.7V .. 5.5V. Check datasheet for the details.
Version 0.5.0 introduced a breaking change to improve handling the SPI dependency. The user has to call SPI.begin() or equivalent before calling MCP.begin(). Optionally the user can provide parameters to the SPI.begin(...)
The version 0.4.0 has breaking changes in the interface. The rationale is that the programming environment of the Arduino ESP32 S3 board uses a remapping by means of the include file io_pin_remap.h. This file remaps the pins of several core Arduino functions. The remapping is implemented by #define macros and these implement "hard" text replacements without considering context. The effect is that methods from this class (and several others) which have the same name as those Arduino core functions will be remapped into something not working.
The following library functions have been renamed:
| old name | new name | notes |
|---|---|---|
| analogRead() | read() | |
| analogWrite() | write() | |
| pinMode() | pinMode1() | |
| digitalRead() | read1() | |
| digitalWrite() | write1() |
The version 0.3.0 has breaking changes in the interface. The essence is that the dependency of Wire (ESP32 / RP2040) is removed from the library. This makes it possible to support the ESP32-S3 and other processors in the future. Also it makes the library a bit simpler to maintain.
#include "MCP_DAC.h"
In reset() the gain is default set to 1x as that gives the lowest output voltages. This is the safest option, although datasheet 4.1.1.1 states default = 2x.
Note: The analog output cannot go beyond the supply voltage.
So if Vref is connected to 5V, gain = 2 will not output 10 Volts.
For fastest speed there is an example added MCP4921_standalone.ino. That squeezes the most performance out of it for now. Code for the other MCP4xxx can be written in same way.
To be used only if one needs a specific speed. Check datasheet for details.
Note: pre 0.2.0 versions have the LDAC signal incorrectly inverted.
MCP49xxx series only, see page 20 ==> not functional for MCP48xx series.
Several functions removed in 0.3.0 as they were too processor specific, and prevented support for the ESP32-S3.
ESP32 (first series) has four SPI peripherals from which two can be used.
SPI0 and SPI1 are used to access flash memory. SPI2 and SPI3 are "user" SPI controllers a.k.a. HSPI and VSPI.
| MCP4922 | HSPI = SPI2 | VSPI = SPI3 |
|---|---|---|
| CS | SELECT = 15 | SELECT = 5 |
| SCK | SCLK = 14 | SCLK = 18 |
| SDI | MOSI = 13 | MOSI = 23 |
| not used | MISO = 12 | MISO = 19 |
By using different SELECT pins multiple DAC's can be controlled over one SPI bus.
The ESP32-S3 introduces user access to FSPI (which is reused from flash memory). Depending on ESP32 series e.g. HSPI is different, see code snippet below.
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
#define FSPI 0
#define HSPI 1
#else
#define FSPI 1 //SPI bus attached to the flash (can use the same data lines but different SS)
#define HSPI 2 //SPI bus normally mapped to pins 12 - 15, but can be matrixed to any pins
#if CONFIG_IDF_TARGET_ESP32
#define VSPI 3 //SPI bus normally attached to pins 5, 18, 19 and 23, but can be matrixed to any pins
#endif
#endif
The SPI Port selections happens in the hardware constructor with e.g. &SPI, &SPI1 etc. In pre-0.3.0 an experimental feature void setGPIOpins was supported to adjust the hardware pins however this should now be handled by the user outside the library.
The RP2040 has two SPI peripherals from which two can be used.
SPI (SPI0) and SPI1 can both be used to connect devices.
| MCP4922 | SPI / SPI0 | SPI1 |
|---|---|---|
| CS | SELECT = 17 | SELECT = 13 |
| SCK | SCLK = 18 | SCLK = 14 |
| SDI | MOSI = 19 | MOSI = 15 |
| not used | MISO = 16 | MISO = 12 |
By using different SELECT pins multiple DAC's can be controlled over one SPI bus.
See examples
If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.
Thank you,
No configuration available
Related projects feature coming soon
Will recommend related projects based on sub-categories