Implementation of two-way serial communication between Arduino and LU-ASR01 speech recognition module

    I wrote an article "Arduino's Intelligent Language Input Implementation" before, discussing the realization of Arduino's Chinese voice input through serial communication between Arduino and LU-ASR01, but that communication is incomplete, because LU-ASR01 has only one serial port There is a transmit port TX and no receive port RX. In fact, in real applications, LU-ASR01 usually also needs to receive data from the host computer. For example, to ensure that the host computer has received the data sent by LU-ASR01, the host computer needs to return a response that the data has been received. For example, the host computer If you want LU-ASR01 to send a certain voice message or let LU-ASR01 perform a certain operation, you need to send data from the host computer, and then LU-ASR01 will receive the data and perform corresponding processing.

    In order to solve the problem that the serial port of LU-ASR01 has no receiving port RX, we can use the I/O ports of LU-ASR01 (LU-ASR01 has 8 I/O ports in the previous introduction), and take 2 of them as serial ports Communication, and then use soft serial communication to realize two-way serial communication between Arduino and LU-ASR01.

    Because I can't find any soft serial communication data of LU-ASR01 module on the Internet, I can only pass continuous testing, because I don't even have the most basic detection tools, oscilloscope, etc., only a computer, an Arduino board and a LU-ASR01 board. , in order to detect whether the soft serial port has sent data, I can only connect the LED to the serial port to see if the LED is blinking when the serial port is sending data. After dozens of failures, I almost desperately wanted to give up the soft serial communication implementation of LU-ASR01. Haha, of course I succeeded in the end, otherwise I would not be able to write this article.

    Gossip aside, this experiment is to send a command through LU-ASR01, and after Arduino receives the command, it returns to send the command data (that is, the same value) to LU-ASR01 as a response, and finally LU-ASR01 receives the Arduino After comparing the command data sent back (that is, judging whether the sent data is equal to the received data), it is confirmed that the command has been sent successfully.

   In this experiment, Arduino UNO still uses hardware serial communication, using TX and RX ports, LU-ASR01 uses IO6 port as soft TX for sending data, and IO7 port as soft RX for receiving data. Arduino provides 5V power supply to LU-ASR01 (red and black 2 wires in the figure below), Arduino’s TX is connected to LU-ASR01’s IO7 port (soft RX), Arduino’s RX is connected to LU-ASR01’s IO6 port (soft TX ), the connection diagram is as follows:

LU-ASR01 still uses the "Tianwen block" graphical programming platform, the following is the complete program on the "Tianwen block":

 

 

Then we download the following program to the Arduino UNO board, the complete program is as follows:

/*

   Serial communication experiment between Arduino and ASR01 speech recognition module

   In order to ensure that the data sent by ASR01 can be received, the response method is adopted. Every time Arduino receives an identifiable

   After the command, the command will be sent to ASR01

*/

// define pins

const int LedPin = 13; // The pin connected to the led, as the positive pole of the LED light

char Txbyte; //Character data sent by serial port

char Rxbyte; //character data read by serial port

//initialization

void setup() {

  Serial.begin(9600); //Set the serial port baud rate to 9600

  pinMode(LedPin, OUTPUT); //Set LedPin    

}

//main program

void loop() {

    if(Serial.available() > 0){ //When the serial buffer has data

      Rxbyte=char(Serial.read());

        if(Rxbyte==0x21) { //When Rxbyte is 0x21, LedPin is set to high level, that is, the light is turned on

          Serial.write(Rxbyte);

          digitalWrite(LedPin, HIGH);

        }

        if(Rxbyte==0x22) { //When Rxbyte is 0x22, LedPin is set to low level, that is, the light is turned off

          Serial.write(Rxbyte);

          digitalWrite(LedPin, LOW);

        }

        if(Rxbyte==0x20){ //When Rxbyte is 0x20, the LED will flash 3 times

          Serial.write(Rxbyte);

          for(int i=0;i<3;i++){   //Led灯闪3下

            digitalWrite(LedPin, HIGH);

            delay(100);

            digitalWrite(LedPin, LOW);

            delay(100);

          }

        }

        delay(2);

      }

}

    After the programs on both sides are downloaded and the 4 lines are connected, LU-ASR01 will first broadcast "We are conducting an experiment, please call me "Mr. Unforgiveness" to wake me up. I am resting temporarily, please call me if there is anything to do: Mr. Unforgiveness ", then we call "Mr. Unforgiveness", then LU-ASR01 sends a command of one byte through the soft serial port after answering "I am here": 0x20, after Arduino receives 0x20, it sends 0x20 to LU-ASR01 again; when After LU-ASR01 receives the data, compare it with the data sent before, if they are equal, it will broadcast: "Adu has received the command", otherwise wait for 1 second, if it does not receive the correct data 0x20, it will broadcast: " Timed out, Adu did not receive the command, please resend" (here to explain, in order to simplify the procedure, the program does not resend the data again). You can then test "lights on" and "lights off".

Guess you like

Origin blog.csdn.net/m0_61543203/article/details/126270423