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 Return to the LEAP Catalog

LEAP is just my personal collection of electronics projects - 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.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics 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.

For a while I have also included various scale modelling projects here too, but I've now split those off into a new repository. Check out LittleModelArt if you are looking for the modelling projects!

Project Gallery Notebook LittleCodingKata LittleModelArt More on my blog