Display Potentiometer value as percentage

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