Contributions

Contributions are appreciated and used to host this site. Thank You for your support.

Feature Products
  • Raspberry Pi with Java: Programming the Internet of Things (IoT)
    Raspberry Pi with Java: Programming the Internet of Things (IoT)
  • Raspberry Pi 2 Model B Project Board - 1GB RAM - 900 MHz Quad-Core CPU
    Raspberry Pi 2 Model B Project Board - 1GB RAM - 900 MHz Quad-Core CPU
  • Premium Clear Case for Raspberry Pi 2 Model B Quad Core and Raspberry Pi Model B+ (B PLUS)
    Premium Clear Case for Raspberry Pi 2 Model B Quad Core and Raspberry Pi Model B+ (B PLUS)
  • Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter with EZmax Setup Wizard
    Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter with EZmax Setup Wizard
  • Getting Started with Raspberry Pi
    Getting Started with Raspberry Pi
  • Raspberry Pi User Guide
    Raspberry Pi User Guide
  • PiBrella LED Add On Board
    PiBrella LED Add On Board
  • Diversitech® WS-1 - Wet Switch Flood Detector
    Diversitech® WS-1 - Wet Switch Flood Detector
  • Onsite Pro FS1NPTW Whole Home Wireless FloodStop with 1 Inch Valve
    Onsite Pro FS1NPTW Whole Home Wireless FloodStop with 1 Inch Valve
  • Floodstop Washing Machine Valve Shutoff Kit
    Floodstop Washing Machine Valve Shutoff Kit
  • Onsite Pro FS3/4H Washing Machine FloodStop with Straight Valves
    Onsite Pro FS3/4H Washing Machine FloodStop with Straight Valves
  • Floodstop Individual Water Appliance Additional Water Sensor XS-01
    Floodstop Individual Water Appliance Additional Water Sensor XS-01
  • Aqua Managers - FS 1 1/4-NPT - Floodstop for Water Heaters Water Leak Detection System - White - 1.25 in. pipe
    Aqua Managers - FS 1 1/4-NPT - Floodstop for Water Heaters Water Leak Detection System - White - 1.25 in. pipe
  • Furman MP-20 Power Relay Accessory, 20 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
    Furman MP-20 Power Relay Accessory, 20 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
  • Furman MP-15 Power Relay Accessory, 15 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
    Furman MP-15 Power Relay Accessory, 15 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
  • Metra 70-2002 Radio Wiring Harness for Saturn 00-05
    Metra 70-2002 Radio Wiring Harness for Saturn 00-05
  • Metra Reverse Wiring Harness 71-2002 for Select 2000-2005 Saturn Vehicles OEM Radio
    Metra Reverse Wiring Harness 71-2002 for Select 2000-2005 Saturn Vehicles OEM Radio
Monday
Dec312012

Raspberry Pi - Stepper Motor Control & Breakout Board

Overview

Many of the previous Raspberry Pi projects I had been working on were based on fairly simple GPIO logic to turn things on and off.  Today I wanted a little more of a challenge so I started working to control a stepper motor from my Raspberry Pi using Java.  Fortunately I had previously obtained one of John Jay's Stepper Motor Breakout Board kits which includes all the necessary components (including the stepper motor) to get started.  

The kit includes:
  • 1 @ printed circuit board
  • 1 @ ULN2803 8 Channel Darlington Driver IC (Adafruit)
  • 1 @ 18 pin DIP socket (Amazon)
  • 8 @ green LEDs
  • 1 @ Jumper and Jumper header
  • 1 @ resistor array/network
  • 1 @ capacitor
  • 1 @ 26 pin PCB mount female header (not pictured)
  • 1 @ peel-and-stick on rubber bumper  (not pictured) 
  • 1 @ 28BYJ-48 5 VDC stepper motor (Amazon | Adafruit)
This kit is very easy to assemble and very reasonably priced.  
It is available now on eBay. << Click here to see John Jay's eBay listings >> 
 

Assembly

Please visit John's website for further instructions and photos on assembly:
<< http://mypishop.com/Stepper.html >>
 

Shopping List

The stepper motor is controlled by pulsing the GPIO pins from LOW to HIGH states in a sequence that causes the motor to spin.  Each time a GPIO pin state is HIGH an electromagnet is engaged inside the stepper motor to cause a rotational force on the center rotor. With the proper sequence and timing you achieve motor rotation. 

Here is a fantastic article with photos and illustrations to help learn more about stepper motors and stepping techniquest:
http://www.lirtex.com/robotics/stepper-motor-controller-circuit/

Photos

Below are photos of the assembled board and attached stepper motor.

Stepper Motor Ports

This breakout board includes two ports for controlling two separate stepper motors.

 

 

 

You can purchase an additional stepper motor to use on the second port here:

28BYJ-48 5 VDC stepper motor (Amazon | Adafruit) 

 

 

 

GPIO Wiring Diagram

The two stepper motor controllers are mapped to GPIO pins on the Raspberry Pi's P1 header as depicted in the diagram below.  The GPIO numbering in the diagrams below is based on the WiringPi  / Pi4J numbering scheme.  See below for a mapping chart for other numbering schemes. 

  • GPIO Pins 0 to 3 are mapped to the X1 motor controller port (LEFT).
  • GPIO Pins 4 to 7 are mapped to the X2 motor controller port (RIGHT).

GPIO Pin Cross-Reference Chart

Below is a cross reference chart to better help define the GPIO pin numbers used under the various pin numbering schemes.

JP1 Jumper Information

A jumper is provided on the board to supply 5 VDC to the stepper motors directly from the 5 VDC output from the Raspberry Pi.  If you would prefer to provide 5 VDC from an external power supply, please remove the jumper from JP1 and connect your power source to JP1 pins 2 and 3.  See the diagrams below.


Make sure that you place the jumper between pins 1 and 2.  

ATTENTION !

Never attach JP1 pins 1 and 3 together as this will cause a direct short on the 5 VDC power line from the Raspberry Pi.  This would likely render the Pi defective.   

Java Sample Program using Pi4J to Control Stepper Motor

Below is a sample Java program for controlling the stepper motor.  The Pi4J project now includes a new stepper motor interface and component implementation for GPIO based stepper motors.  This makes it really simple to control a stepper motor from Java.  (Note that the stepper motor implementation is available in 0.0.5 and later versions of Pi4J.)

The source for this example is provided with the Pi4J installation package and can also be found on Github:

Final Thoughts

This was my first time working with a stepper motor and having this kit certainly made it much easier to understand and get started.  For me the timing was the trickiest thing to get right.  Fortunately with a little trial and error I was able to get it working.  Here is a quick video demonstrating the final working project.

Happy New Year!

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (5)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments (14)

Hi Robert,

I have been trying out your pi4j library together with the evealpha board. I think pi4j is great, but would like to let you know of a problem I'm having with the SPI wrapper. In the wiringPiSPIDataRW() JNI wrappper, the String data parameter is used for both transmitted data as well as received data. From the code below (which I copied from your github project), it looks like the received data (should be in datachararr) is not being copied back into the Java data string. I can confirm this behaviour on my program which only ever receives the same data that is being transmitted.

Thanks again for a great product.
Franz

/*
* Class: com_pi4j_wiringpi_Spi
* Method: wiringPiSPIDataRW
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW
(JNIEnv *env, jclass class, jint channel, jstring data, jint length)
{
char datachararr[2048];
int len = (*env)->GetStringLength(env, data);
(*env)->GetStringUTFRegion(env, data, 0, len, datachararr);
return wiringPiSPIDataRW(channel, (unsigned char *)datachararr, length);
}

January 7, 2013 | Unregistered CommenterFranz

@Franz,

Thanks for posting this issue and I agree that it looks like a defect.
I have added it to the issues list on Pi4J.

https://github.com/Pi4J/pi4j/issues/28

Please register on this ticket to be notified of updates.
I am looking into the issue this evening.

Thanks, Robert

January 7, 2013 | Registered CommenterRobert Savage

Hello Robert!

Thanks for this great project!
I think I made a mistake, in which you can help me.
- I soldered the whole thing up
- plugged in the motor
- compiled and run your example

Only LED1 lights up strongly and LED3 weak.
When I unplugg the motor only LED1 lights up.

Please help me. :)

January 27, 2013 | Unregistered CommenterTobias

Aww yes! I just successfully got all this going on my pi.

September 14, 2013 | Unregistered Commentermeep

@meep

Glad to hear it! Thanks for posting your success.

Thanks, Robert

September 29, 2013 | Registered CommenterRobert Savage

Hi,

I have been trying to wire up everything, but haven't got circuit board,

Is there better image of circuit board, or Can I connect everything without board? Any help is appreciated!
Thank you.

Look forward to hear from you guys.

March 16, 2014 | Unregistered Commenterfmiboy

I have got http://img.dxcdn.com/productimages/sku_140716_1.jpg prototype printed board though cannot see images clearly from this post how to wire everything... if you can, please advise any links that has step by step guide?!

thanks

March 16, 2014 | Unregistered Commenterfmiboy

Thanks for this prof. Demo.

March 19, 2014 | Unregistered CommenterDerek

Consider updating your links to ebay. The link now redirects to the search for the product where there are many similair-looking boards.

July 24, 2014 | Unregistered Commentercp

i installed everything as per the instructions... wiringpi... j4pi... i can run the script and i see the echos in the console, but i get no response from my stepper control board... no lights or motor movement.
is the pi2 different? do we need a different install to make this work on a pi2?

October 7, 2015 | Unregistered Commenterjc

Uuu, really nice explanation. Thx.

December 9, 2015 | Unregistered CommenterMatija

Thanks for this great summary - use this template to control a chicken coop door (see author URL, page in German). There is just one thing which makes me curious: When I compile the code in BlueJ the timespan one motor runs seems to be correct. However, when I execute the same code directly in the command promt the time span is aprox. 20% shorter. Why?

For instance, the following code will produce 87 seconds of motor rotation in BlueJ but only ~ 60 Seconds in the command promt. (using double_step_sequence, Stepinterval of 2)

motor.reverse (87000);

July 13, 2016 | Unregistered CommenterRichard

Hey,
it seams to be a great product, but how can I connect the stepper motor to the PI model 3+ ?
Thanks
Katrin

December 13, 2018 | Unregistered CommenterKatrin

Hi Robert,

Where in the code do you make the relationship with 00 01 02 03 to the GPIO pins from the Pi?

Thanks, Karel

August 30, 2022 | Unregistered CommenterKarel

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« Raspberry Pi - Install a Momentary Reset Button | Main | Raspberry Pi - Installing the Airlink 101 Wireless N 150 Ultra Mini-USB Adapter (AWLL5099) »