Controlling stepper motors with Gertbot and Python on the Raspberry Pi

The Gertbot is an add-on board for the Raspberry Pi, compatible with all existing models.

It brings power and motor control capabilities to the Raspberry Pi, but can also be used with other controllers supporting serial communication. Because the Gertbot has it’s own CPU dedicated to the real time activities, the Raspberry Pi is only required to pass high level commands such as “DC motor 1, ON”, “Stepper motor 2, 200 steps at 500Hz”.

The drivers come in two flavours: Python and C/C++. There is also an optional GUI, not requiring the use of code to use the Gertbot.

To know more about Gertbot, visit: http://www.gertbot.com

In this post, I’ll be demonstrating how to control two stepper motors using the Gertbot Python drivers.

Setting up

Getting everything set up is really easy and can be done in a matter of minutes.

The first step is to connect the Gertbot to the Raspberry Pi, but before doing so, ensure the Pi is powered off. Take the Gertbot and connect the 26-pin header, starting from pins 0 and 1. The Gertbot should slide in nicely next to the ethernet port, without touching it.

The result should be as follows:

photo 5

Next, the Gertbot Python drivers need to be installed on the Raspberry Pi. To do this, power on the Pi and using a terminal, browse to where you’d like to deploy the drivers.

The Gertbot Python drivers can be downloaded from the Gertbot website using following command:

  • wget http://www.gertbot.com/gbdownload/src/gertbot_py.tgz

The output of the command should be similar to this:

The downloaded drivers are compressed. In order to be able to use it, the file must be extracted:

  • tar -xvzf gertbot_py.tgz
There you have it, the “gertbot.py” file. By importing this file in other Python scripts, it is possible to call certain functions that will facilitate the control of the motors.

Script

At the bottom of the Gertbot’s download page, some example scripts are provided.

Starting off from the “Simple Rover” Python script and looking at the “Stepper Rover” C code, I came up with my own simple Python script to control two stepper motors. The script allows the control of two stepper motors to either go in one direction or the other. This is for example useful to control the Z-axis of a 3D printer.

It’s possible to define the number of steps and the frequency at which the steps are taken.

The latest version of the code is available on GitHub.

As for the hardware, I used two 200 steps, bipolar, 12V NEMA17 stepper motors and connected them to the Gertbot as per the documentation. The underside of the Gertbot also indicates how to connect the motors.

photo 4 photo 3 photo 1

Demo

As a demo application, I’ve built a prototype of a lift to slide a screen in and out of place. The lift is controlled using the Python script documented earlier.

 

Leave a Reply

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