This build lets you turn a potentiometer knob into a trigger for a relay, which in turn switches something bigger like a light bulb, LED strip, motor, or a 12V solenoid lock. It's a nice intro to reading analog input on a Raspberry Pi and using that value to drive a high-power load safely through a relay.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi - Potentiometer Relay

What you need

Wiring

Raspberry Pi - Potentiometer Relay wiring diagram

The potentiometer is an analog sensor, but the Raspberry Pi only has digital GPIO pins, so you read it through the ADS1115 ADC module, which connects over I2C and converts the potentiometer's voltage into a digital value the Pi can read. The relay connects to a GPIO pin and switches the load, such as a solenoid lock powered by a 12V adapter through a DC power jack. A screw terminal block shield is handy here since it keeps the wiring secure instead of relying on loose jumper wires, especially for the 12V side.

How it works

Turning the potentiometer knob changes the voltage on its wiper pin. The ADS1115 samples that voltage and sends the Pi a numeric reading over I2C. Your script reads that value and decides whether it crosses a threshold, then sets the GPIO pin connected to the relay high or low. The relay is really just a GPIO-controlled switch for a circuit the Pi itself can't handle directly, like the 12V line powering the solenoid lock.

Code

Before running the script, install the RPi.GPIO library and the Adafruit_ADS1x15 library, which handles communication with the ADC module. The Python script, potentiometer_relay.py, continuously reads the potentiometer value through the ADS1115 and toggles the relay's GPIO pin based on that reading. The full logic and reasoning are explained inline as comments in the source code itself.

raspberry-pi-potentiometer-relay.py

""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-potentiometer-relay """ import time import Adafruit_ADS1x15 import RPi.GPIO as GPIO # Create an ADS1115 ADC object adc = Adafruit_ADS1x15.ADS1115() # Set the gain to ±4.096V (adjust if needed) GAIN = 1 # Define the GPIO pin for the relay RELAY_PIN = 20 # Set up GPIO for the relay GPIO.setmode(GPIO.BCM) GPIO.setup(RELAY_PIN, GPIO.OUT) # Define the threshold value THRESHOLD = 1000 # Adjust this threshold value as needed # Main loop to read and display the analog value try: while True: # Read the raw analog value from channel A3 raw_value = adc.read_adc(3, gain=GAIN) # Convert the raw value to voltage voltage = raw_value / 32767.0 * 4.096 # Assumes 4.096 V range for GAIN=1 # Print the results print("Raw Value: {} \t Voltage: {:.2f} V".format(raw_value, voltage)) # Check if the analog value is above the threshold if raw_value > THRESHOLD: GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn on the relay else: GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the relay # Add a delay between readings (adjust as needed) time.sleep(1) except KeyboardInterrupt: print("\nExiting the program.") # Clean up GPIO on exit GPIO.cleanup() 

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-potentiometer-relay