Introduction: True 8bit Photo Engraving on Chinese Laser Cutter - Maybe a Worlds First?
Update 11/04/2016: The new firmware is ready :)
You can download the HEX file here.
To upload it to your Mega2560 please use XLoader.
The program pretty much explains itself: Select hex file, set board and com port, hit upload and enjoy.
All vital values are stored in the EEPROM for easy tweaking like acceleration, jerk, max speeds...
This is an updated addition to my previous Instructable about replacing the Moshi controller.
So if your K40 clone is still a virgin then please head over to the above Ible first for the hardware conversion.
The story so far...
It began with the need to get rid of the original controller to be able to do proper vector cuts and raster engravings.
All this explained in the above Ible in case you missed it.
But soon I noticed that these low power diode lasers can do really good looking photo-engravings.
Ok, what can be easier than doing this on a proper CO2 laser???
That is how the journey to the holy grail of CO2 Laser engraving started....
To understand the problem I have to go bit into the details here, so forgive me for the dry explanations.
Unlike a diode laser our CO2 systems are many, many times more powerful.
And where a diode laser with TTL pulses can cover the full PWM range of 0-255 a CO2 can't do this at all.
Most systems require a minimum power of above 6% for the laser the fire at all.
And to be on the save side of lifetime and problems the max power should be kept under 60% at all times.
So instead of 256 shades of grey - or here power levels, we only have a bit over 50.
Ok, not too bad for simple things...
At least with my machine 6% power means slight burns on wood, 15% already very deep burns if the speed is not high enough.
Trying to engrave a photo with just 9 levels of power between just visible and burnt through is not nice.
After testing a lot of tools, extensions and such I found only one tool being able to generate proper G-code for photo engraving in 8Bit.
It is Image2Gcode but not the outdate version you find quickly through a websearch.
Bryan and Leo69 over at Viscious1 have create a more powerful version of the tool and still update to make it even better!
Please check the above link for the most current version of the program and to leave your feedback.
Only with proper feedback such great tools will see ongoing support and improvements!
Having such a powerful and totally free tool at hand was a great start but to make real use out of it I had to find a way of controlling the power of the beast in more defined ways.
Turnkey Tyranny did a great job with his firmware in terms of documenting it, which allowed me (after some inspiration from our great guys in the forum) to modify the code.
The coding and the problems...
One thing I noticed in the Marlin code was that the PWM signal for the Laser is not controlled in the usual 255 stepping we all now from the 3D printers and spindle speed commands.
Instead Turnkey Tyranny opted for the original 0-100% settings.
Meaning Zero for the power level is zero and 100 is already the max power the tube can provide!
This explains a few things in terms of going up one power level means instead of nothing on the paper the laser burns right trough the paper....
Here is a pic of the best I could do with the limited power settings:
As you can see there are only a few "shades of grey" due to the fact that the power was limited between 6 and 11 and because Marlin does not like decimal digits.
After some testing with a spare Arduino and some LED's I started to experiment with the code for the PWM signal.
If anyone wants to know all the details let me know in the comments and I'll explain more.
Some initial test looked really good so I decided to try to new code on the machine.
Zero still means Laser is basically switched off but the max S level is no longer just 100 or 255, it is increased now to 10000 - yes a one with five zeros.
As a result the PWM resolution is now 100 times more detailed :)
Some example calculations from the original firmware to this one:
If before the laser started to mark wood or paper at S7 it will now start somewhere around 700.
I say "somewhere" as you can now really find the sweet spot between nothing and a barely visible laser mark.
And of course where before at S20 you did you cuttings through play you will now need S2000 for the same result.
For our purpose of engraving:
Let's say at S7 you got enough power to leave a visible mark in wood.
At S10 you already cut through the first plywood layer.
For an engraving you don't won't to go through the first layer, if you need deep engravings you use solid timber instead.
Before we only had 3 power levels to play with, now we can spread this over 300 levels - well enough to fit 256 shades of grey into it ;)
Ok, great! We can do proper engraving now - but how??
Well first off always keep in mind that everything here is based on the Turnkey Tyranny firmware and Inkscape plugin.
This means the machine still does not comply to normal CNC standards as it would when running on GRBL for example.
For me that is perfect as it is easy to to use with freeware alone that I understand to use.
Certainly there are other tools available that claim to do 8bit engravings, Picengrave just to name one comercial product.
But in the freeware region there is not much that can compare to Image2Gcode as linked above.
But since it is not optimised just for our machines there is some manual editing required if you want to place the engraving in a specific place.
Our bed origin is in the upper left, while CNC machine have it in the lower left.
This means our movements are inverted in the Y-direction.
The first step in the program is to flip your image upside down - this way it will be orientated correctly.
Without code modifications the engraving will take place in the top left corner as set by the limit switches.
For this example I will leave it there without code mods.
Load your photo into Image2Gcode and as said flip it.
Pay attention to the size and resolution settings and adjust accordingly!
For small engravings I use a resolution of 0.1, bigger things at 0.2 or 0.25 - this is the distance between the generated laser lines and for a perfect result should match the diameter of your focal point.
The mode of course is Gray scale 8bit.
On the Laser profile tab you can set the parameters you found best after some testburns.
Due to current limitations the speed will not go as high as you are used to - so start with lower power levels as for now I have no found a way to increase the processing speed of all the movement commands (at least nothing easy).
For the below image I used a value of 500 and 755 for low and high power settings.
Speed was set to 3600mm/min to test but I assume the real speed is somewhere around 600-800mm/min - as said still have to find a way to improve the speed here.
The tick box for decimal values was something we tried to get more control over the PWM but Marlin does not support it.
Optimized raster reduces the movements and engravings to areas with laser activity - especially images with large, unengraved areas will benefit from the time saved here.
The profiles are os no use for us right now, so just use "Max" and double check your power values are still the same.
Under the Gcode tab we set the desired feedrate use the "S" parameter for the power settings and the M5 command to turn the laser off.
You can play with the engraving patterns if you like.
After clicking on "Generate file" you open the resulting Gcode in Proterface to send it to the machine - the engraving will happen in the top left corner!
Here are some images of the first (successful) engraving:
Original in grayscale
Engraving before the cleanup, all the burnt ash still in there
Engraving cleaned up
As you can see there is still room for improvement so playing with the power levels to find the perfect match and doing some tests is the current way to go.
If required I might change the firmware again and check if full 16PWM encoding is possible to, this would give even more than 10000 steps for the PWM signal.
Right now it is a good alternative to getting a diode laser if you want to do more than just engravings or work with quite thin materials.
A decent diode laser module with power control (including TTL and PWM) will cost almost the same as one of these K40 clones.
So you get the enclosure and some more or less good working exhaust system free.
With the more controlled power brittle materials like glass, ceramics or some stones should get better results now too.
Simply dial down the power until the amout of splitting is reduced to a minimum.
Using the M649 command in the Gcode to set pulse mode operation also helps a lot here.
Just do some tests with different pulse rates through the Inkscape plugin to set one laser dot next to the other without creating a full line.
Step 1: Image Preparations and the Use of Image2Gcode
As you could see in the previous step, the orignal image was modified so the background is just white - white means nothing to engrave ;)
Depending on what look you are after you either use quite low power settings to just char the wood or you burn a bit deeper into the material for a lithopane like effect.
The color of the image also depends on the material you use, in this example it was just 3mm plywood.
I ran out of testblocks but noticed that for deeper engravings a fine grained wood with very low resin content is best.
Pine and some hardwoods, especially here in AU, contain a lot of resin and grow very fast.
So you end up with different densities for the wood and even in pulsed operation it is very hard to get the same result in all different grain areas.
If you just char the wood it is best to secure the ash with a thin layer of hairspray to prevent it from fading away.
For longer lasting result use some clear spray paint after this.
Resolution and number of colors...
The image resolution does not need to be higher than what your machine can engrave.
So using the 4K images directly from your GoPro might be a bit of overkill.
At moderate power levels I get a spot size of around 0.12mm, smaller for fast moving lines though.
If we convert this into a DPI value we get just over 210DPI.
With a finely tuned machine and fast enough movements you might get the spot size to around 0.08mm or just over 315 DPI.
With that in mind you only have to consider how big you want to engrave, example:
Your photo is in 600DPI with a size of 20 by 15cm.
You want to want engrave this only 4cm wide instead of 20.
To make it easier for Image2Gcode you would resize your image to 300DPI with a width of 4cm.
In the oppsite case, with the image being 600DPI but only 4 by 3cm you can simple enlarge the pic in Image2Gcode as there is enough detail and for enlarging on such a scale you don't need to generate a new image.
Since we do only 256 shades of grey the image should be saved as such and not in 16bit or more.
Considerations for better results...
You want to do some simple tests first to tune your power settings.
With now 10000 levels between zero and full there is plenty of room to play.
In a perfect world you want no engraving at all for everything white and the desired level of burn for total black.
Easiest way to do this is by using one of the many greysacle calibration patterns available within graphics programs or on the web.
If in doubt quickly make your own in boxes with 10% increments for the brigthness level for black to white.
Please keep in mind that we burn the material, so for most organic things this means material is being removed.
To only get a greyscale image without burning into the material means using very fine power settings - still very hard at our power levels.
In most cases it also pays off to increase the brightness of the picture before processing, either within Image2Gcode or in your favourite photo editor.
I assume you are happy with your image so far and that the unwanted background is removed.
Load your picture into Image2Gcode.
As you can see I already flipped it and increased the brightness a little bit.
Orientation is set to the lower left, meaning the engraving starts there and goes up in the machine - but feel free to play with your own settings.
Note that I set the resolution to 0.14 - it is a good compromise between possible speed and higest resolution of the machine.
These are my current settings for engraving in plywood.
If you tick the box for "Optimized raster" you can set a threshold for elimination of white areas.
This means the laser wont even move over these areas saving a lot of time.
Pay attention to the power levels! If you change the profile these values will change, so double check before you continue!
As you can see I operate between 550 and 805 - of 10000 possible.
Change to the old firmware options this would be between 5.5 and 8.05% - but now with 255 different power levels ;)
In the Gcode header I have added some values to set my image into the area of the clamping window instead of the top left corner, this header will replace some of code generated by the program but more on that in a bit.
For the Laser on we use the "M03 S" command and to shut the laser off the M5 command - these relate to the spindle speeds in a normal CNC setup.
Here is the resulting Gcode, without the additions in the header field:
I don't want a box around my engraving, so actual code for it starts at line 21.
In line 12 and 12 you see some mess with the coordinates, this bug should be fixed soon.
But for my purpose the entire section between line 1 and 21 is replaced with what is defiened in the header section, so some manual labour with Notepadd++ or your favourite editor is required.
When done and ready for our machine it looks like this:
As you can see I also fixed problem with in line 22 with the two commands in a single line.
If you use this as a template so to speak you should be fine.
The changes are quickly done in Notepad++ so it pays off to do some tests with lower power settings on a piece of cardboard to get the alignment right before you start burning on the real thing.
To engrave the picture use your favourite Gcode sender like Proterface, Repetier or a dedicated sender - optional if you have the display added you can load the file from the SD card.
Step 2: The New Firmware...
At this point I will only provide the Hex file that is optimised for the K40 Laser cutter clones.
All important things can be changed over the EEPROM settings, use M501 to show them in Pronterface for example.
Adjust acceleration, and jerk to what your machine can handle without loosing steps.
Once I got some more feedback on how this firmware performs I will provide the source files or fork them on Github so everyone with a diode laser or other machine setups can use it as well.
How to flash the firmware:
First make sure you selected the right Com port and board in the Arduino IDE settings.
Next load the EEPROM clear sketch and change the 512 value to 4096 - the size of our memory.
Upload the sketch to clear all EEPROM settings - this is vital to prevent problems, if something does not work right after the flashing it is usually because of missing this step.
Now exit the IDE and star Xloader, if you don't already have it you don download it here.
Usage explains itself:
Load the HEX file, select the Arduino Mega2560 and your Com port and click on upload - done!
If you prefer to use the AVR Studio enviroment you can do so as well.
Step 3: Some Samples of the Engravings I Tried So Far
As before on 3mm plywood:
Same image but on 4mm borosilicate glass:
Glass was a weird thing to engrave as the massive laser power meant in contious wave mode the broken glass pieces were fused together with the next laser line, creating a look like broken safety glass with the image in it.
Took some careful tuning to get the power levels just right but only by using a pulsed laser mode proper results could be seen.
This is almost to my liking ;)
As a funny fact I broke my first glass plate in continous wave mode as the glass was overheating and got more and longer stress cracks with every new line.
I guess the same will happen with hard and brittle materials like granite or slate.
Did a short and bad test on a little piece of clear acrylic only got a melted surface.
IMHO a very short focal lens is required here and very short but inense pulses.
This way the material will be evaporated instead of melted without shooting through.
But not planning anytime soon to get special lens for something I might never need.
Still to follow is some marble but I need to get a cutting disc for my grinder first as the slate is too big for the machine.
Anodized aluminium should be no problem either but I guess on this material a rastered bitmap is to be prefered as the colored layer is ultrathin and I doubt it is possible to adjust the laser levels fine eough for proper results.
Step 4: Customized GCode Generator for the K40 Clones
I did some digging in the Image2Gcode program and changed a few things.
The result is that the filesize of the Gcode file is now quite a bit smaller and the code more to the liking of the Turnkey Turanny Firmware.
The program is linked for download here, so just some hints on the best (IMHO) usage:
When you load your image into the program increase the brightness by 10% and reduce the contrast by 10% - otherwise totally black areas might not be engraved!
If you want the image to be engraved as loaded you need to flip it - this is because of our different homing layout.
Resize as required and double check the aspect ratio box is ticked!
A resolution of 0.12 works quite well, bigger engravings might need a higher value here as otherwise Pronterface complains about memory issues.
Orign set to the top left corner. (We need this for the offset later on!)
For the laser profile tab you have to find the best power levels for your material, cardboard seems to work great with 730 and 985.
Make sure the box for "Optimized raster" is ticked , set the threshold to 0.
In the Gcode tab we set a new orign for the engraving to get it out of the top left corner in the header field:
G92 X-135 Y-50
Means the engraving starts from these coordinates plus the height of the image.
For a 50x60mm engraving this means the image will be located between X135 / X185 and Y50 / 110.
Keep in mind here that we use the top right corner to start the engraving, but as our X-axis moves down and not up the numbers seem a bit weird at first.
Play with it and they soon make sense :)
In the footer we add
G28 Y X
This makes sure the laser really is off and that the head moves into the home position.
And as usual we use the "S" parameter, "M03 S" and "M5".
Last but not least make sure to select the "horizontal scanning" as I still struggle to implement the changes properly to the diagonal scanning pattern.
Engrave as usual but make some tests before starting on the expensive things!
Step 5: Coustom, Custom and More Custom Work ;)
I finally complete the works on the firmware to allow vector- , raster- and 8bit-work.
For the 8bit engravings there is still Image2Gcode required but I optimised the last release to create better usable code for the china clones.
It was quite some hard work as some of the required changes turned out to be quite hard to realise.
Here is a little video showing all three laser modes in a single job.
Where the china original had only limits and problems it is now possible to do quite good work with the machine.
Keep in mind all is freeware and no commercial software is required for all of this.
This means you are not limited anymore.
Sure some things like automatic alignment of parts through the software, adjustable focus / work platform height and the safety mods are still missing - but it finally works like a 4000$ machine :)
Since there is no real feedback in my two K40 laser Instructables I assume either noone uses these mods or that everyone is just happy with it.
So if you want the latest firmware release or the source code - or the modfied Image2Gcode program then please pop a comment and if possible include a pic of your machine.
Would also be nice for the stats if you click the "I made it!" button ;)
Customised display welcome messages are an option too ;)