Monday, March 24, 2014

Getting Started with the RN42 Bluetooth Module

In this video post tutorial we go over the basics of using the RN42 Bluetooth module. This tiny but capable Bluetooth module makes it easy to add wireless capability to any project or design. Topics covered include:
  • Connecting to and communicating with the RN42 wirelessly 
  • Using the RN42 in command mode to change settings
  • Wireless communication with an Arduino Uno using the RN42


RN42 Tutorial Schematics
Basic Setup with Serial Pins Shorted Together
RN42 Connected to Arduino Uno
Communicating with an Arduino wirelessly using the RN42 Bluetooth module
/*This Arduino Uno sketch was used to communicate with an Arduino Uno wirelessly using a serial terminal and the RN42 Bluetooth module. This code is free and open for anyone to use*/

void setup() {
  //set baud rate to match BT module
  Serial.begin(115200);
}

void loop() {

  String t; //string to hold data from BT module
  while(Serial.available()) { //keep reading bytes while they are still more in the buffer
    t += (char)Serial.read(); //read byte, convert to char, and append it to string
  }

  if(t.length()) { //if string is not empty do the following

    if(t == "Hi Uno\r\n") { Serial.print("Hello Neil\n"); } //say hello
    else if(t == "Meaning of life?\r\n") { //find out the meaning of life
      delay(1000);
      Serial.print("Money. ");
      delay(1000);
      Serial.print("Guns. ");
      delay(1000);
      Serial.print("Hoes.\n");
   }
    else { Serial.print("Syntax Error\n"); } //send this for any other string
   }
   delay(20);
}