Go Down

Topic: Ultrasonic HC-SR04 dog anti-poop sketch/project (Read 4936 times) previous topic - next topic

Nov 26, 2012, 05:51 am Last Edit: Nov 26, 2012, 06:35 am by teckel Reason: 1
So, my wife has 3 little dogs, who think it's okay to poop wherever and whenever they want.  Arduino to the rescue! Introducing my Anti_Dog_Pooper project:








What is does... Creates an invisible field to block a part of the house where those stupid little dogs love to poop or pee in.  When they try to enter that area, an alarm is sound along with a bright LED flasher.  The dogs run away and you know it's probably time to let them outside.  Here's the sketch:

Code: [Select]
#include <NewPing.h>

#define POT_PIN        0 // Analog pin 0
#define BATERY_PIN     1 // Analog pin 1
#define BUZZER_PIN     4
#define TRIGGER_PIN    7
#define ECHO_PIN       8
#define LED_PIN       12
#define MAX_DISTANCE 300
#define BUZZER_SECS    3

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
 pinMode(POT_PIN, OUTPUT);
 pinMode(BATERY_PIN, OUTPUT);
 pinMode(LED_PIN, OUTPUT);
 delay(3000); // Give you time to plug in the power before the alarm sounds.
}

void loop() {
 int range = map(analogRead(POT_PIN), 0, 1023, 5, MAX_DISTANCE);
 int volts = map(analogRead(BATERY_PIN), 0, 1023, 0, 500);
 int cm = sonar.ping_cm();
 if (cm != NO_ECHO && cm < range) buzzer();
 if (volts > 300 && volts < 370) batteryDead();
 delay(50);
}

void buzzer() {
 for (int i = 0; i < BUZZER_SECS * 10; i++) {
   digitalWrite(LED_PIN, HIGH);
   tone(BUZZER_PIN, 4000);
   delay(50);
   digitalWrite(LED_PIN, LOW);
   tone(BUZZER_PIN, 3800);
   delay(50);
 }
 noTone(BUZZER_PIN);
}

void batteryDead() {
 while(1) {
   digitalWrite(LED_PIN, HIGH);
   tone(BUZZER_PIN, 500);
   delay(200);
   digitalWrite(LED_PIN, LOW);
   noTone(BUZZER_PIN);
   delay(1000);    
 }
}


It uses a HC-SR04 for the sensor (using the NewPing library), piezoelectric speaker and bright LED for the deterrent, 2S LiPo for the power supply, and potentiometer to set the opening distance.

Because it's using a LiPo battery for the power supply, the sketch also measures the voltage of one cell from the balance port to detect when the voltage drops below 3.7 volts (you never want to drain a LiPo battery below this amount per cell).  If you use an AC power supply for the Arduino, you could remove the code that monitors the battery and alerts you when the battery is low.  I'm using just a cheap $3 1000mAh 2S (7.4v) battery from HobbyKing and it lasts for several hours with this sketch (monitoring at 20 times per second).  If you had a larger battery, it could easily last 24 hours between charges.

Note, the LiPo monitor will sound an alarm when it's low, but doesn't have the ability to cut the power.  So, if using a LiPo power supply, you still need to be around to turn it off once the low battery alarm sounds (which is lower frequency and slower beeps than the anti-dog-pooper alarm).

Why doesn't it use ultrasonic frequencies to detour the dogs?  Because at least with the piezoelectric speaker I used, it doesn't really work at ultrasonic frequencies.  Also, it seems that for at least my dogs, they can't hear the ultrasonic sensor (HC-SR04) pings.  It would seem that you could use the HC-SR04 as an ultrasonic dog whistle, but for at least my dogs, it appears they can't hear it.  My guess is that 40kHz is too high for even dogs to hear.  I tried ultrasonic frequencies with the piezoelectric speaker, but either it can't output ultrasonic frequencies or my dogs were not affected by them.  The loud 2kHz and 4kHz frequencies appeared to be noticed the most by my dogs, so that's what the sketch outputs.  I may try to source an electronic dog whistle to see if I can get better results (and ones that I can't hear).  With that said, it's also kind of nice to hear when the alarm is triggered so you can measure the dog's reaction (and let them outside).  For my dogs, they go running when the sound an lights are triggered, ultrasonic or not.

Enjoy...

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Oh, I'll try and provide a schematic for this at some point.  But, it's really very simple if you've done a few Arduino projects. I used a 220? resister for the LED and a 33? resistor for the piezoelectric speaker, which has 16? resistance to begin with.  Everything else is wired directly to a pin, ground, or 5v.  The 2S LiPo battery is of course wired to Vin and ground and the balance port is used to measure the voltage of one cell (don't monitor the 2S balance port, as it's >= 7.4v and will probably fry your Arduino, monitor the 1S balance port which never goes beyond 5v).

Now go punish, I mean train those little dogs to poop where they should!

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


fun!


Much to my enjoyment, the dogs don't share your opinion.  It even looks scary to them before the siren and lights are triggered.  Even my wife is kind of warming up to it.  She's starting to believe there's something to my hobby and it's not all about getting high from sniffing solder in the basement.  Which means more toys for me.  What other household problem can I tackle next?

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

robtillaart


teens that eat the complete fridge at night ?
a gas sniffer that detects if the dogs have answered natures call somewhere (a poo radar ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up