본문 바로가기

Arduino<>Python

가상 시리얼 RX, TX 통신

arduino uno, nano 보드 D0, D1핀은 시리얼 통신, 팜웨어 업로드의 기능을 갖고 있습니다. 그러므로 다른 용도로 사용하기에는 한계가 있습니다. 

그래서 통신단자를 다른 핀 단자로 이용할 수 있다. 소스에서는 아나로그 단자 A0,A1를 통신단자로 사용한 예이다. 

 

bluetooth 모듈을 연결한 상태에서 해봅니다.

rxPin(A0)-->blue TX핀, txPin(A1)-->blue Rx 

장치관리자에서 통신 포트를 화ㅏㄱ인합니다.

python으로 나노 보드에 내장되어 있는 LED_BUILTIN를 On Off 해 봅니다.

#include<SoftwareSerial.h>
const int rxPin = A0;
const int txPin =A1;
char data;
SoftwareSerial mySerial(rxPin,txPin);
void setup(){
  mySerial.begin(9600);
pinMode(LED_BUILTIN,OUTPUT);
 
  }
  void loop(){
    while (mySerial.available()){
      data =mySerial.read();
    }
      if(data=='a')
      digitalWrite(LED_BUILTIN,HIGH);
     
     
      else if(data=='b')
      digitalWrite(LED_BUILTIN,LOW);
     
      }