Tapatalk

Reading voltage on Talk2 AVR, with RTC on board...

Reading voltage on Talk2 AVR, with RTC on board...

4

PostMar 10, 2020#1

I am using the Wisper Node AVR, without radio.
My project also uses RTC Alarms, to do tasks.
I have closed the JP11. Even though the documentation  doesn’t say so for the AVR, but I read the documentation for the LORA board which say to close the JP11.  Closing the JP11 made my alarms work 😊

To get voltage, I tried to include the T2WhisperNode.h library, - but it forces me to include the RH_RF69.h library, and that library uses the D3 or Interrupt 1 that I need for my RTC alarms. So, - it seems like I cant use RTC alarms and get voltage readings if I use the T2WisperNode library?

To solve that conflict, I tried to use analog reading on A6 and A7 setting the A0 high first, and use the formular from the dokumentation:

Voltage = (MAX_Voltage * ADC_Reading) / 1024
Voltage = (7.282 * 211) / 1024
Voltage = 1.5

My test code looks like this:

Code: Select all

#include <Arduino.h>

float Bat;
float Vin;

float MaxPowerVin = 7.282;
float MaxPowerBat = 7.282;

void GetPower()
{
  
  Bat = analogRead(A6);
  Serial.print("Bat: ");
  Serial.println(Bat);

  Vin = analogRead(A7);
  Serial.print("Vin: ");
  Serial.println(Vin);

  Serial.print("Voltage Bat: ");
  Serial.println((MaxPowerBat * Bat) / 1024);

  Serial.print("Power VIN: ");
  Serial.println((MaxPowerVin * Vin) / 1024);
}


void setup() 
{
  Serial.begin(115200);
  digitalWrite(A0, HIGH);
}

void loop() 
{
  GetPower();
  delay(1000);
}
The output looks like:

Bat: 126.00
Vin: 234.00
Voltage Bat: 0.90
Power VIN: 1.66 
Bat: 128.00     
Vin: 235.00     
Voltage Bat: 0.91
Power VIN: 1.67 …...
..

The factory firmware states: ...

  :: Supply Voltages ::
          Battery: 2790mV
    Micro-USB/VIN: 0mV

What am I doing wrong, - code vice :-) ?

1885
1885

PostMay 27, 2020#2

Hello Anders,

First sorry for the late reply. There was a problem with the forum notification and never saw your message before.

Regarding the D3, can you please double-check it? By default, the radio library relies on the D2 (or INT0).

For the Voltage, here the part of the code from the Talk2Library, most specifically the file utils/T2Utils.cpp:

Code: Select all

uint16_t readVoltage(uint8_t analogPin, uint8_t controlPin, uint16_t maxVoltage, uint8_t samples)
{
  analogReference(INTERNAL);

  // Turn on the MOSFET via control pin
  if(controlPin > 0)
  {
    digitalWrite(controlPin, HIGH);
    pinMode(controlPin, OUTPUT);
  }

  // Read pin a couple of times and keep adding up.
  uint16_t readings = 0;
  for (uint8_t i = 0; i < samples; i++) {
    readings += analogRead(analogPin);
  }

  // Turn off the MOSFET
  if(controlPin > 0)
  {
    digitalWrite(controlPin, LOW);
  }


  /* Uncomment for Debug */
  /*
  Serial.print(F("AVG last X Analog Reads on analog Pin: "));
  Serial.println(readings / samples);
  Serial.print(F("Max Voltage: "));
  Serial.println(maxVoltage);
  Serial.print(F("Calculated voltage: "));
  Serial.println((uint32_t)maxVoltage * (readings / samples) / 1023);
  */

  /*
   * This need to be casted over 32 bits to allow proper calculation.
   * Still safe to return a 16 bits for results up to 65V
  */
  return ((uint32_t)maxVoltage * (readings / samples) / 1023);
}
Please note I have modified the functions name, so it can be used outside of the class. To use the function you need to specify the following parameters for example:

Code: Select all

uint16_t battVoltage = 0;

battVoltage = readVoltage(A6, A0, 7282, 5);

// Print the battery voltage in Millivolts
Serial.println(battVoltage);
The pins A6 and A0 I got from the board pinout you can find at the documentation (https://bitbucket.org/talk2/whisper-node-avr-lora/src). You can use the same function to read any other voltage via analog pins. 

Tips:
  • The second function parameter, the 'controlPin' can be "0" if not used. The Whisper Node has one for the battery so it prevents current to continually be drained by the voltage divider.
  • The 7282 is the maximum voltage (in millivolts) you can archive based on the voltage divider configuration and the reference voltage (INTERNAL = 1.1V). See the documentation for details.
  • The last value, 5, is the number of samples the analog-to-digital will do to get an average.

Finally, you don't need to use Talk2Library. You can always use the Radiohead library to control the Radio as well as additional libraries you need. Just pay attention to the board pin-out and all should work as a standard Arduino.

Hope this helps.

Regards,
Mike M.