Display Potentiometer value as percentage

Hello,

I'm controlling a dc motor speed by a potentiometer
I want to display the potentiometer value as percentage. Here's the code

I'm a beginner so go easy on me . :slight_smile:

// Arduino DC motor speed and direction control
 
#define button   8
#define pot      0
#define pwm1     9
#define pwm2    10

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int potPin0 = A0;
int potPin1 = A2; 
boolean motor_dir = 0;
int motor_speed;
 
void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(pwm1,   OUTPUT);
  pinMode(pwm2,   OUTPUT);

lcd.begin(16, 2);
lcd.clear();

pinMode(potPin0, INPUT);
pinMode(potPin0, INPUT);
}
 
void loop() {
 lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("Motor speed: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin0)); // Prints value on Potpin1 to LCD
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("E-polytechnique "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin1)); // Prints value on Potpin1 to LCD

  motor_speed = analogRead(pot) / 4;
  if(motor_dir)
    analogWrite(pwm1, motor_speed);
  else
    analogWrite(pwm2, motor_speed);
  if(!digitalRead(button)){                // If direction button is pressed
    while(!digitalRead(button));           // Wait until direction button released
    motor_dir = !motor_dir;                // Toggle direction variable
    if(motor_dir)
      digitalWrite(pwm2, 0);
    else
      digitalWrite(pwm1, 0);
  }
}
lcd.print(analogRead((int)((potPin0)/1023.0)*100)); // Prints value on Potpin1 to LCD as percent

analogRead() will return a value between 0 and 1023

If I told you that it had just returned say 123, could you calculate that as a percentage of 1023 ?

If not, then I fear that programming is not for you

accountsays:
Hello,

I'm controlling a dc motor speed by a potentiometer
I want to display the potentiometer value as percentage. Here's the code

I'm a beginner so go easy on me . :slight_smile:

// Arduino DC motor speed and direction control

#define button   8
#define pot      0
#define pwm1     9
#define pwm2    10

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int potPin0 = A0;
int potPin1 = A2;
boolean motor_dir = 0;
int motor_speed;

void setup() {
 pinMode(button, INPUT_PULLUP);
 pinMode(pwm1,   OUTPUT);
 pinMode(pwm2,   OUTPUT);

lcd.begin(16, 2);
lcd.clear();

pinMode(potPin0, INPUT);
pinMode(potPin0, INPUT);
}

void loop() {
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("Motor speed: "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin0)); // Prints value on Potpin1 to LCD
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("E-polytechnique "); // Prints Sensor Val: to LCD
lcd.print(analogRead(potPin1)); // Prints value on Potpin1 to LCD

motor_speed = analogRead(pot) / 4;
 if(motor_dir)
   analogWrite(pwm1, motor_speed);
 else
   analogWrite(pwm2, motor_speed);
 if(!digitalRead(button)){                // If direction button is pressed
   while(!digitalRead(button));           // Wait until direction button released
   motor_dir = !motor_dir;                // Toggle direction variable
   if(motor_dir)
     digitalWrite(pwm2, 0);
   else
     digitalWrite(pwm1, 0);
 }
}

Welcome to the Arduino forum. I am not really sure what you want to do.

How do you have the potentiometer wired? Is it part of a voltage divider? What voltage do you have feeding the voltage divider? Which Arduino are you using?

The analog read will NOT give you a resistance value. The best it can do is give you a value from 0 to 1023. Which is based on the voltage applied to the analog pin. Depending on how you have the pot connected, you can actually have a smaller range of values, so percentage is of questionable use.

Paul

Thank you all for answering but it didn't change. the display still showing the potentiometer value

Post your new program that shows the problem

Hi, I've spent weeks trying to get the answer for this, and now that I have, I want to share the information for all those who will struggle in the future :smiley:

int potentiometerValue = analogRead(potentiometerPin);
int percent = map(potentiometerValue , 0, 1023, 0, 100);
Serial.println(percent);

Here's where I got the solution:
https://www.browncountylibrary.org/wp-content/uploads/2018/03/arduino_potentiometer.pdf

I've spent weeks trying to get the answer for this

If only you had asked here weeks ago ...

I always use the map function.

and never failed on me, This is the code that I use.

int oldVal;
int TOLERANCE= 5
      int val = analogRead(pot);
      val = map(val, 0, 1023, 0, 100);

      int diff = abs(val - oldVal);

      if (diff > TOLERANCE) )
      {
        Serial.print(val);
        Serial.println("%");
        oldVal = val;// only save if the val has changed enough to avoid slowly drifting
      }

it helped me a lot when I need a certain value that isn't jumping around.

aideax:
int potentiometerValue = analogRead(potentiometerPin);
int percent = map(potentiometerValue , 0, 1023, 0, 100);
Serial.println(percent);

Here's where I got the solution:
https://www.browncountylibrary.org/wp-content/uploads/2018/03/arduino_potentiometer.pdf