Raspberry Pi - Potentiometer Relay
This tutorial instructs you how to use Raspberry Pi and potentiometer to control relay. In detail:
- Raspberry Pi turns the relay on if the potentiometer's analog value is above a certain point
- Raspberry Pi turns the relay off if the potentiometer's analog value is below a certain point
By connecting a relay to a light bulb, LED strip, motor or actuator, we can use Raspberry Pi and a potentiometer to control the light bulb, LED strip, motor or actuator.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Relay and Potentiometer
If you are not familiar with relay and potentiometer (including pinout, functioning, programming, etc.), the following tutorials can help you:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update
sudo apt-get install python3-rpi.gpio
- Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
- Create a Python script file potentiometer_relay.py and add the following code:
"""
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()
- Save the file and run the Python script by executing the following command in the terminal:
python3 potentiometer_relay.py
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
- Turn the potentiometer
- Check out the alteration in the relay's condition
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!