Sunday, October 20, 2013

A Raspberry Pi controlled mini CNC Laser engraver [last update June 6, 2017]

I recently made a mini CNC laser engraver using two DVD drives salvaged from old computers and <$10 extra parts bought on eBay. The controller of the CNC machine is a Raspberry Pi, a $35 credit card size computer. The engraver turns out to be pretty successful. So I am sharing it with everyone.

There have been a lot of examples people using Arduino to control CNCs. You can probably find tons of C programs available that run directly on Arduino to process G code. A famous one is grbl. Like this on by Groover in instructable A Chinese translation can be found here. Also there are available CNC controller like MATH3 on the market that can be controlled by parallel or serial ports.

I am taking a different approach by using a RPi (
A Raspberry Pi mini computer. Raspberry Pi is a trademark of Raspberry Pi Foundation
The reason why I choose Raspberry Pi is: it is a much more powerful device than Arduino; it supports complete OS; the GPIO pins can be controlled by python, a more intuitive and simpler language than C (the disadvantage of python would be the slow speed); I don't have to buy a separate controller for this project--I can use a single Raspberry Pi to do a lot of different things without reloading firmware. Most importantly, I have a Raspberry Pi but don't have an Arduino right now!

Also, there are projects people running LinuxCNC on Raspberry Pi and use an external PIC 32 board to control CNC. It's a great idea but I want to minimize the cost. Instead, I wrote my own python interpreter to execute G code directly. For 2D CNC laser engraver, it is actually very easy to control and doesn' require too much program techniques.

First, some pictures.
Laser engraver is working

A coin size university icon on a plastic board

I messed up the direction for this one...

Some words..

The machine is not packaged yet. You can find: machine (top middle), controlling circuit (top left) and RPi (lower right)

The controller and wires in a perfect chaos
Presentation in Caltech/JPL DIY day, April 4, 2015. Middle: laser engraver. Right: chasis that encloses H bridges, LM317 laser drivers, LM340T5 voltage regulators and a tiny dc fan. Right upper: Raspberry Pi.

Other version
D. Miller made his own version of laser engraver and posted a fabulous youtube video here

Please visit his website pxlweavr

He made a couple of great improvements to my python codes, so that it is able to use G Code generated by the inkscape GCodeTools extension, and also allows remote engraving via another small python script he wrote. The modified python package can by obtained from github
Must read!!!
As everyone does, I feel crazy about laser. But it is extremely important to keep in mind that the laser used in this project could burn human eye retina in millisec before eyeball is capable to react. Even a random reflection beam during engraving could be >50 mW (for comparison, a regular laser pointer is 1 mW), and make permanent damage to your eyes, kids' eyes, or pets' eyes. ALWAYS WARE A LASER SAFE GOGGLES when you are close to the working engraver. A suitable one for 650 nm laser should be green color. MAKE SURE THE ENGRAVER IS NOT ACCESSIBLE TO KIDS OR PETS. I would suggest everyone planning to build this should enclose the whole thing inside a large box or cover (however, mounting a little computer fan on the box would be nice for venting). Some people suggest that there should be a master switch on the box, so that unless the box is closed, the laser won't switch on.

AGAIN, think TWICE before you make it.

About the capability of the engraver
Due to the size limit of DVD drives, the machine can only engrave within an area of 36 mm by 36 mm.. So it can do little pieces of wood, plastic board or part of  iPhone cases, but not any larger.

The laser used here is 200 mW 650 mm red laser diode. It cuts letter paper fine. But It is not powerful enough to actually cut through anything thicker and tougher. In fact, the working surface is preferred to be black color so that it can absorb as much laser power as possible. To engrave on transparent plastic board, as shown above in the coin size university icon picture, I have to use a black marker to paint the surface and clean the ink after engraving. However, I believe for a thin black foam sheet (<3mm thick), and given enough engraving time, the laser should be able to cut it through, as Groover showed in instructable.

Now here is the instruction.

Things required:

1. A raspberry pi (running Raspbian or what ever supports GPIO)

2. two DVD writable drives.
To be able to engrave, you need 200mW laser diode from DVD writer. A DVD R or CD R will do nothing. A CD writer might be OK in term of power (~100mW), but the laser diode of a CD writer is infrared, which can be super dangerous (you can't see it!).

3. a TO-18 5.6mm laser housing
(like this one
Make sure you get a 5.6mm one. There is another type 9mm.

4. Two dual channel H-bridges.

A H bridge is a circuit containing four (effective) switches that can apply a voltage across a load (DC motor or one coil of a stepper motor) in either direction.

Stepper motors from DVDs are 4 wire 2 phase bipolar stepper motors. They require truly reversible voltage on each pairs of the wire. You need two H bridges for each stepper motors. So total of four H bridges for two strepper motors. Some famous stepper motor controllers like ULN2003 are for the 5 wire stepper motors, so they cannot be used for controlling the DVD stepper motors.

You can make your own H bridges by using 4 NPN and 4 PNP transistors and probably TTL converters (RPi's GPIO pin are 3.3V so logic TTL chips might be required). Or you can simply buy them. There are a lot of integrated H bridge circuits available in the market, such as L298. The ones I use are L9110s Dual H Bridge purchased on Ebay (like this one: They are low cost (~$2 each), compact (.8"x1") and are powerful enough (~800mA). However, if you buy from a Chinese seller, shipping can cost 3 weeks.

L9110s is also sometimes known as HG7881.

Regarding the H bridge, you need to make sure that the continuous current limit of the circuit is greater than 500mA. Usually the stepper motor in a DVD drive is rated at 5V and each coil has a resistance of 10ohm. So the current through each coil would be 500mA! A very large current!

5. a LM317 regulator, a power NPN bipolar transistor (like E3055, should be able to handle continuous 200mA at least), some resistors, capacitors and a bundle of jumpers.
The LM317 is for the laser driver. The power NPN is for making a switch for the laser. My lab has tons of these components so I don't have buy them. If you don't want to solder a driver by your own, you can surely buy a laser driver for <$5. The laser driver need to be able to output at least 200mA at 2V and have the function of enable/disable.

If fact, it will be a better idea replacing the NPN bipolar with a MOSFET transistor. There are a lot of them available on the market and are very inexpensive. Just make sure that the continuous current supported by the MOSFET is above 200mA.

6. a solder gun, screw driver and some basic hard wares.

STEP 1: Disassemble DVD drives (writers)

There are a lot of picture/video tutorials online for this step so I will keep it short.

Two DVD found in E-waste
They are rewritable

All you need from the DVD are
1. stepper motor with the slider (lower right part in 
the picture below)
tearing down a DVD

2. Laser diodes (see picture below). Be very careful
 that the laser diodes on the DVD are very fragile.
 Make sure you don't break them. 

Two 5.6mm laser diodes (infrared and 650nm red) 
compared with a USB connector.

Stepper motor (right) and linear slider. 
I soldered four wires on the stepper motor.

There are other good things you can salvage from the DVD drive and keep for future projects, such as a 9 V dc motor near the gate, a brushless motor that spins the DVDs, some shock reducers and some miniature lens and optic parts. You can also find four strong magnets near the laser diode. Don't throw them away. They will turn out to be useful later.

STEP 2: Assemble Laser

Now you have two laser diodes. One is infrared which we don't need. The other one is 650nm red diode (usually has a letter 'R' on it) and is the one we need. The diode normally has three pins forming a fat triangle. One is NC. You need a multimeter to figure out which two pins are cathode and anode. The forward voltage across anode and cathode should be around 1.4V and the forward resistance should be 20-40k ohm. If the forward resistance is too high then the laser diode is over used. 

The laser diode housing

Laser diode (middle left) and laser housing 

Pushing the diode into the housing head

Laser diode in the housing head
Solder two wires on the diode
Use some heat shrink to increase the strength

Getting there



Carefully put the laser diode into the head of the laser housing. You can use the laser housing body to help knock against the diode and push the diode into the housing head. The diode should fit perfectly into the head. Make sure the pins are still fine. Then solder two wires to the electrodes and assemble the housing together.


STEP 3: LM317 Laser drive

A laser diode is like a photodiode but equipped with a resonant cavity. Laser diode is a huge current sink. Once the diode conducts, it generates a lot of heat, and the heat further lowers the diode impedance. So it is a unstable positive feedback system. If you simply put a 1.5 V battery across the diode, you will either get the diode burn or battery drained right away. We need a laser driver that can output a constant current to the diode.

There are numerous way to do this. One of the most popular and least frustrating way is to use a DC current regulator. LM317 is a good choice. By adding a resister R across the adjust pin and output pin, LM317 can output a constant current of ~1.25V/R.

[In fact, LM317 is mostly used as a voltage regulator. It maintains a 1.25V across the output pin (pin 2) and the adj pin (pin 1). Meanwhile, it controls the current output from the adj pin to be very low (usually <100 uA). Therefore by adding a resistor R across pin 2 and 3, we can have 1.25V/R current output from pin 2 to pin 1. We then add the laser diode across pin 1 and GND. Since pin 1 is forbidden to sink current, all the 1.25V/R current outputted by pin 2 will flow through R and laser diode to GND. The official datasheet of LM317 can be download here .

Here is a good LM317 laser drive circuit I found here LM317 Laser Driver (this link is already expired...)

 In my case, I replace the two parallel 10 ohm resistors by two .5 Watt 12 ohm resistors. The max current I am going to run through the diode is 200mA.
Make sure you don't mess up the adjust pin and the output pin. A heat sink would be necessary on the LM317.

You also need a switch that can be controlled by RPi. I used a power NPN E3055 transistor. You can choose what every you want, just make sure that the transistor can support continuous CE current >300mA and also put a heat sink on it.
Schematics of the laser driver and the switch. Laser is on only when "Laser switch" port  is logic high (>3V). Make sure you don't mess up the pin order of LM317

LM317 Laser driver

Laser driver (top) and the E3055 power NPN (bottom)

Laser diodes are very delicate device. They are extremely vulnerable to the condition applied on them. Unstable voltage or current, excessive current/voltage (even for very very short time) could damage them permanently. So always discharge yourself before holding the diode, and always use a constant current driver to power it. As pointed out by one of the reader (thank you, J super), you might also damage the diode by connecting the driver to power supply first and then connecting the diode to the driver. The diode should always be connected to the driver before applying any power to it.


STEP 4: Assemble the machine

Now you have two identical linear stages and it is time to put them together! There are a lot of ways to do this. For 2 axes CNC machine, I think the best way is the one given by Groover @ instructable. In Groover's configuration, the engraving sample is attached to x-axis so it only moves in x direction. The laser is attached to y axis so it only moves in y direction. This configuration minimizes the weight on each of the axes.

Laser engraver I made

I cut a 2"x2" steel board out of the DVD case and glued it to the x-axis stage as the sample support base. Since the DVD case is made from iron, you can use the strong magnets salvaged from the laser optical system to help you stabilize the engraving sample on the base.

The laser diode will generate a lot of heat . And it is important to dissipate this heat.  Otherwise the diode will die fast. I cut a 1inch cube heat sink from an old computer CPU heat sink and drilled a hole though it. The hole is perfectly large to hold the laser. I glue the heat sink on the y axis stages.

The most important issue is the x axis, y axis and body of laser have to be perpendicular to each other.


STEP 5: Connect H-Bridge to the stepper motors

Four connect pins on a 4-wire-2-phase stepper motor. They are usually arranged in the following order: a1,a2,b1,b2. (a1 and a2 are the two leads of coil a; b1 and b2 are the two leads of coil b). Using a multimeter will help to verify this.

The stepper motor in DVD is a 4-wire 2-phase bipolar motor. There are two independent coils inside. Each coil has a 10 Ohm resistance. Usually the DVD stepper motors are rated at 5V. Therefor the rated current through each coil is 500mA! The RPi GPIO pins can only output less than 20mA so RPi cannot control a stepper directly. H bridges are required.

Here is a very good tutorial on Bipolar stepper motor Bipolar Tutorial

Conceptual model of 4-wire 2-phase stepper motors, from Bipolar Tutorial.I renamed the pins

It looks like I am using different names for pins of stepper motor. In writeups by most people, they define coil 1 and coil 2 and name 1a, 1b as the two leads of coil 1, and 2a, 2b as the two leads of coil 2. It doesn't matter, as long as we know what we are doing. At least in this post I will keep the terminology thing consistent.

The central spinner of the bipolar stepper motor can be regarded as a bar magnet (actually it is circular). Obviously from the figure above that if we successively conduct current in coil a1, b2, a2 and b1, the spinner will spin in the desired sequence. To do this, we can apply a voltage sequence to a1, b2, a2, b1 as:1) high, low, low, low. So only a1 and a2 are activated. Since a1 a2 have same polarity (or opposite depending on how you define it), the spinner is pointing to a1
2)low, high, low, low. So only b2 and b1 are activated. Spinner is pointing to b2
3)low, low, high, low. So only a2 and a2 are activated. Spinner points to a2
4)low, low, low, high. Spinner points to  b1.
go to 1).

Denote high as 1 and low as 0. The sequence can be written as 1000,0100,0010,0001

The advantage of this configuration is that it is very easy to understand and usually the stepper motor moves very precisely. However, since in each step only one pair of coils is activated, the torque applied on the spinner is not very great.

To achieve high torque, a more popular way is to apply the following sequence: 1100,0110,0011,1001. And the spinner will be pointing to middle of a1 and b2, middle of b2 and a2, middle of a2 and b1, middle of b1 and a1 consequentially. And the torque is doubled. This is called full-step mode or high torque mode or two phase mode.. and is usually the mode used.

If the torque won't be a problem then we can use a 8-step sequence: 1000,1100,0100,0110,0010,0011,0001,1001. The spinner will turn 8 steps instead of 4 steps to turn same angle. This doubles the resolution. And the cost is the non-uniform torque being applied on the stepper motor. This is called half-step mode.

Usually for DVDs, the linear sliders moves about 0.15mm every full step stepper motor turn, corresponding to a resolution of ~170dpi. Good enough for home-made projects. If 8-step mode is implemented, then the resolution is 0.075mm/step or 340dp, similar to regular printer.

For laser engraver, there isn't any serious load on the stepper motor so I choose the half-step mode or 8-step mode.

As mentioned, RPi cannot drive the stepper motor directly because of the current limit. Actually, besides powering low power LEDs, the GPIO pins of a RPi usually serves as logic switches. In output mode, they are either logical High (3V) or logic low (<0.7V). A H bridges is a "translator" that translates these logic High or Low into power source that has high voltage or low voltage.

File:H bridge.svg
A conceptual schematics of H bridge (from wiki)
Above is a conceptual schematic graph of H bridge (has similar shape with letter "H"). One H bridge has two working mode: (S1 S4 close, S2 S3 open) and (S2 S3 open, S1 S4 close). In the first mode, current flows rightwards through the motor and in the second mode, current flows leftwards through the motor. In reality, this H bridge is never used. A common way is to use transistor as electric switches. See figure below.
A very very simplified conceptual schematics of NPN PNP H bridges. NEVER build an H bridge based on this graph, because You will probably burn the transistors or even the Pi. A practical H bridge requires current limiting resistors, reverse diodes and logic TTL chips. Please consult more from other sources if you would like to build a working H bridge from scratch.
When A is logic low (0V) and B is logic high(+V), then transistor 1 and 4 are conducting while 2 and 3 are open; when A is logic high (+V) and B is logic low (0V), then transistor 1 and 4 are oepn while 2 and 3 are conducting. When both A and B are logic high, 2 and 4 are conducting, 1 and 3 are open, the motor stalls; when both A and B are low, 1 and 3 are conducting, 2 and 4 are open, the motor stalls.

Therefore by setting A and B high or low, we can control the current direction through a load. For each 4 wire 2 phase stepper motor, there are two independent coils we need to control. So a total of 4 H bridges are required to control the two stepper motors.

There are a lot of integrated H bridge available on the market. For my case, I need 500mA through each H bridge so L9110S suffices (L9110S can afford 800mA through each H bridge). Each L9110S contains two H bridge so two of them are enough. There are L9110S modules for <$2 each on the market. Very convenient!

I am not exactly sure but I think sometimes people call it L9110 sometimes L9110S.. Datasheet can be found here.

Also, L9110S has internal clamp diodes to conduct the reverse current generated by the sudden stop of the stepper motors. This protects the circuit. L9110S is TTL/CMOS output level compatible so it can be directly connected to RPi.

Two L9110 dual H bridges (also known as HG7881). Each is 0.8 in by 1 in.
Each dual-bridges controls a stepper motors. On the right hand side, there are a1, a2, b1, b2 connectors to the stepper motors (top to bottom). On the left hand side, there are logic control pins for a1, a2, b1, b2 (actually they are named as A-IA, A-IB, B-IA, B-IB) and VCC and GND.

When powered, the chips light up (The big circular thing is my ring. I don't have fat fingers..)

STEP 6: Controlling the machine using Raspberry Pi

Now this is the key part. Because I am not using external stepper motor drivers so I have to incorporate the function of external stepper drivers in software. I choose python to do the job.

People say that RPi is not a real time device because it has an entire OS on it and python is very slow. However, in my case, these issues didn't cause any trouble.

The python code I wrote includes the following functions:
1. An encapsulated bipolar stepper motor class. It enclosed information like phase and position. It has a .move(direction, steps) build-in function that converts movement commands into a sequence of GPIO.output() commands that spins the stepper motors. 
2. A G code interpreter: read G code and send the corresponding commands to the bipolar stepper motor objects. For G02 and G03 commands (circular interpolation), the interpreter performs the interpolation and converts the commands into a sequence of straight motions.

The most challenging part is how to control  more than one stepper motor simultaneously. The idea is actually very simple and can be extended to any number of motors. We already know how to control one motor. Now suppose we have two motors: MX and MY, and we want to turn MX 12 steps and MY 15 steps simultaneously in 6 seconds. First find the least common multiplier (LCM) of 12 and 15, which is 60. Now divide 6 seconds by 60 we get dt=0.1sec. Set 60 loops. Before the end of each loop, we use time.sleep(0.1) commands. So it takes 6 sec to finish the loop. And we move MX one step every 5 loops and move MY one step every 4 loops. After 60 loops, MX moves 60/5=12 steps and MY moves 60/4=15 steps. And both MX and MY moved at constant speeds. 

0000100001 0000100001 0000100001 0000100001 0000100001 0000100001 (60 digits, 12 ones)
0001000100 0100010001 0001000100 0100010001 0001000100 0100010001 (60 digits, 15 ones)

For more than two stepper motors, just find the LCM of all the steps (ignore 0).

I uploaded the code to sourceforge in 2013 however it's no longer available there. I recently upload the code to GitHub so it is easier to download via different platform.

The GitHub URL is

You can find three python codes:

Project: raspberrypi-cnc-laser-engraver

CodeUploaded byDateLanguage
Bipolar_Stepper_Motor_Class.pyXiang Zhaipython
Motor_control.pyXiang Zhaipython
Gcode_executer.pyXiang Zhaipython
spiral.ncXiang Zhaig-code
grid.ncXiang Zhaig-code

and many other g-code examples. defines the Bipolar_Stepper_Motor class. By default, line 5 is commented and line 7 is valid. This corresponds to a 8-step half-angle sequence. If maximum torque is desired, you can comment line 7 and uncomment line 5 to select 4-step full-angle sequence. defines a set of functions such as LCM (for calculating the lcm of two integers) and Motor_Step (for controlling two motors simultaneously). Usually you don't need to modify anything.
This is the main program. You need to modify line 25 (G code file name), line 29 (pin numbers for stepper motor X), line 31 (pin numbers for stepper motor Y), line 32 (pin number for laser switch), line 35, 36 (resolution of the machine in unit of mm/step) and line 38 (engraving speed). The code read and interpret G code, and send corresponding commands to the motor control functions.
This is a simple G code which plots a small spiral. It can perfectly test whether the machine can process G code, especially the circular interpolation G02 and G03, correctly.
A simple G code which plots several straight lines to make a grid. Perfect code to test the machine and make a coordinate system!

Currently the only accepts limited number of G commands: G90, G20, G21, M05, M03, M02, G01,  G02, G03.
The code can recognize G1F commands (engraving speed) but simply ignore it.  Engraving speed is set by line 38 in unit of mm/sec.

As mentioned at the beginning of this post, D. Miller made some improvement to my code so that the code can work along with the inkscape GCodeTools extension and allow remote engraving via another small python script he wrote. The modified version can be downloaded here


STEP 7: Engraving!

Following Groover (by Groover in instructable), I use Inkscape to make G code. Inkscape is an open-source vector graphics editor and support various OS (windows, linux, Mac), which means that you should be able to install it on Raspbian! I didn't try that. I simply use my laptop and send my design to the RPi.

You need a laser engraving extension to convert vector graph into G codes. There are several different G code extension. The one I use can be downloaded here Inkscape-Laser-Engraver-Extension

[Note: I wrote my python G code interpreter based on the G code generated by this Inkscape extension. So the code can only deal with limited numbers of G commands, sufficient for laser engraving though, fortunately. For G codes given by other generator, my codes might not be able to handle all the commands. You might have to modify the python code on your own.]

Here is the step-by-step instruction on how to generate G code that my python code can process.

Step 7-1: Install Inkscape and Inkscape-Laser-Engraver-Extension

Inkscape can be freely download here
It runs on windows, mac and various linux OS.

Then download Inkscape-Laser-Engraver-Extension here.

To install the extension, simply unzip the file and copy everything into the Inkscape installation folder. For example, in Windows 7, if Inkscape is installed under
C:\Program Files\Inkscape
, then just copy everything from Inkscape-Laser-Engraver-Extension to C:\Program Files\Inkscape\share\extensions
and then restart Inkscape, the extension should be already installed. To verify this, run Inkscape and check for "Laser Engraver" item under "Extension" on the manu bar.

The Laserengraver extension is successfully installed

Note: Some people report that the Laserengraver extension does not work in Inkscape of the latest version. If that happens to you, use Inkscape 0.48. The windows installer can be found at the bottom of this page.

Step 7-2: change page size

Open Inkscape, goes to  "File" => "Document properties" => "Page", under the "Custom Size" box, change the "Units" to "mm" (millimeter) and then put 36 and 36 in both "Width" and "Height". Then close the dialog.

You will see the blank page becomes a small square box. Zoom into that box.

Step 7-3: plot, text, create, a lot of fun~

You can type texts, plot graphs, or even paste png/bmp in the box.

Press "Ctrl" and "A" to select everything plotted, under "Path", click "Object to Path". Or simply press "Shift" + "Ctrl" +"C". This steps is required if you have text or other external objects.

Convert objects to path

Step 7-4: Generate G code

Goes to "Extensions" => "Laserengraver" and click "Laser...". A dialog box will jump up. You can select "Directory", "Unites" and modify other preference under "Preference". Under "Laser", type the file name (should appended by .nc extension). Then click "Apply".
Convert the object to G code

A dialog box will say "laser working, please wait..".

If "Draw additional graphics to debug engraving path" is checked under "Preferences" before clicking "Apply", Inkscape will plot a lot of arrows on top of the plot, showing the moves given by the generated G code.

G code is successfully generated! A lot of arrows.

Step 7-5: Pass the G code to RPi

If you use a laptop/desktop to generate the G code, then you have to pass the G code to RPi using ssh or other tools. The G code should be placed under the same folder together with the python routines.

Step 7-6: Make necessary modification to the python code and then engrave!

At least you want to change the G code file name in (line 25). Some other changes, say, pin numbers (line 29, 31, 32), resolution (line 35, 36), engraving speed (38), can be changed if you understand them.

Type "sudo python" in terminal to run the python routines and have fun!


  1. To get accurate laser engraving process work, it is important to determine its type thoroughly and then make the best selection.

  2. Darknet legit financial vendors and scam marketplace reviews,
    FULLZ, CC can be bought from Deepweb - Darknet Financial Vendors.

  3. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hopping for some more informative posts,
    Cnc usb controller

  4. This comment has been removed by the author.

  5. Very useful update. No doubt, laser technology has now been a proven tool to be used perfectly in the industrial manufacture and even our official, individual purpose and even in the medical field. I was really pleased seeing some brilliant focuses in this post regarding the mini CNC engraver laser details. Of course love to drop my thanks to you for this precious contribution. By the way, I also would like to share a reputed laser engraver and etching site that prodived a comparative foucs on thinking many guys here find it very interesting.

  6. For the latest mine and quarry equipment, cases and accessories information, please visit