Hi Becory, I really appreciate you giving me your time.
Can I paste a picture (.PNG) from my desktop in these replies? I've tried to copy & paste. Don't know how to use the insert image button.
A problem I have is what pins on the ESP do I use, and how do I refer to them in code?
I picked GIO pins 2, 0 & 4. No reason, I thought you could use any GIO pins.
But have I reference them correctly in my test code?
The library Digipot X9 was written by somebody years ago. It has two files, a .H file and a .CPP and are reproduced below my test code. I know all my code is doing at the moment is printing out the vale of pot I've set. But this was progress as before it wouldn't compile. (The 6,7,10 reference are the pins used in the Arduino.) I was trying to do my project in stages. First being programming the ESP. No if I can connect the digital pot to the board I can change the code manually and measure the resistance on a multimeter from the pot. If I can get that to work that's more progress. Now when I connect the pot to the ESP using gio pins 2, 0 & 4, it still compiles ok, but when I connect the pot to the ESP's ground and 5v pins it throws an error. So I'm thinking these are not power output pins so I was going to try powering the pot with a separate 5v supply. So I'll end here and see what you think before I waffle anymore.
Code: Select all
#include <DigiPotX9Cxxx.h>
#define POT_CS GPIO2
#define POT_UD GPIO0
#define POT_INC GPIO4
DigiPot pot(4,0,2); //6,7,10
void setup()
{
Serial.begin(115200);
delay(5000);
pot.set(40);
}
void loop()
{
// put your main code here, to run repeatedly:
Serial.print("Pot value is ");
Serial.println(pot.get());
delay(5000);
}
Code: Select all
/*
* DigiPotX9Cxxx.h - Arduino library for managing digital potentiometers X9Cxxx (xxx = 102,103,104,503).
* By Timo Fager, Jul 29, 2011.
* Released to public domain.
**/
#ifndef DigiPotX9Cxxx_h
#define DigiPotX9Cxxx_h
#include "Arduino.h"
#define DIGIPOT_UP HIGH
#define DIGIPOT_DOWN LOW
#define DIGIPOT_MAX_AMOUNT 99
#define DIGIPOT_UNKNOWN 255
class DigiPot
{
public:
DigiPot(uint8_t incPin, uint8_t udPin, uint8_t csPin);
void increase(uint8_t amount);
void decrease(uint8_t amount);
void change(uint8_t direction, uint8_t amount);
void set(uint8_t value);
uint8_t get();
void reset();
private:
uint8_t _incPin;
uint8_t _udPin;
uint8_t _csPin;
uint8_t _currentValue;
};
#endif
Code: Select all
/*
* DigiPotX9Cxxx.cpp - Arduino library for managing digital potentiometers X9Cxxx (xxx = 102,103,104,503).
* By Timo Fager, Jul 29, 2011.
* Released to public domain.
**/
#include "Arduino.h"
#include "DigiPotX9Cxxx.h"
DigiPot::DigiPot(uint8_t incPin, uint8_t udPin, uint8_t csPin) {
_incPin = incPin;
_udPin = udPin;
_csPin = csPin;
_currentValue = DIGIPOT_UNKNOWN;
pinMode(_incPin, OUTPUT);
pinMode(_udPin, OUTPUT);
pinMode(_csPin, OUTPUT);
digitalWrite(_csPin, HIGH);
}
void DigiPot::reset() {
// change down maximum number of times to ensure the value is 0
decrease(DIGIPOT_MAX_AMOUNT);
_currentValue = 0;
}
void DigiPot::set(uint8_t value) {
value = constrain(value, 0, DIGIPOT_MAX_AMOUNT);
if (_currentValue == DIGIPOT_UNKNOWN) reset();
if (_currentValue > value) {
change(DIGIPOT_DOWN, _currentValue-value);
} else if (_currentValue < value) {
change(DIGIPOT_UP, value-_currentValue);
}
}
uint8_t DigiPot::get() {
return _currentValue;
}
void DigiPot::increase(uint8_t amount) {
amount = constrain(amount, 0, DIGIPOT_MAX_AMOUNT);
change(DIGIPOT_UP, amount);
}
void DigiPot::decrease(uint8_t amount) {
amount = constrain(amount, 0, DIGIPOT_MAX_AMOUNT);
change(DIGIPOT_DOWN, amount);
}
void DigiPot::change(uint8_t direction, uint8_t amount) {
amount = constrain(amount, 0, DIGIPOT_MAX_AMOUNT);
digitalWrite(_udPin, direction);
digitalWrite(_incPin, HIGH);
digitalWrite(_csPin, LOW);
for (uint8_t i=0; i<amount; i++) {
digitalWrite(_incPin, LOW);
delayMicroseconds(2);
digitalWrite(_incPin, HIGH);
delayMicroseconds(2);
if (_currentValue != DIGIPOT_UNKNOWN) {
_currentValue += (direction == DIGIPOT_UP ? 1 : -1);
_currentValue = constrain(_currentValue, 0, DIGIPOT_MAX_AMOUNT);
}
}
digitalWrite(_csPin, HIGH);
}