ESP8266 - Potentiometer

This tutorial instructs you how to use ESP8266 with a potentiometer, which is also known as pot, trimmer, variable resistor, rheostat, or rotary angle sensor. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Potentiometer
1×Potentiometer Kit
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

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.

Overview of Potentiometer

A rotary potentiometer is also known as pot, trimmer, variable resistor, rheostat, or rotary angle sensor. It is used to manually adjust the value of something. Examples include the volume of a stereo, the brightness of a lamp, and the zoom level of an oscilloscope.

Potentiometer pinout

The Potentiometer Pinout

A Potentiometer typically has three pins:

  • The GND pin should be connected to the ground (0V).
  • The VCC pin should be connected to the voltage source (5V or 3.3V).
  • The output pin sends the voltage to the Arduino's input pin.
Potentiometer pinout

※ NOTE THAT:

The GND and VCC pins can be swapped.

How It Works

The rotation of the potentiometer's shaft can be from 0°, which is closest to GND, to a maximum angle, which is closest to the VCC pin, referred to as ANGLE_MAX.

※ NOTE THAT:

The value of ANGLE_MAX is determined by the manufacturer. Generally, we do not need to consider this value unless we are computing a rotated angle (see the use cases section).

How Potentiometer Works

The working principle:

  • An user rotates shaft of the potentiometer
  • ⇒ The angle of the potentiometer is changed
  • ⇒ The resistance of the potentiometer is changed
  • ⇒ The voltage in the output pin of the potentiometer is changed
  • ⇒ The analog value read by ESP8266 is changed

ESP8266 - Rotary Potentiometer

Some of ESP8266's pin are capable of functioning as analog input. These analog input pins convert the voltage (ranging from 0v to VCC) into integer values (from 0 to 1023), referred to as ADC value or analog value.

We can connect an output pin of the potentiometer to an analog input pin. This allows us to read the analog value from the pin and convert it into a meaningful value.

The value ESP8266 receives is not an angle or voltage; rather, it is an integer value ranging from 0 to 1023.

Once we have obtained the integer value from the analog input pin, we can rescale this value to a different one. Let us consider the applications.

Use Cases

  • Rescale to the angle of the potentiometer.
  • Rescale to the voltage of the potentiometer.
  • Rescale to a controllable value, such as the volume of a stereo, brightness, or speed of a DC motor - this is the most common use case.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0VVCC
ADC valueread by Arduino 01023
Other valueconverted by Arduino VALUE_MINVALUE_MAX

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Potentiometer

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For Potentiometer

  • Access the output pin of the potentiometer with analogRead() and read the value from the input pin.
analog_value = analogRead(A0);
  • Use the map() function to adjust the potentiometer's angle.
angle = map(analog_value, 0, 1023, 0, ANGLE_MAX);
  • Adjust the voltage to the potentiometer's level.
voltage = map(analog_value, 0, 1023, 0, VCC);
  • Rescale the value to something that can be controlled, such as the volume of a stereo, the brightness, or the speed of a DC motor.
value = map(analog_value, 0, 1023, VALUE_MIN, VALUE_MAX);
  • For instance, we can adjust the brightness of an LED by rescaling. As stated in the , the brightness of the LED can be regulated with a PWM value ranging from 0 (completely off) to 255 (fully on). Thus, we can map the analog value to the brightness of the LED (from off to the brightest) like this:
brightness = map(analog_value, 0, 1023, 0, 255);

If you desire to adjust the LED from a dim nightlight to its brightest,

nightlight = 100; // depending on your desired brightness brightness = map(analog_value, 0, 1023, nightlight , 255);

※ NOTE THAT:

The map() function is limited to rescaling an analog value to the int or long type. If the controllable value is of float type, the floatMap() function should be used instead of map().

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; }

ESP8266 Code

/* * 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 */ #define POTENTIOMETER_PIN A0 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; } // The setup function runs once on reset or power-up void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); } // The loop function repeats indefinitely. void loop() { // read the input on analog pin: int analog_value = analogRead(POTENTIOMETER_PIN); // Rescale to potentiometer's voltage (from 0V to 5V): float voltage = floatMap(analog_value, 0, 1023, 0, 5); // print out the value you read: Serial.print("Analog: "); Serial.print(analog_value); Serial.print(", Voltage: "); Serial.println(voltage); delay(1000); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it in the Arduino IDE.
  • Once opened, press the Upload button to transfer the code to the ESP8266.
Arduino IDE Upload Code
  • Open the Serial Monitor.
  • Turn the potentiometer.
  • Check out the result on the Serial Monitor.
COM6
Send
Analog: 0, Voltage: 0.00 Analog: 0, Voltage: 0.00 Analog: 126, Voltage: 0.62 Analog: 281, Voltage: 1.37 Analog: 517, Voltage: 2.53 Analog: 754, Voltage: 3.69 Analog: 906, Voltage: 4.43 Analog: 1023, Voltage: 5.00 Analog: 1023, Voltage: 5.00
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

This tutorial uses the analogRead() function to get data from an ADC (Analog-to-Digital Converter) that's connected to a sensor or another part. The ESP8266's ADC works well for projects where you don't need very precise readings. But remember, the ESP8266's ADC isn't very accurate for detailed measurements. If your project needs to be very precise, you might want to use a separate ADC like the ADS1115 with the ESP8266, or use Arduino like the Arduino Uno R4 WiFi, which has a more reliable ADC.

Video Tutorial

Challenge Yourself

Utilize the potentiometer to accomplish one of the following tasks:

Additional Knowledge

  • GND and VCC pins can be swapped with no specific convention. All you have to be mindful of is that the voltage value at the output pin will be reversed when these pins are interchanged.

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!