Fork me on GitHub

Project Notes

#605 ATtiny Analog Comparator

Testing sound-triggered interrupts with the ATtiny85 built-in analog comparator

Build

Here’s a quick demo..

clip

Notes

The Analog Comparator is introduced in section 16 of the ATtiny datasheet:

The Analog Comparator compares the input values on the positive pin AIN0 and negative pin AIN1. When the volt- age on the positive pin AIN0 is higher than the voltage on the negative pin AIN1, the Analog Comparator output, ACO, is set. The comparator can trigger a separate interrupt, exclusive to the Analog Comparator. The user can select Interrupt triggering on comparator output rise, fall or toggle.

The ACSR (Analog Comparator Control and Status Register) determines the behaviour of the Analog Comparator.

In the program setup, we:

  • disable multiplexed input to the comparator, so AIN1 is used as negative input
  • clear any existing comparator interrupts
  • enable Analog Comparator interrupts
  • select rising-edge interrupt

The code defines an interrupt service routine on ANA_COMP_vect.

Pins:

  • AIN0 - PB0, pin 5 on ATtiny85 DIP8
  • AIN1 - PB1, pin 6 on ATtiny85 DIP8

The Test Circuit

This is simple test:

  • a voltage divider (variable resistor) sets up a threshold reference on AIN1
  • amplified electret signal is fed to AIN0
    • 10kΩ bias for the electret, AC-coupled input to the OpAmp
    • TL072 OpAmp configured as a high gain inverting amplifier
    • a 100nF cap on AIN0 provides some hold-time for spikey input signals .

Rising, Falling, Change Interrupts?

Setting ACIS1, ACIS0 bits select the interrupt to trigger:

ACIS1 ACIS0 Trigger
0 0 Toggle
1 0 Falling
1 1 Rising

So Does it Work?

Yes! The comparator interrupt is excellent, and beats any other approach for speed, processing overhead and lack of external circuitry.

Here is a trace of the behaviour:

  • CH1 (yellow) - AIN0
  • CH2 (blue) - AIN1

scope

Construction

Breadboard

Schematic

Credits and References

About LEAP#605 ArduinoAVRSensorOpAmp

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

Project Source on GitHub Return to the LEAP Catalog
About LEAP

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.

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 pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader