AIN4-20mA Unit

The following products are supported:

AIN4_20MAUnit

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from unit import AIN4_20MAUnit
11
12
13title0 = None
14label0 = None
15label1 = None
16i2c0 = None
17ain4_20ma_0 = None
18
19
20def setup():
21    global title0, label0, label1, i2c0, ain4_20ma_0
22
23    M5.begin()
24    Widgets.fillScreen(0x222222)
25    title0 = Widgets.Title("AIN 4-20mA Unit Test", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
26    label0 = Widgets.Label("CH1 Current:", 1, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27    label1 = Widgets.Label("CH1 ADC:", 1, 96, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28
29    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
30    ain4_20ma_0 = AIN4_20MAUnit(i2c0, 0x55)
31    ain4_20ma_0.set_cal_current(20)
32
33
34def loop():
35    global title0, label0, label1, i2c0, ain4_20ma_0
36    M5.update()
37    label0.setText(str((str("CH1 Current:") + str((ain4_20ma_0.get_current_value())))))
38    label1.setText(str((str("CH1 ADC:") + str((ain4_20ma_0.get_adc_raw_value())))))
39
40
41if __name__ == "__main__":
42    try:
43        setup()
44        while True:
45            loop()
46    except (Exception, KeyboardInterrupt) as e:
47        try:
48            from utility import print_error_msg
49
50            print_error_msg(e)
51        except ImportError:
52            print("please update to latest firmware")

UIFLOW2 Example:

example.png

ain4_core2_example.m5f2

class AIN4_20MAUnit

Constructors

class AIN4_20MAUnit(i2c, address)

Init I2C port & UNIT AIN 4-20mA I2C Address.

参数:
  • i2c (I2C) – I2C port to use.

  • address (int|list|tuple) – I2C address of the Unit AIN4-20mA.

UIFLOW2:

init.png

Methods

AIN4_20MAUnit.get_adc_raw_value() int

Retrieves the raw ADC value from the channel.

返回:

Raw ADC value as a 12-bit integer.

UIFLOW2:

get_adc_raw16_value.png

AIN4_20MAUnit.get_current_value() float

Retrieves the current value (in mA) from the channel.

返回:

Current value in milliamperes (mA).

UIFLOW2:

get_4_20ma_current_value.png

AIN4_20MAUnit.set_cal_current(val)

Sets the calibration current for the specified channel.

参数:

val (int) – The calibration current value, ranging from 4 to 20 mA.

UIFLOW2:

set_cal_current.png

AIN4_20MAUnit.get_firmware_version() int

Retrieves the firmware version of the AIN 4-20mA unit.

返回:

Firmware version.

UIFLOW2:

get_firmware_version.png

AIN4_20MAUnit.get_i2c_address() str

Retrieves the current I2C address of the AIN 4-20mA unit.

返回:

I2C address as a string in hexadecimal format.

UIFLOW2:

get_i2c_address.png

AIN4_20MAUnit.set_i2c_address(addr)

Sets a new I2C address for the AIN 4-20mA unit.

参数:

addr (int) – The new I2C address, must be between 0x08 and 0x77.

UIFLOW2:

set_i2c_address.png