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 ArduinoAVRATtiny
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 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 (IMHO!).

The projects are usually inspired by things found wild on the net, or ideas from the sources such as:

Feel free to borrow liberally, and if you spot any issues do let me know. See the individual projects for credits where due. There are even now a few projects contributed by others - send your own over in a pull request if you would also like to add to this collection.