I've tried using a Raspberry Pi 5 to control a digital potentiometer X9C104P. I followed this wiring diagram:
Potentiometer (POT) - Raspberry Pi
- INC: GPIO20
- UD: GPIO21
- Vh: 5V
- Vss: GND
- Vcc: 5V
- CS: GPIO16
- Vl: GND
- Vw: LED
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.