0
\$\begingroup\$

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);
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Who voted to close? Why? \$\endgroup\$ – Rocketmagnet Apr 18 '12 at 22:41
  • 1
    \$\begingroup\$ @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.) \$\endgroup\$ – Kevin Vermeer Apr 19 '12 at 2:08
1
\$\begingroup\$

This is what I ended up with.

#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("%");}
\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – Federico Russo Apr 25 '12 at 16:14
  • \$\begingroup\$ Another option is to have decimal point Change: int VoltsRange = 0; To: Float VoltsRange = 0; Get more accurate voltage after decimal point. \$\endgroup\$ – user33742 Dec 6 '13 at 7:11
  • \$\begingroup\$ This is more a different approach than a solution to the original question. Its not likely that this will help anyone in the future because it doesn't even state what was wrong in the first place. \$\endgroup\$ – Rev1.0 Dec 6 '13 at 8:02
0
\$\begingroup\$

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() { }
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Kenny Robinson Apr 18 '12 at 22:42
  • \$\begingroup\$ 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! \$\endgroup\$ – Eduardo Apr 19 '12 at 0:09
  • \$\begingroup\$ Try this code for converting float to string. Where it says Serial.print, change it to LCD.print \$\endgroup\$ – Rocketmagnet Apr 19 '12 at 0:15
  • \$\begingroup\$ 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, \$\endgroup\$ – Eduardo Apr 19 '12 at 1:47
  • 1
    \$\begingroup\$ @Rocketmagnet - What do you expect to happen from lcd.print("Voltage=") + sensorValue; ? I'd expect the function call to return a number, which would get added to sensorValue, and then not assigned to anything. Instead, you want to do lcd.print("Voltage="); \n lcd.print(sensorValue, DEC); \$\endgroup\$ – Kevin Vermeer Apr 19 '12 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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