Setting a PIN Code on the AT-09 BLE module

Yassine Benabbas
6 min readMar 21, 2017

--

The AT-09 module is a Bluetooth Low Energy (BLE) device that can be configured using serial communication. It is also very similar to the HM-10 module and is also compatible with it.

BLE is a protocol that is energy efficient and simple to use. For example, you don’t need to manually pair a BLE device from your OS Bluetooth settings. However, this also means that anyone can connect to a BLE device. This can turn to be annoying if unwanted devices connect to your peripheral. Hopefully, the AT-09 supports setting a PIN code which limits access to only pairs that enter the correct code.

iOS PIN code dialog,

In a previous post, I showed how to configure the AT-09 and use it with an Arduino. This time, I’ll try to explain how to setup a PIN code using a usb-to-serial adapter. Of course, all this can also be achieved using an Arduino instead of the usb to serial adapter. So, if you are interested in only the PIN part, you can skip directly to it :).

First let’s start by setting the usb to serial adapter.

The usb-to-serial adapter

PL2303 USB-To-RS232-TTL Converter Adapter Module with PL2303

I bought a cheap adapter from Aliexpress https://www.aliexpress.com/item/WholeSale-PL2303-USB-To-RS232-TTL-Converter-Adapter-Module-with-PL2303HX/1790718913.html. Even if the adapter looks creepy without any covers, it gets the job done :).

This one has a USB adapter and 6 pins; a 5V PIN, a 3.3V PIN, a GND pin, a Tx pin and a Rx pin. The USB connector goes to the USB port of your computer and the pins connect with AT-09 ones.

In order to control the USB to serial adapter from your computer, we need two things: the drivers of the USB to serial adapter and a serial terminal.

Let’s start with the drivers, we need to download and install the PL2303 drivers from this link: http://www.prolific.com.tw/us/ShowProduct.aspx?pcid=41&showlevel=0041-0041.

Concerning the serial terminal, many tools are available. Since I have a macOS, I will be going on with CoolTerm. It can be downloaded here: https://freeware.the-meiers.org/.

CoolTerm’s main window

Now we are going to test the USB adapter. After installing the drivers, plug the adapter to a USB port and then run CoolTerm. Next, click on “options”. Choose usbserial from the dropdown and click ok. After that, click on “Connect” and that’s it, your computer has a serial interface :).

Configuring the serial port on CoolTerm

We are now ready to connect the AT-09 with the adapter.

Connecting the AT-09 with the usb-to-serial adapter

The connection is not very complicated. First, unplug the adapter from your computer and link the pins as indicated below:

Adapter +5V <-> AT-09 VCC

Adapter GND <-> AT-09 GND

Adapter RXD <-> AT-09 TXD (the adapter receives what the AT-09 sends)

Adapter TXD <-> AT-09 RXD (the adapter sends and the AT-09 receives)

Connecting the AT-09 with the usb-to-serial adapter. The adapter is ont the bottom tright side and the BLE module is in the top left corner

Next, plug the adapter back to your computer, launch CoolTerm and configure the usbserial as indicated in the previous chapter. Click on connect on the main toolbar of CoolTerm. Some lights on the bottom right corner of the window will turn green and you should see the “connected” word on the bottom left corner.

Yay ! we are connected

Great, let’s keep on rocking by sending a test command to the AT-09 module. In the main menu of CoolTerm, click on “Connection” -> “Send string”.

The send String menu

A new window that allows to send commands will appear. Let’s send the most basic command: “AT” and add a line break in the input box (the line break is important). Click on send to ask the USB to serial adapter to send the command to AT-09 module. You should see a “OK” reply in the main window.

AT command and OK reply

The stage is now set for our final main event; setting a PIN code and testing it.

Setting a PIN code and testing it

In order to set a PIN code, we need two commands from the hm-10 datasheet, namely AT+TYPE[Para] and AT+PIN[param].

  • AT+TYPE[Para] sets and asks if the PIN code is needed for connection.
  • AT+TYPE0 sets the value 0 that disables the PIN code. It is the default value.
  • AT+TYPE2 sets the value 2 that enables the PIN code for the connection to succeed.
  • AT+TYPE returns the value of the TYPE field.
  • AT+PIN[para] gets and sets the 6 digit PIN code.
  • AT+PIN132456 sets the PIN code 123456.
  • AT+PIN returns the current PIN code. The default PIN code is 000000.

As you can imagine, in order to setup the PIN code we need to send AT+TYPE2 followed with AT+PINxxxxxx comands. The first one activates PIN code verification and the second one sets a new PIN code.

Let’s not wait any longer and send these two commands: AT+TYPE2 and AT+PIN123456.

TYPE2 means that PIN code is required
We set the PIN code 123456

In order for the AT-09 to take this modifications into account, we can either turn it off and on send the AT+RESET command.

Reset please :)

The configuration of the AT-09 module is officially done at this point. We can test it by running a BLE scan app on an iPhone or an Android and by connecting to the BLE peripheral. The operating system should ask you to enter a PIN code at this point. You should notice that the connection succeeds when you enter a correct PIN code and that it fails when the PIN code is wrong.

Wring PIN code
Correct PIN code
PIN code dialog on Android

I noted a major drawback when setting the PIN code. In fact, the PIN code is required every time we connect to the peripheral, again and again. This can be really annoying if we connect and disconnect regularly from the peripheral.

On Android, there seems to be a way to programmatically enter a PIN code https://stackoverflow.com/questions/19047995/programmatically-pair-bluetooth-device-without-the-user-entering-pin. However, on iOS the PIN code must be entered by the user https://stackoverflow.com/questions/40355805/ios-ble-device-connection-pin-code.

Sorry but it is time to conclude.

Conclusion

Bluetooth Low Energy allows by default any central to connect to a peripheral. If this behavior is unwanted we can require the user to enter a PIN code when a connection request is issued by the central device. Although unauthorized centrals will not be able to connect, the user of the authorized central will need to enter a PIN code for every connection. Thus, he loses the previous comfort of direct and seamless connection. On Android, we may overcome this issue by setting the PIN code programmatically.

In conclusion, the PIN code can be really helpful if we want to restrict connections. Alternatively, we can restrict connections by setting on the peripheral a white-list of allowed centrals.

Happy coding :)

--

--

Yassine Benabbas

Mobile app developer at Worldline, teacher and PhD. I love programming, videos games and manga. Trying hard to learn some Japanese.