프로그래밍

[Arduino] BLE(HM-10) 제어하기

*%$@$#@ 2023. 1. 11. 00:45
728x90
반응형

블루투스를 이용하면 유선을 벗어나 아두이노의 활용 반경을 획기적으로 개선할 수 있습니다. 

 

RFID와 BLE모듈을 Arduino에 연결하는 방법에 대해 정리해 보겠습니다. 

 

오늘 활용할 모듈은 다음과 같습니다. 

- Arduino UNO : 1개 

- RFID-RC522 : 1개 

- BLE(HM-10) : 1개

 

먼저 HM-10 BLE 모듈과 Arduino UNO의 연결 회로도는 다음과 같습니다. 

 

https://rockjjy.tistory.com/2640

MH-10은 총 6개의 핀으로 구성되어 있지만 이 중 가운데 네 개의 핀만 연결하여 사용합니다. 

 

다음으로 RFID 모듈을 연결해 보겠습니다. 아래의 배선도를 참고하여 RC-522 모듈과 아두이노를 연결합니다. RC-522은 3.3V의 전압을 필요로 하기 때문에 MH-10 블루투스 모듈과 전원을 분리해서 사용하게 됩니다. 

 

https://lastminuteengineers.com/how-rfid-works-rc522-arduino-tutorial/

 

저는 위 회로도에서 5번에 연결된 RST를 9번에 연결하여 사용하였습니다.

 

아두이노 프로그래밍은 다음과 같이 작성하였습니다.

 

#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>
SoftwareSerial HM10(2, 3); // RX = 2, TX = 3

#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
byte nuidPICC[4];



int in = A0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  HM10.begin(9600);
  pinMode(in, INPUT);
  
  while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
	SPI.begin();			// Init SPI bus
	rfid.PCD_Init();		// Init MFRC522
	delay(4);				// Optional delay. Some board do need more time after init to be ready, see Readme
	
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.print(F("Using the following key:"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}

void loop() {
  // put your main code here, to run repeatedly:
  // float val = analogRead(in);

  // float val2 = val/205;

  // Serial.println(val2);
  // delay(400);

  

  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

	// Serial.print(F("PICC type: "));
  // MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  // Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  // if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
  //   piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  //   piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  //   Serial.println(F("Your tag is not of type MIFARE Classic."));
  //   return;
  // }

  // if (rfid.uid.uidByte[0] != nuidPICC[0] || 
  //   rfid.uid.uidByte[1] != nuidPICC[1] || 
  //   rfid.uid.uidByte[2] != nuidPICC[2] || 
  //   rfid.uid.uidByte[3] != nuidPICC[3] ) {
  //   Serial.println(F("A new card has been detected."));

  //   // Store NUID into nuidPICC array
  //   for (byte i = 0; i < 4; i++) {
  //     nuidPICC[i] = rfid.uid.uidByte[i];
  //   }
   
  //   Serial.println(F("The NUID tag is:"));
  //   Serial.print(F("In hex: "));
  //   printHex(rfid.uid.uidByte, rfid.uid.size);
  //   Serial.println(rfid.uid.size);
  //   Serial.println();
  //   Serial.print(F("In dec: "));
  //   printDec(rfid.uid.uidByte, rfid.uid.size);
  //   Serial.println();
  // }
  // else Serial.println(F("Card read previously."));

  Serial.print(F("In hex: "));
  HM10.print(F("In hex: "));  
  printHex(rfid.uid.uidByte, rfid.uid.size);
  Serial.println();
  HM10.println();
  // Serial.print(F("In dec: "));
  // printDec(rfid.uid.uidByte, rfid.uid.size);
  // Serial.println();



  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}


/**
 * Helper routine to dump a byte array as hex values to Serial. 
 */
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? "0" : "");
    HM10.print(buffer[i] < 0x10 ? "0" : "");
    Serial.print(buffer[i], HEX);
    HM10.print(buffer[i], HEX);
  }
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? "0" : "");
    HM10.print(buffer[i] < 0x10 ? "0" : "");
    Serial.print(buffer[i], DEC);
    HM10.print(buffer[i], DEC);
  }
}

 

 

 

728x90
반응형