IOT/PLC

[OpenPLC] 아두이노 우노 PLC 예제

IT 기술자 2023. 11. 17. 11:00

0. 개요

OpenPLC 아두이노에서 digital in/out, analog in/out을 테스트한다.  analog in 값을 analog out에 출력한다. anlog out은 실제로는 PWM 출력이 이루어진다.  이 출력을 확인하기 위해 다른 일반 아두이노에 PWM 값을 읽어 시리얼 출력하는 프로그램을 이용하여 동작을 확인한다.

 

1. 연결

  OpenPLC
아두이노 핀
버튼 PULL UP 저항
10K 옴
LED LED 저항
330 옴
가변 저항
5K 옴 (502)
PWM 읽기
아두이노 핀
버튼 2 1 1        
  5 V   2        
LED 7     +      
        - 1    
  GND       2    
Analog In GND         1  
  A0         2  
  5 V         3  
PWM Out 9           A0
  GND           GND

 
2. OpenPLC Editor 프로그램 작성

 
3. PWM 읽기 아두이노

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

int sensorValue = 0;  // PWM value read from the port
int inputValue = 0;  // 0 ~ 255

void setup() {
  Serial.begin(115200);
  pinMode(analogInPin, INPUT);
}

void loop() {
  sensorValue = pulseIn(analogInPin, HIGH);
  // map it to the range of the analog out:
  inputValue = map(sensorValue, 0, 2016, 0, 254);

  if (inputValue == 0) {
    int val = digitalRead(analogInPin);
    if (val == 1) {
      inputValue = 255;
    }
  }

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t input = ");
  Serial.println(inputValue);

  delay(1000);
}

 

일반 아두이노에서 PWM을 읽은 값을 시리올 모니터로 연결하여 확인한다.

시리얼 모니터로 결과 값을 확인한다. 가변저항을 조정하며 출력 값을 확인한다.

4. 아두이노 연결 사진

digital in : 버튼

digital out : LED

analog in : 가변 저항

analog out : 다른 아두이노 연결

OpenPLC 프로그램에 따라 버튼(digital in)을 누르면 LED(digital out)가 켜진다.