DEV Community

Cover image for ESP8266 - Potentiometer Relay
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP8266 - Potentiometer Relay

This build turns a potentiometer into a hands-on switch for anything plugged into a relay: a light bulb, an LED strip, a motor, an actuator. Turn the knob past a set point and the ESP8266 flips the relay on, turn it back and the relay drops out. It is a nice first project if you want to connect an analog input to a real-world load without touching any networking code.

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

What you need

Wiring

ESP8266 - Potentiometer Relay wiring diagram

The potentiometer's wiper output goes to the ESP8266's analog pin, A0. The relay's control pin connects to D8. The relay itself sits between the ESP8266 and the actual load, like a warning light or motor, which typically runs on its own power, such as a 12V adapter, since the ESP8266's pins can't drive that directly. If you are not familiar with how a relay or a potentiometer is wired and powered, it is worth checking a dedicated tutorial for each component before wiring this project.

How it works

A potentiometer is a variable resistor: turning the knob changes the voltage the wiper outputs, which the ESP8266 reads as an analog value with analogRead(). A relay is an electrically controlled switch, it lets a low-voltage pin like D8 safely switch a separate, often higher-voltage or higher-current circuit, like a light bulb, on and off. In this project, the ESP8266 watches the potentiometer's value and uses it to decide when to energize the relay.

Code

The sketch sets RELAY_PIN as an output. In the loop, it reads the potentiometer with analogRead() and compares that value against ANALOG_THRESHOLD. Cross above the threshold and digitalWrite() sets the relay pin HIGH, turning the relay on; drop below it and the pin goes LOW, turning the relay off. A variant of the sketch does the same comparison but converts the analog reading to a voltage value first and compares against a voltage threshold instead of a raw analog value.

esp8266-potentiometer-relay.ino

/*
 * This ESP8266 NodeMCU code was developed by newbiely.com
 *
 * This ESP8266 NodeMCU code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/esp8266/esp8266-potentiometer-relay
 */

#define POTENTIOMETER_PIN  A0 // The ESP8266 pin connected to Potentiometer pin
#define RELAY_PIN          D8 // The ESP8266 pin connected to Relay's pin
#define ANALOG_THRESHOLD   1000

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // Configure the ESP8266 pin to the output mode
}

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

  if (analog_value > ANALOG_THRESHOLD)
    digitalWrite(RELAY_PIN, HIGH); // turn on Relay
  else
    digitalWrite(RELAY_PIN, LOW);  // turn off Relay
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

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

Top comments (0)