Stepper motor control in Python with Raspberry PI

Stepper Motor Control IRP102

Stepper motor control in Python with Raspberry PI

25 February, 2016 DIY, Raspberry Pi 0

In this tutorial we are going to drive a stepper motor with Raspberry PI.

28BJY-48 is a small 5V stepper motor with gear train. It is cheap and easy to get.

This motor is controlled by means of 5 wires:

  • 5V
  • Phase 1
  • Phase 2
  • Phase 3
  • Phase 4

For driving each of these phases we are going to use ULN2803, which give us 8 open-collector outputs controlled by 8 digital output. Never try to drive a motor directly with GPIOs. These outputs are not designed for driving high current devices. 

We can drive the motor with Reapberry PI whit a simple Python application.

1. Connecting the stepper motor

This diagram shows the connections between Raspberry P, ULN2803 and 28BJY-48 :

stepper_motor_connections

 

If you have a Hat Board IRP-102, don’t worry about wires, just plug  X6, OPEN COL OUT, set red wire of motor with  pin 1 at X6. If you are not using the hat-board  you can make use of any available GPIO, it is not necessary to be the same that in the diagram (these are related to IRP-102 pinout).

IRP102 stepper Connection

IRP102 stepper motor connection

2. Install required packages

Assuming you have Raspbian as operating system, you only need  RPI.GPIO library for GPIO manging. For this, just type in shell:

 

3. Motor driving

We can make the engine work in two ways:

  • Full-step.
  • Half-step.

stepper_motor_connection_table

Full Step :

This way the motor is operated with both phases powered at the same time. This mode provides the best torque and speed performance

Half Step:

Half step excitation is alternating single and dual phase operation resulting in steps that are half the basic step angle. This way provides more precision (due to smaller steps) but we loss power and speed.

We are going to do a Python application showing both ways of operation. For this, create a file called stepper.py.

First step is “import” all the libraries we are going to use.

This is the function for GPIO activation in half step mode:

You must call this function repeatedly using steps from 1 to 8 in sequential order for getting motor turns in a sense, or from 8 to 1 to make it turns in the other sense. If you call the function passing 0 as parameter all phases will be deactivated. You should do this when you want set it in idle state to prevent motor overheating.

This is the Phyton function for full step mode:

In this case, the sequence is repeated each 4 steps. In the same way,  0  is reserved for deactivating all phases.

In order to call these functions in the right way, you must build other upper-level functions.

For half step control:

Full step control:

The input value of these functions means the number of steps, whilst the sign represent the turning sense. At the end of the sequence, we set the 0 value to came back to the idle state.

This “main” function is responsible for initializing GPIO’s and for taking input parameters from console. First parameter is the steps number, second parameter is for choosing full step or half step mode.

Exit the editor saving changes.

For testing the application you have to provide at least one parameter, the number of steps which may be positive or negative depending on the turning sense. By default the stepper motor works in full step mode, but with the second parameter you can select the working method.  “0”- full step,  “1” – half step.

Download the whole python file in your Raspberry whit this command.

If you want get it in your computer click this link: stepper.py.

4. Testing application
500 steps anti clockwise full step mode

800 steps clockwise, half-step mode.

About the author

IngeniApp:

0 Comments

Would you like to share your thoughts?

Your email address will not be published. Required fields are marked *

Leave a Reply

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies

ACEPTAR
Aviso de cookies