# L293D is not switching directions

Status
Not open for further replies.

#### jplopez

##### 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...)

#### blueroomelectronics

##### Well-Known Member
Post your schematic and if you can construction details.

#### jplopez

##### 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.

#### blueroomelectronics

##### Well-Known Member
Just make sure to use the code tags. (Go Advanced, #)

Last edited:

#### nike6

##### Banned
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.

#### jplopez

##### New Member
Code:
#include <16f628a.h>

#use delay (clock=4000000)
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOMCLR,NOPUT,NOCPD

#byte PORTB = 6
#byte CMCON = 0x1F

#use standard_io(A)

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

void main(void) {
setup();
delay_ms(1000);
loop();
}

void setup(void) {
setup_oscillator(OSC_4MHZ);
CMCON = 0x07;
set_tris_a(0x00);
set_tris_b(0x00);
output_b(0x00);
}

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

delay_ms(2000);

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

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

Blueroomelectronics:
This is the code I'm running, simple as possible.

Nike6:
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

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.

#### nike6

##### Banned
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.

#### jplopez

##### 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.

#### blueroomelectronics

##### Well-Known Member
I use a SN754410 (improved L293D) in my Mongoose kit. You can see examples of decoupling & filter caps there.

#### ccurtis

##### 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:

#### jplopez

##### 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)

#### nike6

##### Banned
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.

#### jplopez

##### 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.

Status
Not open for further replies.

Replies
50
Views
4K
Replies
4
Views
1K
Replies
11
Views
12K
Replies
1
Views
961
Replies
10
Views
2K