Over a million developers have joined DZone.

Bluetooth Communication With Arduino

Learn how to communicate to an Arduino using Bluetooth in this short tutorial.

· IoT Zone

I am in the process of redesigning my Raspberry Pi based Aeroponic controller. In my new system, I am using Arduino. This time, I decided to use Bluetooth instead of WiFi to communicate between my mobile app and Arduino controller. Later I will write another post explaining about this new design. In this post I will explain how to communicate to Arduino via Bluetooth.

Arduino Bluetooth connection 

I bought a Bluetooth module from Ebay, it’s a very low-cost module. The connection is pretty simple. See the connection below. I am using Arduino Nano in my new design.

connection_v2_bb

Connection Details

  • Nano VCC to BT VCC
  • Nano GND to BT GND
  • Nano RXD to BT TXD
  • Nano TXD to BT RXD

In my sketch I used Softserial and I assign D2 and D3 as RXD and TXD respectively.

I haven’t used the EN and KEY pin in the Bluetooth module. You might want to use EN pin if you want to enter into AT mode to issue any commands to BT module.

Arduino Sketch

#include <SoftwareSerial.h>
SoftwareSerial soft(2,3);// RX, TXvoid setup() {
  soft.begin(9600);
  Serial.begin(9600);
}

void loop() {
  if(soft.available()){
      String serialData = soft.readString();
      Serial.println(serialData); //echo bluetooth data to serial      writeAck();
  }
}

void writeAck(){
  uint8_t payload[]="ACK|0|\n";
  soft.write(payload,sizeof(payload));
}

It’s a very simple sketch to receive the data from Bluetooth module and echo to Serial, also send and Acknowledgment back to the caller.

You can test the program by issuing some text from Hyperterminal or any other similar app. I wrote an Android app to issue a command via Mobile Bluetooth.

Happy coding…

Topics:
bluetooth ,arduino ,iot ,internet of things

Published at DZone with permission of Sony Arouje, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}