To carry out the data transmission test between the Bluetooth module and Arduino, you can follow the steps below:
- Make sure you have connected the Bluetooth module and Arduino. Usually, the VCC and GND pins of the Bluetooth module are connected to the 5V and GND pins of the Arduino, and the TX and RX pins of the Bluetooth module are connected to two digital pins of the Arduino (for example, TX is connected to the RX of the Arduino pin, RX to the Arduino’s TX pin).
- Open a new blank project in the Arduino IDE.
- Import the SoftwareSerial library to emulate serial communication on the digital pins. You can import this library by clicking on the “Sketch” menu, then selecting “Include Library” -> “SoftwareSerial”.
- Define the connection pins of the Bluetooth module in the code. For example, use the following code to connect the TX pin of the Bluetooth module to the Arduino’s digital pin 2 and the RX pin to the Arduino’s digital pin 3:
- In the setup() function, initialize the serial port communication and set the baud rate. Usually, the default baud rate of the MuYu Bluetooth module is 115200, so you can use the following code to initialize the serial port:
- In the loop() function, you can use the Serial object to receive data from the serial port and send it out through the Bluetooth module, and you can also receive data through the Bluetooth module and print it out through the Serial object. For example, the following code sends the data received from the serial port to the Bluetooth module, and prints the data received from the Bluetooth module to the serial port:
- Upload the code to Arduino, and open the serial monitor.
- Open a serial terminal program (such as the serial monitor of Arduino IDE, CoolTerm, etc.), input and send data through the serial port, you should be able to see the data sent by the Bluetooth module in the terminal program, and the data you input in the terminal program is also Should be able to send to Arduino via Bluetooth module and show up in serial monitor.