First off: Although I've done several projects and this one is not even the most complex, I am not much of a hardware guy--I'm still trying to figure out most of this stuff!
The project is a simple switch box that controls three different voltages: 12V, 15V, and 24V sources. The 12 and 24 volt circuits are switched through relays. The 15V circuit is always on and provides power to the microcontroller and other devices via a DC buck converter. Expected loads are about 3.5 amps on the 24V, 2 amps on the 15V, and usually 2 to 4 amps on the 12V, but up to 7 amps in certain configurations. The measurements on all three sensors bounce around by almost 2V. The current readings bounce around also by up to about 0.5 amps. The current readings when no load is attached to the 12V and 24V circuits are large: around 5 amps on the 12V and around 10 amps on the 24V. I read the data sheet and have tried adjusting the conversion times and averaging counts since I really only need to read the sensors once every half second or so. This did not make a difference.
I suspect my problem is the circuit itself. I really don't understand high side vs low side for power sensing. I think I am measuring before the load, so I am using the high side configuration. The examples in the tutorial that the negative side of the circuit to be measures should be tied to the microcontroller ground. I suspect that this is where my problem is. I simply tied all of the grounds together at the incoming connector and routed that to the microcontroller's ground. So three separate circuits now share a ground. Is this the correct way to do that? If not, how should that be done--how does one use multiple sensors to measure the voltage and current of independent circuits?
The microcontroller is a Teensy 4.0. Not connected in the Fritzing diagram (attached) are two buttons, a TFT display, and an ESP32 breakout to allow switch control and voltage/current monitoring via WIFI. These do not affect the sensors and trying to get all of their wires where they need to be makes the Fritzing diagram unreadable.
Here is the class for sensor operations. It really is basically the example program encapsulated to make it easy to control 3 sensors.
Sensor.h:
Code: Select all
#ifndef __SENSOR__H
#define __SENSOR__H
#include <stdint.h>
#include <Adafruit_INA260.h>
#include <Diag.h>
// Global Constants ************************************************************
//******************************************************************************
class Sensor : public Adafruit_INA260
{
public:
Sensor();
bool begin(uint8_t i2c_addr = INA260_I2CADDR_DEFAULT, TwoWire *theWire = &Wire);
bool update(void);
char* voltageValue(void);
char* currentValue(void);
void status(float* volts, float_t* amps);
private:
void _read_sensor(float* volts, float* amps);
bool _sensor_OK;
float _volts;
float _amps;
char _voltageValue[8];
char _currentValue[8];
};
#endif // __SENSOR__H
Code: Select all
#include "Sensor.h"
//******************************************************************************
Sensor::Sensor() : Adafruit_INA260()
{
return;
}
//******************************************************************************
bool Sensor::begin(uint8_t i2c_addr, TwoWire *theWire)
{
_sensor_OK = Adafruit_INA260::begin(i2c_addr, theWire);
setCurrentConversionTime(INA260_TIME_1_1_ms);
setVoltageConversionTime(INA260_TIME_1_1_ms);
setAveragingCount(INA260_COUNT_4);
if (_sensor_OK)
_read_sensor(&_volts, &_amps);
return (_sensor_OK);
}
//******************************************************************************
void Sensor::_read_sensor(float* volts, float* amps)
{
*volts = readBusVoltage() / 1000.0;
*amps = readCurrent() / 1000.0;
diag(3, "SENSOR::read_sensor() - volts: %8.4f amps: %8.4f\n", *volts, *amps);
return;
}
//******************************************************************************
bool Sensor::update(void)
{
if (_sensor_OK == false)
return false;
_read_sensor(&_volts, &_amps);
return true;
}
//******************************************************************************
char* Sensor::voltageValue()
{
if (_sensor_OK == false)
return (char*) "--.-";
snprintf(_voltageValue, sizeof(_voltageValue), "%4.1f V", _volts);
return _voltageValue;
}
//******************************************************************************
char* Sensor::currentValue()
{
if (_sensor_OK == false)
return (char*) "--.-";
snprintf(_currentValue, sizeof(_currentValue), "%4.1f A", _amps);
return _currentValue;
}
//******************************************************************************
void Sensor::status(float* volts, float* amps)
{
*volts = _volts;
*amps = _amps;
return;
}
Thanks - Shane