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);
}
 

Cableguy

Expert
Licensed User
Longtime User
I would use an analog input pin or one of the integrated ADC pins...
Hardware wise, how are you sensing the input voltage?
A voltage divider is the simplest way to do it,.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I would use an analog input pin or one of the integrated ADC pins...
Hardware wise, how are you sensing the input voltage?
A voltage divider is the simplest way to do it,.
I connect the 3v3 pin with A0 pin via a series resistor.
If I supply the Seeeduino with the USB voltage and connect the A0 pin with 2x AA battery(the batteries have about 2,5 volt voltage), then it works correctly, i.e. the LED flashes.
But if I use only the 2x AA battery as supply voltage, then it never flashes, no matter if the voltage is less than 2.5 volts.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The purpose of a resistor is mainly current limitation, specially used in a séries configuration.
If you Google Arduino input voltage sensing circuits, you will fond that your choice is not the best one.
Still thé question remains... How are you monitoring your circuit behavior? You need at least to be sure what the input voltage, drop voltage and sensed voltage is.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Also, bear in mind that most analog/ADC/digital inputs have a voltage limit and very often under Vin voltage.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I have made the circuit in principle as on this website.
I only changed the reference voltage from 5Volt to 3,5Volt.

from
B4X:
float voltage = sensorValue * (5.00 / 1023.00) * 2;
to
B4X:
float voltage = sensorValue * (3.30 / 1023.00) * 2;
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
This may seem an odd question but what colour is your power LED? Below 2.5v Blue LEDs may not work. The red ones operate at the lowest voltage. The available voltage will be even less than 2.5v because of the controlling transistor in the processor.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Have you tried this ?
B4X:
float voltage = sensorValue * (5.00 / 1023.00);
The reference voltage is 5V.
The voltage you want to read 3.3V, is in the reference range, below the 5V, therefore no need to adapt the input voltage.

In the circuit of the website they want to measure a 9V voltage.
But, the reference voltage for for A/D converter is 5V.
Therefore, they divide the battery voltage by two down to 4.5V with the voltage divider, to get a voltage in the reference range below 5V.
And after reading the use:
float voltage = sensorValue * (5.00 / 1023.00) * 2;
They multiply again the voltage by two.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Have you tried this ?
B4X:
float voltage = sensorValue * (5.00 / 1023.00);
The reference voltage is 5V.
The voltage you want to read 3.3V, is in the reference range, below the 5V, therefore no need to adapt the input voltage.

In the circuit of the website they want to measure a 9V voltage.
But, the reference voltage for for A/D converter is 5V.
Therefore, they divide the battery voltage by two down to 4.5V with the voltage divider, to get a voltage in the reference range below 5V.
And after reading the use:
float voltage = sensorValue * (5.00 / 1023.00) * 2;
They multiply again the voltage by two.
Hi Klaus,

I have already tried all this, unfortunately without success.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
This may seem an odd question but what colour is your power LED? Below 2.5v Blue LEDs may not work. The red ones operate at the lowest voltage. The available voltage will be even less than 2.5v because of the controlling transistor in the processor.
Thanks, but unfortunately that has nothing to do.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Die Zeile "batvalue = analogRead(pbAnalog);"
gibt immer als Wert 1023 aus, gleichgütig ob an A0-Pin 3,3V oder 2,5Volt anliegen.
]

In the web site you point to, we clearly see 2 resistors used as a voltage divider... this is the correct way to sense voltage
I have tried this test as well.

The problem is that the line "batvalue = analogRead(pbAnalog);" always returns the value 1023, no matter if A0 pin is 3.3V or 2.5Volt.

Only if the UBS voltage is used as supply voltage and the 2x AA battery is connected to the A0 pin, then the battery voltage is read out correctly.
But so I do not need, it should be supplied only with the 2x AA battery.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Can you please provide the schematics you are using?
Or better yet, some photos of your setup.
If using USB as power it reads correctly then it's a hardware implementation issue
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If I understand well, you supply the power to the board with two AA batteries which means about 3V on the 3.3V pin.
I am afraid that you cannot measure the supply voltage with an analog pin because your supply voltage is also the reference voltage for the A/D converter and therefore always 1023.

In the link you posted, the supply voltage is 9V.
The reference voltage for the A/D converter is always the regulated 5V.
As long as the supply voltage is higher than the reference voltage this remains constant event if the battery voltage gets lower and the measure is correct.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
If I understand well, you supply the power to the board with two AA batteries which means about 3V on the 3.3V pin.
I am afraid that you cannot measure the supply voltage with an analog pin because your supply voltage is also the reference voltage for the A/D converter and therefore always 1023
That's the problem.
But somehow there should be a way, or what do you think?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As long as you do not have a supply voltage higher than the reference voltage, unfortunately, at least for me, there is no solution.
What is the requested voltage at the Vin pin for your device ?
You need to supply a higher voltage than the reference voltage.
I do not know what reference voltage your device has.
And I do not know either, when the operation voltage can be 5V or 3.3V, which one is the reference voltage ?
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
And I do not know either, when the operation voltage can be 5V or 3.3V, which one is the reference voltage ?
The supply voltage can be 5 or 3.3 volts, but I want to use only 2x AA batteries.

1655060451439.png
 
Upvote 0
Top