Go Down

Topic: Arduino Uno not running properly when powered via USB on Laptop (Read 234 times) previous topic - next topic

msm275

I just purchased an arduin UNO, and I am having an odd issue with it.

When I run a script on the board and the board is powered by a power adapter the code runs fine. However when I power the device from a usb cable the code does not run correctly.

I am using the Arduino IDE on Windows 10.

I have verified that my usb port is outputting 5v dc.

The script I am trying to run is a very simple script.


/*script*/
const int button = 2;
const int led = 4;
int buttonState = 0;

void setup(){
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  buttonState = digitalRead(button);
  if(buttonState == HIGH) {
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led, LOW);
  }
}
/***/

When the board is powered via usb nothing happens when I press the button.
However when I touch the wire connected to pin 2 for the button the led lights up.

I am at a loss as to why this is happening, It is making it very time consuming to work on the project I would like to build because I have to disconnect from the PC and power it with the power adapter just to test any code changes.

ballscrewbob

Please read the posts at the top of this forum section as they outline details you have not included.

That you can upload is a good sign.
Your best bet is to include a schematic of what is connected and how.
Even a hand drawn one may suffice.

Bob.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

jpom

You might like to use the on board LED that is driven by digital pin 13 as your output led. Simpler.

I ran your sketch using pin 13. LED comes on when pin 2 not connected to anything, goes out when connected to GND. No reaction to me touching the wires. Same behaviour whether powered by USB or power pack.

The reference material on Digital Pins is worth reading (explore INPUT_PULLUP).

Perhaps you have a flaky USB connector on the laptop. Is the laptop battery drained?

Be patient. Developing a project is always an ongoing process of getting over hurdles. Good luck.

Go Up