Dog Bark Alarm

I have been assembling parts to make a dog-bark alarm with my Uno and I thought some folks might like to see the result. I use the toneAC library which has you connect the speaker wires up to pins 9 & 10 on an Uno. I picked up a cheap piezo tweeter horn off of ebay as well as a rather basic (EP98) sound sensor which uses a small electret microphone. When I first hooked up the sensor I had to adjust it a little until I got a good sensor rate. (I used a simple digitalRead loop echoed to the serial port to do the adjustment)

Then it was just a matter of hooking up the speaker and coming up with a way to trigger a negative feedback to discourage a dog barking. What I ended up doing was just using a weighted average of the sensor value over time and if it gets above a given threshold, it plays a quick audio sweep just above human hearing, within the upper range of my speaker. I may connect it to a small amplifier I picked up or build my own out of spare parts I have in the project box. But it seems to put out a pretty good shot of sound when I drop the range down to human levels.

I left in a few comments as well as a ‘shave and a haircut’ function from the toneAC examples I found online which comes in handy as a human-audible test should you try to adjust the code at all.

#include <toneAC.h>

// change this value (from 0-1) to change the sensitivity
//  note: most effective ranges are going to be in the 0.6-0.85 range
float sensitivity = 0.8; // set lower for more sensitivity

float alertLevel = 0.0; // weighted average

void setup() {
  // uncomment Serial lines for debugging data on serial port
  // initialize serial communication at 9600 bits per second:
  //Serial.begin(9600);
}

void loop() {

  int sensorValue = digitalRead(2);

  
  alertLevel = (alertLevel + sensorValue) / 2;

  //Serial.println(alertLevel);

  if(alertLevel > sensitivity) {
      //playNotes(); // use this for human audible testing
      playSweep(10);
  }

  delay(10); // Wait a second.
}

void playSweep(int sndVol) {
  /**
    Species	Approximate Hearing Range (Hz) 
    human	64-23,000
    dog	        67-45,000 
    cat	        45-64,000 
  */
  int lowf = 22500;  // most humans won't hear this
  int highf = 27000; // many speakers cut off around here

  int stepRate = 200; // increase for longer sweep, decrease (above 0) for faster sweep
  int fstep = abs(highf - lowf) / stepRate;
  for (int repeat = 1; repeat <= 2; repeat += 1) {
    for (unsigned long freq = lowf; freq <= highf; freq += fstep) {  
      toneAC(freq,sndVol); // Play the frequency (low to high sweep in steps).
      delay(1);     // Wait 1 ms so you can hear it.
    }
    for (unsigned long freq = highf; freq >= lowf; freq -= fstep) {  
      toneAC(freq,sndVol); // Play the frequency (high to low sweep in steps).
      delay(1);     // Wait 1 ms so you can hear it.
    }
  }
  toneAC(); // Turn off toneAC, can also use noToneAC().
  delay(1000);
}

void playNotes() {
  // Melody liberated from the toneMelody Arduino example sketch by Tom Igoe.
  int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
  int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    toneAC(melody[thisNote], 10, noteDuration, true); // Play thisNote at full volume for noteDuration in the background.
    delay(noteDuration * 4 / 3); // Wait while the tone plays in the background, plus another 33% delay between notes.
  }
}

But does it actually stop the dog from barking or does it make it bark more?

I might give this a try as my dogs go nuts when anyone passes our window! The only consideration is that my other half can often hear those dog and cat deterrent things. Still, maybe it could also work on her as a 'nag' alarm?! :P

Where does the speaker connect? The only reference I can see to a possible pin is '10' in playNotes ().

Grumpy_Mike: But does it actually stop the dog from barking or does it make it bark more?

That depends on the dog. I'm thinking I might play with some of the mp3 playback modules and add yet another weight system that escalates if the alarm trips more than a couple of times in a given period of time and to play back different verbal commands instead. Sort of an escalated series of scolding.

Maybe even add a praise playback if, after a couple of triggers it settles back to no noise after a minute or so has passed.

That depends on the dog.

Is that just theory or have you actually got positive results with some dogs?

Grumpy_Mike:

That depends on the dog.

Is that just theory or have you actually got positive results with some dogs?

I thinks it's only a "ruff" guess. :) Seriously, it does seem to work and can recall seeing a hardware only solution in the Elektor magazine several years ago which a friend built to "train" his neighbours pesky hound and he reported good results. I don't know what kind of mutt it was.

OK, some more observations. It may be a limit of my microphone, my sound device or my recording software, but I have pretty good stuff in all three of those categories (beyer dynamic m88, cakewalk UA-1G and adobe audition) but I do not think tone-AC can produce pitches that high. You can hear a click in the speaker itself, but I'm not picking anything up when recording. Of course, there may also be limits on digital recording to only sample a given range also. So I'm now using 15000-19000

It could be the sound card as some only sample up to 44.1KHz, meaning that the max input frequency would be 22KHz. Some newer HDA ones can go up to 96KHz which would allow you to sample frequencies up to 48KHz.

If you have an oscilloscope, you could always measure across the speaker/buzzer to be certain that toneAC can in fact produce anything above 20KHz or so.

Hi treii28,

I've been looking all over for a method of detecting barks and your code looks promising! How well does it actually work? I tried an off-the-shelf ultrasonic bark deterrent device and it was activating on anything even slightly loud - even wind! The dog bark I want to detect is extremely loud but usually comes in groups of 4 or less, then silent for awhile, from 30 seconds to 10 minutes. Do you think your method would work for that or only for more prolonged barking?

Either way, thanks for posting your code!

My neighbor has 3 poms that I hate, and they just bark at the trees and the sky. I can't DO anything to them, so I've often thought about a project like this, Except I want a loud speaker that barks back. So every time they bark, I bark. I just don't want to get my blood pressure up hating on all that barking - I just want to annoy them at the same level they annoy me. :D So good luck with your project LOL

To stop a dog from barking use a "Rockstar" or "Nos" size can put about 25ish pennies in it. Cup your hand over the top so pennies don't fly out. Only holding it by the top. Make sure that you have room for the sound to still come out of the can. Shake it once using a vertical drop a few inches then immediate stop... only to stop the dog and get its attention. Then say "No Bark!" and wait a second to see if the dog does it again. If so, use the can again. One Shake only. Don't rattle it around. Or carry it with making noise. It will loose it's effect. After a few days of this the dog will stop barking... I have 2 Lab's 11 months, sisters, partners in crime. I am in 2 weeks and only have to bring the can outside just in case. They don't bark at all, they dont jump up, they don't dig and they don't ruff house with growls or displaying teeth. Try it. Sounds strange, but it works. But, you should still build this... just for the sake of doing it and maybe you have one dog out of hundreds that it has worked for, that will not listen. Which then you know that you need to seek... ready for this... "The Dog Whisper!" Bam! yep, I said. it. lol Good luck!

Like :)

Mithra: My neighbor has 3 poms that I hate,

Why do you hate us English folk?

wopr73 is using an effective training method, unfortunately that's good for dogs you own and not dogs other people fail to train lol. I was kinda going the route of treating the barking dog like a child throwing a tantrum - just find a way to match their energy without consuming yours and wear them DOWN. I wouldn't mind wearing my neighbors down either.

Hey maybe you can make an arduino bot that shakes a can of pennies and says "No Bark!" haha

I am having trouble with getting the sound sensor to calibrate. I can't get a consistent sound detection level. I commented into the sketch the human level test sounds but it either gets triggerred by everything or just goes of randomly.

Does anyone know how I can test and calibrate?

I have a uno, sound sensor and 0.5w speaker.

I'm a bit confused - the code sort of suggests an analogue sensor approach but the digitalRead suggests a simple ON or OFF reading from a digital pin?

Basically this sounded IDEAL for what I'm wanting to do...I want to only "take action" if there is prolonged noise. I don't want a plate being dropped, a drawer being shut or a door being slammed to trigger action.