User Tools

Site Tools


amc2022:grouph:link

This is an old revision of the document!


Build

Schematics

Code & Description

*//Reading of voltage is done by the Analog Pin reader.
*//Because the max input voltage on the pins is 3.3 V we cant directly read the 9V battery voltage.
 
 int potPin = A0;         // Analog pin has to be ADC 1, because ADC 2 are used by wifi and wont work
int potValue;            
float voltage =0;         // float because its a decimal number not integer
float battery_percent;
 
void setup() {
 
  Serial.begin(115200);
 
 
  {
 
  potValue = analogRead(potPin);
  float voltage = (3.3/4095.0) * potValue * 2.73;        
 
 
  *//Resistors and other components can have a varying number of outputs, although they are usually minimal
  *//additional calibration has to be done in order to get accurate readings
  *//We have found that the use of a multimeter will help in determining the values that will provide best outcomes
  *//Because we have different boards, resistors and setups, the final code could have alterations in the calibrating numbers
 
  Serial.print("potValue:");                
  Serial.print(potValue);                   
 
 
*//While the serial monitor output can be changed, we are more interested is in the overall result which is transmitted 
*//to Grafana for easy access
 
 
  Serial.print(" Voltage:");
  Serial.print(voltage);
  Serial.println("V");  
 
 
 
  battery_percent = mapfloat(voltage, 3.0, 9.0, 0 , 100); // min value cut off at 6V and maximum voltage is 9V
 
  if (battery_percent > 100)
  {
    battery_percent = 100;
  }
  if (battery_percent < 0)
  {
    battery_percent = 0;
  }
 
 
 
 
  Serial.print("Battery Percentage = ");
  Serial.println(battery_percent);
 
 
 
 
 
  if (voltage > 7.0 && voltage < 8.2) // THIS VALUES HAVE TO CHANGE ACCRODING TO SOURCE
  {Serial.print("Low bat");                      // USE VOLTMETER TO FIND THE ACCURATE VOLTAGE
  }
  if (voltage <6.5)                //cut-off value is at 6V according to specification , we use 6.5V
  {Serial.print("Replace Battery");
  }
  delay(1000);
 
 
}
}
 
 float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
 
void loop()
{
 
}
 
 
                                     // NO LOOP NEEDED,,, ONE VALUE WITH WARNING SHOULD BE DISPLAYED ON GRAFANA

Issues & Characteristics

*The actual voltage will not be 100% accurate because the ADC pins have a non-linear

===== Results ======

amc2022/grouph/link.1662760923.txt.gz · Last modified: 2023/01/05 14:38 (external edit)