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 43: 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 |
|---|---|
Arduino UNO R3 or R4 |
|
- |
|
- |
|
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:
#include <Wire.h> #include <LiquidCrystal_I2C.h> 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:
int lastRead = 0; // Stores the last read value from the potentiometer int currentRead = 0; // Holds the current read value from the potentiometer
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() { currentRead = analogRead(A0); int barLength = map(currentRead, 0, 1023, 0, 16); if (abs(lastRead - currentRead) > 2) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Value:"); lcd.setCursor(7, 0); lcd.print(currentRead); Serial.println(currentRead); for (int i = 0; i < barLength; i++) { lcd.setCursor(i, 1); lcd.print(char(255)); } } lastRead = currentRead; 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.