Code not running properly from external power

Hello,
first post from me :D.

I am building a clap circuit from this instructable: http://www.instructables.com/id/The-Viciously-Simple-Clap-ON-Clap-OFF-Circuit-For-/. I did write code for two claps on, two calps off.:

const int transistorpin = 2;
int sensorvalue=0;
int val=0;
int eventstatus = 0;



void setup () {
  pinMode(transistorpin, OUTPUT);
}

void loop() { 
  eventstatus = 0;  
  sensorvalue = analogRead(A2); 
  val = analogRead(A3);
  val = map(val, 0, 1023, 3, 10);
  delay(1);
  
  if(eventstatus==0){
    if(sensorvalue>val){
      eventstatus=1;      
    }
  }
  
    if(eventstatus==1){  
    for(int k =10; k>0;k--){
      delay(10);
      sensorvalue=analogRead(A2);
      if(sensorvalue<val){
        eventstatus=2;
        break;
      }      
    }
  }
  
  if(eventstatus==2){
    for(int n=500;n>0;n--){
      delay(1);
      sensorvalue=analogRead(A2);
      if(sensorvalue>val){
        eventstatus=3;
      break;
      }
      
    }
  }
  
  if(eventstatus==3){
    digitalWrite(transistorpin, !digitalRead(transistorpin));
  }
}

My problem is that everything works smooth when the arduino is pluugged in in the PC via USB cable. But when i plug in external power (The DC jack) the LED on pin 2 just flickers very rappidly. I want to use the Arduino closed up with just a power supply and a relay instad of the LED, but it’s not working for some reason.
Hope you can help me. And sorry if maybe wrong category.

What is the specified output voltage and current of your external supply?

My power supply is 12 V 3A

Have you measured Vcc (5V)?

I did, i plug in 12V and i get 5V on the 5V pin.

I would disconnect everything, load the blink example, and try that with the 12V supply. If it is something about the supply it should show up. If not, connect the other circuits one at a time to see what is causing the failure. Can you post a schematic?

What is actually connected to pin2. A transistor, or a LED, or a transistor driving a high-current LED, or.. What else is connected to pins, or the 5volt rail. Arduino's (Uno?) 5volt supply can't deliver more than ~150mA when powered from 12volt (DC jack) before the onboard regulator overheats and shuts down (restarts). Leo..

I tried the blink example and it works fine on the external power supply. I just have a LED on pin 2 with a 300 ohm resistor.

Here is the shematic in the attachment.

I had same problems with external supply. My 2 general observations, might help you:

  1. check if power is neat and clean. (noisy power can cause problems)
  2. Check for ground loops (google star grounding) - (check setup with removing with most needed components) or check all things one by one.

try running other example programs to check whether its programming or circuit

The power is clean, and about the ground loops, I did try to power everythign straight from the Arduino, I tried connecting the ground from the microphone to the DC jacks ground, still the same effect. I really have no idea what the problem is. I even tried just applying 5V to the USB cable, without the data but the effect stays the same.

Anyone have an idea what is wrong?

I would start loading a simple Blink sketch and see what happen with the external psu, or try another psu.

If it work as it is supposed to, I would upload the code but hooking up only 1 thing per time until I get to the actual problem (which in my opinion is a ground problem).

I managet to figure it out. It was the power supply. The point is it works on all other examples but not on this circuit for some reason. I assembled the circuit and plugged in another power supply (9V 1A) and it works like a charm. Thanks everybody for the help.