Battery voltage 2 place decimal

I am monitoring the external power supply that is powering my Electron from a vehicle battery with a value display. The existing code uses

#define batPin A0

Blynk.virtualWrite(V5, map(analogRead(batPin), 0, 4095, 0, 14)) ;

I would like to read a floating value that shows the actual voltage of the battery with 2 decimal places. Looking at the examples I added

float b  = batPin;

int batVoltage = (int)b;

String voltage = (String)batVoltage;

voltage += "VDC";

Blynk.virtualWrite(V5, voltage);

When I attempted to compile it i got an error, ‘voltage’ does not name a type. So, can someone point me in the right direction please. I googled and searched this forum and found examples to get me this far. Thanks in advance

what are you doing in your code is:

  • you #define batPin as A0. if you research what define does, you will find out that at compile time EVERYWHERE where finds batPin will replace with A0.

  • next, you try to fo this: float b = batPin; which is actually float b = A0; <- this is nonsense

  • than you try to assign A0 to batVoltage,

  • than you try to convert A0 to String also on a incorrect way. it should be String(A0), but that still doesn’t makes any sense

for the fastest workaround, just use the labeled value widget, which has built in float decimal reductor. easy to use, just read the docs.

http://docs.blynk.cc/#widgets-displays-labeled-value

Thanks both for your reply. I did indeed read the documents prior to posting my question and had it formatted as /pin.##/ but it reads 14.00 while the input is only 12.6. I understand that I have it mapped this way

Blynk.virtualWrite(V5, map(analogRead(batPin), 0, 4095, 0, 14));

That is why I was attempting to add the float so that it could read to a max of 14.00volts while reading the actual value, which again is currently 12.6V. So, I am still missing something because after formatting the widget I see 14.00 which of course is not the correct value

Thanks

12.6V directly into analog pin? :confused:

If you want to use floats in your map function, do the following:

 float voltage = map(analogRead(batPin), 0, 4095, 0, 140);
 Blynk.virtualWrite(V5, voltage/10); 
1 Like

Thank you, Ill give it a try.

Voltage divider. Thanks

I say Zzzzzzap! :zap::zap::zap: :rofl:

My Arduino UNO’s analog inputs can only handle 5 V… to get a reading with 2 decimals I would do like this:

int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0); 
float roundedValue = ceilf(voltage * 100) / 100;
Serial.print(roundedValue);
Serial.println(" V")
//Blynk.virtualWrite(5, roundedValue);

ceilf() rounds the second decimal upwards like 4,33333 = 4,34, but it’s good enough for me :slight_smile:

1 Like

Particle Electron so only 3.3v

So, would your second line be this instead

float voltage = sensorValue * (3.3 / 4095);

Yes!

1 Like

Thanks all for the help and education!

1 Like

i use this function to manage decimals in floats, you can try it:

String floatReductor(String data, byte dec)  // keeps just the first x decimals in floats
{
  unsigned int point = data.indexOf('.');

  if (point > 0 && point < data.length()) {
    data.remove(point + dec + 1);
  }
  
  return data;
}

and use it like this:

Blynk.virtualWrite(V113, floatReductor(String(yourFloat), 2);  // <- 2 will leave 2 decimals
1 Like

thanks for your reply @wanek