반응형

#include <SoftwareSerial.h>
const int LED_PIN = 13; // Pin for the LED
const int RX_PIN = 2; // RX Pin of the Bluetooth module
const int TX_PIN = 3; // TX Pin of the Bluetooth module
SoftwareSerial btSerial(RX_PIN, TX_PIN); // Create a software serial object
void setup() {
pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
btSerial.begin(9600); // Initialize the Bluetooth module with a baud rate of 9600
}
void loop() {
if (btSerial.available()) { // Check if data is available from the Bluetooth module
char data = btSerial.read(); // Read the data from the Bluetooth module
if (data == '1') { // Check if the data received is '1'
digitalWrite(LED_PIN, HIGH); // Turn the LED on
}
else {
digitalWrite(LED_PIN, LOW); // Turn the LED off
}
}
}
이 코드는 SoftwareSerial 라이브러리를 사용하여 Arduino가 RX 및 TX 핀을 사용하여 Bluetooth 모듈과 통신할 수 있도록 하는 소프트웨어 직렬 객체를 생성합니다. 블루투스 모듈로부터 받은 데이터가 '1'인지 확인하여 맞으면 13번 핀에 연결된 LED를 켜고 아니면 LED를 끈다.
Bluetooth 모듈과 LED를 Arduino 보드의 올바른 핀에 연결했는지, 올바른 핀이 코드에 지정되
어 있는지 확인하십시오. 또한 장치를 블루투스 모듈과 페어링하고 장치에서 '1'을 보내면 LED가 켜집니다.
반응형
'TECH' 카테고리의 다른 글
[Arduino] Xbee 통신 예제 코드 (0) | 2023.01.30 |
---|---|
[Arduino] 7 Segments로 Serial Input 받은 숫자 표시하기 (0) | 2023.01.29 |
PWM vs PPM (0) | 2023.01.25 |
UAV trend and shortcomings (0) | 2023.01.25 |
[Linux] apt-get update와 upgrade 차이 (0) | 2023.01.16 |
댓글