Printing a float on 16x2 LCD

I want to print a float variable with a maximum value of 99.9. I am using sprintf() but somehow end up with a ? only. Any help welcome ! Code is given below :

// Trial to print floating point value to a 16x2 LCD 
#include <LCD.h>
#include<LiquidCrystal_I2C.h>
#include "Wire.h"
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address 

void setup() {
float  volts = 99.9; 
int   cdtSec = 999;
char LCDmsg[16];
  lcd.begin(16, 2);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  delay(2000);
  sprintf(LCDmsg, "VOLT:%04.1fSEC:%03u", volts, cdtSec);
  lcd.print(LCDmsg); // resulting display " VOLT:    ?SEC:999"
 }

void loop() {
  // put your main code here, to run repeatedly:

}

Hi Mogaraghu

Floats and in sprintf() don't work on the Arduino. You could use dtostrf() instead.

http://www.hobbytronics.co.uk/arduino-float-vars

Regards

Ray

As Hackscribble pointed out, something like (I don't have an LCD display handy):

// Trial to print floating point value to a 16x2 LCD
#include <LCD.h>
#include<LiquidCrystal_I2C.h>
#include "Wire.h"

LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address

void setup() {
  float  volts = 99.9;
  int   cdtSec = 999;
  char LCDmsg[21];
  Serial.begin(9600);

  dtostrf(volts, 4, 1, LCDmsg);

  Serial.print("VOLT:");
  Serial.print(LCDmsg);
  Serial.print(" SEC: ");
  Serial.println(cdtSec);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Try this simple function I wrote for a solar cell tester, its rude & crude but worked for what I was doing.

example:

lcd.setCursor(1,0);
lcd.print("Volts");
lcdNumFlt(volts,10,0,2,2);

// Call with number to display (FLOAT), decimal point column,
// line number (0 or 1),number of columns after DP (start with 4),
// number of columns after decimal point (try 3)

void lcdNumFlt (float num, byte dpcol, byte line, byte bdp, byte adp)
{
  long  tmp;
  byte offset = 1;
        
  tmp = num;
  while(abs(tmp) >= 10){
    tmp /= 10;
    offset ++;
  }
  if(num < 0) offset ++;  
  lcd.setCursor(dpcol - bdp, line);
  for(byte i = 0;i < bdp + adp + 1;i ++)
    lcd.print(" ");    
  lcd.setCursor(dpcol - offset, line);
  lcd.print(num, adp);
}

Feel free to crack, hack and repack.

Hackscribble:
Hi Mogaraghu

Floats and sprintf() don't work on the Arduino. You could use dtostrf() instead.

http://www.hobbytronics.co.uk/arduino-float-vars

Regards

Ray

Perfect. The idea worked and I got the required display on the LCD. It is not that sprintf() does not work at all in Arduino - just that it cannot handle a float I guess. Thanks for the tip - it was a critical point. This is the code snippet I used :

// Trial to print floating point value to a 16x2 LCD 
#include <LCD.h>
#include<LiquidCrystal_I2C.h>
#include "Wire.h"
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address 

void setup() {
float  volts = 12.3; 
int   cdtSec = 456;
char LCDmsg[16];
  lcd.begin(16, 2);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  delay(2000);
  sprintf(LCDmsg, "VOLT:    SEC:%03u",cdtSec);
  lcd.print(LCDmsg);
  lcd.setCursor(5,0);
  dtostrf(volts,4,1,LCDmsg);
  lcd.print(LCDmsg);
 }

void loop() {
  // put your main code here, to run repeatedly:

}