-1

I've tried using a Raspberry Pi 5 to control a digital potentiometer X9C104P. I followed this wiring diagram:

Potentiometer (POT) - Raspberry Pi

  1. INC: GPIO20
  2. UD: GPIO21
  3. Vh: 5V
  4. Vss: GND
  5. Vcc: 5V
  6. CS: GPIO16
  7. Vl: GND
  8. Vw: LED

enter image description here

enter image description here enter link description here

Implemented the provided code:

import tkinter as tk
import spidev

# Configuration du SPI (bus série)
spi = spidev.SpiDev()
spi.open(1, 2)  # Bus SPI 1, périphérique 2 (CE2)
spi.max_speed_hz = 1000000  # Vitesse de communication (1 MHz)

# Fonction pour ajuster la position du curseur
def adjust_potentiometer(direction):
    current_position = spi.xfer2([0x00])[0]
    if direction == "up":
        new_position = min(current_position + 1, 127)
    elif direction == "down":
        new_position = max(current_position - 1, 0)
    else:
        return
    spi.xfer2([0x80 | new_position])

# Création de l'interface Tkinter
root = tk.Tk()
root.title("Contrôle du Potentiomètre X9C104P")

# Boutons pour ajuster le potentiomètre
btn_up = tk.Button(root, text="Augmenter", command=lambda: adjust_potentiometer("up"))
btn_down = tk.Button(root, text="Diminuer", command=lambda: adjust_potentiometer("down"))

# Placement des boutons dans la fenêtre
btn_up.pack()
btn_down.pack()

# Boucle principale Tkinter
root.mainloop()

# Fermeture du bus SPI
spi.close()

However, the obtained result is that both buttons have the same effect: they increase the resistance, which decreases the brightness. Unfortunately, I couldn't manage to decrease the resistance.

Thank you for your help, I'm really stuck.

5
  • Where are your buttons defined? Commented Apr 12 at 19:57
  • btn_up = tk.Button(root, text="Augmenter", command=lambda: adjust_potentiometer("up")) btn_down = tk.Button(root, text="Diminuer", command=lambda: adjust_potentiometer("down")) Commented Apr 12 at 20:19
  • yes, but where are they assigned to particular GPIO? Commented Apr 12 at 20:21
  • I utilized the SPI device: spi.open(1, 2) # SPI Bus 1, Device 2 (CE2). Commented Apr 12 at 20:33
  • 1
    There are multiple issues with this question. You are powering from 5V but Pi is 3.3V; Your connection is unclear and incomplete; it is unclear if you have enabled SPI; you are using a 100k POT which will only provide minuscule current; your code is incomplete (just for starters).
    – Milliways
    Commented Apr 13 at 22:21

1 Answer 1

0

Try adding this:

define the following variable:

alt_position = current_position + 1

replace new_position variable definition in the "up" if statement in the function adjust_potentiometer

if not alt_position > 127:
  alt_position += 1
elif alt_position > 127:
  alt_position -= 1

and for new_position's "down" if statement:

if not alt_position < 0:
  alt_position -= 1
elif alt_position < 0:
  alt_position +=1

Comment if you have questions. Hope this helps!

5
  • like that : def adjust_potentiometer(direction): current_position = spi.xfer2([0x00])[0] global alt_position if direction == "up": alt_position = current_position + 1 if not alt_position > 127: alt_position += 1 elif alt_position > 127: alt_position -= 1 elif direction == "down": alt_position = current_position - 1 if not alt_position < 0: alt_position -= 1 elif alt_position < 0: alt_position += 1 else: return spi.xfer2([0x80 | alt_position]) Commented Apr 12 at 20:51
  • Did you define alt_position outside of the function adjust_potentiometer?
    – Saturanium
    Commented Apr 13 at 1:49
  • On a side note, alt_position needs to be assigned in both cases as current_position + 1, or else the overall calculations you want to make will get distorted by fluctuating up and down.
    – Saturanium
    Commented Apr 13 at 1:54
  • it does not work Commented Apr 13 at 10:35
  • What comes out as an output? What error where?
    – Saturanium
    Commented Apr 13 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.