Problem with pullup/L293D

I hooked up a simple gear motor to an L293D, with pin 13 to a switch, 12 to enable, 11 to input 1 and 10 to input 2. Vss to +5 and Vc to Vin (Because I'm guessing it can do more current)

Here's my code

#define buttonPin 13
#define enablePin 12
#define forwardPin 11
#define backwardPin 10

void setup()
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // Enable pullup resistor
  pinMode(enablePin, OUTPUT);
  pinMode(forwardPin, OUTPUT);
  pinMode(backwardPin, OUTPUT);
  digitalWrite(enablePin, HIGH); // Motor enabled
  digitalWrite(forwardPin, HIGH);
  digitalWrite(backwardPin, LOW);

void loop()
  int button = digitalRead(buttonPin); 
  Serial.print("Read switch input: ");
  digitalWrite(forwardPin, button);
  digitalWrite(backwardPin, !button);

problem is that the pullup is enabled, but in the serial monitor reads 0 even though there's a pullup. Basically the opposite of what I would expect happens. I need a pulldown to get it to switch directions, and when it's hooked up to the switch in the proto shield the switch does nothing (since it read 0 before anyways)

Anyone know what's going on?


Did you connect the switch to +5V or GND ?

If you have the pullup resistor enabled, the switch should go from the pin to GND. You can simulate the switch with a fire for testing.

Pin 13 is a bad idea for an input, as the L-LED is connected to it. So even if you enable the internal pullup resistor, the LED will suck it to ground anyway as the LED resistor is 1k.

If you turn on the internal pullup resistor on pin 13, the L-LED will light up very faintly and the voltage at the terminal is just 1.75V. TTL levels for 5V devices require quite a bit more to read HIGH on an input.

Depending on what kind of motor you're using, you're going to want to use a different power supply to power the chip. Drawing too much current is not good, damage your pins quite quick. Both grounds together of course. I think I might have taken out one of my pins on my 168 this way.. my first week :D lol

Ok I'll try another pin. I had the switch connected to ground, but I connected it to 5v and it behaved completely backwards of what I would expect.

If I'm using Vin as the power source, why do I have to worry about burning out pins?

and it;'s a solarbotics GM4, and the l293d has diodes so I assumed I would be ok. Current draw at 5v is 83mA and stalls at about 1 amp (which I wouldn't let it do). Vin (powered through USB) should be able to do that, no?

The backward behaviour is normal when using pullups and the switch to GND. The pin will read LOW when the button is pressed and HIGH when unpressed.

You can amend this if you introduce a small define in your code:

#define BUTTON 8 /* anything but pin 13 ;-) */

if ( digitalRead(BUTTON) == PRESSED ) {
  /* your code */

Well 500mA is the absolute maximum current draw you can get from the USB port. But many EE experts on this forum would advise you not to push it too hard and maybe even get a separate supply for the motor (and some capacitors, because capacitors are good anyway - just like muffins). Just recently I’ve watched a video on youtube elaborating how to blow pins on chips and why it happens so easily. It’s called SCR latchup, quite interesting. Sometimes parasitic devices help to do voltage level converters with MOSFETs, sometimes they blow up chips :wink:

Ohh, I see, you're powering it through the USB, not the actual board. Cause as far as I'm aware, from 5v on the board, the max you can pull is 50ma? But I'm not 100% sure about the VIN.. but if you have it plugged into USB, and using another USB for power, becareful, I've heard of people having problems with the chip warming up ( I think the power switchage ) It's not so much the voltage back fire (haha I know theres a real name for it.. forgot), but the current pull. The L293D will take as much current as it takes to run the motor. (And note there's a 1.3v? drop after the 293)

Take a look at this link, has amazing tutorials for just this. Might be a little extensive, but it'll give you some insight :D

Yeah changing it from pin 13 worked like a charm. Should have through that through. If/when I make a robot it'll have its own power source, I was just fucking around with my new h-bridges.

Thanks for the help guys