Arduino Laser CNC Engraving Machine

Warning: This project uses a laser. It will hurt you if you are not careful. Please take care when handling the laser. Do not look at the beam, do not point it at yourself or anyone else. Remember that an IR Laser can damage the eye just as easily as a visible light one. A good pair of laser goggles must be worn, they must be designed to filter the colour of laser you are using and compliant to  EN207 standards.

Laser CNC

I have built a Laser CNC machine out of a couple of old DVD-RW drives, an Arduino UNO with Grbl v0.8 installed, two EasyDriver Stepper Motor Controllers and bits of wood I have around the home. The laser came from one of the DVD-RW drives.

My Engraver is vaguely based upon the Pocket Laser Engraver by Groover, except I have used an Arduino UNO and have added limit switches. G-Code is a industry standard, of sorts, used to control CNC (Computer Numerical Control) Machines, such as lathes, routers, and in this case Laser Engravers.

Here I will be providing additional information about the electronics and Grbl configuration I discovered while building the Laser CNC. I have used the following software:


Stepper Motor Controller

There are four electronic parts to this setup. The 5v power supply, a relay circuit to control the laser driver, the Laser Driver to control power to the laser, and the limit switch circuit used by Grbl to provide a stop indication on the axes.

The Power supply is a nothing glamorous, its a basic 7805 design giving a 5v output.
5v Power Supply
I use a 2A Switched Mode Power Supply plugged into the mains, similar to this 17W Switched Mode AC/DC Multi Voltage Power Supply from Maplins. Using 7.5v as the input is enough to keep the 7805 running without it having to convert too many volts into heat.

To control the laser from the Arduino, I have added a relay circuit. The 5v relay is powered by the power supply above, on one side of the normally open switches is a 5v fan to blow the smoke away, and on the other the Laser Driver, remember to switch the laser driver circuit rather than the laser diode.
relay circuit

The Laser driver using a LM317 Adjustable Regulator is a little more involved, as calculations have to be made to establish the output current. You want enough power to scorch or cut card, but not so much as to burn the laser diode out.
Laser Driver

The power output of the Laser Driver is set by the resistors R1 and R2. This video tutorial gives some explanation. Going above 500mA with a red laser will definitely cause it to blow, I have limited the power to 330mA as I only have a limited supply of lasers. I used this page for calculating resistors in parallel. Here are some milliamp output values using a couple of standard resistors.

Resistors volts / ohm mA
2 x 10R 1.25v / 5R 250mA
2 x 9R1 1.25v / 4R5 270mA
2 x 8R2 1.25v / 4R1 300mA
2 x 7R5 1.25v / 3R7 330mA

The final circuit is for the limit switches, and is based upon one found here:
limit switches
The switches need to be very sensitive, I found using that the motors were not strong enough to push button switches. I used those found in the one of the drives, and a couple from an old cassette deck. Check that you have connected up the switches correctly, X to X, Y to Y, confusion arises when they are the wrong way round and Grbl doesn’t say which switch has made contact.

In Grbl set $16=0 (hard limits, bool) if your image is larger than the allowed size then will just make your drawing look wrong, setting it to 1 (true) will cause the G-Code program to be aborted. $17=1 (homing cycle, bool) will cause Grbl to expect an $H (home) command when started, $H will cause your axes to move to their start positions.



Software Configuration

The only real issue with setting up Grbl, was getting the X and Y axes operating in the expected direction. There are two problems, getting the right start point, and having them go in the correct direction. You will be wanting the image in Inkscape to appear correctly. There are two settings within grbl v0.8 that need to be configured so that it works: $6 (step port invert mask) and $18 (homing dir invert mask). Having spent a while trying out various settings, I wrote a little mask calculator to assist me with this, to find your mask, click the checkboxes, or you can enter the current mask number and click set to find the binary code.

Invert Bit: $6=0

The documentation says that the first two bits are not used for inversion, but have found that there is a change in the direction of the motors.

Test with your favorite terminal program, on this Linux box I used: minicom -b 9600 -D /dev/ttyAMA0, turn local echo on with Ctrl-A E. Make the table go forward 1cm with X10, or in reverse X-10, Same for the Y axis, Y10, Y-10 further experimentation may be needed when you first start etching, I had a images and text appearing mirrored at first.

Now it is a case of loading your line-art into Inkscape, thin lines work best, creating the G-Code file using the laserengraver option in the extensions menu. Then sending the file to the Arduino with the Universal G-Code Sender. A Guide

My Grbl settings:



The Case of OpenCV and the Missing SURF

I have been wanting to have a play with the OpenCV computer vision framework on Python for a while and finally got some time to some experimenting, I am looking to have the computer recognise LEGO parts, after much research and mucking about it seems I should be using cv2.SURF and/or cv2.SIFT for what I want to do. However on Fedora 19 these are not included in the distribution RPM as they are nonfree in that they are not open source. Attempts to use SIFT or SURF result in the following error:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
>>> i = cv2.SURF()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SURF'
This is annoying as OpenCV will now need to be re-installed the old fashioned way, but there are instructions on the OpenCV site and I will be using them here with additional information I have discovered while following them.

Change to root and add the free and nonfree repositories as ffmpeg and libv41 are unavailble from Fedoras, followed by an update (it seems that Fedora 19 always has something to update):

& sudo su
# yum localinstall --nogpgcheck$(rpm -E %fedora).noarch.rpm$(rpm -E %fedora).noarch.rpm
# yum update

Remove the existing OpenCV packages:

# yum erase opencv*

There are a few packages to install in preparation to compiling the code, you may have some of all of these already but yum will skip those. Install the mandatory packages, these are for compiling the source and using GTK for the GUI:

# yum install cmake python-devel numpy gcc gcc-c++ gtk2-devel libdc1394-devel libv4l-devel ffmpeg-devel gstreamer-plugins-base-devel git wget

Install the optional dependencies, I have gone for the install everything approach, they may be useful later:

# yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel tbb-devel eigen3-devel python-sphinx texlive

Now, we are ready to get the latest source, here there are two ways to do this, install the latest development version using GIT, or download the latest stable version. My preference is to use the latest stable version.

With GIT, exit back to yourself from root, change to your home directory and download OpenCV:

# exit
$ cd ~
$ git clone
Cloning into 'opencv'...
$ cd opencv
$ mkdir build
$ cd build

Or using the latest build, at time of writing this is v2.4.7. Get this via the downloads page and save it to your home directory:

# exit
$ cd ~
$ tar -zxvf opencv-2.4.7.tar.gz
$ cd opencv-2.4.7
$ mkdir build
$ cd build

Now configure:


When complete you should check that your options agree with those displayed. There are many others available. Support for other programming languages may be missed out if they are not already installed. In my case those for Java, if I were to try OpenCV in Java I would need to install the appropriate Java packages using yum, then recompile OpenCV.

Now build and install, this can take a while:

$ make
$ sudo make install

You now need to move the module to anywhere on the Python Path, to find this:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.7/site-packages/PIL-1.1.7-py2.7-linux-x86_64.egg', '/usr/lib64/', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

The directory /usr/lib/python2.7/site-packages looks suitable:

$ sudo mv /usr/local/lib/python2.7/site-packages/ /usr/lib/python2.7/site-packages

And add your new installation to the PYTHONPATH, and add the export to the end of your .bashrc so it survives a reboot:

$ export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
$ echo export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages >> ~/.bashrc

Now to test:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
>>> i = cv2.SURF()

Sorted. Now, perhaps, I can get on with what I actually want to do….


Converting the Canon GPS log

Having been out and about with the Canon 6D, for once I had remembered to turn on the GPS logging built into the camera. Upon returning home after the seven mile walk I saved the GPS data in the cameras memory to the SD card. Now this LOG file found in the GPS directory of the SD card can be read directly into Google Earth but not into Memory Map for viewing on the far superior OS 1:25,000 Ordnance Survey map, also Memory Map does not read Google Earth’s KML or KMZ files.

So, the LOG file needs converting, but what format is it in? Canon are, it seems, rather unhelpful in revealing the secret but a little googeling found a reference to them using the NMEA-0183 format.

For conversion I found the free, excellent and very comprehensive gpsbabel to do the job (the PDF manual is over 200 pages), for me I convert the GPS files on a linux computer, but they do a Windows version and I assume the method and outcome will be the same. On Debian, install gpsbabel with:
$ sudo apt-get install gpsbabel

The basic use of gpsbabel for converting is as follows:
gpsbabel -i <input format> -f <input file> -o <output format> -F <output file>

As I know Memory Map reads the Garmin GPX format, I chose that as the output:
$ gpsbabel -i nmea -f 13120100.LOG -o gpx -F 13120100.gpx

And the rest is, as they say, Topographic.


Infra-Red Coin Detector for Arduino

For something I’m building I need have the Arduino detect a coin being dropped trough a slot, for this I have built an IR detector, it comprises of an IR LED, IR Photo-Diode, Op-amp and ATtiny85 micro-controller.

IR Coin Detector (Mk3)

The circuit works by having the IR LED flood the Photo-diode so that when an object passes between them the Photo-diode stops letting current through, this is fed into the op-amp to provide a consistent output for the ATtiny85 micro-controller to detect the change in signal to then flash a couple of LED’s and provide a signal to an Arduino.

IR Coin Detector (Mk3)

Here is a program that flashes a couple of LED’s and makes output pin 4 high:


  1. DIY Science: Measuring Light with a Photodiode II
  2. Pin Change Interrupt on the ATtiny
  3. Programming an ATtiny85 with an Arduino

Recording Sound on the Raspberry Pi

The Raspberry Pi does not have a microphone socket, which is inconvenient when you wish to record sound. To fix this you will need a USB Sound Card, for which I bought a Creative Sound Blaster Play! for about £20 and a short USB extension lead as the sound card is slightly too large and blocks the other USB port.

With the latest Raspbian “wheezy” installed on a Pi Model B with 512Mb of RAM and the overclocking set to High in raspi-config, here is a recipe for getting your Raspberry Pi to record sound from the command line. For the test setup I connected my iPod to the microphone port of the sound card, plugged everything in and powered up.

Raspberry Pi Records

After logging into the Pi, check that the computer can see the card, use lsusb to find it, here the card is highlighted in blue:
$ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 046d:c52e Logitech, Inc.
Bus 001 Device 005: ID 041e:30d3 Creative Technology, Ltd Sound Blaster Play!
remember that different makes of card will have different names and ID’s

Your user will need to be in the audio group, check this with groups <username>:

$ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users netdev input
if not, then add them with:$ sudo usermod -a -G audio <username>

There is/was an issue with the Pi’s USB port that meant it can/could become overwhelmed1 with data which causes popping and bubbling noises to be included in your recordings, this can be fixed with an update of the Pi’s firmware:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install rpi-update
$ sudo rpi-update

The Raspbian image already has the alsa-utils for sound already installed, the programs I am using for recording and playback are:

  • alsamixer – GUI for setting the recording and playback levels
  • amixer – Command Line for setting the recording and playback levels
  • alsactl – for saving the settings set in alsamixer or amixer to use again after a reboot
  • arecord – For recording the sound
  • aplay – For playing back your recording

All of these programs have a –help option.

There are two methods for setting up the the microphone port on the card, the first is alsamixer:

alsa mixer

$ alsamixerPress F6: Select Sound Card, and choose yours from the list, the bcm2835 ALSA is the on-board sound, for me the one to pick was: USB Device 0x41e:0x30d3 and take a note of the card number, in my case: 1. Now select the Mic and increase the volume to 52, or the first white blob, you’ll need to change it later, but its a good place to start. The Auto Gain Control wants to be off, select the gain control and press M to toggle so it displays [MM] for mute. Press Esc to exit and save the settings with:$ sudo alsactl store 1 where 1 is the card number.

Alternatively, you can use amixer. First find your sound card, in amixer there does not appear to be a method of listing the available cards, but on a Raspberry Pi I guess it will always be card 1, you can list the cards current status with:

$ amixer --card 1 contents
numid=1,iface=MIXER,name='Mic Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=2,iface=MIXER,name='Mic Playback Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=32,step=0
: values=21
| dBminmax-min=0.00dB,max=47.81dB
numid=5,iface=MIXER,name='Mic Capture Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=6,iface=MIXER,name='Mic Capture Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=16,step=0
: values=7
| dBminmax-min=0.00dB,max=23.81dB
numid=7,iface=MIXER,name='Auto Gain Control'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=3,iface=MIXER,name='Speaker Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=4,iface=MIXER,name='Speaker Playback Volume'
; type=INTEGER,access=rw---R--,values=2,min=0,max=151,step=0
: values=52,52
| dBminmax-min=-28.37dB,max=0.06dB

So I want to turn the Auto Gain Control off, and the recording volume to 14:
$ amixer -c 1 cset numid=7,iface=MIXER,name='Auto Gain Control' 0
numid=7,iface=MIXER,name='Auto Gain Control'
; type=BOOLEAN,access=rw------,values=1
: values=off
$ amixer -c 1 cset numid=6,iface=MIXER,name='Mic Capture Volume' 14
numid=6,iface=MIXER,name='Mic Capture Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=16,step=0
: values=14
| dBminmax-min=0.00dB,max=23.81dB

again, store the settings so that they will be used again on a reboot:$ sudo alsactl store 1

Now we are ready to do a test recording, first check that arecord will see your card:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: U0x41e0x30d3 [USB Device 0x41e:0x30d3], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

and now for a ten second test recording, this will create a file called rectest.vav in your home directory. Remember to set the Device (-D plughw:1) number to the right card (card 1):

arecord -D plughw:1 --duration=10 -f cd -vv ~/rectest.wav the vv option displays extra information on the screen as well as a volume meter, this should be peaking at around 95% on the loudest sounds, if it is at 100% all a lot of the time then you are probably recording distortion. Playback the recording with aplay:

aplay ~/rectest.wav

the default settings will play the wav fie through the TV if it is connected by HDMI, To playback through the USB sound card set the device to the card number, like in arecord:

aplay -D plughw:1 ~/rectest.wav

Congratulations, you now have a fully working Pi Recording Device. Remember to experiment with the volume levels, too high and your recording will sound distorted.


  1.  Raspberry Pi Usb Audio fix (10 May 2013)

Python and the Oracle Client

Update 24 Nov 2015: Also, see my post Upgrading the Python Oracle Client for updating from version 11 to 12.

Installing the python cx_Oracle extension module for connecting to Oracle databases on this Fedora 18 workstation turned out to be a bit of a faff by giving an assortment of unhelpful error messages, if you are having the same pain maybe this will help.

You will need two files from the Oracle Database Instant Client download site, the basic client package and the SDK (devel): For Linux choose the correct flavour for your installed operating system: x86 or x86-64 for 64bit operating systems, you will need to register on the site to access the files. For my purposes I got the 11.2 version for x64 RPM files, install them using:
$ sudo rpm -i oracle-instantclient11.2-basic-
$ sudo rpm -i oracle-instantclient11.2-devel-

You will now need to tell the system where the libraries are:
$ sudo su
# echo /usr/lib/oracle/11.2/client64/lib/ > /etc/
# ldconfig
# exit

To install cx_Oracle you will need to set some environment variables otherwise you will get an “error: cannot locate an Oracle software installation” message. The easy_install program is found in python-setuptools I have included it in the recipe as a reminder if you have not installed it already.
$ sudo yum install python-setuptools
$ export ORACLE_HOME=/usr/lib/oracle/11.2/client64
$ export PATH=$ORACLE_HOME/bin:$PATH
$ sudo -E easy_install cx_Oracle

or by downloading the version from and installing it manually, you will still need to set the exports, as above, and do the following for installation:
$ sudo -E python build
$ sudo -E python install

The -E on the sudo takes your environment variables, including the three you just set, into your sudo session.

$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> exit()

When the the library cannot be seen, you get this error:
$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
File "", line 1, in
ImportError: cannot open shared object file: No such file or directory
>>> exit()