Package: haas-python-ina219
HaaS Python介绍HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架，兼容MicroPython编程规范，依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力，从而解决了物联网应用开发难的问题。有了Python轻应用框架，物联网编程不再局限于专业软件开发人员，一般的技术员也可以快速实现复杂的物联网需求。
更多HaaS Python介绍和开发资料见HaaS Python官网,创意案例,硬件积木HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements.
For more HaaS Python introduction and development materials, seeHaaS Python official website,Creative Case,Hardware Building Blocks安装方法在HaaS Python官网下载开发板对应的固件，并完成固件烧录Download the firmware corresponding to the development board atHaaS Python websiteand complete the firmware burning在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题，欢迎在github上向我们提交issue，我们的工程师会及时解答Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us ongithub, Our engineers will answer in timeimport upip
upip.install("haas-python-ina219")
import haas-python-ina219电压电流计 - INA219一、产品简介INA219电压电流计可用于测量负载的电压、电流、功率等参数，可以用于服务器、电脑笔记本、电源、电池管理等电子电路设备提供电流电压值监测提供解决方案。引脚定义VCC：3.3VGND：接地SCL：I2C时钟SDA：I2C数字Vin-：负载输入Vin+：负载输出二、技术参数供电电压：3.3V接口：I2C接口，工作频率高达3.4MHz高度精度：精度范围为0.5%工作温度：-40度～125度支持16个可编程地址总线电压：0-26V支持最大电流：3.2A三、软件接口INA219电压电流计HaaS Python驱动：下载地址INA219(i2cObj) - 创建INA219驱动对象函数原型：ina219Obj = INA219(i2cObj)参数说明：参数类型必选参数？说明i2cObjI2C是调用此函数前需确保i2cObj对象已经处于open状态返回值：
INA219对象成功，返回INA219对象；INA219X对象创建失败，抛出Exception示例代码：importutimeimportina219fromdriverimportI2C# 驱动库i2cObj=I2C()i2cObj.open("ina219")print("ina219 inited!")ina219Dev=ina219.INA219(i2cObj)输出：ina219 inited!getVoltage - 测量电压值函数功能：
测量负载的电压值并且返回状态函数原型：INA219.getVoltage()参数说明：
无返回值：
成功返回电压测量结果（数据类型：浮点型；单位：V）；失败抛出Exception示例：importutimeimportina219fromdriverimportI2C# 驱动库i2cObj=I2C()i2cObj.open("ina219")print("ina219 inited!")ina219Dev=ina219.INA219(i2cObj)whileTrue:print("Bus Voltage:%.3fV"%ina219Dev.getVoltage())utime.sleep(1)输出：No OTA upgrade.
mount fs
 ==== python execute bootpy ====
 ==== python file check /data/pyamp/main.py ====
 ==== python execute from /data/pyamp/main.py ====
ina219 inited!
Bus Voltage: 4.872 V
Bus Voltage: 4.868 V
Bus Voltage: 4.868 V
Bus Voltage: 4.868 VgetCurrent - 测量电流值函数功能：
测量负载的电流值并且返回测量结果函数原型：INA219.getCurrent()参数说明：
无返回值：
成功返回负载电流测量结果（数据类型：浮点；单位：mA）；失败抛出Exception示例：importutimeimportina219fromdriverimportI2C# 驱动库i2cObj=I2C()i2cObj.open("ina219")print("ina219 inited!")ina219Dev=ina219.INA219(i2cObj)whileTrue:print("Current:%.3fmA"%ina219Dev.getCurrent())utime.sleep(1)输出：No OTA upgrade.
mount fs
 ==== python execute bootpy ====
 ==== python file check /data/pyamp/main.py ====
 ==== python execute from /data/pyamp/main.py ====
ina219 inited!
Current: 3.305 mA
Current: 3.402 mA
Current: 3.305 mA
Current: 3.195 mAgetPower - 获取功率值函数功能：
测量负载的功率并且返回测量结果函数原型：INA219.getPower()参数说明：
无返回值：
成功返回负载的功率测量结果（数据类型：浮点；单位：mW）；失败抛出Exception示例：importutimeimportina219fromdriverimportI2C# 驱动库i2cObj=I2C()i2cObj.open("ina219")print("ina219 inited!")ina219Dev=ina219.INA219(i2cObj)whileTrue:print("Power:%.3fmW"%ina219Dev.getPower())utime.sleep(1)输出：No OTA upgrade.
mount fs
 ==== python execute bootpy ====
 ==== python file check /data/pyamp/main.py ====
 ==== python execute from /data/pyamp/main.py ====
ina219 inited!
Power: 15.610 mW
Power: 16.098 mW
Power: 16.098 mW
Power: 15.610 mW四、接口案例此使用实例在board.json中定义了名为ina219的I2C类型的对象。在Python脚本中周期性的获取负载的测量电压、电流、功率结果并打印在日志中。代码：# board.json配置：{"name":"board-name","version":"1.0.0","io":{"ina219":{"type":"I2C","port":0,"addrWidth":7,"freq":400000,"mode":"master","devAddr":64}},"debugLevel":"ERROR","repl":"disable"}# Python代码importutimeimportina219fromdriverimportI2C# 驱动库i2cObj=I2C()i2cObj.open("ina219")print("ina219 inited!")ina219Dev=ina219.INA219(i2cObj)whileTrue:print("Bus Voltage:%.3fV"%ina219Dev.getVoltage())print("Current:%.3fmA"%ina219Dev.getCurrent())print("Power:%.3fmW"%ina219Dev.getPower())utime.sleep(1)输出：No OTA upgrade.
mount fs
 ==== python execute bootpy ====
 ==== python file check /data/pyamp/main.py ====
 ==== python execute from /data/pyamp/main.py ====
ina219 inited!
Bus Voltage: 4.872 V
Current: 3.305 mA
Power: 15.610 mW
Bus Voltage: 4.868 V
Current: 3.402 mA
Power: 16.098 mW
Bus Voltage: 4.868 V
Current: 3.305 mA
Power: 16.098 mW
Bus Voltage: 4.868 V
Current: 3.195 mA
Power: 15.610 mW
Bus Voltage: 4.864 V
Current: 3.195 mA
Power: 16.098 mW五、工作原理INA219传感器寄存器描述如下表所示：序号寄存器地址功能说明默认值10x00配置寄存器0x399F20x01分流电压寄存器--30x02总线电压寄存器--40x03功率寄存器0x0050x04当前寄存器0x0060x05校准寄存器0x00从下图可以看到INA219内置I2C通信单元，与MCU进行数据交互。通过ADC采样输入到数据处理和线性化单元，同时带有校正储存器，处理环境对器件测量的影响。带警报引脚，可通过修改寄存器的值设定阈值，当测量的温湿度超过阈值时它会被置位。引脚分布如下：序号引脚名功能说明1DataI2C数据线2CLKI2C时钟线3GND地线引脚4VCC电源输入引脚5Vin-接负载低电压端引线6Vin+接负载高电压端引线7A0可编程地址线8A1可编程地址线参考文献及购买链接[1]INA219电压电流计传感器介绍[2]INA219电压电流计模块介绍