Change buf value to float Solved

this value on lcd is ok
lcd.print(buf);
but when I try to use Serial.println(buf); I got wrong value

#include <LiquidCrystal.h>    // include Arduino LCD library
 
// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
 
void setup(void) {
  Serial.begin(115200);
  lcd.begin(16, 2);           // set up the LCD's number of columns and rows
  lcd.setCursor(0, 0);
  lcd.print("RMS Voltage:");
  analogReference(INTERNAL);  // set ADC positive reference voltage to 1.1V (internal)
}
 
// get maximum reading value
uint16_t get_max() {
  uint16_t max_v = 0;
  for(uint8_t i = 0; i < 100; i++) {
    uint16_t r = analogRead(A3);  // read from analog channel 3 (A3)
    if(max_v < r) max_v = r;
    delayMicroseconds(200);
  }
  return max_v;
}
 
// main loop
void loop() {
 
  char buf[10];
  // get amplitude (maximum - or peak value)
  uint32_t v = get_max();
  // get actual voltage (ADC voltage reference = 1.1V)
  v = v * 1116/1023;
  // calculate the RMS value ( = peak/√2 )
  v /= sqrt(2);
 
  sprintf(buf, "%03u Volts", v);
  lcd.setCursor(0, 1);
  lcd.print(buf);
  Serial.println(buf);
 
  delay(500);
 
}

Try

char buf[20];

I tried same issue nothing changed

how wrong? Any serial output?

now the output on lcd is 149 and output on serial is 789

it was an issue of arduino IDE I removed from pc and I reinstalled it then now it is ok, but I have a another question how to get buf value as float?

#include <LiquidCrystal.h>    // include Arduino LCD library

// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup(void) {
  Serial.begin(115200);
  lcd.begin(16, 2);           // set up the LCD's number of columns and rows
  lcd.setCursor(0, 0);
  lcd.print("RMS Voltage:");
  analogReference(INTERNAL);  // set ADC positive reference voltage to 1.1V (internal)
}

// get maximum reading value
uint16_t get_max() {
  uint16_t max_v = 0;
  for (uint8_t i = 0; i < 100; i++) {
    uint16_t r = analogRead(A3);
    if (max_v < r) max_v = r;
    delayMicroseconds(200);
  }
  return max_v;
}

void loop() {

  char buf[10];
  // get amplitude (maximum - or peak value)
  // get actual voltage (ADC voltage reference = 1.1V)
  uint32_t v = get_max() * 1116 / 1023 / sqrt(2); // calculate the RMS value ( = peak/√2 )

  sprintf(buf, "%03u Volts", v);
  lcd.setCursor(0, 1);
  lcd.print(buf);
  Serial.print(v);
  Serial.println(" Volts");
  delay(500);
}

it still get value as number no float

it is from beginning uint32_t. why should it magically be float ?
and you say 149 is correct value on LCD

I changed
uint32_t v
to
float v
now result of buf as 27584 it is not float

I want to display value as float

How did you change the rest of the code? Support for floats was cut out of sprintf for most of the Arduinos because it took up too much space. So the common remedy is to use dtostrf to turn the float to a char array and then use sprintf if you need to add more to that.

1 Like

better if not change your question while finding solution

#include <LiquidCrystal.h>    // include Arduino LCD library

// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup(void) {
  Serial.begin(115200);
  lcd.begin(16, 2);           // set up the LCD's number of columns and rows
  lcd.setCursor(0, 0);
  lcd.print("RMS Voltage:");
  analogReference(INTERNAL);  // set ADC positive reference voltage to 1.1V (internal)
}

// get maximum reading value
uint16_t get_max() {
  uint16_t max_v = 0;
  for (uint8_t i = 0; i < 100; i++) {
    uint16_t r = analogRead(A3);
    if (max_v < r) max_v = r;
    delayMicroseconds(200);
  }
  return max_v;
}

void loop() {
  char buf[10];
  // get amplitude (maximum - or peak value)
  // get actual voltage (ADC voltage reference = 1.1V)
  float v = (float)get_max() * 0.770636; // calculate the RMS value ( = peak/√2 )

  sprintf(buf, "%03.3f Volts", v);
  lcd.setCursor(0, 1);
  lcd.print(buf);
  Serial.print(v, 3);
  Serial.println(" Volts");
  delay(500);
}
1 Like

you mean this will not work now?

I mean it hasn't worked on the normal Arduino as long as I've been around them. I don't know about some of the fancy new ones, but for things like UNO and Mega and Nano and such, it's never worked as far as I know.

There is a way to enable it, but it takes up way more program space than it's worth if I understand right. Here's an older thread that claims to have it, but I haven't followed the link.

#include <LiquidCrystal.h>    // include Arduino LCD library

// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup(void) {
  Serial.begin(115200);
  lcd.begin(16, 2);           // set up the LCD's number of columns and rows
  lcd.setCursor(0, 0);
  lcd.print("RMS Voltage:");
  analogReference(INTERNAL);  // set ADC positive reference voltage to 1.1V (internal)
}

void loop() {
  char buf[10];
  float var = (float)get_max() * 0.770636; // calculate the RMS value ( = peak/√2 )
  uint16_t Trunc = trunc(var);
  sprintf(buf, "%05u.%3u", Trunc, uint16_t((var - Trunc) * 1000.0));

  lcd.setCursor(0, 1);
  lcd.print(buf);
  Serial.println(buf);
  delay(500);
}

// get maximum reading value
uint16_t get_max() {
  uint16_t max_v = 0;
  for (uint8_t i = 0; i < 100; i++) {
    uint16_t r = analogRead(A3);
    if (max_v < r) max_v = r;
    delayMicroseconds(200);
  }
  return max_v;
}

What do you have against dtostrf?

i don't know it good enough

Google does: https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42

Ok, then instead of creating a crazy workaround born of ignorance, let someone who does know give that answer.