Arduino only works with PC


I have an HC-SR04 module and it works fine but only with PC. It doesn’t work with a 5V supply.
Probably the problem is with the code:

#define echoPin 13
#define trigPin 12
#define LEDPin 6
int maximumRange = 20;
int minimumRange = 0;
long duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);

void loop() {

digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){

digitalWrite(LEDPin, LOW);
else {

digitalWrite(LEDPin, HIGH);
digitalWrite(LEDPin, LOW);
}void setup() {


void loop() {


Could anyone help me?
Thanks in advance.



Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, the code you post could easily be garbled and is certainly anything but easy to read.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

And - before you post code, use "Auto Format" in the Tools menu to properly present the code.