ESP32 and digital pot X9C102

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

ESP32 and digital pot X9C102

Postby unicorn » Wed Apr 14, 2021 8:08 pm

Hello all.
I have a circuit which uses a 1k digital pot connected to an Arduino Uno board. I can set this pot to any value I desire locally, but
I would like to be able to set its value remotely with my android phone, building the app using the MIT App Inventor program. So I now have a ESP WROOM - 32 dev board. But all my attempts to connect the pot to the board and setting its value has failed. I just need a wiring diagram and a bit of arduino IDE code to adjust the pot with the output value being measured on an ohmmeter. Has anybody got anything to help me please?
regards
victor

becorey
Posts: 92
Joined: Sat Mar 28, 2020 4:18 pm

Re: ESP32 and digital pot X9C102

Postby becorey » Thu Apr 15, 2021 3:15 pm

What have you tried so far that didn't work? Can you share a schematic?

Looking at the datasheet:
https://www.renesas.com/us/en/document/ ... -datasheet

The X9C102 has pins for CS, U/D, and INC, which will need to be connected to GPIO's of the ESP32. You may also need the RW pin connected to an ADC pin, as a feedback loop to check your digipot setting. Because you just control it by incrementing up or down, it is possible the esp32 would lose track or forget where the setting is.

Here is micropython style code for example to simply increment it up or down:

Code: Select all

def adjustPot(UD, nINC):
	csPin.value(0) // set low to select X9C device
	udPin.value(UD) // set it to up or down
	for i in range(nINC):
		incPin.value(0)
		time.sleep(.001)
		incPin.value(1)
	return

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

Re: ESP32 and digital pot X9C102

Postby unicorn » Thu Apr 15, 2021 7:08 pm

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);
}


becorey
Posts: 92
Joined: Sat Mar 28, 2020 4:18 pm

Re: ESP32 and digital pot X9C102

Postby becorey » Fri Apr 16, 2021 11:15 pm

5V on a dev board is straight from USB, that can supply power. But esp32 is a 3.3v device, don't put 5v on any gpio pin. If you can, supply the pot with 3.3v.

Some of the gpio have multiple purposes, like strapping pins, their value is read at boot up to set some settings. So if you have other stuff connected, you could inadvertently set something wrong.
IO12 (MTDI) should be low at boot up or not connected.
IO0 sets the boot download mode, i.e. if the module should boot normally or download new code. You should avoid it for applications. Usually a programmer chip is using it. IO2 can also set the download boot mode.

I believe IO34 to IO39 are actually input only, that's another potential pitfall.

Perhaps you could use IO4, 16, and 17.


Read through the datasheet for esp32 wroom:
https://www.google.com/url?sa=t&source= ... trmh5NsFOT

This is another good tutorial on it:
https://randomnerdtutorials.com/esp32-p ... nce-gpios/


Looks like that library for X9C does an initialize by decrementing the pot to 0, then tracking the value from there. That's a good method and avoids analog reading the actual output.

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

Re: ESP32 and digital pot X9C102

Postby unicorn » Sat Apr 17, 2021 8:50 am

Yip got it working. Thank you for your advice. I fed the pot from the 3.3v output on the ESP and used pins 4,16 &17 and now I can change the value of setting the pot in the code and reading the pots output on a multimeter. All good. Are you familiar with the MIT app designer tool ? My next area is to connect the ESP to my WiFi network ( which I have been able to do as a standalone item. I will now try it with the pot connected) and sending my values of a text box on my android phone to adjust the pot remotely. I’m not sure if it’s a .PostText or .PutText and what code is needed in the ESP to read this value when sent.

becorey
Posts: 92
Joined: Sat Mar 28, 2020 4:18 pm

Re: ESP32 and digital pot X9C102

Postby becorey » Thu Apr 22, 2021 6:00 pm

You can try blynk to handle the phone to esp32 communication
https://blynk.io/en/getting-started

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

Re: ESP32 and digital pot X9C102

Postby unicorn » Thu Apr 22, 2021 6:52 pm

Yip Had a quick look at that. I'll give it a go. I like the fact it can be used on an iphone, unlike MIT app maker which is just for android. Thanks again Becorey. Cheers

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

Re: ESP32 and digital pot X9C102

Postby unicorn » Sun May 02, 2021 2:25 pm

I know I said in my previous post that it works fine. WELL,
I'm now at the dreaded problem where the circuit works as expected while connected to the laptop via USB, but wont work 'properly' when on external 5v PSU. I'm saying 'properly' because the code still runs ok but the resistance values from the pots are not following the code. This happens only on ext power source. They are fine on USB power.
I have two programs running on the ESP32, one on each core (0 &1).
Task1 on core 1 starts WiFi as a station to the home WiFi and using an Android phone I change the value of a pot from a slider type control. This program will not be used as much as the second program.
Task2 on core 0 does not use WiFi . It takes in readings from an external meter and displays these readings on a LCD, while adjusting a second dig pot automatically. This program will be used a lot more.
I'm hoping later to just use one dig pot by sharing/combining the two program's outputs. But for now I'd be happy to get them running on external power correctly.
Why do I get proper ohm values from both pots when connected to a laptop and not when connected to a stable 5v external power source. (An RS 5v 1.2A module)? What difference do the data wires in the USB lead have to these digital pots? Surely it can't be the arduino code to control the pots if it works for one situation. The ESP Wroom 32 breakout module has 38 'yellow' clad pins and black back. Anybody with any ideas?

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

Re: ESP32 and digital pot X9C102

Postby unicorn » Sun May 02, 2021 7:57 pm

Ah, progress....I sacrificed a USB lead cutting the type A plug off and powering the ESP board via the red / black outer wires.
Same problem existed, but when I touched the casing of the ESP32 chip shield or the mini usb socket shield, I got the correct readings from the digital pots. The same correct readings when I touch either or both green/white inner wires ends. um so now should I try tieing one or both of these two inner wires to either ground or +5v through resistors, or one to ground and the other to +5v. Will go and search more on internet for conditions expected on these data wires into the USB-UART chip on the ESP board.

unicorn
Posts: 9
Joined: Wed Apr 14, 2021 7:51 pm

Re: ESP32 and digital pot X9C102

Postby unicorn » Sun May 02, 2021 8:48 pm

I found this

Exiting Idle State
The host includes 15 kΩ pull-down resistors on each data line. When no device is connected, this pulls both data lines low into the so-called "single-ended zero" state (SE0 in the USB documentation), and indicates a reset or disconnected connection. A USB device pulls one of the data lines high with a 1.5 kΩ resistor. This overpowers one of the pull-down resistors in the host and leaves the data lines in an idle state called "J". For USB 1.x, the choice of data line indicates of what signal rates the device is capable; full-bandwidth devices pull D+ high, while low-bandwidth devices pull D− high.

I tried a 1K but no joy and then tried a 2.7k with also no joy. Is it something to do with a touch pin used on the ESP32 board I wonder.

Who is online

Users browsing this forum: No registered users and 41 guests