Using with the Raspberry Pi


The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries.The original model became far more popular than anticipated, selling outside of its target market for uses such as robotics.

The PTHAT has been designed to fit directly onto the Raspberry Pi expansion connector and complies to the Raspberry Pi HAT specifications.

More information can be found at the official site

Development Platforms

The Raspberry Pi can run a number of different operating systems and the popular one is RASPBIAN JESSIE WITH PIXEL .

This is Linux with a Windows GUI and has a number of different development options, with Python being the main one used by devlopers.

Check out the official site for more information and image downloads. 

Windows 10 IoT Core is a new edition of Windows targeted towards small embedded devices and maker boards such as Raspberry Pi2 or Pi3. IoT core is designed to work with low-level bus interfaces such as Serial, I2C, SPI, USB. You can write a Universal Windows Application using a variety of programming languages in Visual Studio to rapidly create maker projects.

Running a dedicated application in a Windows 10 sandbox is pretty cool and more information can be found on the following link which includes how to setup the development environment for your Raspberry Pi

Using the PTHAT with Windows 10 IoT Core.

We have created an example application using Visual Studio 2015 that can be used with Windows 10 IOT.
The example has been written in C# as a Universal Windows Platform (UWP).

This allows you to send commands using the serial port and covers the Instant Commands that are shown on the Command Set Page. Also it automatically formats the commands to the correct syntax. 

Please use the following link to download the full source code. 

To go through every step of setting up Visual Studio 2015 development environment with Windows 10 IOT is a big task and no point repeating it here as there is already good existing information.
A good starting place is Microsoft’s own site

Once you have your Raspberry Pi computer setup with the Windows 10 IOT image and Visual Studio configured, the you will be able to simply load the example application we have provided and start to send commands back and forth to the PTHAT.

When you launch the application it should enumerate the serial ports on your Raspberry Pi and list them in the top box.

1.) Highlight the Serial Port.

2.) Click the Connect button.

Once connected you will find that all the buttons at the bottom of the application will become enabled and the Read Data box will display Waiting for data..
You are now ready to send commands.

Each Axis has to be set before it can be started and in the example we have listed individual settings for the X,Y,Z and E Axis’s.

The following settings can be set:

Frequency: Sets the speed of the pulse train
Pulse Count: Sets the required amount of pulses to be sent out
Direction: Sets motor direction 0=CW 0=CCW
Ramp UP: See command Set page for more details.
Ramp Down: See command Set page for more details.
Ramp Divide: See command Set page for more details.
Ramp Pause: See command Set page for more details.
Link to ADC: See command Set page for more details.
Enable Polarity: See command Set page for more details.

1.) Click Set button

2.) Once set you will see that the Start button has been enabled.


Once the Start button has been pressed the PTHAT will start sending out the pulses and also the Pause and Stop buttons will become enabled. At this point you can pause or stop the pulse train at any time.

Also while the pulse train is running, you can send other request commands such as requesting the amount of pulses that have been sent out so far, the value of the ADC’s etc.

The Read Data box will be updated with the replies from the PTHAT.

You can of course select more than one Axis to set and start at the same time or even start then at different times while the others are running.

Just choose Set for each Axis and the click on Start All.

As you can see when selecting more that one Axis, it will still then allow you to individually pause, Stop each Axis or to control all at the same time.
Here is a video of us testing our application with our 4 motor rig.

Rather than clutter this one page with too much information, we have created an Example Page that has more Windows 10 IOT example applications you can download and try out, along with pictures, screen shots and videos.

Please click here to go to the PTHAT Examples Page


There is a lot of information already out there covering the use of the serial port on the Raspberry Pi, but there is a few considerations when running Raspbian as the serial port may need freeing up or enabled. A good site covering this information is and especially the section near the bottom of the page named Connection to a microcontroller or other peripheral

In the future we will also be providing some PYTHON examples.

A big thanks to Bill for the following Python Code. New Release 30/12/2018

Python Module for PT Hat

This module uses Python3 or higher to control the CNC Design’s Pulse-Train Hat for the raspberry-pi3 (or rpi2 if baud is set*) . It requires PySerial installed (check by typing: >>> import serial<<< at the command line) and the RPi configured to allow the use of the serial port (by default is is used for debug so needs to be fiddled with).

The module acts as a wrapper class for the PTHAT’s serial commands . The user only has to set the motor/drive characteristics (steps per unit etc.) to get the hat to work.

PT = PTHAT() #create a PThat object

Xaxis = PT.GetXaxis # create an axis object

Xaxis.StepsPerUnit = 100 * 16 * 3 # 100step/rev motor * 16 microstep driver * 3:1 ratio belt

# the units can be mm , inches or anything else –  the input is a float()

Xaxis.MotorEnable = True  #enable the motor
Direction = True # true is oneway , false is the other. default is true

Revs = 100

RPM  = 100

# the number of pulses sent to the motor drive is calculated by the PTHAT object (in inputs are signed float()  so will reverse if negative e.g. -12.62 .
Xaxis.SetMove(Revs, RPM, Direction)  # Direction is default so Xaxis.SetMove( -6, 34.6) is fine
Xaxis.Start() # make it move OR  use PT.StartAll() to … well guess 😉

Note: if the axis is heavy you may need to set the acceleration/deceleration time with Xaxis.AccelerationTime = (time in seconds (float) max~65s).

If responses are required from the PTHat, the object need to be polled in a loop

(if anyone knows how to make pyserial event driven – please let me know!)


    while axis.IsRunning:
        x = PT.Pollport # must poll the port to read the serial data
        if x : print(x)

# PT.pollport returns fault codes:
8 = ESTOP flagged
4 = BufferEmpty
2 = Limit hit
1 = OK

in any combination e.g. 4+2 = 6 buffer empty and limit hit
If the module is used with Tkinter , it is best to poll the PT object from an app.after() callback


app = MyTestApp(root)
app.after(150,PT_update) # PT_update polls PT.pollport and updates stuff every 150mS
The PT object can also use the ADC’s and AUX outputs:



Relay = PT.GetAUX1

Relay.SetAux(False) # for OFF or true for ON

Setting the PT.BufferEnable(True) will start buffering all following commands until PT.BufferStart() or PT.BufferEnable(False)

I have not as yet implimented the PWM pins .



*The RPi3 uses a baud rate of 115,200 , this rate is hard coded into the PTHmodule , if you need to use the higher rate (806400) you will need to edit the module


All usual caveats apply : Do not use for mediacal use or anything that might be in anyway dangerous .

Code is provided without warrenty and is royaly free. It’d be nice if you at least acknowledged the author.

If you have improvements please let me know . I am not any kind of python expert , in fact this is my first real python project.

W.K.Todd (2018)


Also there is now a Python API that has been developed and more information can be found on the link

Closer look at the PCB's

Share This