Chinese sound sensor Config

Hello, I am working on a sound sensor. My problem is that i do not get the data over uart. Has anyone a example code to read the data?
https://www.aliexpress.com/item/4001051084583.html i the sound sensor.
@hans00008 maybe?

What are you trying to do with the sound sensor? Please read:

I would think you'll need a serial terminal for testing/developing/debugging. That would be a computer with a terminal program, and RS-232 port, and a level shifter (like the MAX232 chip, but I don't know if the MAX232 works at 3.3V).

That would allow you to send & receive data to & from the sound sensor so you can understand how it's working, and to/from the Arduino to make sure it's transmitting/receiving data correctly.

I haven't used a terminal emulator for a long time (at least not much) and you'll (probably) need one that can work in hexadecimal.

You could also write a little program for the computer and that might help with development but I'd start with a terminal so there are fewer unknowns. ...If you can find a terminal program that works in hex (raw numerical data) without converting to/from ASCII.

One important thing I'm not seeing on the AliExpress website is how to set the baud rate. ...But I don't expect complete data from AliExpress.

P.S.
There are easier to use and better documented sound sensors that you can get from SparkFun or Adafruit, but I haven't seen one that's calibrated in dB (or calibrated at all).

Post the code you used, using code tags, and describe what happened when you ran it.

Also post a diagram showing how you wired it. Hand drawn is preferred.

Note: the sensor uses 3.3V I/O. Do not connect a 5V Arduino to it, unless you use a logic level shifter.

Hi,
in the first place i want to read the data as described in the short documentation on the aliexpress website.

I use an ESP32 for this project, connected over GPIO 16/17 (TX2/RX2) for data and connected to ground and 5V VCC as described on aliexpress.

#define RXD2 16
#define TXD2 17

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_6N1, RXD2, TXD2);
  Serial.println("Serial Txd is on pin: "+String(TX));
  Serial.println("Serial Rxd is on pin: "+String(RX));
}

void loop() { //Choose Serial1 or Serial2 as required
  while (Serial2.available()) {
    Serial.println(char(Serial2.read()));
 }
}

In the serial monitor (baud 115200 i only get data shown like:

08:03:54.624 -> <
08:03:55.025 -> <
08:03:55.450 -> >
08:03:55.884 -> <
08:03:56.284 -> <
08:03:56.692 -> <
08:03:57.136 -> >
08:03:57.544 -> >

Try SERIAL_8N1 instead, and various Baud rates.

It would be a good idea to figure out what these instructions mean. I suspect that the sensor waits for you to send a "get data" command, before responding.

2>.Serial data packet protocol:
1.1>.Data packet format:Start character+command+data+check value
1.2>.Start character:BBAA,2 bytes of data (0xBB,0xAA)
1.3>.Command:1 byte length.01,means returning the decibel data command
1.4>.Data:2 bytes in length,decibel data is placed in this data segment
1.5>.Check value:1 byte length,arithmetic and check of the start character,command and data,excluding overflow value exceeding 256.
3>.Decibel data command:
1.1>.For example: BB AA 01 7F 02 E7
1.2>.Start character:BB AA,which indicates the beginning of a frame.
1.3>.Command:01,indicating that this frame is returning decibel value data.
1.4>.Data:7F 02,which indicates that the detected decibel value is 63.9dB.Low byte comes first,high byte comes after,and each unit is 0.1dB.
1.5>.Check value: E7,which is the arithmetic and check value of BB AA 01 7F 02.
1.6>.This frame is returned every 300ms after the module is powered on.

Solved!

#include <HardwareSerial.h>

HardwareSerial SerialSensor(0); // UART interface on ESP32 is labeled as Serial2

float mittelwertdB;

void setup() {
  Serial.begin(115200); // Initialize Serial Monitor for debugging
  SerialSensor.begin(115200); // Initialize UART for sensor communication
}

void loop() {
  if (SerialSensor.available() >= 6) { // Check if at least 6 bytes are available from sensor
    String hexString = ""; // Initialize empty string for storing hex data
    for (int i = 0; i < 6; i++) { // Read all 6 bytes from sensor
      uint8_t sensorData = SerialSensor.read(); // Read the data as byte
      if (sensorData < 0x10) { // Append leading zero for single-digit hex values
        hexString += "0";
      }
      hexString += String(sensorData, HEX); // Convert byte to hex and append to string
    }
    
    // Extract 4th and 5th byte and swap their positions
    String swapped_str = hexString.substring(8, 10) + hexString.substring(6, 8);
  
    // Convert the swapped string to decimal
    unsigned int decimal_val = strtoul(swapped_str.c_str(), NULL, 16);
  
    // Divide the decimal value by 10
    double result = decimal_val / 10.0;

    //Filter

    mittelwertdB = 0.9 * mittelwertdB + 0.1 * result;
  
    // Output the hexString, result
    Serial.print(hexString);
    Serial.print(" ");
    Serial.print(result, 2);
    Serial.print(" ");
    Serial.println("dB");
    Serial.print(mittelwertdB, 2);
    Serial.print(" ");
    Serial.println("dB");

  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.