Search This Blog

Thursday, 18 December 2014

Teensy 3.1 Repstrap printer with DC motor control and Flex Timer Quadrature Encoders

As I re-engage my 3D Repscrap printer project,  I have decided to replace the two 8bit AVR based  Arduino pro minis running at 16Mhz with a single  Teensy 3.132bit ARM core M0 based board running at 96Mhz.   The Teensy is significantly faster, more powerful, and still only $20USD.

I was considerably influenced to change my direction based on this incredibly detailed Blog over at Freescale Nikki  Verriddagari, a Freescale developer, managed to replace the typical Arduino Mega2560 seen in most Reprap style printers with the Teensy 3.1 inside of 3-4 weeks, as a side project.

Both the Teensy and Reprap communities rallied to assist Nikki through a few difficult spots, and huge kudos to the developer of the Teensy 3.1, Paul Stoffregen, for working on the Teacup code with Nikki and getting it debugged and working!

The Freescale MK20DX256VLH7 processor used on the Teensy has two hardware based Quadrature Decoder modules built in. There is a good appnote on how to use them at Freescale. 
"The FlexTimer is a complex, general-purpose timer module that also possesses special features dedicated to a motor control application"

While perusing the Teensy 3.1 developer's site, I came across a forum article where Trudy Benjamin had created a library to use these FTM to read two quadrature encoders.   This looked like as good as anything for a starting point.

I'm using her QuadDecode library for the X and Y axis, and have decided to manage both Z-Axis encoders through hardware interrupts. My thoughts being that the Z-Axis travel is both minimal and predictable. I believe (I've been wrong before!) that Z-Axis movement is conducted typically after a horizontal layer of X/Y is completed.  However!!!  This does not preclude error correction in the Z-Axis PIDs. If something were to bump the extruder or a collision occurred that caused the Z-Axis to change, the PID would try to correct, and the interrupts must be responsive enough to handle this. Running at 96Mhz, I'm confident enough to give this a good shot.

My Design Concept:

I'm basing my requirements loosely on the RAMPS/Mega design, but for DC motors and quadrature encoders.

There will be four DC motors:  One each for the X and Y axis, and two for the Z axis. All will have inexpensive DC brushed motors.  The X and Y axis will have linear optical encoders, while the Z axis will have rotary optical encoders. (This is a mechanical decision for MY implementation, the electronics and firmware should not care).  All motor/encoder pairs will have endstops at each end of their travel.

To manage PWM for four DC motors, as well as PWM for the Extruder heater, Bed heater, and fans, I am choosing to borrow a design from Adafruit's Motor Shield V2.3.  In this shield, they employ a PCA9865 I2C 12 bit PWM controller, driving a pair of dual Mosfet H bridge TB6612FNG motor drivers.

A standard Pololu stepper motor driver board will be used to manage the extruder.

Primary communications will be via the Teensy's USB Serial interface, however I am including a uSD card reader for future plans to read G-Code directly from uSD card.

Starting with the example pinout used in the QuadDecode library,   I have drafted up a connectivity chart and initial schematic that I will be wiring up over the next few days.

Teensy 3.1 Based 3D printer with DC motor/Quad Encoders

Next blog will include photos of the build.

Please feel free to provide constructive criticism as I journey down this path.

PJRC Forum: Hardware Quadrature Code for Teensy 3.x  
PJRC: Teensy 3.1 Encoder Library
Freescale: Configuring the FlexTimer for Position and Speed Measurement with an Encoder
Freescale: PMSM Vector Control with Quadrature Encoder on Kinetis
Kinetis: K20 Sub-Family Reference Manual 
Data sheet: Supports: MK20DX64VLH7, MK20DX128VLH7, MK20DX256VLH7 
Teensy 3.1: Pulse Width Modulation 
Loglow: Interval Timer Library for Teensy 
PJRC: New I2C library for Teensy3  
PJRC: Connecting headers to those pins on the bottom of the Teensy 3.1 


  1. Wow, incredible blog layout! How lengthy have you been blogging for? you made running a blog glance easy. The whole glance of your site is wonderful, as well as the content!

  2. I’m very happy to uncover this web site. I wanted to
    thank you for ones time just for this fantastic read!!
    I definitely liked every bit of it and I have
    you saved as a favorite to see new stuff in your blog.

  3. I have read some excellent stuff here. Certainly value bookmarking for revisiting.
    I wonder how so much attempt you put to create any such fantastic informative website.