Continue to Site

Welcome to

Welcome to our site! 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.

Interfacing PIC 16F876A with Driver L298

Not open for further replies.


Advanced Member level 4
Dec 2, 2010
Reaction score
Trophy points
Activity points
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.

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

Well, I tried the example

Code C - [expand]
code is as follows

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:


I assume wrong wiring.

Try to change the connection of one coil.


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

Last edited:

I changed the code to


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

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.


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

Show your shematic and a picture of your PCB / wiring.


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.


Show your schematic and a picture of your PCB / wiring.

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.
I just checked your code.
You need either 4 steps in a loop (fullste)
Or 8 steps in a loop.
But you have 5 and 7.

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.


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

1. **broken link removed**

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

No of Leads - 4

2 . **broken link removed**
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
•leads: 6 wires 12" long

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.


simple use of Ohm´s law:

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



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

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.


@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 ?)


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)


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.

Not open for further replies.

Part and Inventory Search

Welcome to