B4R Question Monitoring the power supply

Filippo

Expert
Licensed User
Longtime User
Hi

I know questions here should only be for B4r, but maybe someone can help me.

Has anyone already done something with monitoring the power supply?
I'm trying to write such a program for my Seeeduino xiao, but it doesn't really work.
When the voltage drops below a certain level(2,50 Volt), the power led should start blinking.
But no matter how high the voltage is, the Power-Led does not blink.
I use 2x AA batteries as power supply.
I'm doing something wrong, but what?

Here is the sketch I use:
B4X:
const int pbPwLed = 1; // Power-Led
const int pbAnalog = 0; // Analog-Input

float voltage = 0.0;
int batvalue = 0;
unsigned long lastblink = millis();

void setup() {
  Serial.begin(115200);

  pinMode(pbPwLed, OUTPUT);
  pinMode(pbAnalog, OUTPUT);
 
}

void loop() {
  ManageBattery();
}

void ManageBattery() {
  batvalue = analogRead(pbAnalog);
  voltage = batvalue * (3.30 / 1023.00);
  if (voltage < 2.50) {
    //Power-Led Blinken lassen
    if ((millis() - lastblink) > 250){
      digitalWrite(pbPwLed, !digitalRead(pbPwLed)); // LED wird ein- bzw. ausgeschaltet
      lastblink = millis();
    }
  }
  else{
    digitalWrite(pbPwLed, HIGH);  // turn ON the LED
  }
 
  Serial.print("Input Voltage = ");
  Serial.println(voltage);
}
 

klaus

Expert
Licensed User
Longtime User
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
and 1.1V.

With ESP8266 NodeMCU board i used 300K serial resistor to the ADC pin and measured 3....4.2 VDC (of Li-battery power supply) like:

B4X:
    pinADC.Initialize(pinADC.A0, pinADC.MODE_INPUT)
    Dim adc As UInt = pinADC.AnalogRead
    Log("adc = ", adc)
    VCC = adc / 1024 / 0.157 - 0.18    'ESP-07 300K resistor (100K from ADC to GND parallel resistor is already at the board)
    Log("Vcc, volts = " , VCC)
 
Last edited:
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I have googled around a bit, below some links:
Some devices have two internal reference voltages 5V or 3.3V and 1.1V.
With the 1.1V reference the measurement is possible.
https://www.b4x.com/android/forum/t...tatus-battery-and-recharge.104730/post-656259
https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
Hi Klaus,

I asked 2 days ago the SeeedStudio forum why it does not work and how it should work, here is the answer:
Sorry to be late.
I need to explain some complex circuit knowledge. What is read using analogRead() is the value of the ADC. When there is a USB cable supply, at this time the IC supply voltage is 3.3V and the components are operating at normal voltage, so the ADC input of 2.9V can be distinguished normally.
When the USB cable is unplugged, because the input voltage is less than the set ldo input voltage range, the ldo cannot step down and the output becomes 2.9V, at this time the IC can only work at 2.9V, it will be misjudged as full voltage (3.3V) supply, which is a phenomenon caused by illegal operation, we also do not allow such a low input voltage, although the IC can work normally, but the ADC has appeared The situation of inaccurate reading has occurred.
If you need to measure voltage, our advice is. Use a battery higher than 3.3V (not greater than 5V) and let him work with a voltage lower than 3.3V (or equal) then the alarm warns that it should be charged and used.

Now it seems logical to me why my plans could not work.
I will now use the solution with the 3.7 volt Lipo battery.
 
Upvote 0
Top