Let's Make Robots!

(Solved!) Low current from Arduino Output (Arduino Start Here robot)

 

EDIT: LED problem solved thanks to RobotFreak. I forgot to set the pins to output mode using the pinMode command. As RobotFreak points out, the Arduino sets all pins as inputs by default. I added a For loop to set all the pins to outputs, and everything worked great.

void setup() {
  pinMode(13, OUTPUT);  
  for (int pinindex = 0; pinindex < 14; pinindex++) {
     pinMode(pinindex, OUTPUT);  // set pins 0 to 13 as outputs
  }

I have updated the attached code with the working version.

Note that the USB still resets when the board is not connected to external power and the servo is commanded to move. I suspect the servo draws close to 500mA or whatever my laptop's USB port can support.


I'm working on an Arduino version of the Start Here robot. I haven't got all the hardware yet, but I'm testing out the code on my Arduino Mega clone. I've got an LED that I'm using on different outputs to test their state during program operation.

Here's the weird thing. The LED is running really dim. I can barely see it. At first I thought maybe my cheap Chinese knock-off copy of the Mega was having a problem. I had previously tested all the outputs and they were fine. However, when I download the standard "Blink" program to the Mega with the the output pin changed to my LED pin, it works fine.

So now I suspect something in my code is weird. This is where you come in, my friends. ; j

I've attached my code and would appreciate a quick review to see if I've done something foolish. Here's some info on my setup and things I've tried so far.

Setup

  • LED and 150 ohm resistor on Output 7 (one of the motor outputs), with other side going to ground
  • Arduino connected to USB port
  • Arduino powered by external wall wart: 12VDC, 2A
  • Dagu mini servo connected to pin 9 (plus power and ground, of course)
  • Sharp GP2Y0A21YK IR sensor connected to Analog pin 0 (pin 54 on Arduino Mega)
  • Attached code pulls all unused Analog ports to HIGH using internal pullups
  • Built in board LEDs are lit up nice and bright

Observations/Symptoms

  • LED dim when running on pin 7 or 5 (motor outputs that are high during normal operation)
  • LED lights up bright if I connect to pin 9 in parallel to servo, or directly to board 5V supply
  • Running attached code, if I disconnect the external 12V supply, my computer keeps dropping the USB connection. I suspect I'm drawing more than the 500mA the USB is rated for. Works find with the external supply.
  • Running standard Arduino 'Blink' program with output on pin 8, LED lights up nice and bright.
  • Servo and Sharp sensor seem to work fine.

Stuff I've tested

  • I've tried commenting out the For loop that sets all the internal pullup resistors: no effect
  • I've tried commenting out all the servo commands: no effect
  • I've tried commenting out the Serial.begin command: no effect
  • I've tried phyically disconnecting power to the servo and to the Sharp: no effect
  • I've tried adding a digitalWrite command to set an unused output to HIGH and moved the LED to that output: LED lights dimly
  • I've tried asking for help from you guys: Effect to be determined ; j

If there's a problem with my code, I can't see it. I'm going to try to wire up the SN754410NE motor driver and see if outputs 7,6,5,4 are providing enough current to drive the logic. I'll post an update when I have some results. In the meantime, can anyone check over my code?

I'm also open to any suggestions to changes in my code. I'm pretty new to the Arduino. I'm not trying to do anything fancy with this one yet. Just trying to replicate the posted Start Here robot. I'll elaborate on the code and physical robot design later.

 

AttachmentSize
starthere_-_working_now.pde3.41 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
TinHead's picture

... I lost a full day once trying to figure out exactly the same thing ... late in the night I finally found my problem: forgot to set the pin as output ... doooh:)

We should have a page to write this stuff down ... a check list of all the little stupid things that prevent smart things to work :D I mean you write the code, do all the soldering ... debug like crazy to find you forgot a stupid little line ....

ignoblegnome's picture

I broke out my multimeter, the Swiss Army Knife of troubleshooting. ; j

Pin 7 (normally HIGH) is putting out 4.99V during normal operation. However, when I connect the LED-resistor pair, it drops to 1.76V.

Pin 6 (normally LOW) is putting out 0.75V. When I connect the LED-resistor pair, it goes up to as high as 4.12V.

This is crazy! That same LED-resistor pair work fine in the 'Blink' program.

OddBot's picture

I'm waiting for your Avatar to go "Thumbs down!" :P

Other than that, welcome to my world. I regularly have the problem that I build a prototype and it works fine but when the first samples come off the line I have all sorts of weird and wonderful problems to solve.

As Sherloch Holmes once said "When you have eliminated the impossible, whatever remains, however improbable, must be the truth!"

Just to eliminate it as a possibility, use a different LED and resistor for testing. If a LED gets too much mechanical stress on a leg or heat from soldering then it can do some weird stuff.

One problem I had a bit like this turned out to be a hair thin trace in the tracks that shorted two servo outputs together causing the servos to do weird and not so wonderful things. I have also had a hair thin break in a track that caused the output to read ok when the only load was a high impedance multimeter or oscilloscope probe but die when you put the slightest load on it. In both cases it took a lot of patience and eyestrain to solve the problem.

Another possibility is a dry solder joint on the processor. it can be hard to track down. Use the datasheet to locate which of the processor pins is responsible for a troublesome output. Then use something like a multimeter probe or even a sewing needle mounted in a cork (makes it easier to hold) to apply pressure to that joint and see if the LED changes brightness.

Failing that, see if you can borrow another Arduino Mega for comparison.

ignoblegnome's picture

Though the problem turned out to be software, I appreciate your detailed response. You have great experience, and it is always helpful to hear how you've addressed problems you've run into. Thanks for the help.

OddBot's picture

Sorry I didn't glance harder at your code then.

RobotFreak's picture

I didn't see in your program any initialisation for your IO pins. You must set at least the output pins as output with the pinMode command. If you only call digitalWrite you will only switch the pullUp resistor on and off. All pins are inputs by default.

ignoblegnome's picture

That was it! So obvious!. Thanks, RobotFreak.

ignoblegnome's picture

I just did another test. I powered up the board with both USB and external power connected. Then I disconnected the external supply. The USB connection did not drop. However, as soon as I put my hand in front of the Sharp (which causes the robot to stop and turn the servo), the USB dropped. Each time the servo turns, the USB dropped.

Next I commented out all the servo commands. Then when I placed my in front of the servo, I could see the LEDs indicating a stop and look left-right sequence. But the USB didn't drop because the servo wasn't in operation.

My conclusion is that the servo is definitely pulling enough current when operating to cause a problem on the USB power. However, since my LED is still dim without the servo, the USB dropping issue may be entirely separate from the dim LED issue.

Ro-Bot-X's picture

The code is good, it should work fine.

Try to connect the LED this way:

- when the pin is Low, connect the LED (and series resistor) from pin to Vcc

- when the pin is High, connect the LED from pin to GND.

I designed a motor driver board some time ago that had a bi-color LED (just 2 pins) that I wired with a series resistor between the motor driver input pins directly and it was working fine, it even fade in at PWM acceleration. So it should work correctly.

I use bright LEDs for my boards so I wire a 1k resistor in series with them. This way the current is kept at a minimum and the LEDs shine brightly.

ignoblegnome's picture

Good idea.

I tried connecting the LED from VCC to pin 6, which should be LOW when the robot is driving forward (normal operation). Here's the weird thing... the LED is not lighting at all. At least not perceptively. Since it barely lit when wired from a HIGH pin to GND, it may be that the voltage drop from VCC to a LOW pin is slightly less than in the other direction, and it was enough of a difference to not light the LED.

The LED lights fine wired from VCC to GND instead of VCC to a LOW pin.