Potentiometer, NeoPixel RGB
This project instructs you how to use ESP32 to control LED strip using Potentiometer
Video Tutorial
you can watch this video tutorial
Hardware Required
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | rgb LED strip | |
1 | × | POT resistor | |
1 | × | Breadboard | |
n | × | Jumper Wires |
About this project
Introduction to LED Strip
please review our tutorial about LED Strip
Introduction to Potentiometer
The potentiometer (also known as rotary angle sensor) is used to change the settings (e.g. the speaker's volume, room's temperature, lamp's brightness...)
potentiometer Pinout
A potentiometer usually has 3 pins:
-
VCC pin: connect this pin to VCC (5V or 3.3v)
-
GND pin: connect this pin to GND (0V)
-
Output pin: outputs the voltage to ESP32's input pin.
※ NOTE THAT:
The GND pin and VCC pin are interchangeable
How Potentiometer Works
The potentiometer's shaft can be rotated from 0° (closest to GND pin) to an angle (closest to VCC pin), called ANGLE_MAX.
The voltage in the output pin is in direct proportion to the angle position of the shaft, varing from 0 to VCC. The below table shows the relation between the angle and the voltage on the output pin:
Angle | Voltage |
---|---|
0° | 0v |
ANGLE_MAX° | VCC |
angle° | angle° × VCC / ANGLE_MAX° |
※ NOTE THAT:
The ANGLE_MAX value is depended on manufacturers.
ESP32 - Rotary Potentiometer
The ESP32's analog input pin converts the voltage (between 0v and 3.3V) into integer values (between 0 and 4095), called ADC value or analog value.
We can connect the potentiometer's output pin to an ESP32's analog input pin, and then read the analog value from the pin.
The analog value from input pin can be rescaled into another value. Let's see the use cases:
-
Rescale the analog value to the potentiometer's angle.
-
Rescale the analog value to the potentiometer's voltage.
-
Rescale the analog value to the the setting value (e.g. the speaker's volume, room's temperature, lamp's brightness...)
Rescale Range
FROM | TO | |||
---|---|---|---|---|
Angle | rotated by user | 0° | → | ANGLE_MAX° |
Voltage | from potentiometer's pin | 0V | → | 3.3V |
ADC value | read by ESP32 | 0 | → | 4095 |
Setting value | converted by ESP32 | VALUE_MIN | → | VALUE_MAX |
Wiring Diagram
Breadboard Connections with LED Strip
Image is developed using Fritzing. Click to enlarge image
Breadboard Connections with potentiometer
ESP32 Code
#include <Adafruit_NeoPixel.h> #define potPIN 4 #define NeopixelPIN 15 #define N_LEDS 60 float potValue; int ledCount; Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, NeopixelPIN, NEO_GRB + NEO_KHZ800); void setup() { strip.begin(); pinMode(potPIN,INPUT); Serial.begin(9600); } void loop() { potValue = analogRead(potPIN); ledCount = floatMap(potValue, 0, 4095, 1, 60); uint32_t c = strip.Color(255, 0, 0); for(uint16_t i=0; i<ledCount; i++) { strip.setPixelColor(i , c); // Draw new pixel strip.show(); //delay(25); } for(uint16_t i=0; i<60-ledCount; i++) { strip.setPixelColor(60-i, 0); // Erase pixel a few steps back strip.show(); //delay(25); } } 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; }
Quick Steps
- power up your board
- Open Arduino IDE
- Select the right board
- Select the right port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Press and keep pressing the button several seconds
- See the changes you made
Book Tutorial
We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book
See Also
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!