Atmega328p not working after clock switch

Hello everyone,

I’m building a presence detector with a standalone atmega328p, a 433mhz transmitter and a PIR sensor.
It is supposed to stay in sleep mode when no presence is detected, but when someone passes by the PIR detection range it should wake up and send a number over RF and then it should go back to sleep.

This worked as it should with the Atmega328p with Arduino bootloader @ 16 Mhz crystal oscillator

Since this should run on 3 x AAA (4,5v in total) i reduced the clock of the Atmega using Optiboot for 8Mhz from Gammon website (Found it HERE) and changed the 16Mhz crystal with a 8Mhz ceramic resonator

I can upload the code but now it isn’t working, it just goes out of sleep mode when PIR detects motion and goes back in sleep mode after a while, the “debug” LED that i put in the circuit don’t even light up (wich did when the IC ran at 16 MHz)

What am i doing wrong?

CODE IS ATTACHED BELOW

Thanks everyone in advice for help

CODICE_RILEVATORI.ino (1.59 KB)

Hmm. I don't see a smoking gun. Don't most PIR units pull low when motion is detected, not high?

DrAzzy:
Hmm. I don't see a smoking gun. Don't most PIR units pull low when motion is detected, not high?

I'm sorry..what do you mean for "smoking gun"?

However looks like PIR pull HIGH when motion is detected..

Revengeic3:
I'm sorry..what do you mean for "smoking gun"?

However looks like PIR pull HIGH when motion is detected..

Ah - not a native english speaker?
a "smoking gun" is a definite cause for a problem (from detective stories - Smoking gun - Wikipedia - it's a very common phrase, at least in the US)

Well, start printing stuff to serial then, and see if you can figure out what's going on.
The three PIR modules I have are all open drain, and go low on detected motion.

Why are you attaching the interrupt twice?
It's being fired on change - could you be ending up in a situation where it's sleeping while motion is detected, and only waking when it's not? maybe RISING is a better choice?

DrAzzy:
Ah - not a native english speaker?
a "smoking gun" is a definite cause for a problem (from detective stories - Smoking gun - Wikipedia - it's a very common phrase, at least in the US)

Well, start printing stuff to serial then, and see if you can figure out what's going on.
The three PIR modules I have are all open drain, and go low on detected motion.

Why are you attaching the interrupt twice?
It's being fired on change - could you be ending up in a situation where it's sleeping while motion is detected, and only waking when it's not? maybe RISING is a better choice?

I'm italian so i don't understand all these words, but thank for explanation, it's always good to learn something new, even better if it's slang related!

However as you can see i attached the interrupt twice because when PIR detected motion in the previous code it dettached the interrupt (an advice that gave me long time ago in the italian forum)

Should i remove it? or i should keep the double attachInterrupt with detachInterrupt when motion is detected?

However, the IC always sleeps, it only wake up when it receive the interrupt from the PIR, then it goes back to sleep..
I'll also try using RISING event in interrupt instead of CHANGE, but i remember i didn't got it working long time ago

And also, if nothing is wrong with the code why LED and RF actions don't get fired?
This is the same code i used in another Atmega328p but running at 16Mhz as i told..

Thank you very much for your help!

Up…Please i really really need help, it’s the last step of part one of my projet…

I suggest trying a couple of things in this code:

void sleep()
{
  delay(400);
  attachInterrupt(0, vuota, CHANGE);
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_cpu ();

  sleep_disable();
}

Disable interrupts until the last moment, and make sure there is no pending interrupts. eg.

void sleep()
{
  delay(400);
  noInterrupts ();          // make sure we don't get interrupted before we sleep
  attachInterrupt(0, vuota, CHANGE);
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  EIFR = bit (INTF0);  // clear flag for interrupt 0
  sleep_enable();
  interrupts ();           // interrupts allowed now, next instruction WILL be executed
  sleep_cpu ();

  sleep_disable();
}

It is possible there is a pending interrupt which immediately gets serviced before you go to sleep, and then there is no interrupt after you sleep.

void vuota()
{
//  Serial.println("vuota");
}

Leave that commented-out, you must not do serial prints inside an ISR.

Oh god i cannot belive, this is working!!!

Thanks, thanks, million times thanks!!