Electrical – Arduino nano, unexpected sound from buzzer when receive data from radio module HC-12

arduinocode-reviewnoisepiezo-buzzerradio

I make a simple circuit with an:

  • Arduino Nano
  • HC-12 Long distance radio module
  • HC-12 Antenna
  • 1000 uF Capacitor
  • Buzzer
  • 220 Ohm Resistor

I need that the buzzer emits a noise when Arduino receives specific string from HC-12.
Everything work as expected but I have a strange problem that I can't fix.
During the data receiving phase, I hear a strange noise sound from the buzzer.
Is something like the good old modem sound. Seem an interference or similar but is something different because I move the buzzer to 20 cm away from my board and the problem still exists.

Probably is some kind of error on my Arduino code or in my wiring, the sound can be heard before the warningTone() (that you can see inside the code) function that is the call that emits the correct sound.

Some background information: the string can be received more than one time in a single transmission, during the same transmission each string is separated by newline so I can have something like:

AL_S001

AL_S001

AL_S001

etc..

Even 100 time for same transmission (data come from an HC-SR501 with is trigger set to repeatable, the problem exists also with trigger set as "single trigger mode"), but I need to sound the speaker only once per event, if the event is repeated after 10 seconds then I need to sound again.

I did the tests to try to found a solution:
– Move the buzzer 20cm away from the board
– Create a var in the code that checks if the sound is currently emitted (something like a mutex), in this way I try to execute the warningTone() function only once per loop

How can I solve this problem? I already reach my target because the sound is loud and clear when produced.. but I want to understand what cause the strange sound for my own knowledge.

Below the schematic and code, please be patient with me. Probably my schematics is a nightmare, I know, but I'm a self-learner and everything I know on electronics field and English language came from my own self-study…

#include <SoftwareSerial.h>

//--- Begin Pin Declarations ---//
const byte buzzerPin = 8;

//pin HC-12 module
const byte HC12RxdPin = 2;
const byte HC12TxdPin = 3;
const byte HC12SetdPin = 4;
//--- End Pin Declarations ---//

//--- Begin variable declarations ---//
char byteIn;                                        // Temporary variable
String HC12ReadBuffer = "";                         // Read/Write Buffer 1 -- HC12 
String SerialReadBuffer = "";                       // Read/Write Buffer 2 -- Serial
boolean serialEnd = false;                          // Flag for End of Serial String
boolean HC12End = false;                            // Flag for End of HC12 String
boolean soundStart = false;                         // Flag that allow only one sound even if multiple events


// Create Software Serial Port for HC12
// Software Serial ports Rx and Tx must be inverted so HC12 Rx go as Tx and Tx go as Rx 
SoftwareSerial HC12Serial(HC12TxdPin,HC12RxdPin);

/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);                    // Set buzzer - pin 8 as an output

  HC12ReadBuffer.reserve(128);                   // Reserve 128 bytes for Serial message input

 //enable radio module
 Serial.println("Activate Radio Module"); 
 pinMode(HC12SetdPin, OUTPUT);                   // Output High for Transparent / Low for Command
 digitalWrite(HC12SetdPin, HIGH);                // Enter Transparent mode
 delay(80);
 HC12Serial.begin(9600);  
 HC12Serial.listen();

 //send sound to user for confirm system activation
 tone(buzzerPin, 1000);                         // Send 1KHz sound signal...
 delay(1000);                                   // ...for 1 sec
 noTone(buzzerPin);                             // Stop sound...
}

////////////////////////////
//LOOP
void loop(){
  while (HC12Serial.available()) {                  // If Arduino's HC12 rx buffer has data
    byteIn = HC12Serial.read();                     // Store each character in byteIn
    HC12ReadBuffer += char(byteIn);                 // Write each character of byteIn to HC12ReadBuffer
    Serial.println( char(byteIn));
    if (byteIn == '\n') {                           // At the end of the line
      HC12End = true;                               // Set HC12End flag to true.
    }
  }
     if(!soundStart){
 if (HC12End) {
    HC12ReadBuffer.trim();
    Serial.println("data:" + HC12ReadBuffer);        
    if( HC12ReadBuffer == "AL_S001"){
       soundStart = true;      
       warningTone();
       soundStart = false;
    }
    HC12ReadBuffer = "";                            // Empty Buffer
    HC12End = false;                                // Reset Flag
 } 
     }
}

void warningTone(){ 
        tone(buzzerPin, 1000); // Send 1KHz sound signal...
        delay(1000);        // ...for 1 sec
        tone(buzzerPin, 1500); // Send 1KHz sound signal...
        delay(1000);        // ...for 1 sec
        tone(buzzerPin, 1000); // Send 1KHz sound signal...
        delay(1000);        // ...for 1 sec
        tone(buzzerPin, 1500); // Send 1KHz sound signal...
        delay(1000);        // ...for 1 sec
        noTone(buzzerPin);     // Stop sound...  

}

2017-11-10 – Last version of my breadboard:
Following all suggestion I change some routing:
1- Move the 1000 uF capacitor closer as possible to the radio module
2- Change buzzer pin to be farther as possible from the capacitor
3- Move the buzzer GND to another pin from the pin used by the radio module

Also, I update the firmware with the changes suggested.

Still no luck.. the strange "transmission" sound is still here 🙁

enter image description here

06-12-2017 UPDATE – Hi-Res image of the breadboard and video with the strange sound

enter image description here

This is the video, the only sound that I expect is the dual tone starting around second 3 the previous sound is unexpected and seem some sort of noise

Best Answer

Please try the following code:

#include <SoftwareSerial.h>

const byte buzzerPin = 8;
const byte HC12RxdPin = 2;
const byte HC12TxdPin = 3;
const byte HC12SetdPin = 4;

char byteIn;                                     // Temporary variable
String HC12ReadBuffer = "";                      // Read/Write Buffer 1 -- HC12

SoftwareSerial HC12Serial(HC12TxdPin, HC12RxdPin);

void setup() {
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);                    // Set buzzer - pin 8 as an output
  HC12ReadBuffer.reserve(128);                   // Reserve 128 bytes for Serial message input
  pinMode(HC12SetdPin, OUTPUT);                  // Output High for Transparent / Low for Command
  digitalWrite(HC12SetdPin, HIGH);               // Enter Transparent mode
  delay(80);
  HC12Serial.begin(9600);
  Serial.println("Radio Module Activated");
  //send sound to user for confirm system activation
  tone(buzzerPin, 1000);                         // Send 1KHz sound signal...
  delay(1000);                                   // ...for 1 sec
  noTone(buzzerPin);                             // Stop sound...
}

void loop() {
  HC12ReadBuffer = "";                            // Empty Buffer
  while (HC12Serial.available()) {                // If Arduino's HC12 rx buffer has data
    byteIn = HC12Serial.read();                   // Store each character in byteIn
    HC12ReadBuffer += char(byteIn);               // Write each character of byteIn to HC12ReadBuffer
  }
  HC12ReadBuffer.trim();
  Serial.println("data:" + HC12ReadBuffer);
  if ( HC12ReadBuffer == "AL_S001") {
    HC12Serial.stopListening();
    warningTone();
    HC12Serial.listen();
  }
}

void warningTone() {
  tone(buzzerPin, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  tone(buzzerPin, 1500); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  tone(buzzerPin, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  tone(buzzerPin, 1500); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzerPin);     // Stop sound...
}