Help needed in Interfacing X9C104 100K Digital Potentiometer Module with Arduino

Hi There. I am new at this and looking for help to write code for the following. I have played around trying to write my own code using examples from the digital pot product info but struggling.

Some more background

I purchased a speed Pedal for a kids go cart that we are building that outputs a variable voltage and also purchased a motor speed controller that uses a potentiometer to control the speed. I am using a Arduino nano to convert the voltage input from this speed pedal to a variable resistance 100k digital pot to interface into the motor speed controller to replace the normal 100k pot.

Requirement

Write Arduino code for Arduino Nano to use a X9C104S 100K Digital Potentiometer Module to convert a "Foot Pedal Speed Controller" that outputs a variable voltage of 0.82 Volts to 2.52 Volts input into the Arduino. As the input voltage changes from 0.82V to 2.52V then proportionally change the X9C104S 100K Digital Potentiometer's variable resistance from 100k ohms to 20k ohms. 0.8 Volts is the slowest speed and 2.52 Volts is the Maximum Speed. Include the X9C10X library Write input voltage to serial monitor. Include a "govern speed" button. If "govern speed" button is on then limit the X9C104S 100K Digital Potentiometer's variable resistance 100k ohm to 60k ohm where 100k is the slowest and 60k ohm is the fastest. If "govern speed" button is off then change the X9C104S 100K Digital Potentiometer's variable resistance range of 100k ohm to 20k ohm where 100k ohm is the slowest and 20k ohm is the fastest. Include a "govern speed" LED. If "govern speed" button is on then turn on "govern speed" LED. If "govern speed" button is off then turn off "govern speed" LED.

Here’s some sample code, if anyone can improve on it to get it to work.


#include <X9C10X.h>

#define CS_PIN 10
#define INC_PIN 9
#define UD_PIN 8
#define PEDAL_PIN A0
#define GOVERN_SPEED_BUTTON_PIN 2
#define GOVERN_SPEED_LED_PIN 3

X9C10X digiPot(CS_PIN, UD_PIN, INC_PIN); // Initialize the digital potentiometer

bool isSpeedGoverned = false;

int mapResistance(float voltage) {
  if (voltage < 0.8) {
    voltage = 0.8; // Ensure the minimum voltage is 0.8V
  } else if (voltage > 2.52) {
    voltage = 2.52; // Ensure the maximum voltage is 2.52V
  }

  if (isSpeedGoverned) {
    return 60000 + (voltage - 0.8) * 40000 / 1.72; // Maps the 0.82V-2.52V input range to 60k-20k ohm resistance range
  } else {
    return 100000 - (voltage - 0.8) * 80000 / 1.72; // Maps the 0.82V-2.52V input range to 100k-20k ohm resistance range
  }
}

void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(GOVERN_SPEED_BUTTON_PIN, INPUT_PULLUP);
  pinMode(GOVERN_SPEED_LED_PIN, OUTPUT);
  digitalWrite(GOVERN_SPEED_LED_PIN, LOW);
}

void loop() {
  float voltage = analogRead(PEDAL_PIN) * (5.0 / 1023.0);
  int resistanceSetting = mapResistance(voltage);

  if (digitalRead(GOVERN_SPEED_BUTTON_PIN) == LOW) {
    isSpeedGoverned = true;
    digitalWrite(GOVERN_SPEED_LED_PIN, HIGH);
  } else {
    isSpeedGoverned = false;
    digitalWrite(GOVERN_SPEED_LED_PIN, LOW);
  }

  digiPot.setWiper(resistanceSetting); // Set the resistance on the digital potentiometer

  Serial.print("Input Voltage: ");
  Serial.print(voltage);
  Serial.println(" V");

  delay(100); // Adjust the delay time as needed
}

Hi, @warrenm
Welcome to the forum.

What does or doesn't your code do?

Can you please post a link to data/specs of the controller?

Thanks... Tom.. :grinning: :+1: :coffee: :australia:

Thanks Tom for responding.

Here is the spec of the digital pot I’m using.

The code I posted is reading the voltage output from the speed pedal and can see this output in the serial monitor changing from 0.8. V when the pedal is not pressed and then change incrementally to 2.52V when pressed down to its max.

What’s not working is the resistance changing on the output wiper of the X9C104 digi pot when I connect it to the multimeter. I am expecting to see 100K when voltage from the pedal is at rest (not pressing down ) 0.8V and then 20K when the pedal is fully pressed down.l and Voltage is 2.52V.

I have also explained what happens when the govern switch is on.

Hi,
If you are using the " AUTHOR: Rob Tillaart" library.

Looking at the test example of the library, you need to output an integer representative of the % of max resistance of the digital pot.

Also you can read back the actual digital pot resistance.

 someVariable = digiPot.getOhm();

Try using the map() function.

Tom... :grinning: :+1: :coffee: :australia:

Thanks Tom, will give it a try.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.