Tapatalk

Problem measure VBAT AND other analog input

Problem measure VBAT AND other analog input

4

PostMar 18, 2017#1

Hello,
I have problems measuring battery voltage and an analog sensor connected to A5. The sensor works fine when I do not read the battery voltage. But i can not read battery voltage AND the sensor an A5. When i try to re :cry: d both values the value on A5 is always 1023. I also tested other analog pins with the same effect.
Here is some of the code I use

Code: Select all

#include <RFM69.h>
#include <SPI.h>
#include <T2WhisperNode.h>
#include <LowPower.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 3
#define SOIL_SENSOR A5

#define NODEID      2
#define NETWORKID   1
#define GATEWAYID   1
#define FREQUENCY   RF69_868MHZ
#define SERIAL_BAUD 9600
#define ACK_TIME    60  // # of ms to wait for an ack

byte sendSize = 0;
boolean requestACK = false;

//RFM69 radio;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

typedef struct {
  float temp;
  int soil_humidity;
//  float voltage;
} Payload;
Payload soilData;


void setup() {
  Serial.begin (SERIAL_BAUD);
  // radio.initialize(FREQUENCY, NODEID, NETWORKID);
  //  radio.setHighPower();
  sensors.begin();
}


void loop() {

  //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

  sensors.requestTemperatures();
  soilData.temp = sensors.getTempCByIndex(0);

  //soilData.voltage = T2Utils::readVoltage(T2_WPN_VBAT_VOLTAGE, T2_WPN_VBAT_CONTROL) / 1000.0;

  soilData.soil_humidity = analogRead(SOIL_SENSOR);

  Serial.print ("Soil Temperature = ");
  Serial.println (soilData.temp);
  Serial.print ("Soil Humidity = ");
  Serial.println (soilData.soil_humidity);


  //radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData)));
  //radio.send (GATEWAYID, (const void*)(&soilData), sizeof(soilData), requestACK);
  //radio.sleep();
  delay (1000);
}

1885
1885

PostMar 22, 2017#2

Hi walto,

Have you tried to use a voltmeter in parallel with your circuit to see what's showing up?

One important thing to keep in mind is that the T2Utils::readVoltage function will change the Analog Reference to "INTERNAL". In other words the ADC max voltage will be 1.1V. If your soil moisture is returning 1.1V or higher, the ADC will always show 1023.

For the situation above you have two options:
1. Change the ADC reference back to default, which will use the MCU supply voltage as reference (approximately 3.3V):

Code: Select all

analogReference(DEFAULT)
2. Change your sensor to return values between 0V and 1.1V. To do that you need to use a resistor voltage divider.

Please let me know how it goes.

Cheers

4

PostMar 23, 2017#3

Hello,
One important thing to keep in mind is that the T2Utils::readVoltage function will change the Analog Reference to "INTERNAL". In other words the ADC max voltage will be 1.1V. If your soil moisture is returning 1.1V or higher, the ADC will always show 1023.
This is exactly what happened. Thanks for your advice. I added

Code: Select all

analogReference(DEFAULT)
to the function T2Utils::readVoltage in T2Utils.cpp so I can call the readVoltage func and access other analog data without caring about the voltage reference.

Thanks for your help, Mate!

3

PostNov 25, 2017#4

I suggest adding that change (place "analogReference(DEFAULT)" at the end of the function) to the original code so that it is transparent to the user. I was experiencing that problem and spent 2 days to realize what it was the readVoltage function.

Thanks!