Rat B@stard mk.1 – a rodent scarer

They’re back!

It could be squirrels. Maybe mice. Possibly rats. But whatever they are, the bastards are making themselves at home in the engine compartment of the car again.

We had this with the Citroen C5. Twice the twitchy-nosed little feckers chewed through the cables for the top-dead-centre sensor, leaving the car in a state where it would turn over but never fire. On the third occasion, they gnawed through so much stuff that repairing the car was not economically viable. We got €200 for it and felt lucky to get that. (Okay, there was a lot of other stuff wrong with it too.)

There was also the time that a family of rabbits nested in the engine compartment. That was the C5, too. I noticed clumps of grass deep down in the gloom. Then a mother bunny ran out. I reached down and grabbed grass, tufts of fur and … skin. I pulled a lot of bodies out of there that day. All of them small. Not all of them complete. I … I can’t talk about it.

Anyhoo, I was topping off the windscreen washer reservoir on our Nissan and digging chestnuts and their rotting husks out of the bit where the windscreen wipers live (we have a lot of chestnut trees around our courtyard) when I noticed something odd. There are two baffles make of thick foam – the kind they use for yoga mats – that insulate the engine compartment from part of the body just in front of the doors. And on both sides of the car, this foam had been nibbled.

This is after I’d already started cleaning up. Note the chewed insulation in the top right of the picture.

On one side, a flat area was covered in the remains of rotted walnut husks (we have a lot of those trees too), bits of shell and one complete chestnut. It was clearly a nest of some kind, or maybe a pop-up rat restaurant.

Something had to be done.

As we both work from home, we don’t run the car every day. In fact, a week can go by without it being fired up.

We’re spraying all kind of deterrent stuff in there – stuff that smells, basically. We’re leaving the bonnet open when possible. And we’re going to move the car each day. But I wanted an electronic solution.

Bad noise

You can buy ultrasonic rodent scarers, but I have my doubts about those. We’ve been using one in the More Significant Other’s sewing room, but the mice still go in there.

I wanted something loud. And thus was born the Rat B@stard – so-called because it’s a bastard to rats.

I’m not going to do circuit diagrams or go into huge detail because it’s really very simple.

There are two things that rodents find disturbing when they’re about this kind of wickedness – light and noise.

The first would be provided by simple LEDs. The second would involve a tiny amplifier and a speaker. And controlling it all? An Arduino Nano. What else?

There are four LEDs, treated as two pairs. One pair is mounted like a pair of eyes on one side of the box. These flash in a sinister manner.

The other two LEDs are mounted on the ends of the box and come on whenever the Rat B@stard is making a noise. All four LEDs are driven directly from GPIO pins on the Nano.

For the noise, I had an old 50mm speaker knocking about. To power it, I used a Modern Device LM4889 (not sure if this is still available, although it looks like it wouldn’t be too hard to copy). It’s a tiny board but capable of putting out a lot of racket.

The audio in pin of the amp is connected to a digital pin on the Nano. The Arduino framework has the handy tone() function that will create a noise with a given frequency (in Hertz) and, if desired, for a given duration (in milliseconds).

The amp also needs 5V power and connection to the speaker. And that’s pretty much it.

Source of power

As for a power source, I toyed with the idea of an 18650 cell and a boost converter. But putting a device designed by me and powered by a lithium-ion battery in the engine compartment of our car sounds like it would end with a very difficult interview with the claims adjuster. So I mounted a 5.5/2.1mm barrel jack socket on the box and am using an external USB power bank – one I trust.

(For a future project, I may use a 12V-to-5V buck converter and a couple of big alligator clips and just run the thing off the car battery. It doesn’t need much juice.)

Alas, the first two power banks I tried wouldn’t work. The Rat B@stard draws so little current at times that the power banks go into auto-off mode. So I’ve ordered a power bank that allows you to turn off that feature.

All of the above was stuffed into a black ABS project box. To protect the speaker, I used a small section of metal grille. The Nano and amp were hot glued into place, as was a small daughter board I threw together from stripboard. This holds the four current-limiting resistors for the LEDs and a handy place to bring together various ground connections.

All stuffed inside and fixed with hot glue. Neat! Well, not really…

Random acts

Now, I could have programmed the lights to flash and the noise to emit at regular intervals. But that would have let the little buggers off too easy. I decided to randomise the actions of the Rat B@stard as much as possible, to prevent them getting used to it.

The main loop pauses for a random period of time (between 5 and 15 seconds – I may increase that upper limit). Then it randomly does one of the following three actions – in ascending order of likelihood:

  • It cycles the four LEDs, then flashes the two front LEDs.
  • It flashes the two front LEDs a random number of times (between 3 and 10) with a random duty cycle (between 50 and 200 milliseconds).
  • It makes a noise with a random frequency between 50-5,000Hz for a random duration of 50-2,000ms. During this time, the side LEDs are lit.

I did consider noise frequencies lower and higher, but wasn’t convinced the speaker was capable of putting them out. While still well within the audible range it started to break up outside of the range I eventually chose. In any case, the noises it does make are pretty intimidating – just ask my cat.

The idea is that I’ll place this box inside the engine compartment each night. To ensure I don’t drive off with it in place, I’ll leave the hood popped – that way the car will alert me that it’s open. [UPDATE: Although it nags me about every other little bloody thing, the car doesn’t warn me that the bonnet is popped. So every time we put the Rat B@stard in the engine compartment, we’ll also put a ‘Remove before driving’ notice on the steering wheel.]

Let’s see how the furry little feckers like this…

The code

* Rat B@stard mk.1

// Pin assignments
#define SPEAKER 8
#define FLASHER1 3
#define FLASHER2 4
#define LED1 5
#define LED2 6

#define NUM_LEDS 4

#define PAUSE_MIN 5000
#define PAUSE_MAX 15001
#define LED_CYCLE_DELAY 250

uint8_t LEDs[] = { LED1, LED2, FLASHER1, FLASHER2 };

void pause() {
  int pause = random(PAUSE_MIN, PAUSE_MAX);

void random_flash() {
  int flashes = random(3,11);
  int fl_pause = random(50,201);
  flash(flashes, fl_pause);

void flash(int flashes, int fl_pause) {
  for(uint8_t i = 0; i < flashes; i++) {
    digitalWrite(FLASHER1, HIGH);
    digitalWrite(FLASHER2, HIGH);
    digitalWrite(FLASHER1, LOW);
    digitalWrite(FLASHER2, LOW);

void makenoise() {
  unsigned int freq = random(50, 5001);
  unsigned long duration = random(50, 2001);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  tone(SPEAKER, freq, duration);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);

void cycleLEDs() {
  for(uint8_t i = 0; i < NUM_LEDS; i++) {
    digitalWrite(LEDs[i], HIGH);
    digitalWrite(LEDs[i], LOW);

void setup() {
  for(uint8_t i = 0; i < NUM_LEDS; i++) {
    pinMode(LEDs[i], OUTPUT);
  flash(3, 100);

void loop() {
  int choice = random(0,10);
  if (choice < 4) {
  } else if(choice == 9) {
    flash(3, 100);
  } else {


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.