ESP8266 Web Potentiometer Gauge: Turn the Knob and the Needle on Your Phone Follows Live
Turn the potentiometer and the needle on your phone turns with it. The NodeMCU V3 joins your home Wi-Fi, reads the position of the potentiometer on A0, the only analog input of the board, and draws it on a page as a gauge from 0 to 100. The page never reloads: a small script asks for the value five times a second. A0 wobbles a little while the Wi-Fi is on; the code smooths the readings so the needle stays calm. The circuit is just a potentiometer, no resistor. 2.4 GHz networks only.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// ESP8266 Web Potentiometer Gauge: Turn the Knob and the Needle on Your Phone Follows Live
4// ─────────────────────────────────────────
5// The NodeMCU joins your home Wi-Fi and shows the position of a
6// potentiometer as a needle on a web page. Open the board's IP address in
7// the browser of a phone on the same network and turn the potentiometer:
8// the needle follows live. The page asks the board for the value five
9// times a second.
10//
11// Pins:
12// 3V3 -> one outer leg of the potentiometer
13// A0 -> the middle leg of the potentiometer
14// GND -> the other outer leg of the potentiometer
15
16#include <ESP8266WiFi.h>
17#include <ESP8266WebServer.h>
18
19// ============ WI-FI NETWORK ============
20// Write the name and password of your own network inside the quotes. The ESP8266 only joins 2.4 GHz
21// networks.
22const char *const WIFI_SSID = "YOUR_WIFI_NAME";
23const char *const WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
24const unsigned long WIFI_HINT_MS = 20000; // Time until the hint (ms)
25
26// ============ MEASUREMENT ============
27// A0 is the only analog input of the board; the NodeMCU reads 0 to 3.3 V as 0 to 1023. Reading A0
28// on every pass disturbs the Wi-Fi, so the readings are spaced out.
29const unsigned long READ_MS = 50; // ms
30
31// ============ PAGE TEXTS ============
32const char TEXT_TITLE[] = "Potentiometer";
33const char TEXT_NOT_FOUND[] = "Page not found";
34
35// ============ WEB PAGE ============
36// The fixed top of the page: UTF-8 encoding, a phone-friendly width and the looks (CSS).
37const char PAGE_HEAD[] = R"rawliteral(<!DOCTYPE html><html><head><meta charset="utf-8">
38<meta name="viewport" content="width=device-width,initial-scale=1"><title>ESP8266</title>
39<style>
40body{margin:0;padding:28px 20px;background:#0f172a;color:#f8fafc;font-family:sans-serif;
41text-align:center}
42h1{font-size:26px;margin:0 0 28px}
43div{position:relative;width:280px;height:140px;margin:0 auto;border-radius:140px 140px 0 0;
44background:#334155;overflow:hidden}
45i{position:absolute;left:136px;bottom:0;width:8px;height:124px;border-radius:4px;background:#f472b6;
46transform-origin:50% 100%}
47p{font-size:72px;font-weight:bold;margin:20px 0 0;color:#f472b6}
48</style></head><body>)rawliteral";
49
50// The script of the page: five times a second it asks the board for /value, writes the percentage
51// it gets and turns the needle to that angle (0 on the left, 100 on the right).
52const char PAGE_SCRIPT[] = R"rawliteral(<script>
53setInterval(function(){
54fetch('/value').then(function(r){return r.text()}).then(function(t){
55document.getElementById('n').textContent=t+'%';
56document.getElementById('i').style.transform='rotate('+(t*1.8-90)+'deg)';
57})},200);
58</script></body></html>)rawliteral";
59
60ESP8266WebServer server(80); // Port 80: the web default
61float smooth = 0;
62int potPercent = 0;
63unsigned long lastRead = 0;
64bool wifiUp = false;
65bool hintDone = false;
66
67// Reads the potentiometer. A0 wobbles a little while the Wi-Fi is on, so the readings are smoothed:
68// the new reading counts for 25 percent, the old average for 75 percent.
69void readPot() {
70 if (millis() - lastRead < READ_MS) {
71 return;
72 }
73 lastRead = millis();
74 smooth = smooth * 0.75f + analogRead(A0) * 0.25f;
75 int percent = lround(smooth * 100 / 1023);
76 if (percent != potPercent) {
77 potPercent = percent;
78 Serial.print("Potentiometer: ");
79 Serial.print(potPercent);
80 Serial.println("%");
81 }
82}
83
84// Builds the page with the current value and sends it to the browser; the script keeps it up to
85// date after that.
86void sendPage() {
87 String html = PAGE_HEAD;
88 html += "<h1>";
89 html += TEXT_TITLE;
90 html += "</h1><div><i id=\"i\" style=\"transform:rotate(";
91 html += potPercent * 180 / 100 - 90;
92 html += "deg)\"></i></div><p id=\"n\">";
93 html += potPercent;
94 html += "%</p>";
95 html += PAGE_SCRIPT;
96 // The browser must not keep a copy of the page; every tap has to reach the board.
97 server.sendHeader("Cache-Control", "no-store");
98 server.send(200, "text/html; charset=utf-8", html);
99}
100
101// Every address has its own function; it runs when the browser asks for that address.
102void handleRoot() {
103 sendPage();
104}
105
106// The script asks for this address. The answer is a short text, the page stays where it is.
107void handleValue() {
108 String text = String(potPercent);
109 server.sendHeader("Cache-Control", "no-store");
110 server.send(200, "text/plain", text);
111}
112
113void handleNotFound() {
114 server.send(404, "text/plain; charset=utf-8", TEXT_NOT_FOUND);
115}
116
117// Watches the connection: prints the IP address once connected and reports a drop. The ESP8266
118// rejoins a lost network by itself.
119void checkWifi() {
120 bool up = WiFi.status() == WL_CONNECTED;
121 if (up && !wifiUp) {
122 hintDone = true;
123 Serial.println("Wi-Fi connected");
124 Serial.print("Open in your browser: ");
125 Serial.print("http://");
126 Serial.println(WiFi.localIP());
127 } else if (!up && wifiUp) {
128 Serial.println("Wi-Fi lost. Reconnecting");
129 }
130 wifiUp = up;
131
132 if (!up && !hintDone && millis() > WIFI_HINT_MS) {
133 hintDone = true;
134 Serial.println("Could not join the Wi-Fi");
135 Serial.println("Check the network name and password");
136 Serial.println("The ESP8266 only sees 2.4 GHz networks");
137 }
138}
139
140void setup() {
141 Serial.begin(115200);
142 Serial.println();
143 Serial.println("Web potentiometer gauge");
144 if (strncmp(WIFI_SSID, "YOUR_", 5) == 0) {
145 Serial.println("Put your Wi-Fi name and password in the code");
146 }
147 Serial.println("Connecting to Wi-Fi");
148 // The board opens no network of its own, it joins yours
149 WiFi.mode(WIFI_STA);
150 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
151
152 server.on("/", handleRoot);
153 server.on("/value", handleValue);
154 server.onNotFound(handleNotFound);
155 server.begin();
156}
157
158// loop never waits: a long delay would freeze the page and can drop the Wi-Fi.
159void loop() {
160 checkWifi();
161 readPot();
162 server.handleClient(); // Handles a waiting request
163}
164FAQ
How does a potentiometer work?
Inside a potentiometer there is a strip of resistance and a wiper that travels over it. The outer legs are the two ends of the strip, the middle leg is the wiper. With 3V3 and GND on the outer legs, the voltage on the middle leg moves between 0 and 3.3 V with the position of the shaft. So a potentiometer is an adjustable voltage divider by itself; no extra resistor is needed.
Which value of potentiometer should I use?
10 kΩ is the most common and the best fit. Any potentiometer from 1 kΩ to 100 kΩ works; small values draw a little more current, very large ones make the reading wobble more. Pick a linear one (marked B): with logarithmic ones (marked A) the needle is not in the middle when the shaft is halfway.
Why does the needle not jitter?
While the Wi-Fi is working, the A0 readings move by a few counts. The code does not use each new reading directly: it takes a quarter of the new reading and three quarters of the old average. Small jumps dissolve in the average that way. The price is a small delay: after a quick turn the needle settles within half a second.
Why does the needle go left when I turn the shaft right?
The direction depends on which outer leg goes to 3V3. If the needle turns the wrong way, swap the 3V3 and GND wires on the potentiometer; the middle leg stays on A0. If you would rather not touch the wires, subtract the result of the percentage line from 100 in the code. Both ways give the same result.
