User Tools

Site Tools


en-us_commands:reference:ianalogwrite2

This is an old revision of the document!


IanalogWrite2()

IanalogWrite2() converts a user integer value to the 4~20mA output range.

IanalogWrite2 command operation flow

void IanalogWrite2(uint8_t ch, int32_t min, int32_t max, int32_t value)

Commands

Command Arguments Return Value Operation
IanalogWrite2(ch, min, max, value) uint8_t ch
int32_t min
int32_t max
int32_t value
void Converts a user integer value to the 4~20mA output range.

Example

void setup() {
  Serial.begin(115200);
}
 
void loop() {
  uint16_t raw2 = analogRead2(0);  // Read the 4–20mA-adjusted input value of the specified channel.
  int32_t filtered2 = analogRead2Avg(0, 5);  // Return the integer average of 4–20mA input values.
  float filtered2f = analogRead2Avgf(0, 5);  // Return the floating-point average of 4–20mA input values.
  float currentmA = IanalogRead2f(0, 4.0F, 20.0F);  // Map the 4–20mA input value to the specified floating-point range.
 
  if (raw2 == ANALOG_READ2_ERROR || filtered2 == ANALOG_READ2_AVG_ERROR ||
      filtered2f == (float)ANALOG_READ2_AVG_ERROR ||
      currentmA == (float)ANALOG_READ2_ERROR) {
    Serial.println("4-20mA input error");
    delay(500);
    return;
  }
 
  int32_t percent = Iscale((int32_t)raw2, 0, 32767, 0, 100);  // Map an integer input to the specified integer range.
 
  IanalogWrite2(0, 0, 100, 50);       // 50% → approx. 12mA
  IanalogWrite2f(1, 0.0F, 10.0F, 2.5F); // 25% → approx. 8mA
 
  Serial.print("4-20mA raw: ");
  Serial.println(raw2);
  Serial.print("4-20mA filtered: ");
  Serial.println(filtered2);
  Serial.print("4-20mA filtered float: ");
  Serial.println(filtered2f, 2);
  Serial.print("percent: ");
  Serial.println(percent);
  Serial.print("current: ");
  Serial.println(currentmA, 2);
 
  delay(500);
}

Precautions

  • Match the module DIP switches, external power supply and output range.
  • Do not configure these together with PWM functions sharing the same timer.
  • Do not output values to uninitialized channels.
  • Currently, MPAINO Y analog output supports channels up to 0~11; do not use 12~15.
  • analogWriteInit(), analogWriteFreq() and analogWriteMax() are for Y analog output channels and must not be used for high-speed pulse channels 20~31.
  • Do not use high-speed pulse GPIO numbers 130~141 with analogWrite().

← Built-in Command List

en-us_commands/reference/ianalogwrite2.1788791733.txt.gz · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki