Skip to content

Commit

Permalink
Initial commit of working library.
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kreitzer committed Dec 14, 2023
0 parents commit 758b606
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.vscode
.idea
40 changes: 40 additions & 0 deletions AD5292.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
AD5292.h - AD5292 library for Arduino
MIT License
Copyright (c) 2023 Markus Kreitzer.
Website: https://github.com/markuskreitzer/AD5292
*/

#include "AD5292.h"

AD5292::AD5292(uint8_t csPin, uint32_t spiFreq) : _csPin(csPin), _spiFreq(spiFreq) {}

void AD5292::begin() {
pinMode(_csPin, OUTPUT);
digitalWrite(_csPin, HIGH);
SPI.begin();
}

bool AD5292::setWiperPosition(uint16_t position) {
if (position > 1023) {
return false; // Invalid position value
}

uint16_t command = (0x01 << 10) | (position & 0x03FF);
sendCommand(command);
delay(6);

return true; // Successful operation
}

void AD5292::sendCommand(uint16_t command) {
SPI.beginTransaction(SPISettings(_spiFreq, MSBFIRST, SPI_MODE1));
digitalWrite(_csPin, LOW);
SPI.transfer16(command);
digitalWrite(_csPin, HIGH);
SPI.endTransaction();
}

void AD5292::setSpiFrequency(uint32_t spiFreq) {
_spiFreq = spiFreq;
}
29 changes: 29 additions & 0 deletions AD5292.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
AD5292.h - AD5292 library for Arduino
MIT License
Copyright (c) 2023 Markus Kreitzer.
Website: https://github.com/markuskreitzer/AD5292
*/

#ifndef AD5292_H
#define AD5292_H

#include <Arduino.h>
#include <SPI.h>

class AD5292 {
public:
AD5292(uint8_t csPin, uint32_t spiFreq = 1000000);

void begin();
bool setWiperPosition(uint16_t position);
void setSpiFrequency(uint32_t spiFreq);

private:
uint8_t _csPin;
uint32_t _spiFreq;
void sendCommand(uint16_t command);
};

#endif // AD5292_H
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# AD5292 Arduino/ESP32 Library

This is an Arduino library for interfacing with the AD5292 digital potentiometer using the SPI communication protocol. I've mainly used this with an ESP32, but the initial testing was on an Arduino MEGA so should work on anything that complies with the Arduino API. It should be trivial to adapt to other Analog Devices digital potentiometers. PR/MRs are welcome.

## Features

- Easy interfacing with the AD5292 digital potentiometer.
- Support for setting the wiper position.
- Configurable SPI frequency.
- Simple and efficient methods for communicating with the device.

## Installation

1. Download this library as a zip file.
2. In the Arduino IDE, go to `Sketch` > `Include Library` > `Add .ZIP Library...` and select the downloaded file.
3. Restart the Arduino IDE.

## Usage

Include the library in your sketch and follow the example below to get started:

```cpp
#include <SPI.h>
#include "AD5292.h"

// Define the Chip Select pin
#define CS_PIN 5

// Create an instance of the AD5292 class
AD5292 digitalPot(CS_PIN);

void setup() {
// Initialize the AD5292 device
digitalPot.begin();

// Set the wiper position
if (digitalPot.setWiperPosition(512)) {
Serial.println("Wiper position set successfully.");
} else {
Serial.println("Failed to set wiper position.");
}
}

void loop() {
// Your code here
}
```
## Methods
- `begin()`: Initializes the SPI interface and configures the chip select pin.
- `setWiperPosition(uint16_t position)`: Sets the wiper position of the AD5292.
- `setSpiFrequency(uint32_t spiFreq)`: Sets the SPI communication frequency.
## License
This library is released under the [MIT License](LICENSE).
## Author
[Markus Kreitzer](https://github.com/markuskreitzer)
## License
MIT License
Copyright (c) 2023 Markus Kreitzer
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.
32 changes: 32 additions & 0 deletions examples/led_fader/LED_Fader.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <SPI.h>
#include "AD5292.h"

#define CS_PIN 5 // Chip Select pin for AD5292


/*
AD5292 library for Arduino
MIT License
Copyright (c) 2023 Markus Kreitzer.
Website: https://github.com/markuskreitzer/AD5292
Example Use of the AD5292 library
*/


AD5292 digitalPot(CS_PIN);

void setup() {
Serial.begin(115200);
digitalPot.begin();
}

void loop() {
for (int i = 0; i <= 1023; i++) {
digitalPot.setWiperPosition(i);
delay(5); // Adjust for desired speed of fading
}
for (int i = 1023; i >= 0; i--) {
digitalPot.setWiperPosition(i);
delay(5); // Adjust for desired speed of fading
}
}
53 changes: 53 additions & 0 deletions examples/led_fader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Breathing LED with AD5292

This example demonstrates how to use the AD5292 digital potentiometer Arduino library to create a breathing LED effect. The LED's brightness is controlled by adjusting the resistance of the potentiometer in a voltage divider configuration.

## Circuit Diagram

Connect your AD5292 and LED as follows:
- A (Terminal A of AD5292) to VDD.
- W (Wiper of AD5292) to the anode of the LED.
- Cathode of the LED to a 330 Ohm resistor, then to the ground. This is to limit current and prevent the LED from burning out.
- B (Terminal B of AD5292) to the ground.

## Setup

1. Connect the AD5292 to your Arduino as per the circuit diagram using the respective SPI pins for your model. For the ESP32 you can follow this table. It is also helpful because it seems like there are many nomenclatures for SPI lines.

| AD5292 | ESP-WROOM-32 | Comment |
| ------ | ------------ | --------------------- |
| RESET | VLogic | |
| RDY | - | Float |
| SDO | - | Float (MISO) |
| SYNC | CS (D5) | aka Slave Select (SS) |
| SCLK | CLK (D18) | Clock |
| DIN | MOSI (D23) | Data In |
| VSS | GND | 0V |
| VDD | <15V | Can use 3.3V on ESP32 |
| A | VDD | Pot A |
| B | GND | Pot B |
| W | LED Anode | Wiper |
| VLogic | 3.3V | |
| GND | GND | |


2. Upload the `BreathingLED.ino` sketch to your Arduino.

## Code Explanation

- The sketch gradually increases and then decreases the wiper position, causing the LED to fade in and out, creating a breathing effect.
- The `setWiperPosition` method is used to change the resistance, thus controlling the LED brightness.

## Notes

- Ensure the LED and AD5292 are correctly oriented and connected.
- Adjust the delay in the loop for faster or slower breathing effects.
- The AD5292 has many command functions such as programming preset wiper positions, etc. Feel free to send an MR to add more methods.

## License

This project is released under the MIT License.

## Author

Markus Kreitzer
9 changes: 9 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#######################################
# Syntax Coloring Map For AD5292
#######################################

AD5292 KEYWORD1
begin KEYWORD2
setWiperPosition KEYWORD2
sendCommand KEYWORD2
setSpiFrequency KEYWORD2

0 comments on commit 758b606

Please sign in to comment.