Advertisement
microrobotics

X9C104S is a 100K digital potentiometer

Apr 3rd, 2023
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The X9C104S is a 100K digital potentiometer module that can be used to control the resistance in a circuit. To interface with the X9C104S, you can use a microcontroller such as Arduino. Here's a simple example code to control the X9C104S using an Arduino:
  3.  
  4. In this example, we have defined the chip select (CS), increment (INC), and up/down (UD) pins connected to the Arduino. The setResistance function takes an integer value from 0 to 100, where 0 represents minimum resistance and 100 represents maximum resistance. This function sets the desired resistance by sending appropriate signals to the X9C104S module.
  5.  
  6. In the loop function, we change the resistance value to 50 (mid-range), 100 (max), and 0 (min) with a delay of 2 seconds between each change.
  7.  
  8. To use this code, connect the X9C104S module to an Arduino as follows:
  9.  
  10. CS_pin (Arduino) -> CS (X9C104S)
  11. INC_pin (Arduino) -> INC (X9C104S)
  12. UD_pin (Arduino) -> UD (X9C104S)
  13. VCC (X9C104S) -> 5V (Arduino)
  14. GND (X9C104S) -> GND (Arduino)
  15. Upload the code to the Arduino and monitor the resistance change across the potentiometer's output pins (H, W, L).
  16. */
  17.  
  18. #include <Arduino.h>
  19.  
  20. const int CS_pin = 10;  // Chip Select Pin
  21. const int INC_pin = 9;  // Increment Pin
  22. const int UD_pin = 8;   // Up/Down Pin
  23.  
  24. void setup() {
  25.   pinMode(CS_pin, OUTPUT);
  26.   pinMode(INC_pin, OUTPUT);
  27.   pinMode(UD_pin, OUTPUT);
  28.  
  29.   digitalWrite(CS_pin, HIGH);
  30.   digitalWrite(INC_pin, HIGH);
  31.   digitalWrite(UD_pin, HIGH);
  32. }
  33.  
  34. void setResistance(int value) {
  35.   digitalWrite(CS_pin, LOW);
  36.   delay(10);
  37.  
  38.   for (int i = 0; i < 100; i++) {
  39.     digitalWrite(UD_pin, value > i ? HIGH : LOW);
  40.     delay(10);
  41.     digitalWrite(INC_pin, LOW);
  42.     delay(10);
  43.     digitalWrite(INC_pin, HIGH);
  44.   }
  45.  
  46.   digitalWrite(CS_pin, HIGH);
  47. }
  48.  
  49. void loop() {
  50.   int resistance_value = 50; // Value from 0 to 100, where 0 is min resistance and 100 is max resistance
  51.   setResistance(resistance_value);
  52.   delay(2000);
  53.  
  54.   resistance_value = 100;
  55.   setResistance(resistance_value);
  56.   delay(2000);
  57.  
  58.   resistance_value = 0;
  59.   setResistance(resistance_value);
  60.   delay(2000);
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement