You are on page 1of 41

instructables

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS

by tuenhidiy

I had one Dell printer that stops working and I Let's get started.
disassembled it into small components. This printer head
frame has 2 DC motors and 2 optical encoders which are
still in good condition & can be reused to build XY axis
for the CNC plotter.
//www.youtube.com/embed/Qe13E-eHeJg

Today, I'd like to share how to build 3 axis CNC plotter


from this printer, as well as, how we can control a DC
motor plus its optical encoder by P.I.D through 2 signals:
Update on October 10, 2020 at STEP 15: Better
STEP & DIRECTION. In this project, DC motors can be
pictures with hatch ll extension.
simulated as same as stepper motors and we can control
them via GRBL rmware for CNC application.

I was inspired by cswiger on his GitHub:


https://github.com/cswiger/dcservodrive. He had a //www.youtube.com/embed/KE8XSEPzA7M
good idea to turn step/direction signals into DC servo
motor position control.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 1


Step 1: THINGS WE NEED

Main components:

1pcs x Arduino Uno R3.


1pcs x Arduino Mega 2560.
1pcs x Arduino CNC Shield V3 GRBL .
1pcs x Arduino L293D Motor Shield.
1pcs x Stepper Motor Driver A4988.
1pcs x Old Printer Head Frame with 2 DC motors plus their optical encoders. I used old printer head frame
from DELL printer, one optical encoder is linear type and the other is rotary type.
1pcs x Old CD/DVD Burner Writer Rom Player Drive.
4pcs x R10K (Or 2pcs x R5K).
1pcs x R150.
2pcs x XH2.54mm – 4P 20cm Wire Cable Double Connector .

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 2


2pcs x XH2.54mm – 2P 20cm Wire Cable Double Connector.
1pcs x Single/ Double Sided Printed Prototyping Board.
2pcs x 2.54mm Pitch 40 Long Pin Single Stackable Shield Female Header.
2pcs x Male & Female 40pin 2.54mm Header.
1pcs x Aluminum Flexible Shaft Coupling, Inner Hole Size: 10mm x 10mm .
1pcs x Power Supply 12/24 VDC
1pcs x Power Supply 5 VDC.
2 meter x 8P Rainbow Ribbon Cable.
Some small cable ties.

Softwares:

GRBL rmware.
Inkscape.
Universal Gcode Sender.

Step 2: CIRCUIT DIAGRAM

In my circuit diagram, the X and Y stepper motor driver The schematic with PDF high resolution is HERE.
are not plugged on the Arduino CNC shield. The STEP
and DIR signals of both X and Y axis on CNC Shield are
used to control 2 x DC motors.

Download

https://www.instructables.com/ORIG/FTE/TE6B/KFB41S05/FTETE6BKFB41S05.pdf

Step 3: DC MOTOR AND OPTICAL ENCODER IN THE PRINTER

Details of printer components are described below:

1. DC MOTORS: There're 2 DC motors for X & Y axis as follows:

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 3


X axis DC motor: RS-455PA-17150

Y axis DC motor: RS-385SH-14180

Through my searches on internet, perhaps these motors belong to the MABUCHI MOTOR manufacturer. But I could not
nd out the speci cations of these motors on MABUCHI MOTOR website according to the order number written on the
3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 4
motors body. I only saw two DC motors equivalent to my printer motors, as follows:

2. OPTICAL ENCODERS: There’re 2 kinds of optical encoders in DELL printer as follows:

X axis linear optical encoder

- Optical sensor and control board: It is not clear, part number maybe J15 (0947).

- Encoder strip: H-06/1PM326727.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 5


Y axis rotary optical encoder:

- Rotary disc: 1782CPR/300LPI (B-12).

- Optical sensor: H30 (0942).

- PCB board nameplate: 94V-O/ KY033H/ BJ4500F01CP4-1.

Notes:

In the case of rotary encoders, resolution is speci ed as the cycles per revolution ( CPR), some
manufacturers use terms like “counts per revolution” (also abbreviated CPR) or pulses per revolution
(PPR).
Lines per inch (LPI) is a measurement of printing resolution. High LPI indicates greater detail and
sharpness.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 6


Step 4: QUADRATURE OPTICAL ENCODER

The optical encoder is widely used due to its low cost and ability to provide signals that can be easily interpreted to
provide motion related information such as speed or position. The two output channels from an encoder, with one being
o set by 90 electrical degrees, or one quarter of a cycle that usually called quadrature encoder.

With a single output encoder, it has no way of detecting in which direction the motion is happening. But for quadrature
encoder, it produce two channels, named channel A and channel B. When it moves/ rotates in a forward/ clockwise
direction, channel A leads channel B, and in case it moves/ rotates in backward/ counterclockwise direction channel B
leads channel A. We can increase encoder’s resolution by counting the rising and falling edges of two channels, detail is
explained below.

1. X1 ENCODING

X1 encoding: the rising or the falling edge of channel A is counted. When channel B is leading, the movement is
considered as counterclockwise or backward, and the count number is decreased.

2. X2 ENCODING
X2 encoding: both the rising and falling edges of channel A are counted. When X2 encoding is used, it increases encoder
resolution by two times.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 7


3. X4 ENCODING

X4 encoding: both the rising and falling edges of channels A and B are counted. When X4 encoding is used, it increases
encoder resolution by four times.

Picture below summarizes the quadrature function of optical encoder:


3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 8
For example, we consider a 1782CPR rotary optical encoder - Y axes of Dell printer as described in previous step:

X1 – if we count the rising edge of each Channel A pulse, we’ll get 1 pulse per cycle or 1782 pulses per
revolution (1782PPR).
X2 – if we count each rising edge and each falling edge of Channel A, we’ll get 2 pulses per cycle, or 1782 x
2 = 3564 pulses per revolution (3564PPR).
X4 – if we count each rising edge and falling edge of both Channel A and Channel B, we’ll get 4 pulses per
cycle, or 1782 x 4 = 7128 pulses per revolution (7128PPR).

Step 5: DETERMINING OPTICAL ENCODERS PINOUTS IN THE PRINTER

I have found a website that is very helpful in identifying the pins of optical encoder from the printer:

https://reprap.org/wiki/Optical_encoders_01

I couldn’t nd part number, manufacturer or data sheets on these optical encoders. In this case, I used a multimeter to
measure the resistance among optical sensor pins (6 pins in my case) to identify its pinouts: VCC, GND, channel A,
channel B. As mentioned in previous step, there are 2 kinds of optical encoders in DELL printer: X axis linear optical
encoder and Y axis rotary optical encoder.

1. LINEAR OPTICAL ENCODER FROM PRINTER

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 9


There are many connectors / headers on the PCB board which confused me and I could not nd the encoder pinout
diagram in these headers to get its feedback correctly. Finally, I decided to do my own control circuit for this optical
sensor as following steps:

Firstly, I measured the resistance among the optical sensor pins (6 pins) and recorded them on a table.
Secondly, based on my knowledge about the IR LED and Photo-transistor, I gured out its connection
schematic and double checked whether I had any mistakes. With the resistor values measured, I guessed
this linear optical sensor operating at 5V voltage level.
Thirdly, I removed the optical sensor from the PCB control board. Note that this optical sensor is very easy
to be broken if we hold the soldering tip on it too long time.
Fourthly, for safety, I supplied 3.3V voltage level for powering this linear optical encoder. In my own
schematic, I used 2 x 10K resistors connected in parallel because I did not have 5K resistors in my hand.

2. ROTARY OPTICAL ENCODER FROM PRINTER

There is 4 pins - header on the PCB control board and I easily found the encoder pinout diagram on this header. I kept
this original PCB control circuit, just removed 4 pins - header and replaced by 4 cables.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 10


Header pinout detail is as follows:

PIN 1 – GND.
PIN 2 – CHANNEL A.
PIN 3 – VCC (3.3V).
PIN 4 – CHANNEL B.

Step 6: THE P.I.D CONTROLLER

The PID controller is a closed-loop controller which is widely used in electrical, automated, and electronic systems. The
goal of PID controller is to adjust the control value at the OUPUT by continuously evaluating the ERROR (e(t) = (SP - PV))
between a SETPOINT (SP) and the PROCESS VARIABLE (PV) being controlled and applies a correction based on
proportional, integral, and derivative terms, to achieve the stability and rapid response in the system. PID algorithm for
DC motor plus optical encoder is described in the diagram below:

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 11


You can read one of the best blog articles about PID algorithm at website:

http://brettbeauregard.com/blog/2011/04/improving-...

In my project, Arduino Mega 2560 is used just like a DC servo controller. It performs P.I.D control for the X and Y axis DC
motors.

Usually, a motor will be driven by speed or position but with this PID controller, the setpoint are STEP plus DIRECTION
signals from Arduino Uno R3 which has GRBL rmware pre-installed.

The PID control signals are as follows:

SETPOINT - SP: They are X.STEP/ X.DIR plus Y.STEP/ Y.DIR signals that are sent from Arduino Uno R3
with a CNC Shield to Arduino Mega 2560. Note that Arduinno Uno has GRBL rmware pre-installed.
PROCESS VARIABLE - PV: The measured feedback value from quadrature optical encoders to Arduino
Mega 2560.
OUTPUT: The PWM signals from Arduino L293D Motor Shield (controlled by Arduino Mega 2560) to
printer DC motors.

Step 7: ARDUINO L293D MOTOR SHIELD & CNC SHIELD V3

1. Arduino L293D Motor Shield Overview:

This motor driver expansion board is based on the L293D chip which is designed to drive up to 4 bidirectional DC motors
with individual 8-bit speed selection. It can also drive 2 unipolar or bipolar stepper motors. It contains 4 H-bridges which
provide up to 0.6 A per bridge (1.2A peak) at voltages from 4.5 V to 36 V. This shield has pull down resistors to keep the
motors disabled during power up. It also features a 2-pin terminal block to ensure separate logic and motor external
power supplies. This motor driver shield is capable of driving:

Four DC motors and two servos


Two DC motors, stepper motor, and two-way servo
Two stepper motors and servos

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 12


The following pins are in use on the L293D Motor Shield:

Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control).


Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control).
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control).
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control).
Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch.
Digital pin 9: Servo #1 control.
Digital pin 10: Servo #2 control.

2. CNC Shield V3

The Arduino CNC Shield makes it easy to get your CNC projects up. It uses opensource rmware on Arduino to control 4
stepper motors using 4 x Stepper Motor Driver.
Before using this CNC shield with Arduino Uno R3, a control rmware “GRBL” need to be downloaded into Arduino
board.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 13


Step 8: ASSEMBLY & CONNECTION

I. CONTROL BOARDS ASSEMBLY:

1. Adapter Shield:

To do Adapter Shield, I cut one PCB prototype board size 60x90mm and soldered the wires connections following the
circuit on STEP 2. Adapter Shield is used for connecting the Arduino Mega 2560 to the L293D Motor Shield and some
male headers such as: 4 pins - headers for encoders (Vcc, GND, Channel A, Channel B), X.STEP, Y.STEP, X.DIR, Y.DIR are
also soldered on it.

Top view of Adapter Shield:

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 14


Bottom view of Adapter Shield:

2. Control board assembly:

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 15


Plug CNC Shield on Arduino Uno:

Two copper pillars were installed at the bottom of Arduino Uno board.

Connect Arduino Uno to L293D Motor Shield by 2 copper pillars above.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 16


Plug L293D Motor Shield on Adaper Shield and nally plug Adapter Shield to Aduino Mega 2560. I've got
a neatly organized controller.

II. PRINTER HEAD FRAME ASSEMBLY

The underside of the printer head frame is not at, it has convex and concave supports. I had to use an
3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 17
insulation sheet and cut some long grooves corresponding to the convex supports so that the printer
head frame could be xed horizontally and vertically.

For Z axis, I used a DVD player which is very compact, called " HP Super Multi DVD Rewriter" as shown
below:

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 18


I mounted this DVD stepper motor frame on the printhead.

I soldered a cable to Y axis - rotary optical encoder (See the detail how to dertimine its pinouts at STEP 5).

X axis - linear optical encoder cable was soldered and xed along the printer frame. (See the detail how to
3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 19
determine its pinouts at STEP 5).

My DIY linear encoder control board is hidden inside the printhead, I forgot to take pictures of it while I
soldered the circuit. I cannot disassemble this printer frame again because it can cause the linear encoder
strip damaged. On my previous disassembled, the encoder strip lost some black lines on it.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 20


III. CONNECTIONS:

Arduino Mega 2560 and Arduino Uno connect together by 4 signals: X.STEP, Y.STEP, X.DIR, Y.DIR to
control 2 DC motors, in which the Arduino Mega 2560 acts as a DC servo controller and Arduino Uno plus
CNC Shield sends the control commands from its GRBL rmware.
DC Motors and Optical Encoders: Two DC motors are connected to L293D Motor Shield at M1 & M2
terminals. And their optical encoders are connected to 4pins - headers on Adapter Shield.

I plugged stepper motor driver A4988 on CNC shield for Z axis and connected cable from DVD stepper
motor to A4988 driver.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 21


Mounting exible coupling to the DVD frame and put the pen into the exible coupling. To clamp the
pen, we can tighten small screws on the exible coupling. It looked like this, after my assemblies and
connections were completed.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 22


Step 9: ARDUINO LIBRARIES

Libraries are used in this project:

PID by Brett Beauregard (PID_v1):

https://playground.arduino.cc/Code/PIDLibrary/

https://github.com/br3ttb/Arduino-PID-Library/

Adafruit Motor Shield Library (AFMotor):

https://github.com/adafruit/Adafruit-Motor-Shield-...

Quadrature Encoder Library:

https://github.com/PaulSto regen/Encoder

https://www.pjrc.com/teensy/td_libs_Encoder.html

FlexiTimer2 Library:

https://playground.arduino.cc/Main/FlexiTimer2/

Step 10: ARDUINO CODE

The 3 axis CNC plotter code is available at my GitHub. Thanks to cswiger for inspiring me.

In my code, Arduino Mega 2560 pins usage are shown in table below. Take note that digital pin D3 (Interrupt 0) is used
for L293D Motor Shield.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 23


Encoders have 2 signals, which must be connected to 2 pins. There are three options when we use "Encoder" library.

Best Performance: Both signals connect to interrupt pins.


Good Performance: First signal connects to an interrupt pin, second to a non-interrupt pin.
Low Performance: Both signals connect to non-interrupt pins.

I used the second option and it was declared in program as follows:

// Set up pins for the Quadrature Encoder


#define EncoderX_ChannelA 18 // Interrupt 5
#define EncoderX_ChannelB 22
#define EncoderY_ChannelA 20 // Interrupt 3
#define EncoderY_ChannelB 24
-------------------------------------------------------------------------------------------------
// Encoders
Encoder XEncoder(EncoderX_ChannelA, EncoderX_ChannelB);
Encoder YEncoder(EncoderY_ChannelA, EncoderY_ChannelB);

With "AFMotor" library, when used with the L293D Motor Shield, the AF_DCMotor class provides speed and direction
control for up to 4 DC motors. Here below is the constructor for X and Y axis DC motors

AF_DCMotor motorX(1, MOTOR12_8KHZ);


AF_DCMotor motorY(2, MOTOR12_8KHZ);

To simulate a DC motor plus its encoder as same as a stepper motor, we used interrupts to detect rising edge at STEP
pulse and combine with DIR signal to determine motor direction and position.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 24


#define STEP_XPIN 19 // Interrupt 4
#define STEP_YPIN 21 // Interrupt 2
#define DIR_XPIN 23
#define DIR_YPIN 25
--------------------------------------------------------------------------------------------------
// Simulate DC motor as same as stepper motor
attachInterrupt(4, doXstep, RISING); // PIN 19 (Interrupt 4) - Interrupt X step at rising edge pulses
attachInterrupt(2, doYstep, RISING); // PIN 21 (Interrupt 2) - Interrupt Y step at rising edge pulses
--------------------------------------------------------------------------------------------------
void doXstep()
{
if ( digitalRead(DIR_XPIN) == HIGH ) SETPOINT_X--;
else SETPOINT_X++;
}

void doYstep()
{
if ( digitalRead(DIR_YPIN) == HIGH ) SETPOINT_Y--;
else SETPOINT_Y++;
}

PID controllers for X/Y axis are created and linked to the speci ed Input, Output, and Setpoint:

// PID
PID myPID_X(&INPUT_X, &OUTPUT_X, &SETPOINT_X, KP_X, KI_X, KD_X, DIRECT);
PID myPID_Y(&INPUT_Y, &OUTPUT_Y, &SETPOINT_Y, KP_Y, KI_Y, KD_Y, DIRECT);

The parameters of the PID controllers are described below:

SETPOINT_X/ SETPOINT_Y: The STEP and DIR signals are sent from GRBL CNC Shield of Arduino Uno to
Arduino Mega 2560. Arduino Mega 2560 receive these STEP and DIR signals of each axis X/Y, then
combine them to create the SETPOINT values for each PID controller.
INPUT_X/ INPUT_Y: They are feedback signals which are read from quadrature encoders of X/ Y DC
motors.
OUTPUT_X/ OUTPUT_Y: They are PWM output signals which control the X/Y motors.
K_P/ K_I/ K_D: They're tuning parameters. These a ect how the PID will change the output.

Step 11: GRBL CALIBRATION

GRBL parameters for my printer are as follows:

$0 10.000 Step pulse time


$1 25.000 Step idle delay
$2 0.000 Step pulse invert
$3 3.000 Step direction invert
$4 0.000 Invert step enable pin
$5 0.000 Invert limit pins
$6 0.000 Invert probe pin
$10 1.000 Status report options
$11 0.010 Junction deviation
$12 0.002 Arc tolerance

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 25


0.000
$13 Report in inches

0.000
$20 Soft limits enable

0.000
$21 Hard limits enable

0.000
$22 Homing cycle enable

0.000
$23 Homing direction invert

$24 25.000 Homing locate feed rate


$25 500.000 Homing search seek rate
$26 250.000 Homing switch de-bounce delay
$27 1.000 Homing switch pull-o distance
$30 1000.000 Maximum spindle speed
$31 0.000 Minimum spindle speed
$32 0.000 Laser-mode enable
$100 24.500 X-axis travel resolution
$101 192.000 Y-axis travel resolution
$102 53.333 Z-axis travel resolution
$110 20000.000 X-axis maximum rate
$111 20000.000 Y-axis maximum rate
$112 2000.000 Z-axis maximum rate
$120 50.000 X-axis acceleration
$121 20.000 Y-axis acceleration
$122 50.000 Z-axis acceleration
$130 210.000 X-axis maximum travel
$131 297.000 Y-axis maximum travel
$132 40.000 Z-axis maximum travel

The important parameters which I have done the calibrations are highlighted in table above.

1. STEP/MM setting:

Z AXIS - $102:

The step/mm setting for Z axis stepper motor is shown in table below by formula:

Steps/mm = (Steps per Revolution)*(Micro-steps) / (mm per Revolution)

The working length of the screw: 40.00 mm


Step angel: 18 °
The number of steps required for DVD
20 step/rev
stepper to make 1 complete revolution:
A4988 micro-steps setting: 8 -
DVD stepper screw pitch
3.0 mm/rev
(mm/revolution):
3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 26
STEP/MM: 53.333 step/mm

Y AXIS - $101

If we're using belts and pulleys, the XY steps/mm can be calculated based on the motor, pulley, and belt speci cations
but in my case, the printer need to be disassembled. I did it once and don't want to do it again because it can cause my
printer damaged.

Y axis have a 1782CPR rotary optical encoder. When X4 encoding is used, it increases encoder resolution by four times:
1782 x 4 = 7128 pulses per revolution (7128PPR). For setting step/mm of Y axis, I did following steps:

- Firstly, I connected Universal Gcode Sender program to Arduino Uno (with GRBL), set $101 = 7128 in tab "Firmware
Setting". The purpose is I want to check when I command Y axis to move 1mm, the Arduino Mega 2560 will generate
PMW to move the DC motor and count 7128 pulses feedback from optical encoder.

- Secondly, I opened Arduino IDE serial port for Arduino Mega 2560 and turn on debug mode for Y axis in program to
monitor all parameters.

- Thirdly, I marked the home position of Y axis.

- Fourthly, I used the "Machine Control" tab in Universal Gcode Sender, instruct the printer Y axis to move 1
millimeters. Checked:

Is it rotate 1 revolution?
Is count number from encoder in serial monitor around the value of 7128?

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 27


- Fifthly, I made a measurement of the true movement from home position: 37.59mm.

- Sixthly, I applied this formula to calculated the new step/mm = (old step/mm) x (1mm/ real measured value)=192.0
step/mm then update to $101 GRBL.

- Eighthly, I reset the Arduino Uno (with GRBL), marked the home location and instructed some more commands to move
Y axis with di erence position. I made a measurement of the true movement and repeated the above step. Finally,
step/mm for X was determined to be: 192.0.

X AXIS- $100

- For X axis linear optical encoder, rstly I counted the number of black lines per 1mm on the encoder strip. In my case, it
is between 6 and 7 black lines per 1mm. When X4 encoding is used, it increases encoder resolution by four times so
step/mm could be between 24 to 28 .

- Same as Y axis, I marked the X axis home location and instruct some commands to move X axis with di erence position
then made a measurement of the true movement. Finally, step/mm for X axis is: 24.5

2. MAXIMUM RATE AND ACCELERATION

To speed up the printer, both X and Y axis maximum rate ( $110 and $111) are set to 20000.
The Y-axis acceleration ( $121) is set to 20 if we set it at a big value then when the motor switches from
forward to backward and vice versa its belt will slip and there is a loud noise.

3. AXIS MAXIMUM TRAVEL

X-axis maximum travel ($130): A4 size width 210 mm.


3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 28
Y-axis maximum travel ($131): A4 size length 297 mm.
Z-axis maximum travel ($132): DVD working length 40 mm.

Step 12: P.I.D TUNNING

GRBL and P.I.D parameters ne-tuning are the most important and the most di cult works of this project. They a ect to
the printer operation, for example when I draw a line in the X and Y axes with 20mm length, the printer did it precisely,
but when I draw a 20mm diameter circle, it got distorted.

That means, in order for the printer to work correctly, the X and Y axes both have to work precisely when separated each
other, as well as, ensure their synchronization when combined together. These calibrations are not easy for both PID and
GRBL parameters. It is time consuming and I wished I had a oscilloscope in my hand to gure out all things quickly. I
nally found out the PID optimal parameters which are in accordance to the GRBL setting values in the previous step.
With these values, my plotter has worked very well.

// The PID parameters


double KP_X = 20.0; // P for X motor
double KI_X = 0.03; // I for X motor
double KD_X = 0.01; // D for X motor

double KP_Y = 9.0; // P for Y motor


double KI_Y = 0.02; // I for Y motor
double KD_Y = 0.01; // D for Y motor

In industrial eld, the PID controller is usually integrated with a "deadband" function. The goal is saving maintaince cost,
it prevent our equipment, for example big valves, oscillating all the time around the setpoint by small worthless
movements and cause equipment wear and tear or other overheat issues.

The deadband represents the range at which the PID controller will allow the process variable (PV) to deviate from
setpoint (SP) without applying any correction. For example, imagine that we are trying to maintain the printer's Y-axis at
100mm from home position. When the Y axis moves to the set point, if its position was to uctuate between 99.9mm and
100.1mm, the PID controller would exert no correction. The dead band or acceptable error would be 0.1mm .

#define STEPSPERMM_X 24.5 // STEP/mm ($100) is used in the GRBL for DC motor X axis.
#define DEADBW_X 4.5 // Deadband width in pulses = 4.5 --> Acceptable error for positioning in mm: 0.18mm.

#define STEPSPERMM_Y 192.0 // STEP/mm ($101) is used in the GRBL for DC motor Y axis.
#define DEADBW_Y 19.2 // Deadband width in pulses = 19.2 --> Acceptable error for positioning in mm: 0.1mm.

When we apply the deadband in PID controller, it will increase the service life of the motor because it will stop when its
process values are in deadband window. Otherwise, the printer DC motor is always oscillating around the setpoint by
PID regulators and it can be getting hot or overheat.

ERROR_X = (INPUT_X - SETPOINT_X);


if (abs(ERROR_X) < DEADBW_X) // If the motor is in position within the deadband width (acceptable error)
{
motorX.setSpeed(0); // Turn off the motor
}
else
{
motorX.setSpeed(abs(int(OUTPUT_X))); // Motor is regulated by PID controller ouput
}

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 29


We can turn on the debugging mode in Arduino program and check the PID parameters on the serial monitor:

As we can see, the error between the X axis setpoint (735) and actual counting feedback from encoder (731) is 4, in this
case X-axis motor stops because it is within the deadband range (4.5).

Step 13: TESTING

1. PREPARATIONS AND FINAL CALIBRATIONS:

For testing, I used a thin ruler to creat plotting surface of Y axis. Take note that the distance from the
center of printer roller to the pen tip is as short as possible, it is about 20 mm in my case.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 30


Finally, I added the A4 paper to printer. It's ready to do testing.

We should do the PID and GRBL nal calibrations and ne-tunings in this step when the printer is
working with load. It can work smoothly when we do a no load test but it can shake crazily when paper is
loaded. Making a printer acts like a CNC plotter is really not easy!!!

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 31


2. INKSCAPE

- From the Inkscape menu go to File ‣ Properties and in the Page Tab set the Display Units (millimeters), the
Orientation to Portrait and Page Size A4: 210.0 x 297.0mm.

- Import a suitable image by using the menu File ‣ Import. In the menu, go to Path ‣ Trace Bitmap and convert the
Object to Path.

- Go to Extensions ‣ Gcodetools ‣ Tools Libary

Select Tools Type: Cylindrical and click Apply.


I tried feed speed: 5000mm/s. My CNC plotter worked fast and stable at this speed rate.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 32


- Go to Extensions ‣ Gcodetools ‣ Orientation Points

Orientation type: 2-points mode.


Z Surface: 0.0mm. This is the top of your paper surface.
Z Depth: -1.0mm. This is working position of Z axis when CNC plotter is drawing object. This negative
number ensure that the pen tip can touch the paper.

- Go to Extensions ‣ Gcodetools ‣ Path to Gcode

Z safe height: 3mm. It is height above the plotting surface when moving between drawing points.
Click the Path to Gcode Tab before clicking Apply. This creates the G-code le.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 33


3. UNIVERSAL GCODE PLATFORM

Open Universal Gcode Platform, select Port and set Baud to 115200, click on Connect tab.
Select the appropriate position by moving X axes left - right, Y axes forward - backward and set the
original coordinates by button Reset Zero.
Click Open ‣ Browse to the G-code le that generated by INKSCAPE.
Click Send and CNC plotter will perform drawing picture following the G-code.
Monitor the plotter in action on the Visualizer tab.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 34


And here below is my result:

Here's a slightly more complicated drawing, a ower. CNC plotter has worked very well.

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 35


We can see CNC plotter operate as the video below. I set it in low speed rate: 500mm/s.

//www.youtube.com/embed/9W9TkUC3g4M

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 36


With exible coupling diameter 10mm, we can easily change to many kind of pens/ pencils by two small screws.

You can watch the video below after I changed a di erent color pen and feed speed has been set to 15000 mm/s.

//www.youtube.com/embed/EOd5K3Is4xI

Step 14: FINISH

You can see some pictures of this project. My 3 axis CNC plotter works pretty good but it need to be more ne-tuned and
improvement.

Thank you very much for reading my work and hope you enjoyed my article this time!

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 37


3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 38
Step 15: BETTER PICTURES WITH HATCH FILL EXTENSION

AxiDraw Software 2.6.3 by Evil Mad Scientist


Laboratories - This extension ll the classic hatch and
crosshatch on selected closed objects in an Inkscape 1.0.
//www.youtube.com/embed/KE8XSEPzA7M
It is really useful and now my plotter can draw some
better quality images.

You can check at video below:

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 39


The most detailed project i have found for controlling brushed motors with optical encoders by
using PID controller!
Thank you for your interesting in my project!

I am joining other comments, your documentation about the material is just amazing. I have
plenty of them, and the way you are making it understable is very valuable.

Just all thumbs up!


Hi harnon. Thanks! I agree with you. As for using Arduino to drive a dc servo motor, there're a lot
of references on the internet. I just collected, studied, applied it in my way, wrote this article, and
hope it will help someone.:-)
Very nicely documented! I have several disassembled printers like yours, and this gives me a lot
of ideas on using them.
Hi bruce.desertrat! Thanks. You should take times to build a cnc plotter like me or another CNC
application from your old printers. It has many interesting things.
This is great idea and very well worked out too..! Love it. Another example how to make good use
of discarded products.
Hi DirkFG! Thank you.

This is quite an exceptional Instructable. Not only useful and clever but explained very well. The
optical encoding route gives closed loop movement and is adaptable to other tools. Truly
deserving of an accolade.
3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 40
Hi I8mAll! I'm very glad you're interested in my project. Your comment will be a valuable source of
encouragement for me. :-)

3 AXIS CNC PLOTTER FROM DC MOTORS AND OPTICAL ENCODERS: Page 41

You might also like