Getting Started with RN42 Bluetooth Module
Total Page:16
File Type:pdf, Size:1020Kb
Getting Started with RN42 Bluetooth Module Purpose Purpose Quick Links Useful Documentation The RN42 is a Class 2 Hardware Description certified Bluetooth module Setting Up Your Terminal from Microchip. It RN42 UART Commands includes an embedded Connecting to Remote Devices Bluetooth stack that Authentication Setting supports SPP and DUN Automatic Connect Bluetooth profiles. A Manual Connect special version of Sending Wireless Data firmware also supports Manipulating GPIO HID Bluetooth profile. A Restoring Factory Settings PCB trace antenna was Hardware Restore included on the module to Software Restore eliminate the need for Contact the Author antenna design and castellated edges make the module simple to solder onto any baseboard. Any host device can interface with the module through a simple UART interface. The RN42 supports baud rates up to 3Mbps and SPP data rates up to 300kbps (master). This page is meant to help familiarize you with these modules. You will find some useful links and downloads below, as well as some tutorials and design considerations on various features of the module. The RN-42-EK board was used as a simple evaluation board for the module. Quick Links Useful Documentation RN42-I/RM - Class 2 certified Bluetooth module with PCB RN42 Datasheet trace antenna RN-42-SM Datasheet RN-42-EK - Eval board for RN42 module RN42 Command Reference Guide RN42 Landing Page - Microchip online product page Tera Term - Free terminal emulator software for serial communication with PC Hardware Description The RN-42-EK board was used to evaluate the RN42 module. If your computer has a built-in Bluetooth adapter, you can use that to communicate with a single RN42 module. If you don't have a built-in Bluetooth adapter in your computer, you can still experiment with the RN42...you'll just need two modules. You can connect the module(s) to your computer using a hard- wired connection. I would recommend using the RN-42-EK board to evaluate the module since it comes with an on-board USB interface. You don't need to worry about interfacing with any USB/UART bridge cable. The RN-42-EK comes with everything RN-42-EK Pin Assignments you need to start prototyping with the RN42. Here are some additional items you might find useful: Helpful Accessories 2mm, 1x12 male header: 951112- 8622-AR 2mm, 1x12 female R/A header: 95051 2-5002-AR 0.1", 1x6 male header: S1011EC-06- ND Setting Up Your Terminal Once you have your module hard-wired to your computer, you need to setup your terminal software with the proper settings to properly communicate over UART. There are many free terminal programs available like Tera Term or RealTerm. If you are running Windows XP, you will have HyperAccess installed by default. Tera Term is my terminal emulator of choice. It supports multiple serial communication protocols, file transfer protocols, and also has a built in scripting language. For the purposes of this page, we are just interested in simple UART communication, so I will show you how to configure Tera Term to communicate with these modules. 1. Download Tera Term (link) 2. Open a Tera Term window 3. Make sure the RN42 is properly connected to your computer and that it is powered on 4. Select the "Setup" dropdown from the Tera Term menu bar 5. Click on "Serial Port" 6. Select the port corresponding to whatever bridge device you are using. You may need to wait for the USB driver to finish installing. 7. Use the following serial settings: Baud: 115,200 (use 9600 if jumper 4 is set) Data Bits: 8 Parity: None Stop Bits: 1 Flow Control: None 8. You can leave transmit delay set to zero. 9. Press OK to apply your changes. You should be ready to communicate with your module over UART. To test it out, type the characters '$$$' and you should get 'CMD' as a response. If not, double check your connections and serial settings. Make sure all the Ground pins are connected! If you want to see every character that you type, you can turn on local echo by selecting "Setup" from the menu bar and clicking on "Terminal". The "Terminal Setup" dialog box has a check box for enabling/disabling local echo. Also, within the "Terminal Setup" dialog box, I like to set the New-line Receive setting to CR+LF. That way when you send a command to the RN42, it will send a newline before displaying a response. RN42 UART Commands I know you want to start wireless transmission right away without even having to think, but just tap the brakes for a minute. Let's take a quick look at some of the UART commands as they will be involved in later sections. There are four types of UART commands for the RN42: Get Commands: Used for reading configuration Set Commands: Used for setting configuration. Need to reboot or power-cycle the device for "Set" commands to take effect. Change/Action Commands: Used for temporarily changing settings or causing immediate actions GPIO Commands: Used for manipulating I/O pins The module must be put in Command Mode in order to properly receive commands. This is accomplished by sending '$$$' over UART. You should get 'CMD' as a response indicating the module is in Command Mode. Type 'X' and press <enter> to view all of the current settings for the module. To exit Command Mode, send three minus signs and press enter (- - -<enter>). The image shown to the right displays the serial readout after sending the 'X' command. We can see the firmware revision and date loaded at the top. Then we see the General Settings section which includes the module's Bluetooth address, name, UART baudrate, mode of operation, authentication setting, 4-digit pin code, whether or not bonding is enabled, and the remote address stored in memory. Next, we see the Advanced Settings which are specific to the Bluetooth standard and how you want your link setup. We won't mess with any Advanced settings on this page. At the bottom of the readout, we see the Other Settings section. This is where you can set the Bluetooth profile, CMD character, enable sniff mode, enable low power mode, change the TX power, set/read I/O pins, set/read the sleep timer, enable Debug mode, and switch roles. I recommend reading through all of the available commands in the Command Reference Guide. For now, let's just touch on a few important ones. Set Syntax: SA,<value><enter> Get Syntax: GA<enter> Factory default authentication setting is '1' which requires a host device to verify it wishes to connect to a remote device. For auto-connect feature to work, Authentication needs to be set to '0' or '4'. Example: Set Authentication Setting to Open Mode Set Syntax: SM,<value><enter> Get Syntax: GM<enter> Read or set the current mode of operation. By default, the RN42 is set as a slave device (0). Example: Set Module to Master Mode (1) Set Syntax: SN,<string><enter> Get Syntax: GN<enter> Read current name or set a unique name for the Bluetooth device. Example: Set module name to 'MyDevice' Set Syntax: SR,<value><enter> Get Syntax: GR<enter> Read or set the address of the remote device you intend to communicate with. Example: Set Remote Address to '00066663D0BF' Set Syntax: SU,<value><enter> Get Syntax: GU<enter> Change the UART baud rate Example: Change UART baud rate to 57,600 bps Set Syntax: SY,<value><enter> Get Syntax: GY<enter> Read or set the transmit power for the RN42 Need to reboot module for setting to take effect Example: Set Transmit Power to -5 dBm Set Syntax: SF,1<enter> Get Syntax: none Restores all settings to factory default values except for the module name Example: Restore Factory Settings Syntax: IN<value1>,<optional value2><enter> <value1> is the scan time in seconds <value2> is the desired COD you are scanning for and is an optional parameter Example: Perform a 10 Second Scan for Any Bluetooth Device Connect to Known Address Syntax: C,<address><enter> Connect to Remote Address Stored in Memory Syntax: C<enter> Displays "Trying" after sending the Connect command Can check connection status using GK command (see below) Example: Connect to Address 000666674655 Get Syntax: GK<enter> Returns 1,0,0 if connected Returns 0,0,0 if not connected Example: Check Connection Status Syntax: R,1<enter> Perform software reboot of the device Device needs to be rebooted after changing configuration for the changes to be applied. Example: Reboot the Device Set Syntax: S@,<hex value><enter> Get Syntax: G@<enter> <hex value> is a 2-byte value First byte of <hex value> contains the GPIO mask (only pins set to '1' will be written) Second byte of <hex value> contains the value to write to the pin (1=output, 0=input) This setting will be lost when power is removed Example: Set GPIO3 as input and GPIO4 as output Set Syntax: S&,<hex value><enter> Get Syntax: G& <hex value> is a 2-byte value First byte of <hex value> contains the GPIO mask (only pins set to '1' will be written) Second byte of <hex value> contains the value to write to the pin For outputs, a value of 1 will set the pin level high, a value of 0 will set the pin level low. For inputs, a value of 1 will set the pull-up resistor, a value of 0 will set the pull-down resistor. Example: Set GPIO2 high level (assuming GPIO2 is configured as output ahead of time) Set Syntax: S*,<hex value><enter> Get Syntax: G*<enter> <hex value> is a 2-byte value First byte of <hex value> has no effect Second byte of <hex value> contains the value to write to the pin (lsb corresponds to GPIO8) GPIO8~11 can only be used as outputs, but G* can still be used to read the current value A value of 1 will set the pin level high, a value of 0 will set the pin level low.