I'm trying to get my LCD to display "Voltage= (variable)". I have it programmed to write the text and I have a pot wired to my arduino and I'm trying to get a voltage number to come after the equals sign. How can I program it so when I turn the pot that the actual voltage will come after "Voltage="?

Here's my program

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  
void setup()
{
}
void loop()
{
lcd.init();                      
lcd.backlight();
int sensorPin = A0;
int sensorValue = 0;
sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
lcd.print("Voltage=");
lcd.print(sensorValue);
}
link|improve this question
1  
Who voted to close? Why? – Rocketmagnet Apr 18 at 22:41
1  
@Rocketmagnet - When this question started out, it was in pretty poor shape. It looks a good deal better now. Still, if the reason isn't obvious, whoever's voting to close should leave a comment so the OP can fix the question. (Obviously, it wasn't me, as I can't cast an ordinary close vote.) – Kevin Vermeer Apr 19 at 2:08
feedback

2 Answers

This is what I ended up with. Thanks for all your help, guys!

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  

void setup() {}

void loop()
{ lcd.init();                      
lcd.backlight();

int VoltsInput = A0;
int VoltsRange = 0;
int VoltsPercent = 0;

VoltsRange = (5.0/1023.0) * analogRead(VoltsInput);
VoltsPercent = (((5.0/1023.0) * analogRead(VoltsInput)) / 5) * 100;

lcd.print(VoltsRange);
lcd.print("V    ");

lcd.print(VoltsPercent);
lcd.print("%");}
link|improve this answer
You may want to accept either answer so that we know the problem is solved. (you can accept your own answer, it just doesn't get you reputation) – Federico Russo Apr 25 at 16:14
feedback

So the Arduino's ADC is 10-bits resolution, this means that analogRead will return a value between 0 and 1023 (2^10-1). You need to re-scale this to the range 0v - 5v.

You can do this simply by dividing by 1023, and multiplying by 5. I'm not totally familiar with the Arduino language, but I hear that it's quite like C. So this should probably work.

LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{
    lcd.init();
    lcd.backlight();
    int sensorPin = A0;
    float sensorValue = 0;
    sensorValue = (5.0/1024.0) * analogRead(sensorPin);

    lcd.print("Voltage=") + sensorValue;
}

void loop() { }
link|improve this answer
Take care if you are running on 3V3, or are using a voltage reference (aref), one may be internal and can help to increase resolution on your reading. – Kenny Robinson Apr 18 at 22:42
Hi, Rocketmagnet, thanks for the code. I typed it in and I only get "Voltage=" to come up on the screen. I'm not familiar with Arduino. We've been learning to program the 68HC11 chip for this whole semester and then last week our professor throws the Arduino at us and tells us to develop a program for this by next week. Needless to say this is proving to be a little challenging. Thanks for all your help! – Eduardo Apr 19 at 0:09
Try this code for converting float to string. Where it says Serial.print, change it to LCD.print – Rocketmagnet Apr 19 at 0:15
thanks, Rocketmagnet, I'll give it a shot! – Eduardo Apr 19 at 0:45
Hi, Rocketmagnet, I finally figured out a code that would let the pot work and I could change the digits on the screen by turning it. Unfortunately because it's in a loop, the whole screen is either filled with all 1s, 2s, 3s, 4s, or 5s depending what the pot is set on. Is there any way that I can get the results of what's in a loop outside of a loop? Thanks, – Eduardo Apr 19 at 1:47
show 8 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.