Arduino - Potentiometer Triggers Relay

by DIYables in Circuits > Arduino

11 Views, 0 Favorites, 0 Comments

Arduino - Potentiometer Triggers Relay

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-potentiometer-triggers-relay

This build uses a potentiometer to turn a relay on and off. Turn the knob past a set point and the relay switches, letting you control a light bulb, LED strip, motor, or actuator wired through the relay. It is a simple way to use an analog input as an on/off trigger for higher-power loads.

Supplies

Wiring

Connect the potentiometer's output to the Arduino's A0 pin. Connect the relay's control pin to Arduino pin 3. Power and ground the potentiometer and relay as required by their modules. Once wired, upload the sketch and rotate the potentiometer to see the relay click on and off.

How It Works

The Arduino continuously reads the analog value from the potentiometer on pin A0. That value ranges based on the knob's position. When the reading rises above a set threshold, the Arduino drives the relay pin HIGH, switching the relay on. When the reading drops back below the threshold, the relay pin goes LOW and the relay turns off.

Code

The sketch defines the potentiometer pin (A0), the relay pin (3), and an analog threshold value of 500. In setup, the relay pin is set as an output. In the main loop, the code reads the potentiometer's analog value and compares it to the threshold: above it, digitalWrite sets the relay pin HIGH; otherwise it sets it LOW. A separate version of the code converts the analog reading to a voltage value first and compares that voltage against a voltage threshold instead of a raw analog number.

arduino-potentiometer-triggers-relay.ino - the sketch from the tutorial, unchanged.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer-triggers-relay
*/

// constants won't change
const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin
const int RELAY_PIN = 3; // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD = 500;

void setup() {
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin

if(analogValue > ANALOG_THRESHOLD)
digitalWrite(RELAY_PIN, HIGH); // turn on Relay
else
digitalWrite(RELAY_PIN, LOW); // turn off Relay
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-potentiometer-triggers-relay