Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Driving a DC Motor with PIC16F877A using L298, the motor is not moving

Status
Not open for further replies.

celegorn

Newbie level 5
Joined
Jul 31, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,384
Hello,

I am trying to control a DC motor using PIC16F877A through the L298 H-bridge IC.

I programmed the PIC, i set the circuitry up on a breadboard (the PIC, the L298, the power connections etc.) and the motor won't move AT ALL.

I tried to measure the voltage at the CCP1 pin (RC2 pin) and it is just as it should be, meaning if i set the duty cycle to 255/255 it is ~5V and if i set the duty cycle to 127/255 it is ~2.5V. However, the voltage at the OUT3 pin of the L298 remains ~11V no matter what i do.

The part that really confuses me is although i am measuring ~11V on the OUT3 pin of the L298 (the pin where the motor is connected) the motor doesn't move. I have tested my motor directly to a supply of 12V and it is working perfectly, so it's not the motor.

I have attached the schematic as well as the source code for the PIC, but i really don't think that that's the problem.


####################################### The source code ####################################
Code:
int cnt = 127;          //init - pwm cycle 

void main() {

TRISB = 0xFF;           // B is input
TRISC = 0X00;           // C is output
PORTC = 0x00;           // Init - C=0
TRISD=0x00;             // D is output
PORTD=0x40;             // D.f6=1 (motor turns clockwise)

ADCON1 = 0x7F;          // all pins digital
INTCON = 0xD0;          // gie i int enabled
OPTION_REG = 0x00;

PWM1_Init(5000);         // pwm freq = 5000hz 
PWM1_Set_Duty(cnt);
PWM1_Start();

while (1)
  {

  }
}

void interrupt (void)
{
if (INTCON.INTF)
{
  //motor changes direction on each interrupt:
  PORTD.f7=~PORTD.f7;             // D.f7=1 (motor turns clockwise);
  PORTD.f6=~PORTD.f6;             // D.f6=1 (motor turns counterclockwise)

  PORTD.f0=~PORTD.f0;             // LED on D.f1 changes state on each interrupt
  INTCON.INTF = 0;                // reset int flag
}
}
######################################### The source code #################################
pwm.jpg
View attachment pwm.bmp

Feel free to ask questions, i will try to check for replies as often as i can.
Thanks
 
Last edited by a moderator:

//motor changes direction on each interrupt:
PORTD.f7=~PORTD.f7; // D.f7=1 (motor turns clockwise);
PORTD.f6=~PORTD.f6; // D.f6=1 (motor turns counterclockwise)

Greetings,

I think your problem might be that you are providing improper direction signals to the L298 device. In my experience, and from what I have seen, a single direction signal is used in combination with a FET two provide direction signals, D+ and D-, to the L298 (I have included a circuit diagram showing what I mean). I think the way you are doing it might be causing the L298 to try and drive the motor in two direction simultaneously. Also, I am not sure about this, but I think that some of the pins, namely the Enable 1 and the SENSE A pins, that are floating might need to be tied either high or low. I seem to recall that this was the case in a previous project of mine, but you might want to consult the data sheet because I am really not sure.

When I look at the comments in your code, it looks to me like both D.f7 and D.f6 are 1, and then both are 0 and so on and so forth. This will not work. Either they need to always be opposite of each other, or you should use the FET approach as described above (shown in the schematic).
**broken link removed**

Also, on the schematic, is pin 9 tied to +5 volts? I do not see a label. Also, although this shouldn't drastically affect performance, it might be wise to add a filtering cap to the logic supply. Does the L298 get hot at all when you try to run a motor?

-Willis
 

If you take a look at the line of the code:

PORTD=0x40; // D.f6=1 (motor turns clockwise)

this says that only PORTD.f6 is logic "1", the rest of port D are logic "0". I also tried to measure the voltage on the PORTD.f6 and PORTD.f7 pins of the pic and they change from 5V to 0V or the other way around on each interrupt.

The 9th pin from the L298 is tied to 5V and both of the sense pins (for both of the motor drivers) as well as the GND pin are tied to the ground ( the sense pins through a resistor).

Thanks
 

Do you have a logic analyzer that you can use to see if a pwm signal is being generated from pin 17? If it is, and since you have verified the directions signals, I would guess that something is wrong with the L298.

Can you see what happens when you use port 1 on the L298 instead (ENA, IN1, IN2, etc.). While it is just a shot in the dark, it might be that port A is used to drive a single motor and port a and b are used when you need to drive multiple motors. Can you feel if the L298 gets hot?

Lastly, what is the current rating for your motor. While this is rarely a problem, since they can handle 4A for short periods of time, overdrawing from the L298 can damage the device.

Regards,
Willis
 

The next thing i was going to do was to try to use the other port of the L298, like you said. I don't know exactly how they work, but maybe the port B is damaged, but port A remained intact. A shot in the dark indeed.

I do not have an oscilloscope and i cannot see the pwm signal on RC2, but when i measure with the multimeter it gives me readings compatible with the duty cycle i set. (2.5V for 127/255 Duty Cycle).

The current rating of my motor is 1.2A according to the datasheet. Although the motor is rated for 27V +/-2,7 i am inputting 12V in the L298 Vs pin (pin 4). I figured that this will not affect the motor or the L298 badly at all, since it's well below the nominal voltage. Am i correct or this is my problem and i have blown the L298.

Thanks again Willis
 

The next thing i was going to do was to try to use the other port of the L298, like you said. I don't know exactly how they work, but maybe the port B is damaged, but port A remained intact. A shot in the dark indeed.

I do not have an oscilloscope and i cannot see the pwm signal on RC2, but when i measure with the multimeter it gives me readings compatible with the duty cycle i set. (2.5V for 127/255 Duty Cycle).

The current rating of my motor is 1.2A according to the datasheet. Although the motor is rated for 27V +/-2,7 i am inputting 12V in the L298 Vs pin (pin 4). I figured that this will not affect the motor or the L298 badly at all, since it's well below the nominal voltage. Am i correct or this is my problem and i have blown the L298.

Thanks again Willis

The voltage and the current ratings for the motor fall well within the maximum limits for the L298, so if the L298 is damaged, I do not think that it was caused by the motor. If you change the PWM to something like 25%, can you verify that the voltage drops to ~1.2 V?

Regards,
Willis
 

Sorry for not responding immediately.

I was waiting to buy a new L298 IC and once i did i hooked it up ( i was fearing that the first one was blown). As soon as i hooked it up and then turned the power on i first tried to see if it was getting too hot. It wasn't getting hot at all. I also checked the PIC to see if it was getting hot and it was okay too.

Next, i tried to measure the voltage on the OUT1 pin. I rearranged my code a tiny bit, so that every time i hit the RB0 pin (external interrupt) the PWM duty cycle changes for a quarter upwards until it hits maximum and then resets to zero. So the PWM duty cycle starts with 0-->64-->128-->192-->255-->0... etc. I also removed the part of the code that changed the direction of the motor on each interrupt. Now the direction stays the same all the time. I measured the voltage and it is 5V on one and 0V on the other pin.

I measured the voltage on both the PWM pin on the PIC and the OUT1 pin of the L298. Here's what i got for the respective duty cycle values:

DC PIC OUT1
0-------0V-------0.15V
64------0.99V----4.22V
128-----1.99V----7.09V
192-----2.99V----9.95V
255-----3.99V----11.8V

The Vs of the L298 is set on 12V.

But whatever the outputs were, the motor wasn't turning at all.

I also have an LED hooked up through a resistor on the PWM pin on the PIC and it works perfectly, meaning the light intensity changes according to the PWM duty cycle.

I really do not know what is wrong.

I appreciate your help.
 

Try checking wheather the motor rotates in any one direction.

Code:
int cnt = 127;          //init - pwm cycle 

void main() {

TRISB = 0xFF;           // B is input
TRISC = 0X00;           // C is output
PORTC = 0x00;           // Init - C=0
TRISD=0x00;             // D is output
PORTD=0x40;             // D.f6=1 (motor turns clockwise)

ADCON1 = 0x7F;          // all pins digital
INTCON = 0xD0;          // gie i int enabled
OPTION_REG = 0x00;

PWM1_Init(5000);         // pwm freq = 5000hz 
PWM1_Set_Duty(cnt);
PWM1_Start();

while (1)
  {

  }
}

void interrupt (void)
{
if (INTCON.INTF)
{
  //motor changes direction on each interrupt:
  PORTD.f7=~PORTD.f7;             // D.f7=1 (motor turns clockwise);
  
  PORTD.f0=~PORTD.f0;             // LED on D.f1 changes state on each interrupt
  INTCON.INTF = 0;                // reset int flag
}
}

zip and post your proteus .dsn file. Check the attached file. It is working.


--------Update---------------

This code is working fine. The motor changes the direction. Check pwm_motor.rar

Code:
int cnt = 127;          //init - pwm cycle

void interrupt (void) {
if (INTCON.INTF)
{
  //motor changes direction on each interrupt:
  PORTD.f7 = ~PORTD.f7;             // D.f7=1 (motor turns clockwise);
  PORTD.f6 = ~PORTD.f6;             // D.f6=1 (motor turns counterclockwise)
  Delay_ms(20);
  PORTD.f0 = ~ PORTD.f0;
  PORTD.f1 = ~ PORTD.f1;             // LED on D.f1 changes state on each interrupt
  INTCON.INTF = 0;
  }                // reset int flag
}

void main() {

    TRISB = 0xFF;           // B is input
    TRISC = 0X00;           // C is output
    PORTC = 0x00;           // Init - C=0
    TRISD=0x00;             // D is output
    PORTD=0x40;             // D.f6=1 (motor turns clockwise)

    ADCON1 = 0x7F;          // all pins digital
    INTCON = 0xD0;          // gie i int enabled
    OPTION_REG = 0x00;

    PWM1_Init(5000);         // pwm freq = 5000hz
    PWM1_Set_Duty(cnt);
    PWM1_Start();
    
    while(1) {
    
    }
}
 

Attachments

  • celegorn.rar
    15.8 KB · Views: 69
  • ss19.jpg
    ss19.jpg
    531.3 KB · Views: 116
  • pwm_motor.rar
    15.9 KB · Views: 72
Last edited:

Sorry for not responding immediately.

I was waiting to buy a new L298 IC and once i did i hooked it up ( i was fearing that the first one was blown). As soon as i hooked it up and then turned the power on i first tried to see if it was getting too hot. It wasn't getting hot at all. I also checked the PIC to see if it was getting hot and it was okay too.

Next, i tried to measure the voltage on the OUT1 pin. I rearranged my code a tiny bit, so that every time i hit the RB0 pin (external interrupt) the PWM duty cycle changes for a quarter upwards until it hits maximum and then resets to zero. So the PWM duty cycle starts with 0-->64-->128-->192-->255-->0... etc. I also removed the part of the code that changed the direction of the motor on each interrupt. Now the direction stays the same all the time. I measured the voltage and it is 5V on one and 0V on the other pin.

I measured the voltage on both the PWM pin on the PIC and the OUT1 pin of the L298. Here's what i got for the respective duty cycle values:

DC PIC OUT1
0-------0V-------0.15V
64------0.99V----4.22V
128-----1.99V----7.09V
192-----2.99V----9.95V
255-----3.99V----11.8V

The Vs of the L298 is set on 12V.

But whatever the outputs were, the motor wasn't turning at all.

I also have an LED hooked up through a resistor on the PWM pin on the PIC and it works perfectly, meaning the light intensity changes according to the PWM duty cycle.

I really do not know what is wrong.

I appreciate your help.

I apologize; I was gone on business this past week.

I would like to try another approach and see if this works. The L298 has the capability four 3 different drive states: 1 motor in low power mode, 2 motors in low power mode, or 1 motor in high power mode. Since we are having trouble driving the motor in low power mode, let us see if we can get the motor to move by driving it in high power mode.

1. Connect Enable A and Enable B together. This should still be the main enable signal coming from the uC.
2. Tie In 1 and In 4 together. this is your direction signal.
3. Tie In 2 and In 3 together. this is your direction ' signal.
4. Tie Out 1 and Out 4 together. This is your motor +.
5. Tie Out 2 and Out 3 together. This is your motor -.

Try this and let me know your results. If this is not working, then I would venture that the problem is stemming from one of two sources. Either there is a code problem that we are not seeing, or the L298 does not like the way that you are doing your direction signals. Like I mentioned earlier, I am not saying the way that you did it wrong, or that the L298 even sees the difference between the two methods; I have just never done it like that before. I have always used a FET to generate the D' signal.

Let me know if you get the motor working.

Regards,
Willis
 

I am sorry for not replying sooner. I was away on vacation.

I would like to try another approach and see if this works. The L298 has the capability four 3 different drive states: 1 motor in low power mode, 2 motors in low power mode, or 1 motor in high power mode. Since we are having trouble driving the motor in low power mode, let us see if we can get the motor to move by driving it in high power mode.

1. Connect Enable A and Enable B together. This should still be the main enable signal coming from the uC.
2. Tie In 1 and In 4 together. this is your direction signal.
3. Tie In 2 and In 3 together. this is your direction ' signal.
4. Tie Out 1 and Out 4 together. This is your motor +.
5. Tie Out 2 and Out 3 together. This is your motor -.

Willis, i tried this and it didn't work either. I think that the problem is somewhere in the motor. Since i borrowed this motor from my faculty, i will ask the guys that used it before what they used to drive it.

I am so grateful to you for your help. Thanks.
 

Were you able to find any information about the motor. I am afraid that this problem has reached the limit of my experience; I am out of ideas unfortunately.
 

Hi,I saw your schematic.As Willis said you must put non inductive capacitor between pin9 and4.It is much important as mentioned in data sheet in page 7.I built motor controller without inserting filtering capacitor but when I connected it to the pic18f452 to run my robot,pic was reset in every 3 to 5 seconds.Then I built the motor controller with non inductive capacitors.You can see more details on
**broken link removed**

I want to know If you test your circuit using any other motor.If it runs the problem is on the motor.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top