Search This Blog

Tuesday, July 30, 2013

Upgrading Arduino XYZ CNC Machine Tool Motor

For a tool motor I previously used a scrounged rechargeable DC drill motor and chuck.  The motor was from a 12V rechargeable drill.  I used an Arduino and PWM to modulate the speed, but had to make a custom MOSFET hbridge add on to deal with the high current.  This post:  poorly documents my previous work ;-).

The circuit I had worked fine for controlling the motor, but after some discussion with friends I decided that my motor was really not spinning at high enough RPM for most engraving and cutting jobs.

Two options I see

  • Use my dremel and add a solid state relay to control the AC.  I probably won't be able to modulate speed any more
  • Buy a purpose build CNC DC spindle motor.  
I also struggled a bit with a good motor mount for the old drill motor.  I had a hose clamp around it but it looked lame and didn't seem very well aligned and stable.

I decided to invest in a high RPM spindle motor.  Zen toolworks has two grades they sell.  Fairly expensive, and the cheaper one is known to have high runout.   I don't really like the plastic brackets, although the whole machine is made out of it, so I suppose it's strong enough.

Another vendor had a motor with a fan, and an aluminum mount.  Total price would be about the the same.

However I noticed that the vendor ships from China.  Long wait.  I've bought stuff this way and it is usually OK, but I avoid it if I can.   Impossible to return things if you are unhappy.

I poked around ebay.  Found the same motors everywhere for a variety of prices.  Almost all ship from China or Hong Kong.

Finally decided to buy this one for $79 from a US vendor.  A little pricey. I hope it's worth it.  It had a fan and the mounting block I need.  Nice.  I'll pay the premium for US seller.

I previously used a MCH3484 and a MCH3383 MOSFETs to make an Hbridge to PWM the speed from the Arduino.  That should work again.

I also need a power supply.  The specs say this:

  • Work voltage: 12-48vdc (Suggest to use DC 48V)
  • Rotation speed: 3000-12000r/min
  • Power: 300w
  • Torque:230mn.m
300W at 48V means 6.25A at highest power

This little $12  DC/DC converter gives me 120W/5A, but I have to supply it with another DC input.  That means I need a power supply to drive my power supply.  No thanks.

A bigger one for 10A and 500W.  $28

I poked around looking for an AC powered 36V or 48V DC lab supply with little success on ebay or amazon.  Digikey has stuff for >$200.  Wow.  Maybe this wasn't such a good idea.
I'll keep looking for now for a deal on eBay, before I build a power supply.

The motor came.  It is a beast.  Much bigger than the old drill motor.  The aluminum mount is nice.
I'll need to drill mounting holes in the plate on the Zen.

I used my drill press to stick through the four mounting holes and drill through the face plate of the gantry.  (I took the faceplate off).   I found four lockwashers in my junk bin that fit the bolts that came with the mounting bracket and tightened it all up.
 It went together surprisingly well, no issues whatsoever!

Here it is mounted back on the Zen toolworks machine.  I'm a little worried I went over the top on this one, since the faceplate is attached with 6 tiny screws and the mounting bracket has four giant bolts and 1/4 aluminum.  I'll go easy with this thing and not rev it up full speed.

Slight assembly issue, the gantry screws are now behind the motor.  I need to loosen the bolts, remove the motor from the mount, re-attach the gantry and put the motor back in.  A little extra work.

Clearance looks good inside the gantry.  The bolts don't hit anything.

Found a power supply on ebay for $45, free shipping.  It is direct from China so there is some risk and it will take a while to get.  Not sure why it is so much cheaper than everything else.  Just a transformer is $25 if I'm going to make my own.   Bought this one:

36V 10A 350W AC to DC Switch Power Supply Transformer for LED Strip light CCTV
Input Voltage: 100~120V AC, 200~240V AC (Preset 220V)
Output Voltage: 36V DC
Output Current: 10A
Shell Material: Metal case / Aluminum base
Protection: Shortage Protection, Overload Protection, Over Voltage Protection

It is only 36V versus the 48V that the motor can go to, but I think that 36V will be enough and 48V will be crazy fast.   I have some hope of my hbridge motor driver still working up to 36V.

Waiting for it to come from China.....

Motor finally came, a little faster than I expected but I was out of town.

The power supply is a bit confusing,  There is no power plug and mysteriously labeled terminal screw strip.  A little googling on the part number LIHUA-360W on the side is encouraging.  Looks like lots of people use these for reprap 3D printer power supplies (the 12V version).

You connect the power cord to the GND, N(neutral) & L(line) connections.   =V and -V are 36V output.  Hopefully the rails aren't divided such that I can't get the 3A I need from one terminal, or maybe I can gang them.

I cut the end of an extra standard heavy duty three prong monitor power cord, and stripped the wires.  There is a green (GND), white (neutral) and black (line) wire.

Hooked them up like this to the power supply like this.  Green to gnd, White to N and black to L.

I plugged it in and nothing happens.  DC output is 0V.  I used the DVM to measure the line voltage, and it reads 120V AC between N and L.  Arggh.  Fail.  Then I remembered I forgot to flip the switch on the side to 115V, it had been on 240V.   Plugged it in again and it works!  No harm done.  Win!

You can see the green light on the left comes on.

Voltmeter on the V- and V+ terminals reads 36V.  The small pot next to the light adjusts it, and it is pretty touchy.  It started at 36.4 and I dialed it down to 36.0.   The motor takes up to 48V, so I might later turn it up as high as it will go.

I connected a cord to the DC output voltage.

Hooked it temporarily to the motor supply and plugged it in.    Whirrrrrrrrr!   
The motor is smooth and quiet.  Almost no vibration and quieter than I expected.  I have no idea yet what the RPM I'm getting is, seems fast, but I need to think of a way to measure it.  Left it run for 5-10 minutes and everything seems fine.  No smoke.  Hopefully I've got enough voltage for good operation.   Looks like I'm in business.

While the motor was running, I measured the power supply voltage.  If I was overloading the supply then I'd expect the voltage would have drooped.  Good news, it still read 36.0V with the motor running.

I used the adjustment potentiometer to turn up the voltage to max, which measured around 39.1V.  There was a noticeable pitch change in the speed of the motor, so the extra couple of volts made a difference.   The motor is rated to 48V but I'm going to settle for 39V.

Saturday, July 27, 2013

Arduino XYZ CNC / 3D Printer Do-over

Now that I've polished my Java skills over the past couple years I wanted to come back and improve a previous project of an XYZ CNC machine that was controlled by Arduinos and a crude PC Java program.

This is the hardware I'm using.  I've covered how to build all these pieces in earlier blog posts.  As I work through the pieces I'll refresh the info on the hardware and sketches.

I'll need to make some software updates to the Arduinos, but the original Arduino sketches are posted on my download pages.    I want to improve the handshaking, auto detect the arduinos, and process HPGL files better, plus make a better GUI.

There are three Arduinos.  The first is the XYZ motor controller that uses two motor shields from ADAfruit.  I posted a hack on how to attach two to the same Arduino.  I also replaced the motor driver chips with some higher capacity chips and put on heat sinks.  The reason one arduino does all this work is I absolutely want to synchronize the x, y and z moves.  I don't want to risk dropping a move or mistiming a move due to USB communication glitches.  This arduino gets HPGL format commands that give position from and to commands and it calculates all the sub stepping to get there.

The second arduino only controls the speed and direction of the cutter.  The cutter is a cordless drill motor and chuck controlled by some heavy duty MOSFET switches.

The third arduino is connected to position encoders and an LCD.  It reads back the position and also displays it and any other messages the machine has to say.   This is mostly used as a watchdog, since the steppers count position.  If the position gets off from what the machine thinks it should be I should stop and let the user figure things out.

Power is supplied by a old PC desktop power supply.

My PC original control program was very crude and didn't work if the Arduinos were in different COM ports and didn't have file spooling capability.  I'm way better at coding now, that was my very first program after "Hello World".

So my project this time is to write a respectable java program that will work for everyone, It will read/spool in a standard HPGL command file and control the CNC.  It will also have buttons for manual controlling the CNC.

I'm using Java and the netbeans development environment, which is free from oracle.
I'm starting with the basic Java serial tool I just wrote for the PC that will communicate with the Arduinos.

The first piece will be a startup routine that finds all three Arduinos and establishes a link.  Each arduino sketch will be updated to send an initialization sequence I can look for.

After that there will be a choice of entering manual mode to push buttons on the GUI to move, or load an HPGL drawing file.  There should be a routine for setting the home position.

First I'm going to work on communicating with the position sensor I described in this post:
I fired it up with the serial tool and the original code in the post is working fine, the encoder is constantly spitting out the position on the COM port as fast as it can.  For now I shouldn't need to alter the Arduino sketch.  My PC program will have to seek and identify this data stream and map it properly.

Next I fired up the XYZ stepper Arduino and that seems to be in good order as well.  The original sketch was built from this post.  You need to use the custom library to use all four steppers
and the sketch for the XYZ control is posted on my download site:

Writing the Java to connect to all the Arduinos

First step is to perfect the serial port autodetection.  I'll use the touchport command and then look for a unique broadcast from each Arduino in order to assign them to ports.

After a long stall, I finally got this working in a recent project.

I had been hung up from a mistake.  I used touchPort, to get the port names, and then instantiated a serial class to talk to each one.  However that process wiped out my list of ports so the loop failed.   Also I learned I have to wait and let the listener have time to hear each Arduino before moving on.  Here are the basic pieces.

This Java loop goes through all the ports:
     int iii=0;
        while (clientPortName.equals("")) {
            //scan for devices and list
            serial.touchPort("", Integer.valueOf(serialRates.getSelectedItem().toString()));
                    jListPorts.setModel(new javax.swing.AbstractListModel() {
                    String[] strings = serial.portNames;
                    public int getSize() { return strings.length; }
                    public Object getElementAt(int i) { return strings[i]; }
            if (!serial.portNames[iii].equals("")) {        

                System.out.println("PORT:" + serial.portNames[iii]);
                // give it some time to hear the Arduino calling
                long currentTimeMillis = System.currentTimeMillis();
                while (System.currentTimeMillis()<currentTimeMillis+3000 ) {
                    //just hang out hoping for a letter
          if (iii>19) {
              System.out.println("Exiting, Arduino not found");

This routine below grabs each port and sets up a listener and sets a flag that breaks the loop above if the desired character is found.

       public void setupCom(final String portName) throws SerialException{
                serial = new Serial(portName, Integer.valueOf(serialRates.getSelectedItem().toString()), 'E', 8, 2 );
        //Add the listener to pipe remote responses 
        serial.addSerialEventListener(new Listener_SerialEvent() {
            public void SerialEventOccurred(int message) {
                String message_out = formatRead(message);           
                if (message_out.equalsIgnoreCase("Q")) {
                    System.out.println(portName + " detected Q - gotcha!!!");
                    clientPortName = portName;
                    send("Hello World!");  //respond so Arduino stops sending Q

I don't have to break the upper loop, I could go back and set up the port at the end of looping, but this is faster if I stop when the right port is found.

I've updated the serial Arduino tool with a field for the character is it going to look for to connect. In this picture the GUI has three Arduinos connected.  One is transmitting Z, one is busy, and one is transmitting A.  The one transmitting Z , COM4, is grabbed and now send and receive can begin.

Download the executable here:

Now I will use multiple of these tools to search and connect to the three Arduinos.
I've got that code working, each Arduino is set to broadcast a different character to be found by the serial comm port scanner.  However that means I had to resurrect the old arduino code and modify it.

I had no trouble with the encoder program, the rotary encoder sketch.  I was able to add the establishContact() function from the Arduino serial response example.

The XYZ motor controller program is broken.  The old AFMotor.h libraries I had been using from Adafruit don't seem to be working, they don't compile in the new release.   I updated new library from here:
that library seems OK, but my modified AFMotorA.h library for use in stacking shields is broken.  maybe I need to remake it.

The big problem turned out to be that now Arduino installs to the Program Files (x86) directory, which is read only and blocked by default.   I had to make all the library files unblocked by right clicking and selecting properties.  In the process I did remake the AFMotorA library.  All I did was search and replace AFMotor for AFMotorA and AF_Stepper with AF_StepperA, and microsteppercurve to microsteppercurveA in the .cpp, .h and keywords files.  Also rename the files AFMotorA.cpp and AFMotorA.h.   Now the old sketch compiles pointing to the AFMotor libraries.

I'm using three instances of my serial tool with only slight modification.  One for each arduino.
Had to do some mundane coding to pass the serial responses up to the main Arduino GUI by adding listeners to each of the serial port GUIs and passing them up to the main GUI.

The connection process takes so long that I want to move it into a thread by itself rather than locking up the GUI for 15 seconds.  that fixed a lot of weird problems I was having with the GUI not drawing, messages not scrolling etc.

Cool, now I have a main GUI that when you press connect, runs out and finds 3 different Arduinos and scrolls a log of what it is doing.   This simple task had eluded me for a while due to stupid mistakes.

Now the fun part, re-writing the code to actually move the XYZ machine.

Cleaning up the Arduino Encoder Program & Sketch

Updated the code to pull the position from the Arduino, parse the string and display it.  I'm finding the response time from the Arduino is really slow between when the encoders and LCD report the change, and when the string shows up on the serial bus.  I'm going to change the encoder code to only print to serial when there is a change to reduce the traffic and hopefully prevent the backlog.  It worked!  Now I only send serial traffic when there is a change, and response is much faster.

Realized I need a function for the Encoder program to tell it to reset it's counters.  That is for a Home position command.  Added that and updated the position encoder program.  Also found a bug that the position counters were unsigned integers!!  The LCD calculation seemed right but the serial information signs were mixed up.  Fixed it.  It is working much better now!

 /* Rotary encoder readout for XYZ CNC machine */

// Encoders connected as follows
// X encoder PIN A - Analog 0
// X encoder PIN B - Analog 1
// Y encoder PIN A - Analog 2
// Y encoder PIN B - Analog 3
// Z encoder PIN A - Analog 4
// Z encoder PIN B - Analog 5
// Pin C of all encoders is grounded, center pin of
// thus variable PINC will read this entire register simultaneously
// this avoids missing states of the encoder
//Based on 
//Expanded to 3 encoders and written to be more clear and less over elegant

//Variables to set the counts/inch
#define TH_PER_IN  20 //lead screw threads per inch
#define CT_PER_REV  48 //encoder counts per revolution
// Thus the accuracy of the measurement 1 inch /(TH_PER_IN*CT_PER_REV)

//Variable to reverse the sign if the machine and encoder don't agree with your sense of things
// set to 1 or -1
#define REFLECTX -1
#define REFLECTY -1
#define REFLECTZ 1

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

unsigned int olddata;
unsigned int newdata;
char newX;
char newY;
char newZ;
char oldX;
char oldY;
char oldZ;
long signed int counterX = 0;
long signed int counterY = 0;
long signed int counterZ = 0;
char inByte = 0;         // incoming serial byte
// table of meaning of states, input is two bits old value, two bits new value
// the encoder has either moved +1, -1 or stayed in the same place
int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; 

void setup()
  /* Setup encoder pins as inputs */
  DDRC = B00000000;   //Sets input direction all to analog inputs
  PORTC = B11111111;   //Sets pull ups on the analog pins to 1
  Serial.begin (9600);
  Serial.println("Encoders Found");
  counterX = 0;
  counterY = 0;
  counterZ = 0;
  pinMode(13 ,OUTPUT);  //turn on backlight
  digitalWrite(13, HIGH); //turn on backlight
  delay(500); //Optrex display needs to rest after a begin
  delay(100); //Optrex display needs to rest after setting cursor
  lcd.print("XYZ CNC Position");
  lcd.print("X: ");
  lcd.print("Y: ");
  lcd.print("Z: ");
  //send initial position
   Serial.print(1000*counterX/(TH_PER_IN*CT_PER_REV*REFLECTX), DEC); Serial.print(":");
   Serial.print(1000*counterY/(TH_PER_IN*CT_PER_REV*REFLECTY), DEC); Serial.print(":");
   Serial.print(1000*counterZ/(TH_PER_IN*CT_PER_REV*REFLECTZ), DEC); 

void loop()
  //This loop needs to run fast to avoid missing encoder states
  newdata = PINC;
  if( newdata != olddata ) {
    newX = newdata & 0x03;  //mask out just the X bits
    oldX = olddata & 0x03;  //mask out just the X bits
    oldX = oldX << 2;  //move over to form address to table
    counterX += enc_states[oldX + newX] ;  //increment or decrement counter from table

    newY = newdata & 0x0C;   //mask out just the Y bits
    newY = newY >> 2;  //shift to LSBs
    oldY = olddata & 0x0C;   //mask out just the Y bits
    counterY += enc_states[oldY + newY] ; 
    newZ = newdata & 0x30;   //mask out just the Z bits
    newZ = newZ >> 4;  //shift to LSBs
    oldZ = olddata & 0x30;   //mask out just the Z bits
    oldZ = oldZ >> 2;  //shift to form address to table
    counterZ += enc_states[oldZ + newZ] ;
    // only send updates over serial bus when data has changed to reduce traffic
     Serial.print(1000*counterX/(TH_PER_IN*CT_PER_REV*REFLECTX), DEC); Serial.print(":");
     Serial.print(1000*counterY/(TH_PER_IN*CT_PER_REV*REFLECTY), DEC); Serial.print(":");
     Serial.print(1000*counterZ/(TH_PER_IN*CT_PER_REV*REFLECTZ), DEC); 
  }  else {
    // only update the display when the data is NOT changing, this speeds up polling
    lcd.print(1000*counterX/(TH_PER_IN*CT_PER_REV*REFLECTX), DEC);
    lcd.print(" mils    ");
    lcd.print(1000*counterY/(TH_PER_IN*CT_PER_REV*REFLECTY), DEC);
    lcd.print(" mils    ");
    lcd.print(1000*counterZ/(TH_PER_IN*CT_PER_REV*REFLECTZ), DEC);
    lcd.print(" mils    ");
    // check if any serial messages have come in, since not busy
    if (Serial.available() > 0) {
      // get incoming byte:
      inByte =;
      // reset position on indicated counter
      if (inByte == 'X')  counterX = 0;
      if (inByte == 'Y')  counterY = 0;
      if (inByte == 'Z')  counterZ = 0;
      if (inByte == '@' )  {
        counterX = 0;  counterY = 0;  counterZ = 0;
      // any other serial traffic respond with current position
      Serial.print(1000*counterX/(TH_PER_IN*CT_PER_REV*REFLECTX), DEC); Serial.print(":");
      Serial.print(1000*counterY/(TH_PER_IN*CT_PER_REV*REFLECTY), DEC); Serial.print(":");
      Serial.print(1000*counterZ/(TH_PER_IN*CT_PER_REV*REFLECTZ), DEC); 
  olddata = newdata;

 void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('Q');   // send a capital Q to identify myself

I realized I need to be able to calibrate and send a coordinate to the encoders and have them update their math to scale that to the known zero position.  I'll do that once I make some progress on other fronts.

Writing the GUI to Manually Control The Motors

Set up a GUI to send manual commands to the CNC machine.  Functions for Jogging X,Y, and Z by a select-able step size.  Turn on and off the tool and change it's speed.  Display the encoder's value and the calculated location both.   Finally an interpolated move command to calculate the vector to an inputted coordinate.

Next I need to go back and clean up the programming for the heavy duty motor driver.
I used a bunch of mosfets and random bipolars to convert an Adafruit motor sheild to drive a drill motor.   I left this pretty rough.  I need to clean up the control sketch for the Arduino and test it out.   I set up a slider to control the speed.  I'm not sure if I'll do active motor braking or not, depends on how things work.

Decided to update the motor controllers and eliminate one of the Arduinos!

In the process of resurrecting the heavy duty motor shield circuit, and noticing that my blog post was kinda missing a lot of information, I discovered that Adafruit had updated their motor shield and no longer sold the old one I used.   They now support stacking, which I had hacked up before.
It's totally cheating to use their shield, and maybe pricey as well, but it will save me a lot of time and what I build will be repeatable by others.  Additionally it looks like I could stack and use a single Arduino to run all four motors X, Y, Z, and Tool.  They now have a dedicated PWM chip on board. Before as well the load on the Arduino PWM was too much for four motors and I backed off to two Arduinos.  I will still need to use the heavy duty Hbridge MOSFETs for the tool motor, but that is a minor mod.   I pulled the trigger to upgrade the motor shields to the new version.  It will save me an entire Arduino.  Too bad I already wrote code, but it will be worth it.

I'm rewriting the XYZ Arduino code now to support both the XYZ and the Tool speed and use the new Adafruit v2 motor shields stacked up.  Additionally I'm going to change the architecture of the system.  Previously I had the Arduino interpreting the HPGL code directly.  That was very limiting, but I did it because I didn't know Java at the time.   A better approach is to have the PC software do the hard work, and only send specific movement commands to the Arduino.  The Arduino will still linear interpolation to move the X and Y motors together to make smooth diagonals.  I don't think it's necessary to do linear interpolation with the Z axis.

New Arduino XYZTool code

Java program will need to send the following commands to the Arduino.
Semicolon indicates command termination and begin executing.
Command set proposed:

  • R; = Reset position counters to 0:0:0 (Set Home)
  • C; = Coordinates:  Report back the current stepper counter position over the serial link for use in calibrating and catching errors
  • Pn; = Z axis relative move n steps
  • Zn; = Move to absolute Z coord
  • Mx:y; = Move relative by x:y steps
  • Ax:y; = Move to absolute coordinate x:y
  • Tn; = Set tool speed to n.  May be positive or minus.  0 is off.
  • S; = Stop all motors
  • I; = power on/initialize motors

After each command the Arduino will send back characters:

  • B; - Arduino received the command and is busy executing it
  • R; - Ready for a new command
  • Cx:y:z - Arduino counters say this is the current coordinates

I added each of these commands to the buttons in the Java program.  Now I need to jump back to the Arduino to interpret them.

The Adafruit v2 motor shields came in the mail.   I assembled them exactly like the tutorial.  I stacked two shields, and shorted the address bit on the bottom one.  One for XY and one for Z and tool.
I hooked up a tiny DC motor and three hobby steppers to the shields for a mock up to use while I write the code, without worrying about running the motors into a wall, etc.
A basic motor party script with both shields worked right off the bat.

Now I'm rewriting the motor control program for the Arduino to interpret the simple commands from the Java GUI, rather than try to decode HPGL all by itself.  That was limiting the functions I could perform.   It didn't take as long as I thought it would, because I was able to reuse the heavy lifting from the old program and just change the command interpretation loops.  I still need to add the timeout and error catching stuff but it's working pretty well already!

Arduino sketch for XYZ CNC control:

Program to intepret CNC commands and activate motors
Uses Adafruit v2 motor shields

Commands that will be received:
R; = Reset position counters to 0:0:0 (Set Home)
C; = Coordinates:  Report back the current stepper counter position over the serial link for use in calibrating and catching errors
Pn; = Z axis relative move n steps
Zn; = Move to absolute Z coord
Mx:y; = Move relative by x:y steps
Ax:y; = Move to absolute coordinate x:y
Tn; = Set tool speed to n.  May be positive or minus.  0 is off.
S; = Stop all motors
I; = Power all motors/ initialize

After each command the Arduino will send back characters:
B; - Arduino received the command and is busy executing it
R; - Ready for a new command
Cx:y:z - Arduino counters say this is the current coordinates


#define INCH 4000 //steps per inch of the machine = threads per inch * steps per rotation 
#define RPM 30 //speed of the stepper motors

#include <Wire.h>
#include <Adafruit_MotorShield.h> 
//#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address for Z axis stepper and Tool
Adafruit_MotorShield AFMS_ZT = Adafruit_MotorShield(); 
// Or, create it with a different I2C address for stacking for X and Y axis steppers
Adafruit_MotorShield AFMS_XY = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *stepperZ = AFMS_ZT.getStepper(200, 2);
// And connect a DC motor to port M1
Adafruit_DCMotor *myTool = AFMS_ZT.getMotor(1);
// Connect two stepper motor with 200 steps per revolution (1.8 degree)
// to motor ports on bottom shield
Adafruit_StepperMotor *stepperX = AFMS_XY.getStepper(200, 1);
Adafruit_StepperMotor *stepperY = AFMS_XY.getStepper(200, 2);

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
long previousMillis = 0;        //used for timeout counter
long timeout = 10000;           //timeout length

// Set up the basic machine position variables
signed long int Xpos = 0;
signed long int Ypos = 0;
signed long int Zpos = 0;

// Set up the destination machine position variables
signed long int newXpos = 0;
signed long int newYpos = 0;
signed long int newZpos = 0;

void setup()
  // start serial port at 9600 bps:
  establishContact();  // send a serial byte to establish contact until receiver responds 
  //Start the PWM
  AFMS_XY.begin();  // create with the default frequency 1.6KHz
  AFMS_ZT.begin();  // create with the default frequency 1.6KHz
  //initialize motors
  // Home position
  Serial.println("Stepper Controller Online;");

void loop()
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get the new byte:
    char inChar = (char); 

    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == ';') {
      stringComplete = true;
    } else {
      // add it to the inputString:
      inputString += inChar;
    // emergency stop command, process immediatly no matter what I'm doing
    if (inChar == '*') emergencyStop();
    if (stringComplete) {
      //Indicate busy 
      previousMillis = millis();         //set the timeout counter to start

      //Echo the command
      //decode and do what the command said
      switch( inputString[0] ) {
        case '*' : emergencyStop(); break;
        case 'I' : initMotors();  break;
        case 'T' : setToolSpeed();  break;
        case 'C' : reportCoords();  break;
        case 'P' : relativeZMove(); break;
        case 'Z' : absoluteZMove(); break;
        case 'M' : relativeXYMove(); break;
        case 'A' : absoluteXYMove(); break;
        case 'R' : resetPositionCounter(); break;
        default  : break;
      //clear the command
      inputString = "";
      stringComplete = false;
      // Indicate ready


//    else if ((previousMillis+timeout) > millis()) {
//       //Waited too long for the end of the command
//        intputString = "";
//        stringComplete = false;
//       Serial.println("E;");
//    } 

void establishContact() {   //for initial identification
  while (Serial.available() <= 0) {
    Serial.print("~");   // send a charachter to identify myself

void emergencyStop() {   //release all motors, limit hit or user intervention
      //Serial.println("All motors released;");

void setToolSpeed() {  
    signed long int newSpeed = extractCoord(inputString, 0);
    if (newSpeed>0) myTool->run(FORWARD);
    else myTool->run(BACKWARD);
    if (newSpeed == 0) myTool->run(RELEASE);
    else myTool->setSpeed(abs(newSpeed));

signed long int extractCoord(String numberString, int pos) {  //pulls the signed coord from the serial input string
  // pos indicates first or second coord desired
  int splitPos = 0;   //location of the : character
  signed long int coord = 0;   //temp storage for coords 
  signed int sign = 1;         //temp storage for coord sign 
  char c;  
  //First find the position of the :
    for (int i=1; i<numberString.length(); i++) {
      if( numberString[i] == ':' ) splitPos = i ; 
  if (splitPos ==0)  {  // there is only one coord  
    for (int i=1; i<numberString.length(); i++) {
      c = numberString[i];
      if( c == '-' ) sign = -1; // capture the sign 
      if( c >= '0' && c <= '9') coord = (10 * coord) + (c - '0') ; // convert digits to a number     
  } else {
    switch (pos) {
      case 0 :
      for (int i=1; i<splitPos; i++) {
          c = numberString[i];
          if( c == '-' ) sign = -1; // capture the sign 
          if( c >= '0' && c <= '9') coord = (10 * coord) + (c - '0') ; // convert digits to a number     
      case 1: 
        for (int i=splitPos; i<numberString.length(); i++) {
          c = numberString[i];
          if( c == '-' ) sign = -1; // capture the sign 
          if( c >= '0' && c <= '9') coord = (10 * coord) + (c - '0') ; // convert digits to a number     
      default: //something is wrong, return 0;
  return(coord * sign);
void resetPositionCounter() {
  Xpos = 0;
  Ypos = 0;
  Zpos = 0; 
  newXpos = 0;
  newYpos = 0;
  newZpos = 0;

void reportCoords() {
    Serial.print(Xpos);Serial.print(":");Serial.print(Ypos); Serial.print(":");Serial.print(Zpos);Serial.println(";");  

void initMotors() {
  //initialize motors
  //step motors one step and back to power the coils and hold
  stepperX->step(1, FORWARD, SINGLE); 
  stepperY->step(1, FORWARD, SINGLE);
  stepperZ->step(1, FORWARD, SINGLE); 
  stepperX->step(1, BACKWARD, SINGLE); 
  stepperY->step(1, BACKWARD, SINGLE);
  stepperZ->step(1, BACKWARD, SINGLE); 

void relativeZMove(){
    zMove(Zpos + extractCoord(inputString, 0));

void absoluteZMove(){
    zMove(extractCoord(inputString, 0));

void relativeXYMove(){
    linearInterpolationMove(Xpos+extractCoord(inputString, 0), Ypos+extractCoord(inputString, 1));

void absoluteXYMove(){
    linearInterpolationMove(extractCoord(inputString, 0), extractCoord(inputString, 1));

void linearInterpolationMove ( signed long int newX, signed long int newY) {
  float distance = 0;
  int stepnum = 0;
  signed long int nextX;
  signed long int nextY ;
  signed long int oldX = Xpos;
  signed long int oldY = Ypos;

  //find the hypotenuse, the total distance to be traveled
  distance = sqrt((newX - oldX)*(newX - oldX) + (newY - oldY)*(newY - oldY) ) + 0.5;

  //round to integer number of steps that distance. Step by two to minimize 0 size steps.
  for (stepnum=0; stepnum <= distance; stepnum++) {
    //calculate the nearest integer points along the way 
    nextX = oldX + stepnum/distance*(newX-oldX);
    nextY = oldY + stepnum/distance*(newY-oldY);

    //move machine to that new coordinate, if 0 delta, don't move
    if ((distance < 4*INCH) && (distance > -4*INCH)) { //trap crazy values
      if ((nextX-Xpos) >=  1)  stepperX->step((nextX - Xpos), FORWARD, SINGLE); 
      if ((nextX-Xpos) <= -1)  stepperX->step((Xpos - nextX), BACKWARD, SINGLE);
      if ((nextY-Ypos) >=  1)  stepperY->step((nextY - Ypos), FORWARD, SINGLE); 
      if ((nextY-Ypos) <= -1)  stepperY->step((Ypos - nextY), BACKWARD, SINGLE);
    //update the machine current position
    Xpos = nextX;
    Ypos = nextY;
    } else {
      Serial.println("ERROR! Distance too big");
  // nudge to the exact final desired coord to correct for rounding down errors
  if ((abs(newX-Xpos)<6) && (abs(newY-Ypos)<6)) { //trap crazy values
      if ((newX-Xpos) >  0)  stepperX->step((newX - Xpos), FORWARD, SINGLE); 
      if ((newX-Xpos) < 0)  stepperX->step((Xpos - newX), BACKWARD, SINGLE);
      if ((newY-Ypos) >  0)  stepperY->step((newY - Ypos), FORWARD, SINGLE); 
      if ((newY-Ypos) < 0)  stepperY->step((Ypos - newY), BACKWARD, SINGLE);
    //update the machine current position
    Xpos = newX;
    Ypos = newY;

void zMove (signed long int newZ) {
  //calculates steps to move, and moves, to new Z position requested
  signed long int distance = 0;
  signed long int oldZ = Zpos;

  distance = newZ - oldZ;
  if ((distance < 8000) && (distance > -8000)) {
    if (distance >=  1)  stepperZ->step(distance, FORWARD, SINGLE); 
    if (distance <= -1)  stepperZ->step(-1*distance, BACKWARD, SINGLE);
  } else {
    //movement requested is large and likely an error
    Serial.println("ERROR - Z axis range");

  Zpos = newZ;  //update machine current position

Fired up the Java GUI and started clicking buttons.  It works!  All the buttons are doing the right things.  Immediately noticed I needed to make some GUI changes though.

-Stop button works fine, but once I've done a release I need to have a function for re powering the motors to recover.   Using the I; command for that;
-Tool speed scale is way too low, need to bump it up -
-Tool control, I didn't put on a direction button for forward/reverse - Done
-I need to look for and obey the busy flags from the arduino - Done, and made a indicator light

Things are working with the Java GUI, the busy light comes on, all the motors move the way I want.  I'm in business.   Here is what it looks like now.  It has gotten a bit busy.

Next step is to figure out what to do on the calibration page.
The way this should work is this:

  • Start with a calibrated, precisely measured square printed on paper.
  • Manually move the XY to a the lower left position
  • Hit a button to say it is there
  • Manually move the XY to the upper right position
  • Enter the size of the square and hit a button to say it is there
  • Lower the tool down to touch the work
  • Hit a button to say it is at 0
  • Lift it and put it down on an object of known thickness
  • Enter the height and hit a button to say it is there
  • Now the encoders and counters can calculate the size of their steps in inches/mm.
  • The units should be changed from counts to inches in the display
Here is the calibration GUI.  Added code to calculate the calibration and reset counters in the Arduinos as needed.

Next step is to go back and modify the GUI I previously made for Manual stepping to report values in the calibrated units that calibration measured or in raw steps as it does now.  Easy added the multiply to the calculated position display.

Alright... now off to figure out how to read and write graphics files.   Basic communication and control all seem to be working well with the mockup machine.

I posted the code for all the parts at
This is a working version for manual movement of the machines, haven't implemented the file reading and transferring part yet.

'm rewriting my Java PC program.  The task remaining is to add the file reading and translation code to my communication and control software.  I need to be able to pick up some common format CNC files and execute them.

I want to leverage as much as I can of existing software, and just write a simple line by line translator to take an existing file format and convert it to the commands i need to send to the Arduino.

I used HPGL last time, but it is a dead language and is no longer supported by many tools and graphics editors.   It will be really hard to convert pictures into HPGL.  I need something new.

Researching 3D and CNC file formats... Again

Lots of random scribbling as I google....
.stl – STL is a file format native to the stereolithography CAD software created by 3D Systems.
.iges – The Initial Graphics Exchange Specification (IGES) (pronounced eye-jess) defines a neutral data format that allows the digital exchange of information among Computer-aided design (CAD) systems.
.obj – OBJ (or .OBJ) is a geometry definition file format first developed by Wavefront Technologies for its Advanced Visualizer animation package. The file format is open and has been adopted by other 3D graphics application vendors. For the most part it is a universally accepted format.
.3ds – 3DS is one of the file formats used by the Autodesk 3ds Max 3D modeling, animation and rendering software.
Gerber - PC board drawing format

Wood carvers like STL files it seems
DXF shows up too - that is autocad

This site has a lot of STL files

Tried to read one of the STL files.  It is not in ASCII and GIMP can't open it.  So that may not be the best option for me, to write an STL file interpreter.  Most people are trying to create these files, not read them.

b2G that will convert monochrome .bmp file to stl file

.bmp I have in the past written many programs that read and write .bmp files.  The format is pretty simple XY pixels and intensity.   I could use bmp as and easy option, but something more vectory seems better.

This guy is way ahead of me.   I'd have to switch to Linux to borrow his work.

G-code is used to run cnc machines at the lowest level.

Some tools for creating gcode

This software converts Eagle PCB into gcode looks like a convertor to gcode

I looked at gcode on wikipedia and it's fairly hairy.  Lots of commands to deal with.

Windows .bmp picture drawing

Starting to think I will write a simple bmp drawer to start with.
I want to get the tool running with something.

bmp files say their size and have rows and columns of pixels.  Easy to read.  I can use gimp to create files of any size, scale them, etc to make them suitable.  I'll start with black and white bmp files.

I need to do some googling to make sure there isn't some tool out there I can borrow.

If I choose to write it myself, the programming is going to go like this:
  1. Read in the bmp file into an array
  2. Find the outer extent on top, left and right
  3. move to the rough center
  4. put the pen down on a black dot.
  5. look for a neighbor that is black and move there
  6. continue until no neighbors are black
  7. lift
  8. find another pixel that is black
  9. move there
  10. repeat until the drawing is done
I have some old skill code that read bmp files, I will pull that out and use it to start.

Exploring other options

Found out vectorizing is called "autotracing".   That gave me another thing to google

There is an "autotracing" program on line.
But the problem is I would then need to be able to read EPS, SVG or PDF files. Maybe the demo will be enough, but it looks like it costs money.

Found another that looks free and online.  Takes a graphics file and creates vector files.

  • Tried it, and EPS output looks like a usable, ASCII vector file.  
  • DXF file is ASCII readable as well.  I'm sure this could get hairy, Autocad is pretty complex.  But this file looks manageable.
  • SVG looks good too.  Seems to be a human readable graphics file that I could interpret
SVG could get out of hand, with lots of shapes and fonts to support.  However the basic file looks good
<path style="fill:#545a64; stroke:none;" d="M30 3L30 10C31.0595 7.46568 31.0595 5.53432 30 3z"/>

This means Move to 30:3, Line to 30:10, Bezier Curve to a list of coords, z=end.

Here is the spec on the SVG file format

Played with and I'm pretty happy with the files it produces.  It is really for 2D drawing but it makes simple vector files that are easy to turn into tool control files.  Gimp can read and display them.   Looks like a good quickie option to get off the ground.

This is a little graphic with some letters and a square:

<?xml version="1.0" standalone="yes"?>
<svg width="161" height="150">
<path style="fill:#ffffff; stroke:none;" d="M0 0L0 150L161 150L161 0L0 0z"/>
<path style="fill:#010101; stroke:none;" d="M1 1L1 109L117 109L117 1L1 1z"/>
<path style="fill:#ffffff; stroke:none;" d="M3 3L3 107L115 107L115 85C87.676 91.5053 87.5147 47.4563 115 54L115 3L3 3z"/>
<path style="fill:#010101; stroke:none;" d="M41 43L41 85L49 85L49 69C54.56 69 61.8856 70.2342 66.9568 67.5432C74.8268 63.3671 75.3453 49.6136 67.956 44.7423C61.5192 40.4989 48.4628 43 41 43M80 43L80 50L88 50L88 43L80 43z"/>
<path style="fill:#ffffff; stroke:none;" d="M49 50L49 62C66.8383 61.9882 66.8383 50.0118 49 50z"/>
<path style="fill:#010101; stroke:none;" d="M80 54L80 85L88 85L88 54L80 54z"/>
<path style="fill:#ffffff; stroke:none;" d="M107.043 59.9707C98.9687 61.792 101.299 81.8813 109.794 79.1489C117.684 76.611 116.364 57.8683 107.043 59.9707z"/>

I will need to implement the Curve function in the Arduino, and convert the control letters to match just to make life easier.

Also found this code for running in a browser:

It has a demo which is a fully functional svg editor that produces similar graphics files that can be edited.  Looks like a winner.

At this point it seems appropriate to start other posts with this topic, this post is getting too long and covers too much.