GetBatteryVoltage()

Purpose

Read the potential difference between the 12V and GND green terminals (the board supply voltage).

When to use

If you want to know the robot’s battery voltage during the code execution.

Where to use

Most probably in the void loop() part of your .ino file, since we’d usually want to actively monitor the value.

Click here to learn more on the power supply circuits of the 9880 board.

This function requires the use of the following CrcLib functions at some point of your .ino file in order to work properly:

Returns

This function returns the value of the board supply voltage in Volt as a float type.

Main prototype

static float CrcLib::GetBatteryVoltage(float correction)

The following parameters must be passed to the function for it to work properly:

  • correction: The voltage divider correction factor that is unique to your board. Must be of the type float. See below for more information.

Overloads

#1: Without a correction factor

static float CrcLib::GetBatteryVoltage()

The following parameters must be passed to the function for it to work properly:

  • No parameters needed. A correction factor of 1.0 will be used automatically.

Examples

#include <CrcLib.h> //Variable declarations float voltageValue; //Represents the continually refreshed battery voltage void setup() { CrcLib::Initialize(); //Open the communication with the Serial monitor at a 2000000 baud rate Serial.begin(2000000); /* The rest of your setup code ... */ } void loop() { CrcLib::Update(); //Transfer the voltage value into the VoltageValue variable voltageValue=CrcLib::GetBatteryVoltage(); //Print the value on the serial monitor Serial.println(voltageValue); /* The rest of your looping code ... */ }
#include <CrcLib.h> //For clarity, let's give nicknames to our physical IOs #define LowBatteryLED CRC_DIG_6 void setup() { CrcLib::Initialize(); //Set Digital input #6 as an Output CrcLib::SetDigitalPinMode(LowBatteryLED, OUTPUT); /* The rest of your setup code ... */ } void loop() { CrcLib::Update(); if(CrcLib::GetBatteryVoltage(1.0235)<=11.96) { CrcLib::SetDigitalOutput(LowBatteryLED,HIGH); } else { CrcLib::SetDigitalOutput(LowBatteryLED,LOW); } /* The rest of your looping code ... */ }

More on this function

How does it work ?

The board uses a micro-controller analog input hooked up to a voltage divider embedded on the board to read the potential between the 12V and GND green terminals. The voltage divider steps down the 12V voltage to something that can be read by the micro-controller.

For more information on voltage divider, read this!

How to identify the correction factor for a given CrcDuino board

  1. Write a simple program to print to the serial monitor the battery voltage reading. Start by using a 1.0 correction factor, and upload it to your board. On the serial monitor appears the voltage read by the micro-controller.

  2. Hook a multimeter to the battery terminals to read the voltage of your battery.

  3. By trial and error, change the correction factor until the reading on the serial monitor corresponds to what is read by the multimeter.

Related articles