ATTINY 85 analog read problems

I'm trying to implement the knock example on an ATTINY 85 to turn an led on and off after alternate knocks. I'm using a piezo contact mic with a 1M ohm resistor in parallel to it. I have connected one of the contact mic wires to pin 4 (Analog pin 2) on the ATTINY85 and the other to GND. I have also connected a standard led with a 220 ohm resistor to pin 3 of the ATTINY85 to turn it ON and OFF on alternate knocks. I am using the TinyAVR Programmer to program it.

The led switches ON after the first knock for a moment and turns OFF again automatically without me using a knock. I have no idea what I'm doing wrong!

The circuit works perfectly well on an Arduino UNO.

My code is as follows:

int a = 0;
int led = 3;
void setup() {
  // put your setup code here, to run once:
  pinMode(0, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(A2, INPUT);

void loop() {
  // put your main code here, to run repeatedly:

  digitalWrite(0, HIGH);

  int piezo = analogRead(A2);

  if (piezo > 5) { //minimum analog input value of 5 to filter out random stuff
    if (a == 0) {
      digitalWrite(led, HIGH);
      a = 1;
    else if (a == 1) {
      digitalWrite(led, LOW);
      a = 0;

The circuit works perfectly well on an Arduino UNO.

Have you changed the wiring from the Uno?
Can you post a photograph of your wiring please.

The wiring hasn't changed at all, which is what is confusing me. I'll be able to post a picture tomorrow.

The fact that you swap out an Uno for an ATtiny means you have changed wiring (even if the overall circuit is basically the same) and most likely also even the power source.

Please post complete circuit diagram.

The ATtiny needs a filter cap between Vcc and GND, and probably a pull-up resistor on RESET for stability.

Increasing the piezo threshold may also help. 5 is a very low value. What is the actual reading you get when knocked?