• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

L293D is not switching directions

Not open for further replies.


New Member
Hi all,

I'm building a white-line follower based on a PIC16F628A and a L293D IC.

My idea, before connecting the sensors stuff, was testing some movement sequences (PORTB = X, delay t seconds, PORTB = Y, delay t seconds...) but it's not working: the L293D looks like it can only run on the first value assigned to PORTB, I mean, it's X all the time, like if there was an infinite loop after the first PORTB = X.

I've tried it separately controlling the L293D with an Arduino and it worked like a charm, and I've tried to remove the L293D and attached some LEDs and the LEDs work as the L293D should.

The setup is really simple: B0 and B1 control right DC motor, B2 and B3 control left DC motor. L293D is always enabled with Ven attached to 5V all the time.

I've tried programming it in C (CCS compiler) and in ASM (MPLAB's default) and the behaviour is the same.

Any idea? Should I add any other component? (capacitors, resistors...)

Thanks in advance.


New Member
Hi Blueroomelectronics,

I don't have any schematics available right now, but as I said the setup is really simple: B0 and B1 are connected to phase 1 inputs, and B2 and B3 are connected to phase 2 inputs on L293D. Both PORTA and PORTB are configured as outputs, and I don't have any other component attached to them (sensor, pull-up/down resistors, etc) nor in the rest of the circuit. The power source are 6x1.5 batteries which provide 9V for the motors, and a 7805 provides the 5V for the electronics.

I've followed the Texas Instruments datasheet for the L293D and it works if I control it from Arduino (as I said, with a simple movement sequence like PORTB = LEFT, delay t seconds, PORTB = right, delay t seconds...)

Do you mind if I post the whole C or ASM code? It's short, actually.

Thanks in advance.


in such cases I always attach LED probes, have done this with Hitachi text LCDs for instance.
if you have higher frequency control signals, it can also be helpful to connect a small piezo speaker to the line.
in many cases you can hear something upon activity, and almost ever you'd know if that's what you want it to do.

if there is no LED activity, no piezo sound, then you have a software bug.


New Member
#include <16f628a.h>

#use delay (clock=4000000)

#byte PORTB = 6
#byte CMCON = 0x1F

#use standard_io(A)

// configuracio
void setup(void);
void loop(void);

void main(void) {

void setup(void) {
	CMCON = 0x07;

void loop(void) {
	while (1) {
		output_high(PIN_B0);   // turn the
		output_low(PIN_B1);    // motor RIGHT

		delay_ms(2000);         // wait 2 seconds

		output_low(PIN_B0);   // turn the
		output_low(PIN_B1);   // motor OFF

		delay_ms(10);           // wait 10 ms to stabilise

		output_low(PIN_B0);  // turn the
		output_high(PIN_B1); // motor LEFT


		output_low(PIN_B0);  // turn the
		output_low(PIN_B1);  // motor OFF

		delay_ms(10);           // wait 10 ms to stabilise
This is the code I'm running, simple as possible.

I attached some LEDs while driving the L293D and some times you can see a rapid blink from the LEDs that are supposed to be on, but remain off for almost all the time.

Given the fact that without the L293D the code runs fine, I thought that maybe there are bounces from the motor which the PIC can't handle, since as I said there is no more electronics involved than a 7805, a PIC and the L293D, but I'm more a coder than an electronic and I'm probably wrong :)

Thanks in advance.

fact1: the code works fine using LEDs instead of the L293D
fact2: the L293D works fine using an Arduino instead of a PIC16F628A

ps: in the evening (Spanish evening) I'll post a picture of the robot.


one simple thing you can try is to insert a diode towards the PIC power supply, and buffer with a small capacitor.

i think this should get you rid of spikes/bouncing.
But i do not have such a H-bridge to verify it.


New Member
Hi Nike6,

Any example of your proposal? I suppose you mean to modify the power supply "module", not the L293D nor the PIC.

Thanks in advance.


Well-Known Member
All things considered, it looks like you might have a ground loop issue. Try connecting all four ground pins of the L293 directly to the negative terminal of the battery pack that is supplying power to the motor. Make a connection between the gnd pin of the 7805 and directly to the negative terminal of the battery pack. Make these gnd wires as short as possible.

I am assuming the PIC does not need a pull-up, and goes to almost 5v when "high".
Last edited:


New Member
I'm glad to announce that the little robot is working pretty fine :)

I think it had to do with the calls to delay_ms(t) because I added the sensor module (an array of 3 CNY70's filtered through a 74HC14) and the L293D response is immediate using a switch() statement in my C code, but I don't really know why it didn't work...

Anyway, thanks all for your help, I really appreciated it. I hope I can stay around here and help other folks :)

ps: I'll post the code tomorrow, I'm now going to sleep (it's 3:00 over here! :-O)


jplopez, it is a simple modification.

just insert the diode into the line that goes to the PIC Vcc.
add a small capacitor, 1uF to 47uF, between that Vcc PIC pin, and GND.

that's pretty much it.

the intended function is a small backup power supply, in case there are spikes caused by motors.

since PIC only draw a few mA, a small capacitor is enough.
if you work at high frequency you may need a larger capacitor.

the diode drop is also neglible, at least if you have 5 volts supply.

alternatively use a seperate regulator (switching maybe) for the motor supply.

that is not academic advice, but what I would try in case there are power supply problems.
for instance, if a motor is blocked, current is much higher, and voltage may break down.
you should ensure the PIC still can work, and will switch off that motor immediately.


New Member
Hi all,

Thanks all for your advice. I finished the robot and it works great, you can see a video of the black-line follower here.

I noticed that some times the robot got stuck and stopped running, so I've used the WDT and the restart_wdt() instruction, so when it freezes, the WDT resets the PIC and the first action is running backwards and keep following the line.

I hope you enjoy the video.

Thanks all and see you soon.
Not open for further replies.

Latest threads

EE World Online Articles