Advertisement
microrobotics

JDY-23 Bluetooth 5 Module

Apr 26th, 2023 (edited)
3,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To use the JDY-23 Bluetooth 5 Module with an Arduino, you can connect it via the serial interface (TX, RX). Here's an example code that sends several AT commands to test the JDY-23 Bluetooth module. This code sends the AT commands automatically one after another, with a delay between each command.
  3.  
  4. Hardware Requirements:
  5.  
  6. JDY-23 Bluetooth 5 Module
  7. Arduino board (e.g., Arduino Uno)
  8. Jumper wires
  9. Connection:
  10.  
  11. Connect the JDY-23 Bluetooth 5 Module to the Arduino as follows:
  12.  
  13. VCC pin of the JDY-23 to the 3.3V pin on the Arduino
  14. GND pin of the JDY-23 to a GND pin on the Arduino
  15. TX pin of the JDY-23 to the RX pin on the Arduino (Digital pin 0)
  16. RX pin of the JDY-23 to the TX pin on the Arduino (Digital pin 1)
  17. Note: It is recommended to use software serial for communication with the JDY-23 module so that the hardware serial pins can be used for debugging and communication with the Serial Monitor.
  18.  
  19. This code sends AT commands one by one with a delay of 2 seconds (2000 ms) between each command. The AT commands sent are AT+VER, AT+MAC, AT+NAME, AT+BAUD, and AT+POWR. The responses from the JDY-23 module will be displayed on the Serial Monitor.
  20.  
  21. Please note that this code is designed to send AT commands automatically in a sequence
  22. Code:
  23. */
  24.  
  25. #include <SoftwareSerial.h>
  26.  
  27. // JDY-23 module connected to pins 2 and 3 on the Arduino
  28. const int jdy23_rx = 2;
  29. const int jdy23_tx = 3;
  30.  
  31. // Create a SoftwareSerial instance
  32. SoftwareSerial jdy23Serial(jdy23_rx, jdy23_tx);
  33.  
  34. // Array of AT commands to test
  35. const char* atCommands[] = {
  36.   "AT+VER",
  37.   "AT+MAC",
  38.   "AT+NAME",
  39.   "AT+BAUD",
  40.   "AT+POWR"
  41. };
  42.  
  43. const unsigned long commandDelay = 2000; // Time delay between AT commands (milliseconds)
  44. unsigned long lastCommandTime = 0;
  45. int commandIndex = 0;
  46.  
  47. void setup() {
  48.   // Open the hardware serial port for debugging
  49.   Serial.begin(9600);
  50.  
  51.   // Open the software serial port for communication with the JDY-23 module
  52.   jdy23Serial.begin(9600);
  53.  
  54.   Serial.println("JDY-23 Bluetooth Module AT Command Test");
  55. }
  56.  
  57. void loop() {
  58.   // Read data from the JDY-23 module and print it to the Serial Monitor
  59.   if (jdy23Serial.available()) {
  60.     char received = (char)jdy23Serial.read();
  61.     Serial.print(received);
  62.   }
  63.  
  64.   // Send AT commands to the JDY-23 module with a delay between commands
  65.   if (millis() - lastCommandTime > commandDelay) {
  66.     if (commandIndex < (sizeof(atCommands) / sizeof(atCommands[0]))) {
  67.       // Send the AT command to the JDY-23 module with "\r\n" terminator
  68.       jdy23Serial.print(atCommands[commandIndex]);
  69.       jdy23Serial.print("\r\n");
  70.  
  71.       Serial.print("Sent AT command: ");
  72.       Serial.println(atCommands[commandIndex]);
  73.  
  74.       commandIndex++;
  75.       lastCommandTime = millis();
  76.     }
  77.   }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement