ATtiny13 – clap clap switch

This project shows how turn ATtiny13 with sound detector module into smart Clap Clap Switch. The project has been designed to react on only double claps (clap clap – ON, clap clap – OFF). So, how it works? There is one pin for a MIC (digital output from sound detector module) and one pin for a LED (to show on/off cases). External interrupt (INT0) has been used to check microphone state. In the main loop there is an algorithm which take care of processes of detecting claps, counters, resetting states and toggling the LED. The user of this smart clap-clap switch has a wide timeframe <100ms; 1s> between first and second clap. In my opinion, It’s enough of time for a double clap but feel free to experiment with other values! The single clap / noises should not affect the process of double-clap detecting (what was the goal!). This project can be easly extended by adding extra components like for example relay to control high voltage devices. The code is on Github, click here.

Parts Required

Circuit Diagram


This code is written in C and can be compiled using the avr-gcc. All information about how to compile this project is here.

 * Copyright (c) 2019, Łukasz Marcin Podkalicki <>
 * ATtiny13/050
 * Clap-Clap Switch.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define LED_PIN PB0
#define MIC_PIN PB1

static volatile uint8_t state = 0;

    uint8_t reset = 0, claps = 0;

    /* setup */
    DDRB |= _BV(LED_PIN); // set LED pin as output
    MCUCR &= ~(_BV(ISC01)|_BV(ISC00)); // trigger INT0 interrupt when low level detected
    GIMSK |= _BV(INT0); // enable INT0 interrupt
    sei(); // enable global interrupts

    /* loop */
    while (1) {
        /* if single clap detected then start procedure */
        if (state) {
            claps++; // increment claps count
            if (claps == 1) { // if it's first clap,
                reset = 0; // set reset counter to zero
            } else if (claps == 2) { // if it's second clap,
                PORTB ^= _BV(LED_PIN); // toggle LED on/off
                claps = reset = 0; // and set initial values
            state = 0; // for each clap detection, reset state
           _delay_ms(200); // and wait 0.2s
        /* if rest counter is max (1s) then set initial values */
        if (++reset >= 10) {
            claps = state = reset = 0;
        _delay_ms(100); // loop delay


    state = 1;

Leave a Comment