Arduino library for controlling the Microchip MCP4251/MCP4252 digital potentiometers.
Currently compatible with MCP4251, MCP4252,MCP4231, MCP4232 digital potentiometers, but support is planned for further models.
Tested with the following Arduino boards, but it should be compatible with any boards that can support SPI:
- Arduino Uno
- Adafruit Metro M0 Expressto
To install open Arduino IDE -> Sketch -> Include Library -> Add .ZIP Library... and select the source code (.zip) file.
To use, simply include MCP425X.h
in your sketch.
Each potentiometer connected to the Arduino is an instance of the Microchip_MCP4251
class.
Lines 26 to 27 in 4e0578f
setup()
section of the sketch, begin()
needs to be called to initialize the CS pins and the SPI bus.
This needs to be called before any other operations are run on the potentiometer.
Below is an example initialization:
Microchip_MCP4251 digiPot(5, 10000);
void setup()
{
digiPot.begin();
}
This library supports all the operations that are possible with the digital potentiometers, with the exception of shutdown.
This can still be achieved by manually toggling the shutdown pin of the potentiometer using digitalWrite()
.
All commands will set commandError
to true if they fail.
Any subsequent successful commands will set it back to false.
To select which potentiometer an operation is for, either P0
or P1
needs to be passed in the potSelect
argument.
To increment/decrement the wiper, the following functions are provided.
Lines 31 to 35 in 4e0578f
The wiper position is stored in volatile memory in the IC.
Therefore, it can be set or retrieved directly using the setWiper()
and getWiper()
functions.
Lines 37 to 38 in 4e0578f
0x100
) when setting the wiper, as it's the maximum value the 8-bit digital potentiometers use, respectively 0x80
is the max for 7-bit models.
Instead of setting the wiper position directly as an unsigned integer between 0x00
and 0x100
, the resistance between terminal B and the wiper can be set and retrieved using the setResistance()
andgetResistance()
functions.
Lines 40 to 41 in 4e0578f
The getStatus
function can be used to get the status of the potentiometer (if in shutdown or normal state).
Line 43 in 4e0578f
After use of the potentiometers have finished, SPI.end()
can be safely called to disable the SPI bus.
An additional call to begin()
can re-establish normal functionality.