Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
š Ready to explore and create with us? Click [here] and join today!
Lesson 41: Potentiometer scale valueļ
This project focuses on reading a potentiometerās value and displaying it on an LCD 1620 equipped with an I2C interface. Additionally, the value is transmitted to the serial monitor for live monitoring. A distinctive aspect of this project is the graphical representation of the potentiometerās value on the LCD, which is depicted as a variable-length bar proportional to the potentiometerās reading.
Required Componentsļ
In this project, we need the following components.
Itās definitely convenient to buy a whole kit, hereās the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
- |
|
- |
|
Wiringļ
Codeļ
Code Analysisļ
The core functionality of this project is to consistently read the potentiometerās value, map it to a scaled range (0-16), and display the result both numerically and graphically on the LCD. The implementation minimizes jitter by updating the display only when significant changes in the reading occur, thus maintaining a smooth visual experience.
Library Inclusion and Initialization:
// Required libraries for I2C and LCD operations #include <Wire.h> #include <LiquidCrystal_I2C.h> // Initialize LCD at I2C address 0x27 with 16 columns and 2 rows LiquidCrystal_I2C lcd(0x27, 16, 2);
This segment incorporates the necessary libraries for I2C communication and LCD control. It then initializes an LCD instance with the I2C address of
0x27, specifying its dimensions as16 columnsand2 rows.Variable Declaration:
// Variables to hold the potentiometer readings int lastRead = 0; // Previous potentiometer value int currentRead = 0; // Current potentiometer value
Variables
lastReadandcurrentReadare used to keep track of the potentiometerās readings across different moments.setup() Function:
void setup() { lcd.init(); // Initiates the LCD lcd.backlight(); // Activates the LCD's backlight Serial.begin(9600); // Commences serial communication at 9600 baud }
This function prepares the LCD and starts serial communication, setting up the environment for the projectās operation.
Main Loop:
void loop() { // Read the current potentiometer value int currentRead = analogRead(35); // Map the read value from 0-4096 to 0-16 int barLength = map(currentRead, 0, 4096, 0, 16); // Update LCD only if the difference between current and last reading is greater than 2 to avoid jitter if (abs(lastRead - currentRead) > 2) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Value:"); lcd.setCursor(7, 0); lcd.print(currentRead); Serial.println(currentRead); // Display a bar on the second row of LCD proportional to the potentiometer value for (int i = 0; i < barLength; i++) { lcd.setCursor(i, 1); lcd.print(char(255)); } } // Update the last read value for the next iteration lastRead = currentRead; // Introduce a delay for a stable reading delay(200); }
Reads the potentiometer and converts its value to a scale suitable for visual representation.
Updates the LCD only when a meaningful change is detected, displaying the numeric value and a corresponding bar graph.
Also sends the reading to the serial monitor for external observation.
Ensures stability and responsiveness by introducing a brief delay between iterations.