Unusual behaivor with PIR sensing

I've an Uno with many connections, mostly to reed switches and the install works well.

However, after a few minutes a pin begins to become noisy. That is, there is zero tone() calls in the code, yet a noise is heard through a speaker attached to it. I did have tones being played and removed all calls to try to get rid of the sound. It is an undulating whine, not coming from anyone such as wife.

The effect is directly related to PIR trigger. The PIR shares a 12V+ power supply and ground with the Uno.

Think the board is toasted?

Do you have any analogWrite() in your code? On the UNO, the default frequency is relatively low, in the audible range.

For more help, read my signature below...

No anlogReads() or writes, i do believe thats around 490hz or some such thing. I had them in there but took them out on an earlier version. I got frustrated and cut the base of transistor to speaker, effectively muting the whole thing. Problem is, it was designed to make noise. Calls to beep(x,y) would make a sound of x hertz and y loops duration, but the function was gutted for diagnostic purposes.

UPDATE: I cut line from PIR and it still was squealing, I then figure it's doing it when it is lighting an LED through a 220ohm restistor, as if the squealing on pin A1 is "pain" of having to drive a 20ma or so load. Sound is about 700-1000Hz, undulating.

Thanks... its a curious little problem.

332 lines.... it won't let me post the code!!! Too long...

You can attach the code to the post. Use the full editor that comes up when you hit "preview".

mattlogue:
The effect is directly related to PIR trigger. The PIR shares a 12V+ power supply and ground with the Uno.

Probably not related, but are you sure the PIR you have doesn't mind being powered by 12volt.
Safer to power a common HC-SR501 PIR sensor (if that's what you have) from the Uno's 5volt pin.
Leo..

mattlogue:
I've an Uno with many connections, mostly to reed switches

Just to make sure: do your really mean "reed switches" or is that a typo of "read switches"? That one letter gives your sentence a quite different meaning!

Schematics and code needed for any chance of a diagnosis.

Thanks. I will post code in two posts. Meanwhile I’ll work out a schematic…

I did mean reed switches. The PIR is happy with 12V per datasheet.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

// New7 - Removed beep function.
/*
   ToDo:
      New RJ11 Headers "C" for wn7 and pr7?
      Once established, wire A0 to Audio Amp (connects via RJ11 "C")
      Try to re-program G4 opener
      Add functions for:
        rd8 - Reed 8
        LB8 - LED Blue
        pr8 - PIR Outside
      Fix wierd sounds!

*/


//*****************************************************************************************************************************************************************
// -1: InputPullup, 0: Not installed, 1: Input Positive, 2: OUTPUT
const int installed[20] = {   0,  0,       0,   -1,    -1,     -1,    -1,     2,     0,    0,   1,     2,    2,      1,    2,     2,      2,     -1,    0,     0 };
const char* pinNames[20] = { '0', '1',     '2', '3',   '4',   '5',    '6',   '7',    '8', '9', "10",  "11",  "12",  "13",  "A0",  "A1",  "A2",  "A3",  "A4",  "A5"  };
const char* ssNames[20] = {  "Rx", "Tx",  "x", "mc7",  "bo7", "dr7", "pb7", "PX7",   "x", "x", "gl7", "LG8", "LR8", "pr8", "SPK", "SPX", "LB8", "rd8", "x", "x" };
const int                                            bo7 = 4,      pb7 = 6,  PX7 = 7,             LG8 = 11,       pr8 = 13,       SPX = A1,    rd8 = A3 ;
const int                                      mc7 = 3,        dr7 = 5,                      gl7 = 10,      LR8 = 12,     SPK = A0,      LB8 = A2;
// *****************************************************************************************************************************************************************
int arrMax = 20, beeps, beepLast, deBeounceMS, dirc, i, hertz, ledState, loops, mc7Reads, parsed, pr8Hits, pexUnlock, pirCount, beepOn, speaker, shoves, target, volume;
float loudBeeps, pirQ;

long unsigned doorMS, dsAlarmMS, pexPush, pirMS, pr8MS;
String eventName, eventOld;

int now[30] = { 0 } ; //Set prior to parse
int was[30] = { 0 } ; //Set after parse
int last[30] = { 0 }; //Millis of last trigger (state change)

const int deBounceMS = 30; // Debounce delay
const int delayMS = 15; //Loop Delay
const int loopsMax = 5; //Secondary Loop Divider
const int mc7ReadsMax = 20; // # loop delay for magnet contact alarm
const long unsigned noKeysDelay = 120000; // Delay to unlock once
const int delayDoorOpen = 30100; // Please Close Door Delay
const unsigned long pirDelay = 20123; // Duration to trigger PR8 alert

// Frequncies for event tones
const int hzCloseDoor = 400;
const int hzMagNonContact = 500;
const int hzNoKeys = 1200;
const int hzDoorShove = 700;
const int hzDoorOpen = 700;
const int hzBolting = 600;
const int hzPexPushed = 1100;
const int hzPirMotion = 200;
const int hzKeyIn = 1200;
const int hzDoorbell = 1600;

// Beep repetitions
const int repCloseDoor = 2;
const int repMagNonContact = 2;
const int repNoKeys = 4;
const int repDoorShove = 20;
const int repDoorOpen = 2;
const int repBolting = 1;
const int repPexPushed = 1;
const int repPirMotion = 1;
const int repKeyIn = 3;
const int repDoorbell = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pexPush = 99999999;
  pexUnlock = 0;
  doorMS = 999100200;
  loops = 0;
  loudBeeps = 0;
  beeps = 0; //No. of beeps left in current tone pattern, zero at init.
  beepOn = 0;
  volume = 0;

  for (i = 0; i < arrMax; i++) {
    // Array init loop

    // Set pin-modes based on installed flags
    if (installed[i] == -1) {
      pinMode(i, INPUT_PULLUP);
    }
    if (installed[i] == 0) {
      pinMode(i, INPUT_PULLUP);
    }
    if (installed[i] == 1) {
      pinMode(i, INPUT);
    }
    if (installed[i] == 2) {
      pinMode(i, OUTPUT);
    }
  }
  digitalWrite(PX7, LOW);
  lcd.init();
  lcd.print("Node 7 - Starting");
  lcd.backlight();
}

Part II/III+

void loop() {
  int readVal;
  loops = loops + 1;

  if (loops > loopsMax) {
    loops = 0;
    loop2();
  }
  beep(0, 0);
  for (i = 0; i < 20; i++) {
    delay(delayMS); // DELAY #1
    if (i < 20) {
      now[i] = digitalRead(i);
    }

    if (abs(installed[i]) == 1) {
      if (now[i] != was[i]) {
        //state change!
        delay(delayMS); // DELAY #2
        if (now[i] == was[i]) {
          goto next;  //If switch back, skip down.
        }
        // DeBounce
        if ((millis() - last[i]) < deBounceMS) {
          goto next;
        }
        last[i] = millis();
        inParse();
        Serial.println(i);
      }
    }
    was[i] = now[i];
next:
    int y = 1;
  }
  //State Checks - Lowest Priority First


  //Magnet Non-Contact Warning
  if ((digitalRead(mc7) == HIGH) && (digitalRead(dr7) == LOW)) {
    mc7Reads ++;
    if (mc7Reads > mc7ReadsMax) {
      eventName = "Magnet Non-Contact Warning";
      volume = 1;
      beep(repMagNonContact, hzMagNonContact);
    }
  }


  //Timers
  if (millis() > doorMS) {
    beepOn = 1;
    eventName = "Please Close Door!";
  }
  if ((millis() - pexPush > noKeysDelay) && (pexUnlock == 0)) {
    beep(repNoKeys, hzNoKeys);
    pexUnlock = 1;
    eventName = "NoKeys Unlock";
    digitalWrite(PX7, HIGH);
    delay(100);
    digitalWrite(PX7, LOW);
  }
  if (millis() - pirMS > pirDelay) {
    //turn off PIR indicators
    pirMS = millis() + pirDelay;
  }

  if (millis() - pr8MS > pirDelay) {
    //turn off PIR indicators
    pr8MS = 999999123;
    pr8Hits = 0;
  }

  eventOld = eventName + ">>" + eventOld;

}
void loop2() {

  if (digitalRead(bo7) == LOW) {
    digitalWrite(LR8, HIGH);
    delay(50);
    digitalWrite(LR8, LOW);
  }
  if (digitalRead(bo7) == HIGH) {
    digitalWrite(LR8, LOW);
    delay(50);
    digitalWrite(LR8, HIGH);
  }


  delay(25);

  if (eventName == "Please Close Door!") {
    volume = 1;
    beep(repCloseDoor, hzCloseDoor);
  }
  if (digitalRead(pr8) == HIGH) {
    pr8Hits++; //Increase pir 8 hit count for every secondary cycle
  }

  if (loudBeeps < 0) {
    loudBeeps = 0;
  }
  lcd.clear();
  lcd.print(eventName);
  lcd.backlight();
  eventName = "";

}

Final Part III

void inParse() {
  Serial.print(ssNames[i]);
  if (millis() < 5000) return; //Start-up clearance period.
  dirc = -1;
  if (was[i] == 0) dirc = 1;
  parsed = 0;
  // This function will parse input to perform function

  if (ssNames[i] == "gl7" && dirc == -1) { // Green Light on!
    digitalWrite(LG8, HIGH);
    //See if push button was pressed recently
    if ((millis() - last[6]) > 15000) { //ABSOLUTE REF!
      eventName = "Key In";
      beep(hzKeyIn, repKeyIn);
    }
    parsed = 1;
  }
  if (ssNames[i] == "gl7" && dirc == 1) { // Green Light off!
    digitalWrite(LG8, LOW);
    parsed = 1;
  }
  if (ssNames[i] == "pr8") {
    pr8Hits = pr8Hits + 2;
    if (pr8Hits < 3) {
      pr8MS = millis(); // Set time to unset hit counter
    }
    if (pr8Hits > 9) { // Trigger
      beep(hzDoorbell, repDoorbell);
      pr8Hits = 0;
      eventName = "Doorbell";
      parsed = 1;
    }
  }

  if (ssNames[i] == "dr7" && was[i] == 1) { // Door Close
    eventName = "Door Closed";
    doorMS = 999100200;
    parsed = 1;
  }
  if (ssNames[i] == "dr7" && was[i] == 0) { // Door opens!
    doorMS = millis() + delayDoorOpen; //Set timer for door open alarm
    eventName = "Door Open.";
    beepOn = 1;
    beep(repDoorOpen, hzDoorOpen);
    parsed = 1;
  }

  // Door Shove Alarm Requires Green Light
  if (ssNames[i] == "bo7" && dirc == 1) {
    eventName = "Bolting.";
    beepOn = 0;
    beep(repBolting, hzBolting);
    parsed = 1;
  }
  if (ssNames[i] == "bo7" && dirc == -1) {
    eventName = "UnBolting.";
    parsed = 1;
  }
  if (ssNames[i] == "pb7" && dirc == 1) {
    pexPush = millis();
    pexUnlock = 0;
    beepOn = 1;
    beep(repPexPushed, hzPexPushed);
    eventName = "PEX Pushed.";
    parsed = 1;
  }

  if (ssNames[i] == "pr7" && dirc == 1) {
    eventName = "PIR Motion";
    parsed = 1;
    pirMS = millis();
  }

  if (parsed == 1) {
    eventOld = eventName + ">>" + eventOld;
    Serial.print("eventOld:");
    Serial.println(eventOld);
    lcd.clear();
    lcd.print(eventName);
    lcd.backlight();
  }
  if (parsed == 0) {
  }
}
void beep(int hz, int rep){}

Nothing obvious in there that causes sounds. The very short variable names are not helping with any understanding of what it's supposed to do when skimming the code. Some really odd constructions, especially around that installed array.

I'd try by disconnecting everything, letting just the code run. See what happens. No sounds? Connect the inputs one by one and trigger them. See what happens. Still no sounds? Continue with the outputs.

Then when you have the thing that triggers the sound, disconnect everything else and see if it persists. It may be a combination of things.

I tried unhooking everything first and no odd sounds. My learning is a bit crippled through depression, although that installed array helps visualize what pins do what. It directs pinMode in a latter loop. The stuff is tabbed with whitespace to allign everything. Its a lot of crap IMO below.

"It may be a combination of things" - that's what I fear. It is somehow correlated to the blinking of LEDs. They blink and the thing emits a strange shriek akin to the "most annoying sound in the world". The speaker isn't passively driven, it is driven by a PNP transistor the gate is attached to A0 through 220ohm resistor. I used a PNP as it suited the installation much better to go against ground for the speaker.

I've been safe with the board, only took a solder iron to it for 2 seconds total, have a ground clip dangling and ground plane throughout a paperboard sandwich.

Thanks for taking your time to look at it.

You basically keep the software going at all times. Connect parts one by one (the speaker first) until you hear the noise. I assume at least it's the speaker where it comes from.
To see if it's a combination of things: the moment you start hearing the noise, the component you added last is the suspect. Now start removing everything else one by one. If it's a combination, it should sooner or later stop. If not a combination, it persists until you remove that one offending part.
Those LEDs, are they driven by the pins or do you have a separate driver?