Fork me on GitHub

Project Notes

#529 ATtiny Pin Change Interrupts

How to use pin change interrupts with the ATtiny85

Build

Here’s a quick demo..

clip

Notes

The ATtiny only has one hardware interrupt pin (PB0) but since this is often used for other purposes (like I²C), handling interrupts on other pins needs to be done using the pinc change interrupt (see 9.2 External Interrupts in the datasheet).

This example demonstrates pin change interrupts being used to detect and debounce a pushbutton. On every change, and LED is toggled.

Setting up Pin Change Interrupts

Two steps required:

  1. The relevant bits of the Pin Change Mask Register (PCMSK) register are set to enable interrupts for pins PCINT0 to PCINT5

  2. Enable Pin Change Interrupt by setting bit PCIE in the General Interrupt Mask Register (GIMSK) register

The ATtiny hardware support for the Arduino IDE includes a number of macros to help generalise the register control, including:

  • digitalPinToPCMSKbit for setting PCMSK register
  • digitalPinToPCICRbit for setting GIMSK register

Pin definitions, with specific reference to the 8-pin PDIP8 ATtiny85:

Interrupt pin Port bit PDIP8 Pin
PCINT0 PB0 5
PCINT1 PB1 6
PCINT2 PB2 7
PCINT3 PB3 2
PCINT4 PB4 3
PCINT5 PB5 1

Example Sketch

The PinChangeInterrupts.ino sketch is a simple demonstration of pin change interrupts. It uses the ATtiny register macros to setup the registers accordingly.

The ATtiny85 is programmed using the Arduino ISP sketch running on an Arduino Uno. See LEAP#070 ProgrammingWithArduinoISP for more info.

Construction

Breadboard

Schematic

Build

Credits and References

About LEAP#529 ArduinoAVR
Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.