Advertisement
microrobotics

HC05 Bluetooth Module

Apr 4th, 2023
3,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The HC-05 Bluetooth module can be easily interfaced with an Arduino using the SoftwareSerial library. Here's an example code to send and receive data between an Arduino and a Bluetooth device (e.g., a smartphone) using the HC-05 module:
  3.  
  4. This code sets up a simple "echo" between the Serial Monitor and the Bluetooth device. It reads data from the Bluetooth module and sends it to the Serial Monitor, and vice versa.
  5.  
  6. Connect the HC-05 Bluetooth module to the Arduino as follows:
  7.  
  8. VCC pin to 5V (or 3.3V, depending on your module's voltage level)
  9. GND pin to GND
  10. TX pin to digital pin 2 on the Arduino
  11. RX pin to digital pin 3 on the Arduino
  12. To connect to the HC-05 module from your smartphone, you'll need a Bluetooth Terminal app. Pair your smartphone with the HC-05 module using the default pairing code (usually "1234" or "0000"), open the Bluetooth Terminal app, and connect to the HC-05 device. You should now be able to send and receive data between your smartphone and the Arduino via the HC-05 module.
  13.  
  14. Please note that you may need to adjust the pin numbers in the code depending on the specific pins you choose to use for connecting the HC-05 module to your Arduino. Additionally, when connecting the TX pin of the HC-05 module to the RX pin of the Arduino, it is recommended to use a voltage divider to reduce the voltage from 5V to 3.3V if your HC-05 module operates at 3.3V.
  15. */
  16.  
  17. #include <Arduino.h>
  18. #include <SoftwareSerial.h>
  19.  
  20. const int btRxPin = 2; // Connect this pin to the TX pin of the HC-05 module
  21. const int btTxPin = 3; // Connect this pin to the RX pin of the HC-05 module
  22.  
  23. SoftwareSerial btSerial(btRxPin, btTxPin);
  24.  
  25. void setup() {
  26.   Serial.begin(9600);
  27.   btSerial.begin(9600);
  28.  
  29.   Serial.println("HC-05 Bluetooth Module Example");
  30. }
  31.  
  32. void loop() {
  33.   // Read data from Bluetooth and send it to the Serial Monitor
  34.   if (btSerial.available()) {
  35.     char data = btSerial.read();
  36.     Serial.print(data);
  37.   }
  38.  
  39.   // Read data from the Serial Monitor and send it to Bluetooth
  40.   if (Serial.available()) {
  41.     char data = Serial.read();
  42.     btSerial.print(data);
  43.   }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement