Arduino 模擬讀取4~20ma 電流感測器 並顯示於 1602 液晶螢幕

分享到社群

Arduino 模擬讀取4~20ma 電流感測器 並顯示於 1602 液晶螢幕

工業用感測器領域有很多是用 4-20mA 來呈現數據。所以本段影片我們使用簡單的 Arduino 搭配 4-20mA 信號發生器 與 電流轉電壓模組 4-20mA 轉 0-5V 這兩個模組來實驗。本實驗可以應用於未來實際接觸到 4-20mA 類型的感測器的數據讀取與應用。

所需材料:

接線方式

針對電流產生器的部分

信號輸出兩線轉接到 電流轉電壓模組

以上兩個模組採共用電源 9-24V 皆可.

接線到 Arduino 的腳位

  • Vout 接 A0
  • 此外 Arduino UNO 的 GND 腳位與 電流轉電壓模組 共地
// include the library code:
#include <LiquidCrystal.h>
int Min = 0;    // This is the Min reading of the sensor
int Max = 1023; // This is the Max reading of the sensor
float Desired_Min = 4.0; // this is the desired number that you want converted with min reading of the sensor
float Desired_Max = 20.0; // this is the desired number that you want converted with max reading of the sensor
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {

   lcd.begin(16, 2);              // start the library

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  float Readings = mapf(sensorValue, Min , Max, Desired_Min, Desired_Max);
  // print out the value you read:
  Serial.print(voltage);
  Serial.print("v / Raw:");
  Serial.print(sensorValue);


 
  lcd.setCursor(0,0);
  lcd.print("4-20mA"); // LCD 標題
  lcd.setCursor(9,0);
  lcd.print(char(61)); // 印出 = 的字元
   
  char mAvalue[28];
  dtostrf(Readings, 4, 1, mAvalue);
  Serial.print(" MA:");
  Serial.println(mAvalue);
 
 
  lcd.print (mAvalue);
  lcd.print ("mA");
  lcd.setCursor(0,1);            // move cursor to second line "1" and 9 spaces over
 
  lcd.print(voltage); lcd.print(F("v / "));
  char buffer [8]; // a few bytes larger than your LCD line
  uint16_t raw = sensorValue; // data read from instrument
  sprintf (buffer, "RAW:%04u", raw); // send data to the buffer
  lcd.print (buffer); // display line on buffer
   delay(1000);
    
 
}

double mapf(double val, double in_min, double in_max, double out_min, double out_max) {
    return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}