en-us_commands:reference:ianalogread2
Table of Contents
IanalogRead2()
IanalogRead2() converts the 4~20mA input value to the specified integer range.
int32_t IanalogRead2(uint8_t ch, int32_t min, int32_t max)
Commands
| Command | Arguments | Return Value | Operation |
|---|---|---|---|
IanalogRead2(ch, min, max) | uint8_t ch — MPINO: analog pin such as A0; MPAINO: overall module channel number starting at 0int32_t minint32_t max | int32_t | Converts the 4~20mA input value to the specified integer range. |
Example
This example scales the MPINO-8A4R(T)-S 4–20mA input A2 to 0–100%.
void setup() { Serial.begin(115200); } void loop() { const int32_t percent = IanalogRead2(A2, 0, 100); // Scale the A2 4–20mA input to an integer percentage. if (percent == ANALOG_READ2_ERROR) { Serial.println(F("4-20mA input error")); } else { Serial.print(F("A2: ")); Serial.println(percent); } delay(500); }
Precautions
- In this example,
A2is a current input on MPINO-8A4R(T)-S. For another product, check its pin map for supported 4–20mA channels and wiring. - An input of approximately 3.6~4.0mA returns the normal value
0; inputs below 3.6mA return an error value. - Error values help detect open circuits. The user must implement final alarms and safety actions in the program.
IanalogRead2()andIanalogRead2f()also return65535on error, rather than a user-range value. Check for errors before using the result.- The full
int32_trange is available formin~maxinIanalogRead2(), but if the normal range includes65535, it cannot be distinguished from an error value. analogRead2Avgf()uses state and a buffer separate fromanalogRead2Avg(). Using both increases SRAM usage.- If
samplesis0or memory allocation fails inanalogRead2Avgf(), it returns the current valid corrected value as afloat. - On MPINO, do not use
analogRead2()on voltage/NTC channels that are not 4~20mA inputs. - For MPINO products requiring
analogReference(EXTERNAL), set it first insetup(). IanalogWrite2()andIanalogWrite2f()are convenience outputs dedicated to 4~20mA. Avoid alternating them withIanalogWrite()on the same channel.- The analog output circuit requires an external 12~24V supply. USB power alone may not provide normal output.
- Verify actual current input/output with measuring instruments after preparing a test product.
Built-in Commands in the Same Category
en-us_commands/reference/ianalogread2.txt · Last modified: by 127.0.0.1
