神刀安全网

Bluetooth Communication With Arduino

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.

Bluetooth Communication With Arduino

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…

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Bluetooth Communication With Arduino

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮