Voltage Divider not working when not connected to USB.

Hi.

I got a problem that it seems that the arduino do not measure the battery voltage when it is not connected to usb. and i don understand why. :’(

this is the my resource i have used.

http://forums.trossenrobotics.com/tutorials/how-to-diy-128/cheap-battery-monitor-using-resistive-voltage-divider-3264/

Here are my code:

int Cycletime = 1800000; //30min
int dryValue_1 = 950;
int dryValue_2 = 950;
int dryValue_3 = 950;
int pumpetime_1 = 500;
int pumpetime_2 = 500;
int pumpetime_3 = 500;
float minbatteryvolt = 6.5;
int batMonPin = 3;    // input pin for the divider
int val = 0;       // variable for the A/D value
float pinVoltage = 0; // variable to hold the calculated voltage
float batteryVoltage = 0;
float ratio = 3.128  ;  // Change this to match the MEASURED ration of the circuit


#include <JeeLib.h>  // Include library containing low power functions
ISR(WDT_vect) {
  Sleepy::watchdogEvent();  // Setup for low power waiting
}


const int analogInPin_1 = A0;
const int analogInPin_2 = A1;
const int analogInPin_3 = A2;


// value read from the soil moisture sensor
int sensorValue_1 = 0;
int sensorValue_2 = 0;
int sensorValue_3 = 0;



void setup() {
  pinMode(2, OUTPUT); //power sensor 1
  pinMode(3, OUTPUT); //power sensor 2
  pinMode(4, OUTPUT); //power sensor 3
  pinMode(5, OUTPUT); //battery check relay
  pinMode(8, OUTPUT); //blue led
  pinMode(9, OUTPUT); //red led
  pinMode(10, OUTPUT); //pump 1
  pinMode(11, OUTPUT); //pump 2
  pinMode(12, OUTPUT); //pump 4

  Serial.begin(115200);
    digitalWrite(5, HIGH);
      digitalWrite(10, HIGH);
        digitalWrite(11, HIGH);
          digitalWrite(12, HIGH);
}

void loop() {

  digitalWrite(5, LOW); //Activate relay so it can read battery voltage
  delay(500);
  val = analogRead(batMonPin);    // read the voltage on the divider
  pinVoltage = val * 0.00488;       //  Calculate the voltage on the A/D pin
  //  A reading of 1 for the A/D = 0.0048mV
  //  if we multiply the A/D reading by 0.00488 then
  //  we get the voltage on the pin.



  batteryVoltage = pinVoltage * ratio;    //  Use the ratio calculated for the voltage divider
  //  to calculate the battery voltage
  Serial.print("Minimum: ");
  Serial.print(minbatteryvolt);
  Serial.print("Voltage: ");
  Serial.println(batteryVoltage);
  digitalWrite(5, HIGH);
  delay(250);
  if ((batteryVoltage < (minbatteryvolt + 0.3)) && (batteryVoltage > minbatteryvolt)) {
    for (int i = 0; i < 5; i++) {
      digitalWrite(8, HIGH);
      delay(250);
      digitalWrite(8, LOW);
      delay(250);
    }
    digitalWrite(8, HIGH);
  }
  else
  {
      digitalWrite(8, LOW);

  }

  if (batteryVoltage < minbatteryvolt) {
    for (int i = 0; i < 1000; i++)
    {
      digitalWrite(9, HIGH);
      delay(250);
      digitalWrite(9, LOW);
      delay(250);
    }
  }
  if (batteryVoltage > minbatteryvolt) {

    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    Sleepy::loseSomeTime(150);
    sensorValue_1 = analogRead(analogInPin_1);
    sensorValue_2 = analogRead(analogInPin_2);
    sensorValue_3 = analogRead(analogInPin_3);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);


    if (sensorValue_1 > dryValue_1) {
      Serial.println("Pumpe 1 dosering" );

      digitalWrite(10, LOW);
      delay(pumpetime_1);
      digitalWrite(10, HIGH);


    }

    if (sensorValue_2 > dryValue_2) {
      Serial.println("Pumpe 2 dosering" );
      digitalWrite(11, LOW);
      delay(pumpetime_2);
      digitalWrite(11, HIGH);

    }

    if (sensorValue_3 > dryValue_3) {
      Serial.println("Pumpe 3 dosering" );
      digitalWrite(12, LOW);
      delay(pumpetime_3);
      digitalWrite(12, HIGH);

    }

    Serial.print("sensor 1 = " );
    Serial.print(sensorValue_1);
    Serial.print(" sensor 2 = " );
    Serial.print(sensorValue_2);
    Serial.print(" sensor 3 = " );
    Serial.println(sensorValue_3);
    delay(10);
    Sleepy::loseSomeTime(Cycletime);
  }



}

How exactly is the voltage divider wired ?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom.... :)

So how are you powering the Arduino from the battery? Unless you use a reference voltage of some sort you can't measure, so for instance you would use the 5V output of the Arduino's on-board regulator as the reference if the battery is connected to Vin.

If the battery is 5V nominal and connected to Vcc you would be using the battery as its own voltage reference, which merely measures the resistor ratio, not a voltage.

Here is a fast drawing!

https://www.dropbox.com/s/mofx15fllr1k726/sketch.png

Hi,

You cannot drive a relay directly from the output of an arduino, unless the current is less than 40mA.
You need to use a transistor or a MOSFET.

Also the values of your potential divider of 100R and 47R are way too low,
Try 10K and 4K7 resistors.

Tom… :slight_smile:

I found the problem. it was a bad electrical connection and it was pushed together when the usb was connected :sweat_smile:

TomGeorge: Hi,

You cannot drive a relay directly from the output of an arduino, unless the current is less than 40mA. You need to use a transistor or a MOSFET.

Thanks, but it is a relay shield:)

TomGeorge: Also the values of your potential divider of 100R and 47R are way too low, Try 10K and 4K7 resistors.

Tom... :)

you may be right but it works fine when the usb is conneced. but i see i got some strange readings when it is not usb connected. ( i have programmed the blu light to blink one blink per volt on each cycle. ) i will do som more testing maybe just adding a delay will work?!