# Interfacing PIC 16F876A with Driver L298

Status
Not open for further replies.

#### sunil21

I am trying to interface PIC micro 876A with stepper motor driver board build with L298.
I searched in the internet, found lot about L298 and Arduino but not much about PIC.
I have just one question.

I have Pic 16F876A, one board L298 (from ebay), stepper motor 12v, 2amp.
I connected stepper motor wires (bipolar) to the side connections of L298 (many videos on youtube)
connected 12v supply to the L298 board. and am left with 4 pin header to connect to micro.

IN1, ,,,,IN4.
Just want to know how to run it . just one complete rotation.

Just like - connect IN1 to RC4, IN2 to RC5, IN3 to RC6 and IN4 to RC7
turn on all power.
set RC4 -0, RC5-1 and wait 1 milli second
then Rc5 - 0 and RC4 - 1

just like that.. Plz.

#### pic.programmer

Is it unipolar stepper motor or bi-polar stepper motor ?

#### sunil21

Well, I tried the example

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
code is as follows
while(1)
{
PORTB=0b0011;
delay_ms(100);
PORTB=0b0110;
delay_ms(100);
PORTB=0b1100;
delay_ms(100);
PORTB=0b1001;
delay_ms(100);

}

It started to run in clockwise direction, say 12-15 steps and the it reversed 1 step and forward 1 step and it goes like that - 1 step forward and 1 step back... then after few seconds it goes forward again few steps and does this 1 step forward backward few time and then goes forward.

Also how long should each pulse be and what is delay needed?

Last edited by a moderator:

#### KlausST

##### Super Moderator
Staff member
Hi,

I assume wrong wiring.

Try to change the connection of one coil.

Klaus

* for better readability I recommend to wirte binary values as 8 bits instead of four bits.

Klaus

Last edited:

#### sunil21

I changed the code to
Code:
     {
PORTB=0b00000001;
delay_ms(15);
PORTB=0b00000101;
delay_ms(15);
PORTB=0b00000100;
delay_ms(15);
PORTB=0b00000110;
delay_ms(15);
PORTB=0b00000010;
delay_ms(15);
PORTB=0b00001010;
delay_ms(15);
PORTB=0b00001000;
delay_ms(15);

}

it rotates sometimes, if I touch the motor side body or the L298 IC the motor continuously runs

#### pic.programmer

RC4 to RC7 used for stepper motor ? If yes, the code in post #5 will not work. If you want to use code in post #5 then you have to use RC0 to RC3.

#### KlausST

##### Super Moderator
Staff member
Hi,

if I touch the motor side body or the L298 IC the motor continuously runs
this sounds like a wiring or layout problem. Caused by ESD, ground bounce or something similar.

I assume the uController resets accidentally. Please check on this. (maybe by a short LED flash after reset).

Klaus

#### sunil21

With software and hardware setup in the last post (post 5), I changed the motor wires ; and in all configurations , still the same result.

I have one question and one request
1. On the L298 board there are 4 inputs - IN1 to IN4 and 4 output pins , 2 pair of Output pins (Out1, Out2, Out3 and Out4) . so Out1 and Out2 represents IN1 and In2 , Out3 and Out4 represents In3 and In4 ? i.e., if I apply a pulse across IN1 (+ve) and IN2 (-ve) is equivalent to applying a pulse across (say 12v - or enough to drive it) Out1(+ve) and Out2 (-ve). And the same applies when +ve and -ve reversed. (Also same with OUT3 , OUT4 --> IN3 & IN4 ?). so when I am asked to change one set of motor wires, instead of reversing the two corresponding OUTx connections is it okay to reverse the INx connections ?

2. I ask somebody to help me with C code for this stepper motor movement with connections from Micro port to the IN1-4 and with Out1-4 connections. The stepper came with 4 pin (female) connector to connect to pin header or equivalent. I checked it for continuity and wire 1 and 2 are same phase, 3 and 4 same phase. with this wires marked a,b,c,d which is to connect to out1 -4. I am asking for this because I already wasted some write cycles on my micro with code not working(It already had many write with pickit).

When I am building something, I used to check parts individually instead of checking it as a whole (mostly never worked when done as a whole) after assembling.

#### KlausST

##### Super Moderator
Staff member
Hi,

One point came into my mind: is your power supply strong enough, especially is it stable when switching the load?

****
is it okay to reverse the INx connections ?
Yes, you could even change the code.
****
You need either 4 steps in a loop (fullste)
Or 8 steps in a loop.
But you have 5 and 7.

Halfstep:
1: 0000 0001
2: 0000 0101
3: 0000 0100
4: 0000 0110
5: 0000 0010
6: 0000 1010
7: 0000 1000
8: 0000 1001 ( this is missing)
Then 1

***
1. On the L298 board there are 4 inputs - IN1 to IN4 and 4 output pins , 2 pair of Output pins (Out1, Out2, Out3 and Out4) . so Out1 and Out2 represents IN1 and In2 , Out3 and Out4 represents In3 and In4 ?
How can we answer this? We don't have any information or picture what hardware you use.
***

Klaus

#### sunil21

Okay it is rotating now....
I bought 2 motors from Ebay

1. https://www.ebay.in/itm/231609683280?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

Step Angle - 1.8 deg
Current - 1 A
Resistance - 2.4 ohms
Inductance - 6.5 mH
Holding Torque - 4.2 kgcm
Voltage - 2.4v
Weight - 350 grams

2 . https://www.ebay.in/itm/321840479961?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649
Size: 56mm, NEMA 23
•Step angle: 1.8 deg ( 200 steps/revolution)
•Holding torque: 55 oz-in (0.39 N-m) unipolar / 68 oz-in (0.48 N-m) bipolar-series
•Voltage: 1.9V unipolar / 2.6V bipolar-series
• Current: 3.0A unipolar / 2.1A bipolar-series
•Inductance: 0.6mH unipolar / 2.4mH bipolar-series

Item 1 is what I am trying to run now. Item 2 is given as 2.6V bipolar but the graph showing using 24 and 48 volts. With item 1 I am applying 12v.As somebody asked me in one of the previous posts - is it driven with enough power ? on the motor it is written 2.4ohm, 1amp. so can I run this at 24volt (item 1) as long as the phase current don't go beyond 1amp? if a relaxation is possible to what extend can I go ? voltage and current.

#### KlausST

##### Super Moderator
Staff member
Hi,

simple use of Ohm´s law:

24V / 2.4 Ohms = 10A !!!!

Klaus

I think it has been said before: You should not run a stepper with constant voltage (12V or 24V) but with constant current.

Why:
Current is generating heat according P = I * I * R. This means running a 1 A rated motor with 10A will dissipate about 100 times the rated power. I expect livetime to be less than 5 minutes.
(but most probably your power supply will loose voltage and the driver IC also)

With the motor´s inductivity it needs a known time (depending on voltage) to reach the current. --> With higher voltage it needs less time to reach the current, after the the current control loop will drop voltage to keep current constant.
Current is about proportional to mechanical torque.
--> high frequency (RPM) --> low time for current to rise --> low current --> low torque.
***

Please read about the theory on how to control a stepper motor.There are a ot of good documents, tutorials and videos around.

Klaus

#### sunil21

@KlausST thanks for the reply. I have searched; went thru a lot of docs and etcc.... The problem is there are too many documents and too much information to process....
One question : Running the above sequence it works, but supplying current to the motor and after the motor complete the specific movement the power is actually wasted ? other than in the half step mode( in the half step mode we have to hold it there in the half step until another half step is completed ? ). so I send a pulse to the microcontroller port of 15ms, then the driver will supply current to the stepper motor for 15ms? for a normal stepper motor how long the pulse duration for a single step (depends on any other factors ?)

#### KlausST

##### Super Moderator
Staff member
Hi,

documents: look for reliable information: IC manufacturer, stepper manufacturer, complete driver PCB manufacturer..
(many hobby projects are good also. But it is hard to find out which is good and which is not good)

the power is actually wasted ?
You dissipate power in half step mode and in full step mode.
If you don´t need holding torque, then switch off both coils (as well as in full or half step mode) But you may loose position, if it is moved..

Usually a stepper motor is powered all the time. With current control you can adjust current to expected mechanical torque. With this you may lower holding current. This saves power.

A stepper motor needs to start with low RPM (you do now with 15 ms per step) and then it is accelerated. Often a constant acceleration until expected RPM.

The mechanical motor inertia combined with the magnetic force generates a mass-spring-system with it´s resonance (frequency). During acceleration this is the critical frequency where the motor may stall.
(you can avoid this with microstepping technology or mechanical damping)

Klaus

#### FvM

##### Super Moderator
Staff member
Relevant parameters are start/stopp frequency, available torque versus frequency, moment of inertia and can be found in motor datasheets.

Parameters are valid for a specific driving method, e.g. constant current with xx volt driver. Higher driver voltage means higher maximal frequency and less torque reduction versus speed. Constant voltage operation (low driver voltage and current limited by the motor resistance) means lowest available speed.

To get started, you can operate the motor with a constant step frequency lower than the maximal start/stopp frequency.

Status
Not open for further replies.