Raspberry Pi controls a Stepper Motor

Raspberry Pi2 with MOTORplate and stepper motor
Raspberry Pi2 with MOTORplate and stepper motor

This article is all about how to turn a Stepper Motor using a Raspberry Pi using the MOTORplate Controller and some software

A short Video

Stepper motor in action

Index

  1. Hardware needed
  2. Setup Raspberry Pi2 with raspbian using Noobs
  3. Steppermotor with MOTORplate hookup
  4. Enable GPIO on Raspberry
  5. Software

1. Hardware needed

There are several pieces of hardware (HW) needed to test this

  • HDMI Monitor (not needed if you install a VNC server)
  • Raspberry Pi-2 or Pi-3 or Pi-3+, I bought it from Pi-Shop (about CHF 40)
  • Raspberry Pi heatsink, I think a must (Pi-shop about CHF 4)
  • 16GB, 32GB or 64GB Memory card (SD card), ex. SAMSUNG EVO Plus 64GB from Microspot (about 30)
  • Power-supply raspberrry Pi, 5.1V, 2.5A (Pi-shop, about 20)
  • Stepper motor, Nema 17 Bipolar 1.8deg, 26Ncm (36.8oz.in), 0.4A 12V, 42x42x34mm, 4 Wires (about 7)
  • MOTORplate, (Pi-shop, about 50)
  • USB mouse (not needed with VNC)
  • USB keyboard (not needed with VNC)
  • HDMI cable (not needed with VNC)
  • Network cable or Wifi

2. Setup Raspberry Pi-2 using NOOBS

  • Download the latest Version of NOOBS from Raspberrypi.org. I like the Lite Version because the download is very fast and the setup of the SD card is easy.  During the install, the raspberry needs to be hooked up to the internet.
  • Get the SD card ready. Be careful, when your card it >32GB and you want to boot from it, you need to format the SD card with a special tool using a Fat32 filesystem. I used Guiformat.
  • Now insert the SD card, boot NOOBS and install raspbian. If you are using NOOBS-lite you need to select what you want to install. Click on below image to see the install.

 3. Stepper motor with MOTORplate hookup

Well I cut the connector of from the motor and soldered the ends.

I used the User-Guide from Pi-Plates to determine the stepper motor hookup and the reference card that came with the motor. You need to select an address using the little jumpers for each Pi-module. This is important for the software to address each module.  Based on the User-Guide I selected address = 6.

4. Enable GPIO on raspberry

By default the raspbian OS has the GPIO interface disabled. I had to learn this the hard way when I noticed a run failures executing the software.
I used the terminal window and modified the config.txt file or you can use the GUI. (see below)

#sudo nano /boot/config.txt
un-comment the following line and save your changes and reboot
#dtparam=spi=on

or use the GUI:
Menu > Preferences > Raspberry Pi Configuration > Interfaces and make sure SPI is set to Enabled.

5. Software

First things first, we update to all the latest OS patches via terminal window

5.1 OS

  1. #sudo apt update
    (in order to get the system ready)
  2. #sudo apt upgrade
    (the actual upgrade)
  3. #sudo rpi-update
    (the firmware upgrade)
  4. #reboot
    (and the reboot which is needed after a firmware upgrade)

5.2 Development Editor

Now we choose our Dev environment, I like Geany
#sudo apt install geany

5.3 Python

Since raspbian is linux and development is usually done in Python I want to go forward with the newer Version of Python 3. The current Version is 3.5. With raspbian, python is already installed, what we need though are some libraries from pi-plates.

5.3.1 Pi-Plates

pi@raspberrypi:~/Downloads $ sudo pip3 install Pi-Plates
Collecting Pi-Plates
Downloading https://www.piwheels.org/simple/pi-plates/pi_plates-4.1-py3-none-any.whl
Installing collected packages: Pi-Plates
Successfully installed Pi-Plates-4.1
pi@raspberrypi:~/Downloads $

to update to a newer Version of Pi-Plates:
#sudo pip3 uninstall Pi-Plates
#sudo pip3 install Pi-Plates

In case “pip3” is not yet installed:
#sudo apt install python3-pip

Where are the files being installed to?
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/DESCRIPTION.rst
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/INSTALLER
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/METADATA
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/RECORD
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/WHEEL
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/metadata.json
/usr/local/lib/python3.5/dist-packages/pi_plates-4.1.dist-info/top_level.txt
/usr/local/lib/python3.5/dist-packages/piplates/DAQC2help.txt
/usr/local/lib/python3.5/dist-packages/piplates/DAQC2plate.py
/usr/local/lib/python3.5/dist-packages/piplates/DAQChelp.txt
/usr/local/lib/python3.5/dist-packages/piplates/DAQCplate.py
/usr/local/lib/python3.5/dist-packages/piplates/MOTORhelp.txt
/usr/local/lib/python3.5/dist-packages/piplates/MOTORplate.py
/usr/local/lib/python3.5/dist-packages/piplates/RELAYhelp.txt
/usr/local/lib/python3.5/dist-packages/piplates/RELAYplate.py
/usr/local/lib/python3.5/dist-packages/piplates/__init__.py
/usr/local/lib/python3.5/dist-packages/piplates/__pycache__/DAQC2plate.cpython-35.pyc
/usr/local/lib/python3.5/dist-packages/piplates/__pycache__/DAQCplate.cpython-35.pyc
/usr/local/lib/python3.5/dist-packages/piplates/__pycache__/MOTORplate.cpython-35.pyc
/usr/local/lib/python3.5/dist-packages/piplates/__pycache__/RELAYplate.cpython-35.pyc
/usr/local/lib/python3.5/dist-packages/piplates/__pycache__/__init__.cpython-35.pyc

5.3.2 GPIO

pi@raspberrypi:~/Downloads $ sudo pip3 install RPi.GPIO
Requirement already satisfied: RPi.GPIO in /usr/lib/python3/dist-packages  (already installed)

5.4 MOTORdemo Software from Pi-Plates

Download Demo:

pi@raspberrypi:~/Downloads $ wget https://pi-plates.com/downloads/MOTORdemo.tar.gz
–2018-10-19 09:35:42– https://pi-plates.com/downloads/MOTORdemo.tar.gz
Resolving pi-plates.com (pi-plates.com)… 50.87.178.143
Connecting to pi-plates.com (pi-plates.com)|50.87.178.143|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 11480 (11K) [application/x-gzip]
Saving to: ‘MOTORdemo.tar.gz’

MOTORdemo.tar.gz 100%[============================>] 11.21K –.-KB/s in 0s

2018-10-19 09:35:49 (27.1 MB/s) – ‘MOTORdemo.tar.gz’ saved [11480/11480]

Unzip Demo:

pi@raspberrypi:~/Downloads $ sudo tar -xzvf MOTORdemo.tar.gz
MOTORdemoOOP.py
3D-ppLogo.gif

5.5 Create Geany Project

Now we make a few modifications:

  • We need to change the terminal for Geany, Menu – Edit – Preferences
    to verify that xterm is installed, use
    #sudo apt install xterm
  • Now the Build Command change for Python3
    Menu – Build – Set Build Commands
    Notice the python3 in the Compile line and IN the Execute line

5.5.1 Code Modifications

I ran into some minor problems and will tell you the fixes.
First in MOTORdemoOOP.py change to the following 2 lines

old:
from Tkinter import *
import tkFont

new:
from tkinter import *
from tkinter import font
#init globals
addr = 6

then search and replace all of the following:
“tkFont.Font” to “font.Font”
and
“(0,” to “(addr,”

Explanation:

  • tkinter is now lowercase
  • I used the library font because tkFont is already within tkinter
  • In addition the demo code was hardcoded to Address = 0 and I changed my Pi-Plates MOTORplate to Address ID = 6, so whereever there was a call to a function
    “MOTOR.stepperCONFIG(0, ” it now reads “MOTOR.stepperCONFIG(addr,”

5.6 Run the code

press F5 and the code should run

Geany MOTORplate software
Geany MOTORplate software
Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.