Skip to content

Instantly share code, notes, and snippets.

@justind000
Last active December 12, 2021 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justind000/b9d6dd3b118494040635f9b64eff4a16 to your computer and use it in GitHub Desktop.
Save justind000/b9d6dd3b118494040635f9b64eff4a16 to your computer and use it in GitHub Desktop.
Simple class to use Analog Device's AD5243 - 256-Position Dual Channel I2C Compatible Digital Potentiometer
/* MIT License
Copyright (c) 2017 Justin Decker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Datasheet: https://www.analog.com/en/products/ad5243.html
Resistance set is measured between B1 and W1 for channel 0, and B2 and W2 for channel 1
Simple example
---
#include "AD5243.h"
AD5243 variable_pot(100000);
void setup() {
Wire.begin();
variable_pot.begin();
variable_pot.setResistance(0, 12700); // channel (0 or 1), and resistance in ohms
variable_pot.setResistance(1, 47000);
}
void loop() {}
---
*/
#pragma once
#include "Arduino.h"
#include <Wire.h>
#define AD5243_I2C_ADDRESS 0x2f
class AD5243
{
public:
uint32_t channel1Resistance;
uint32_t channel2Resistance;
AD5243(long total_resistance)
{
_total_resistance = total_resistance;
}
bool begin(TwoWire &wirePort=Wire, uint8_t address=AD5243_I2C_ADDRESS)
{
_address = address;
_i2cPort = &wirePort;
return connected();
}
bool connected()
{
_i2cPort->beginTransmission(_address);
uint8_t retval = _i2cPort->endTransmission();
if (retval)
{
return false;
}
else
{
return true;
}
}
bool setResistance(uint8_t channel, long resistance)
{
uint8_t b[5];
byte step = (255.0 * ((float)resistance / (float)_total_resistance));
byte _channel = 0;
if (channel == 1)
{
_channel = 128;
channel2Resistance = resistance;
}
else
{
channel1Resistance = resistance;
}
b[0] = _channel;
b[1] = step;
_i2cPort->beginTransmission(_address);
_i2cPort->write(b, 2);
uint8_t retval = _i2cPort->endTransmission();
if (retval)
{
return false;
}
else
{
return true;
}
}
private:
uint8_t _address;
TwoWire *_i2cPort;
long _resistance;
long _total_resistance;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment