Arduino Nano - Potentiometer Relay

This tutorial instructs you how to use Arduino Nano and potentiometer to control relay. In detail:

By connecting a relay to a light bulb, LED strip, motor, or actuator, we can use the Arduino Nano and potentiometer to regulate the light bulb, LED strip, motor, or actuator.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Potentiometer
1×Relay
1×Warning Light Bright Waterproof
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Relay and Potentiometer

If you are unfamiliar with relay and potentiometer (including pinout, operation, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Potentiometer Relay

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Analog Threshold

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-potentiometer-relay */ const int POTENTIOMETER_PIN = A0; // The Arduino Nano pin connected to Potentiometer pin const int RELAY_PIN = 2; // The Arduino Nano 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 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 }

Detailed Instructions

  • Connect your Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE, select the appropriate board and port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
Arduino IDE Upload Code
  • Turn the potentiometer
  • Check out the alteration of the relay's condition

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Arduino Nano Code - Voltage Threshold

The analog value of a potentiometer is changed to a voltage value. This voltage is then compared to a voltage threshold. If the voltage is greater than or equal to the threshold, the relay is triggered.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-potentiometer-relay */ const int POTENTIOMETER_PIN = A0; // The Arduino Nano pin connected to Potentiometer pin const int RELAY_PIN = 2; // The Arduino Nano pin connected to Relay's pin const float VOLTAGE_THRESHOLD = 2.5; // Voltages void setup() { pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode } void loop() { int analog_value = analogRead(POTENTIOMETER_PIN); // read the input on analog pin float voltage = floatMap(analog_value, 0, 1023, 0, 5); // Rescale to potentiometer's voltage if(voltage > VOLTAGE_THRESHOLD) digitalWrite(RELAY_PIN, HIGH); // turn on Relay else digitalWrite(RELAY_PIN, LOW); // turn off Relay } float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

Video Tutorial