1

(Here is finally a solution).

I'm using the following code on a ATtiny45 to assign an interrupt to a button press (pin #7, PB2, INT0). However the LED doesn't blink when the button is pressed, as if the interrupt is not working. Why?

Note: I'd like to use the least power when no interrupt happens, to have a current in the order of magnitude of ~ 1 µA, to run from batteries for years.

#include <avr/sleep.h>

void setup() 
{
  pinMode(4, OUTPUT);  // LED
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);      // pin #7 = PB2
  digitalWrite(4, HIGH); delay(100); digitalWrite(4, LOW);  // LED blink, working here
}

void wake()
{
  sleep_disable(); 
  detachInterrupt(0); 
  digitalWrite(4, HIGH);  // LED not working here when pressing the button
} 

void loop()
{
  sleep_enable();
  ADCSRA = 0;  
  attachInterrupt(0, wake, CHANGE);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
  sleep_cpu(); 
}

Note:

  • I'm using Arduino IDE with damellis' attiny, I also tried with Spence Konde's ATtinyCore but it's the same

  • In my real code, I have 3 buttons, on pin #5, #6, #7, so eventually I'd like to have interrupts for all these three pins.

  • I've read https://gammon.com.au/forum/?id=11497 but using noInterrupts() / interrupts() did not change anything in my case.

  • I've tried "Sketch J" from https://gammon.com.au/forum/?id=11497 but EIFR = bit (INTF0); gives an error: 'EIFR' was not declared in this scope (I'm compiling from Arduino IDE); probably this works on an ATmega but not ATtiny.


Edit: Here is a working code of deep sleep, with an interrupt when the pin #5 (PB0) changes...

#include <avr/interrupt.h>
#include <avr/sleep.h>

ISR(PCINT0_vect) {
  if (digitalRead(0) == LOW)
    digitalWrite(4, HIGH);
  else
    digitalWrite(4, LOW);
}

void setup() {  
  pinMode(4,OUTPUT); // LED
  pinMode(0,INPUT_PULLUP);
  pinMode(1,INPUT_PULLUP);
  pinMode(2,INPUT_PULLUP);
  ADCSRA = 0; // ADC disabled
  GIMSK = 0b00100000;  // General Interrupt Mask Register, / Bit 5 – PCIE: Pin Change Interrupt Enable / When the PCIE bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt is enabled. Any change on any enabled PCINT[5:0] pin will cause an interrupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI Interrupt Vector. PCINT[5:0] pins are enabled individually by the PCMSK0 Register. / see https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf
  PCMSK = 0b00000111;  // Pin-change interrupt for PB0, PB1, PB2
} 

void loop() {
  sleep_enable();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
  sleep_cpu(); 
}

...but for an unknown reason, if I add this code:

ISR(PCINT1_vect) {
  if (digitalRead(1) == LOW)
    digitalWrite(4, HIGH);
  else
    digitalWrite(4, LOW);
}

the pin-change interrupt for PB0 still works (PCINT0), but not the interrupt for PB1 (PCINT1). Why?

3

Look at the datasheet of the ATtiny45. In the section “Sleep Modes”, there is a table that lists the wake-up sources available for each sleep mode. For the mode “power-down”, INT0 is listed as a possible wake-up source, but there is a small footnote:

For INT0, only level interrupt.

This means that the CHANGE mode you are trying to use will not wake-up the MCU out of power-down. You have to instead

attachInterrupt(0, wake, LOW);

In my real code, I have 3 buttons, on pin #5, #6, #7, so eventually I'd like to have interrupts for all these three pins.

You may then have to look into the “pin-change” interrupt. This is something different from the INT0 you are using here, and is not generally supported by the Arduino core. You will have to either get some pin-change-interrupt library, or work with no library using the information from the datasheet (not that hard). Pin-change interrupt is also a wake-up source for power-down.


Edit: Answering extra questions from comments.

How did you know that "only level interrupt" means only "LOW"?

The Arduino documentation states that the mode parameter of attachInterrupt() can be either LOW, CHANGE, RISING or FALLING. A few boards also support HIGH, but the ATtinies are not among them. A “level” can be either LOW or HIGH, whereas CHANGE, RISING and FALLING are “edges”.

This is corroborated by the ATtiny45 datasheet: “The INT0 interrupts can be triggered by a falling or rising edge or a low level.”

Is pin-change interrupt PCINT?

Yes.

why do you think it isn't supported by the Arduino core?

The attachInterrupt() API only really makes sense for INT0, INT1, etc... not for PCINT. You can also look at the source code.

Isn't it standard to interrupt when a pin changes states?

If you wish, you can configure INT0 to interrupt on a rising edge, a falling edge, or any logical change. But this uses an edge-detection logic which relies on the main clock, and thus does not work in power-down mode.

For more details on INT0 vs. PCINT, their differences, and how to use them, please refer to the datasheet, specifically to the “External Interrupts” section I linked to above.


Addendum: Extra question:

why it doesn't work [with ISR(PCINT1_vect) {...}]?

Gcc tells me “warning: ‘PCINT1_vect’ appears to be a misspelled signal handler”. And indeed, there is no such interrupt vector on the ATtiny45: there is only PCINT0_vect, and this interrupt is shared by all the pins that can generate a pin change interrupt. If you want to know which pin triggered the interrupt, you will have to figure that out within the interrupt handler itself.

This is the single point that makes the PCINTs trickier to work with than INT0 and co.: each PCINT interrupt is shared by a group of pins. On the ATtiny45, there is only one such group, and PCINT0_vect is the only interrupt handler. Larger AVRs have more groups (3 on the Uno/ATmega328P), and PCINT1_vect makes sense on them.

Also beware of the somewhat confusing naming scheme. Avr-libc defines some macros PCINTn, where n is a number. These macros expand to bit numbers, and are meant to be used in code like this:

PCMSK = _BV(PCINT0)   // sense changes in pin PCINT0 = PB0...
      | _BV(PCINT1)   // and PCINT1 = PB1...
      | _BV(PCINT2);  // and PCINT2 = PB2

In these macros, the number n identifies an individual pin. Avr-libc also defines the macros PCINTm_vect for defining interrupt handlers. In these macros, the number m identifies a pin group.

| improve this answer | |
  • Thank you for your answer, and for the update! – Basj Nov 19 '19 at 20:27
  • I edited my question and added some working code of interrupt for PCINT0. Do you know why it doesn't work for PCINT1? – Basj Nov 19 '19 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.