16x2 LCD displaying 0 - 9999 values using Potentiometer with Arduino UNO

Hi, I'm quite new to Arduino and Iam making 16x2 LCD using autoscroll function because values range 0-9999 from potentiometer and contrast adjust to digital pin 6 , pot pin connected to A0 and setcursor also used in lcd but unwanted characters generated like 3cS# and also im displayed in 156 serial monitor but in lcd 1560 generated and adjusting pot values slowly generated in lcd and I'm setting cursor (5,0) but sometime values displayed in (0,0).

code:

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Initialize LCD with pin numbers
int contrastPin = 6;
//int potPin = A0;
void setup() {
lcd.begin(16,2);
lcd.clear();
pinMode(A0, INPUT);
pinMode(contrastPin, OUTPUT);
analogWrite(contrastPin, 100);
Serial.begin(9600);
}
void loop() {
int potPin = analogRead(A0);
int potData = map(potPin, 0, 9999, 0, 1023);
lcd.setCursor(5,0);
lcd.print(potPin);
Serial.println(potPin);
// lcd.clear();
lcd.autoscroll();
delay(1000);
lcd.noAutoscroll();
delay(1000);
}

Welcome

I don't understand, you use autoscroll because what ?

This example may help : exemple_pour_yann_prt - Wokwi ESP32, STM32, Arduino Simulator

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.
int potPin = analogRead(A0);
int potData = map(potPin, 0, 9999, 0, 1023);
  • Tell us what you think is happening in the lines of code above.
1 Like

Sir, autoscroll option also there for serial monitor and Im using lcd 0 to 9999 values generated in both lcd and serial monitor but unwanted characters generated in lcd , eg: #3scS, cursor change (5,0) to (0,0) , 143 generated in serial monitor but in lcd generated 1430 or 1439

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Initialize LCD with pin numbers
int contrastPin = 6;
//int potPin = A0;
void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(A0, INPUT);
  pinMode(contrastPin, OUTPUT);
  analogWrite(contrastPin, 100);
  Serial.begin(9600);
}
void loop() {
  int potPin = analogRead(A0);
  int potData = map(potPin, 0, 9999, 0, 1023);
  lcd.setCursor(5, 0);
  lcd.print(potPin);
  Serial.println(potPin);
  // lcd.clear();
  lcd.autoscroll();
  delay(1000);
  lcd.noAutoscroll();
  delay(1000);
}

This is the code

Since analogRead(A0) returns a value between 0 and 1023, could you please explain why your sketch then tries to map the returned value from [0..9999] to [0..1023]? I have pondered that piece of code at length and I find it puzzling.

int potData = map(potPin, 0, 1023, 0, 9999 );
  lcd.setCursor(5, 0);
  lcd.print(potPin);
  Serial.println(potPin);
  // lcd.clear();
  lcd.autoscroll();
  delay(1000);
  lcd.noAutoscroll();
  delay(1000)
type or paste code here

I want to display 0 - 9999 values in lcd to adjust the pot , I know pot A0 values range from 0 to 1023 but I want to display 0 - 9999 both serial monitor and lcd

if you want to see your mapped values, you need to use the variable which contains the mapped values

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Initialize LCD with pin numbers
int contrastPin = 6;
//int potPin = A0;
void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(A0, INPUT);
  pinMode(contrastPin, OUTPUT);
  analogWrite(contrastPin, 100);
  Serial.begin(9600);
}

void loop() {
  int potPin = analogRead(A0);
  int potData = map(potPin, 0, 9999, 0, 1023);
  lcd.setCursor(5, 0);
  lcd.print(potData);
  lcd.print("        "); // delete characters from last iteration
  Serial.println(potData);

  delay(1000);
}

You will not be able to display every value in the range, because that is 10,000 different values and the pot gives only 1,000 values.

You could add a second pot. One pot can be "coarse" and the other "fine":

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Initialize LCD with pin numbers
int contrastPin = 6;
//int potPin = A0;
void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  pinMode(A0, INPUT);
  pinMode(contrastPin, OUTPUT);
  analogWrite(contrastPin, 100);
  Serial.begin(9600);
}

void loop() {
  int coarsePot = analogRead(A0);
  int finePot = analogRead(A1);
  int potData = map(coarsePot, 0, 1023, 0, 99) * 100 + map(finePot, 0, 1023, 0, 99);
  char buffer[21];
  sprintf(buffer, "%4i", potData);
  lcd.setCursor(5, 0);
  lcd.print(buffer);
  Serial.println(buffer);

  delay(1000);
}

Thank You

You need a 16 bit ADC like ADS1115 that will give values greater than 10,000.

int potData = map(potPin, 0, maxADCvalue, 0, 9999);

what is the value for maxADCValue , please explain

Consider the 16 bit ADS1115, it's aref voltage is 6.144V, if you put 0 ~ 5V on it's input it will return a value of 0 ~ ( 5 / 6.144 * 32767) = 26664.
So, maxADCvalue would be the value read when 5V is applied to the input.
9999 / 26664 = 0.375. So if you put 5V on the input you would have 26664 returned, multiplied by 3 / 8 would print 9999.

1 Like

ok sir, how to code applied this method

Which Arduino (or other "compatible" MCU board) are you using?

Arduino UNO

Here's a link to the ADS1115:

Adafruit ADS1115

The Adafruit library is in the IDE library manager, I like the one by Rob Tillart

When you get your ADS1115 and get it connected and working, let us know, we will go from there.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.