You are here

Using a L298N Chip to drive stepper motor with Psoc 5LP | Cypress Semiconductor

Using a L298N Chip to drive stepper motor with Psoc 5LP

Summary: 3 Replies, Latest post by Bob Marlowe on 27 Jul 2016 03:08 AM PDT
Verified Answers: 0
Last post
Log in to post new comments.
neohobbyist_1746876's picture
4 posts

Hi everyone,

I'm currently using a CY8CKIT-059 prototyping kit and I have a L298N Motor control chip with a stepper motor. I will include all the data sheets below. 

I have read a couple of resources in regards to using the PSoC 3 to drive a stepper motor and I'm fairly new to using PSoC but I have prior experience in using Ardiuno. I have deconstructed the code that is used the Arduino Stepper Library and modified it to work for PSoC.

So now, My stepper motor is able to continuously spin. I have enabled UART for it to do serial communication and I want to be able to input a command such as "On" and  "off" to toggle the stepper motor on and off. I am currently stuck on how I would proceed to modify my code/design schematic to allow for what I want to do. I also understand that the way I've coded it may not be the most efficient way to run the stepper motor, Any improvements suggestions are also welcome. 

Any help would be greatly appreciated.


Thanks in advance





Stepper Motor:

user_1377889's picture
9279 posts

Well, let me give you some advices:

As I can see from your code you are not programming for a very long time as I do. What I have learnt so far (by bad experiences) is that documentation and commenting the source files is essential. You will loose the knowledge why you did something particular after a couple of months have passed.

There is no indication in your main.c what it should perform and why you did it that way.

Write a program like an essay: When you read it you will see (understand) what it does.


Your while (x <= 5){ is rubbish, you never change the x

Better use a while(forever) when you want to perform that.

You may think about to put your 4 output pins into a single port. This will allow for a single Write() statement giving a binary or hex pattern.

In the world of embedded systems you usually have to maintain several different tasks, so "burning MIPS" in a CyDelay() is not a good idea.

So what do you want? Controlling a stepper motor. That can be more than just "On" and "Off", that can even mean controling the speed.

Imagine you have got a timer with a period of your "y" (why the hack do you name it "Y" and not something like "StepperPhaseWait"??)

An interrupt handler could step through your 4 phases and keep the motor running without looping in your main(). That gives you some freedom to handle your UART. Even more: by changing programatically the period of the timer you are able to control the speed of your motor.

For the UART communication you will have to write a "Protocol". This defines the commands (and any answers) your driver will allow for.

M0 - Stop Motor

Mn n=1..100 Run stepper with given speed in % of maximum

More sophisticated could be

S-5000 Drive stepper motor counter clockwise 5000 steps

Write down (document) your protocol!!!



neohobbyist_1746876's picture
4 posts

Hi Bob

Thanks for the insightful advice, you are quite right, I have not been coding for more than 2 month.

I've reflected some of your advice in the following revision of my design and coding.

1) I have changed the stepper motor loop into an ISR so now the ISR can be called by pressing a button and the motor will turn on until the button is let go. However, It is not working as well as I hoped even with the inclusion of an Debouncer. The button/switch I'm using is the onboard switch of the PSoC.  The Stepper motor sometimes runs when the button is pressed and sometimes doesn't....

2) I have removed a lot of the unused variables and renamed a bunch of variables to better define what my code does at the moment

3) In terms of UART communication, I have no clue on how I would go about writing a communication protocol as I can't find any guides/examples that would explain/demonstrate such a procedure, so any additional resources would be great.

4) You mentioned using a single port to control the 4 pins. I will look into this later but i'm making an assumption it is similar to controlling 20 LEDS with 5 pins on an ardriuno?  I think using UART to control the on/off of the motor as well as the speed of the motor holds a greater priority at this point as  I still have a lot to learn


Thanks for your input so far!



user_1377889's picture
9279 posts

See in attached project what I mean.

*** Totally untested! Read comments !!!



Log in to post new comments.