INA3221和 ESP8266 6通道电流表
文章目录
- 1.实际效果
- 1.所用材料:
- 2.原理图
- 1. ina3221接线
- 2.屏幕接线
- 2.程序
- 1下载链接
- 3.注意事项
- 1.电流精度0.4mA
- 2.通过设定电阻值来调整准确度
- 3.通过IIC的地址进行多个挂载ina3221
- 4.ina3221 SPEC
- 5.保留显示的小数位数
1.实际效果
ina3221只能达到0.4mA 的精度
1.所用材料:
(1).128x160 的st7735S 驱动芯片的屏幕
(2).nodeMCU
(3).2个ina3221
2.原理图
1. ina3221接线
例子中的地址是0x40 和0x41
spec中的地址配置
2.屏幕接线
别忘了在user_setup.h中修改引脚以及其他选项
2.程序
程序有点乱
//电流传感器头文件
#include <Wire.h>
#include "SDL_Arduino_INA3221.h"//显示相关头文件
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
//显示字体和坐标调整
int textsize =2;
int textsize1 =2;
int x =0;
int y =20;
int xx =30;
int xx1 =110;
int yy =24;// the three channels of the INA3221 named for SunAirPlus Solar Power Controller channels (www.switchdoc.com)
#define LIPO_BATTERY_CHANNEL 1
#define SOLAR_CELL_CHANNEL 2
#define OUTPUT_CHANNEL 3//两个电流传感器,地址分别是40和41,电阻是R100,即0.1ohm;SDL_Arduino_INA3221 ina3221=SDL_Arduino_INA3221(0x41, 0.1);SDL_Arduino_INA3221 ina32211=SDL_Arduino_INA3221(0x40, 0.1);void setup(void)
{//-----------------电流监测初始化----------------Serial.begin(115200);Serial.println("SDA_Arduino_INA3221_Test");Serial.println("Measuring voltage and current with ina3221 ...");ina3221.begin();ina32211.begin();Serial.print("Manufactures ID=0x");int MID;MID = ina3221.getManufID();Serial.println(MID,HEX);//-----------------显示屏初始化----------------tft.init();tft.setRotation(2);tft.fillScreen(TFT_BLACK);tft.setTextSize(1);tft.setTextColor(TFT_RED, TFT_BLACK);tft.drawString(" CH", 0, 0, textsize); tft.drawString(" I(mA)", 30, 0, textsize);tft.drawString(" V(mV)", 80, 0, textsize);tft.setTextColor(TFT_GREEN, TFT_BLACK);}void loop(void)
{//Serial.println("------------------------------");float shuntvoltage1 = 0;float busvoltage1 = 0;float current_mA1 = 0;float loadvoltage1 = 0;busvoltage1 = ina3221.getBusVoltage_V(LIPO_BATTERY_CHANNEL);shuntvoltage1 = ina3221.getShuntVoltage_mV(LIPO_BATTERY_CHANNEL);current_mA1 = -ina3221.getCurrent_mA(LIPO_BATTERY_CHANNEL); // minus is to get the "sense" right. - means the battery is charging, + that it is dischargingloadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000);/*Serial.print("LIPO_Battery Bus Voltage: "); Serial.print(busvoltage1); Serial.println(" V");Serial.print("LIPO_Battery Shunt Voltage: "); Serial.print(shuntvoltage1); Serial.println(" mV");Serial.print("LIPO_Battery Load Voltage: "); Serial.print(loadvoltage1); Serial.println(" V");Serial.print("LIPO_Battery Current 1: "); Serial.print(current_mA1); Serial.println(" mA");Serial.println("");*/float shuntvoltage2 = 0;float busvoltage2 = 0;float current_mA2 = 0;float loadvoltage2 = 0;busvoltage2 = ina3221.getBusVoltage_V(SOLAR_CELL_CHANNEL);shuntvoltage2 = ina3221.getShuntVoltage_mV(SOLAR_CELL_CHANNEL);current_mA2 = -ina3221.getCurrent_mA(SOLAR_CELL_CHANNEL);loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000);/*Serial.print("Solar Cell Bus Voltage 2: "); Serial.print(busvoltage2); Serial.println(" V");Serial.print("Solar Cell Shunt Voltage 2: "); Serial.print(shuntvoltage2); Serial.println(" mV");Serial.print("Solar Cell Load Voltage 2: "); Serial.print(loadvoltage2); Serial.println(" V");Serial.print("Solar Cell Current 2: "); Serial.print(current_mA2); Serial.println(" mA");Serial.println("");
*/float shuntvoltage3 = 0;float busvoltage3 = 0;float current_mA3 = 0;float loadvoltage3 = 0;busvoltage3 = ina3221.getBusVoltage_V(OUTPUT_CHANNEL);shuntvoltage3 = ina3221.getShuntVoltage_mV(OUTPUT_CHANNEL);current_mA3 = ina3221.getCurrent_mA(OUTPUT_CHANNEL);loadvoltage3 = busvoltage3 + (shuntvoltage3 / 1000);/*Serial.print("Output Bus Voltage 3: "); Serial.print(busvoltage3); Serial.println(" V");Serial.print("Output Shunt Voltage 3: "); Serial.print(shuntvoltage3); Serial.println(" mV");Serial.print("Output Load Voltage 3: "); Serial.print(loadvoltage3); Serial.println(" V");Serial.print("Output Current 3: "); Serial.print(current_mA3); Serial.println(" mA");Serial.println("");Serial.println("------------------------------33333333333333333------------------------------------------------");
*/ //-----------------------------------------------------------------------------------------------------------float shuntvoltage4 = 0;float busvoltage4 = 0;float current_mA4 = 0;float loadvoltage4 = 0;busvoltage4 = ina32211.getBusVoltage_V(LIPO_BATTERY_CHANNEL);shuntvoltage4 = ina32211.getShuntVoltage_mV(LIPO_BATTERY_CHANNEL);current_mA4 = ina32211.getCurrent_mA(LIPO_BATTERY_CHANNEL);loadvoltage4 = busvoltage4 + (shuntvoltage4 / 1000);float shuntvoltage5 = 0;float busvoltage5 = 0;float current_mA5 = 0;float loadvoltage5 = 0;busvoltage5 = ina32211.getBusVoltage_V(SOLAR_CELL_CHANNEL);shuntvoltage5 = ina32211.getShuntVoltage_mV(SOLAR_CELL_CHANNEL);current_mA5 = ina32211.getCurrent_mA(SOLAR_CELL_CHANNEL);loadvoltage5 = busvoltage5 + (shuntvoltage5 / 1000);float shuntvoltage6 = 00.00;float busvoltage6 = 0;float current_mA6 = 0;float loadvoltage6 = 0;busvoltage6 = ina32211.getBusVoltage_V(OUTPUT_CHANNEL);shuntvoltage6 = ina32211.getShuntVoltage_mV(OUTPUT_CHANNEL);current_mA6 = ina32211.getCurrent_mA(OUTPUT_CHANNEL);loadvoltage6 = busvoltage6 + (shuntvoltage6 / 1000);//--------------------------------负变正---------------------------------------------------------------------------if (current_mA1 < 0) current_mA1=-current_mA1;
if (current_mA2 < 0) current_mA2=-current_mA2;
if (current_mA3 < 0) current_mA3=-current_mA3;
if (current_mA4 < 0) current_mA4=-current_mA4;
if (current_mA5 < 0) current_mA5=-current_mA5;
if (current_mA6 < 0) current_mA6=-current_mA6;//--------------------------------串口输出---------------------------------------------------------------------------Serial.print(current_mA1); Serial.print(" ");Serial.print(current_mA2); Serial.print(" ");Serial.print(current_mA3); Serial.print(" ");Serial.print(current_mA4); Serial.print(" ");Serial.print(current_mA5); Serial.print(" ");Serial.print(current_mA6); Serial.print(" ");Serial.println(" ");//----------------------------------显示屏输出------------------------------------------------------------------tft.drawString("CH1", x, y, textsize1); tft.drawFloat(current_mA1, 2,x+xx, y, textsize1); tft.drawFloat(shuntvoltage1,2, x+xx1, y, textsize1);tft.drawString("CH2", x, y+yy, textsize1); tft.drawFloat(current_mA2, 2,x+xx, y+yy, textsize1); tft.drawFloat(shuntvoltage2,2, x+xx1,y+yy, textsize1);tft.drawString("CH3", x, y+2*yy, textsize1); tft.drawFloat(current_mA3,2, x+xx, y+2*yy, textsize1); tft.drawFloat(shuntvoltage3, 2,x+xx1, y+2*yy, textsize1);tft.drawString("CH4", x, y+3*yy, textsize1); tft.drawFloat(current_mA4, 2,x+xx, y+3*yy, textsize1); tft.drawFloat(shuntvoltage4,2, x+xx1, y+3*yy, textsize1);tft.drawString("CH5", x, y+4*yy, textsize1); tft.drawFloat(current_mA5, 2,x+xx, y+4*yy, textsize1); tft.drawFloat(shuntvoltage5, 2,x+xx1, y+4*yy, textsize1);tft.drawString("CH6", x, y+5*yy, textsize1); tft.drawFloat(current_mA6, 2,x+xx, y+5*yy, textsize1); tft.drawFloat(shuntvoltage6, 2,x+xx1, y+5*yy, textsize1);delay(500);//tft.fillScreen(TFT_BLACK);
}
1下载链接
百度云链接
提取码:8mer
3.注意事项
1.电流精度0.4mA
2.通过设定电阻值来调整准确度
//两个电流传感器,地址分别是40和41,电阻是R100,即0.1ohm;
SDL_Arduino_INA3221 ina3221=SDL_Arduino_INA3221(0x41, 0.1);
SDL_Arduino_INA3221 ina32211=SDL_Arduino_INA3221(0x40, 0.1);
3.通过IIC的地址进行多个挂载ina3221
4.ina3221 SPEC
链接
5.保留显示的小数位数
有时候我们需要显示2个精度的小数,但是当从显示19.88到9.88时候,因为刷新的问题会导致字符叠加,
解决方法:
1.可以设置在loop函数中增加tft.fillscreen(BLACK)
但是这样会使屏幕闪烁
2.可以通过控制整个显示的位数,比如
8.8000 18.800 188.00
这都是5个位的,因此不会存在字符叠加的问题
程序设计:
在上述程序中的drawfloat方法中,进行修改
方框中是我增加的几行数据,在tft_espi.cpp中修改的