Tue 3 Mar 2009
Using an Atmega8 2x ULN2803′s and a “Magic Screen” device I made a CNC 2 axis machine.

CNC Magic Screen Machine Project Page
A few weeks ago I started this project knowing almost nothing about CNC, steppers, or controlling them w/ a microcontroller. I have learned so much in that short time and have a lot more to learn.
I chose to do a version of etch-cnc because it looked really simple. In that version it was an open-loop controller and just used 2 uln2803‘s to control 2 steppers. These steppers were driven by the parallel port on a pc using Axis in EMC2.
Steppers are super easy to control. The unipolar ones that a friend gave me have 4 coils. You give them voltage on 1 coil and it snaps to that coil. Give power to the next one and it moves clockwise or counterclockwise depending on the order. This is called a stepping sequence. If you know the stepping sequence for a motor, good. If not it is easy to figure it out.
Next is trying to get a computer to talk to a stepper. Since the steppers I was using were rated at 24V and can draw a considerable amount of current, I learned that you need a driver chip to help handle the current. That is where the uln2803 comes in. It has 8 channels of a “darlington array.” Each channel on the chip can handle 500ma. Following the example from etch-cnc I combined 2 channels for each step.
My first experiment was hooking up a ATMega8 and just having it send step pulses. It worked with only a handful lines of code. Now the fun part. Hook it up to a machine running EMC2 software w/ the parallel port driver. It turned out not so fun. Based on what I saw on the etch-cnc page it looked like you could have EMC2 send the step pulses over the port using pins 2-10. That is not how it works.
It works by using the parallel port to send step and direction. Example X axis uses pin 2 to indicate step and pin 3 to indicate direction. So every time the computer wants the stepper to step it puts a short pulse on pin 2 and sets the pin3 high or low depending on the direction it wants it to go. Easy enough to solve. I put the ATMega8 back in the loop. Connected 4 ports to the motor. Connected pin 2 to one of the interrupt pins and pin 3 to one of the portD pins. Then wrote a little ‘C’ code that setup the interrupts and told it that each time a rising edge interrupt happens to read the status of direction and do a step in that direction. The steps are done round-robin style.
Great, now after running the EMC2 config generator I could actually get it to step forward and backward w/ the software. Now I just added the code and wiring to do one more stepper for Y axis.
Then I did a config for a 2 axis machine. Well that didn’t work. Most of the g-code files included as samples expected a 3 axis mill. So I just told it that it was a 3 axis mill. I just ignore the stuff for the 3rd axis.
Now I started Axis (the program that outputs g-code to the parallel port in EMC2) and hit run. When axis starts it loads a little default file of g-code. It did not work as I expected. Then I noticed a comment in that default file that said that that file was not good for milling. So I loaded the spiral sample g-code and hit run. Worked perfectly. Almost.
I had to figure out the lead screw ratios and maximum step rates and such to make what was on the screen come out to the machine. Through a little trial and error I got it pretty close and the settings I used are on the Project Page.
I presented what I had to The Robot Group.
Next I started the cleanup to make the project presentable. I re-did the breadboard onto a soldier type board. I used the “ATMEGA8 Development Kit” by ProtoStack. This is the first time I used a pre-prepared board for an ATMega8 project. This board made it unbelievably simple for me to wire the project up and make it permanent. The board has many features that helped make it go faster. The first of which is that the ATMega8 has some pins scattered around and not in sequence. This board brings them all out away from the chip and puts them in order ie. PC0…PC7 all in one little row.
In the past I had always transferred my projects to perf-board w/ plated holes using bus wire to make all the connections on the back. It is difficult because the ATMega8 needs 2 sets of power to the chip… and for some reason they are reversed on either side of the chip. It is frustrating. But by using the ATMega8 Development kit I didn’t have to worry about any of that. The board has 2 really nice power planes and all the reversing directions have been taken care of for me. A few other features of the board that were helpful to the project were: Large prototyping area. Since I am using 2 unl2803 chips I needed the room. Built in reset button. I have a POST in the software and just hitting a button to run it is nicer than plugging and unplugging. Built in ISP programming header so I can change the chips brain.
Since I wanted to learn about CNC from the is project, part of the equation is Going from idea to output. The whole CAD/CAM thing. For the demo I did at the robot group I wanted to take the group logo and put it on the magic screen. I loaded the logo as a bitmap into Inkscape and traced it by hand keeping in mind that the CNC MSM cannot lift the pen. So I had to trace it in one continuous path. I exported it to dxf using “Better DXF” plugin for inkscape. Then converted the dxf to g-code using “Über DXF2GCODE.”
The last thing I did for the project was take the whole prototype to a friend who is really good at making cases more elegant. He trimmed it down, painted it, and reduced some of the wooden parts to get the final version of the setup.
The CNC Magic Screen Machine Project Page has a video and some shots of the prototype.
Also see more description of the building of the machine.
Thanks again to all the people who helped me w/ this project, Nyssa, Paul, Vern, Bob, and Rick.
May 16th, 2009 at 7:12 am
This is so cool. I’d like to know how to just control a single stepper to start with. Can you help us?
I’ve got got the ATMEGA8 Development Kit with the USB cable from protostack.
May 16th, 2009 at 11:02 pm
The first thing to do is make sure you know the stepping sequence.
I follow this guide:
http://wwwhomes.doc.ic.ac.uk/~ih/doc/stepper/control2/sequence.html
Then it is just a matter of hooking it up to the atmega8. For simplicity sake lets say coil 1 is hooked to PC1, coil2 to PC2 and so forth. But it is a bad idea to hook them directly as the current draw for the motor may exceed the capacity of the chip. That is why in the cncmagicscreenmachine I used a uln2803. (I personally have hooked small ones directly up to a atmega8 it worked for a while but it killed the atmega8 after a few minutes.)
Then do software that looks something like this:
#include
#include
#define F_CPU 8000000UL
#include
int main(void)
{
DDRC=0b00011110; // set pc1-pc4 as output
while(1)
{
PORTC=0b00000010; // pc1 on – all else off
_delay_ms(100);
PORTC=0b00000100; // pc2 on – all else off
_delay_ms(100);
PORTC=0b00001000; // pc3 on – all else off
_delay_ms(100);
PORTC=0b00010000; // pc4 on – all else off
_delay_ms(100;
}
}
// note the above code is from memory and may or may not work…but it should be close
May 17th, 2009 at 7:18 am
Thank you so much for the response.
Will this work?
http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=296-15777-1-ND
I’ll use 9VDC steppers.
Can I use a multibester then a led to check if the code works.
I’m developing using linux (Gentoo and Fedora C9).
First of all I’m a noob to the ATMEL AVR. All I’ve done in this area is write a simple driver in java and python that calls a routine I wrote in “c” to call the parallel port and make motors rotate with an H-Bridge. I call memory location 378 using lptout (0-255).
May 17th, 2009 at 12:53 pm
If you are using an h-bridge to move the motors then you can do the same thing by hooking it up to the Atmel.
When I mentioned the uln2803 I was assuming you were using a unipolar stepper motor.
When using a bipolar stepper you do need to use the h-bridge.
It would be hooked up something like this
http://i40.tinypic.com/2yye2wx.jpg
I’ve never actually used a h-bride and bipolar setup myself. But I believe that should work.
The code would be the same.
Let me know if you get it working.
May 17th, 2009 at 8:19 pm
Thank you so much! Give me a week or so. I still need to install the drivers for the usb Atmel interface. Hopefully that will go smooth.
May 18th, 2009 at 8:33 am
What Operating System are you using. I downloaded a bunch of drivers for Linux and I’m not sure where to start. I’m using the protostack USB of course.
As for motors. I’m not sure what type of steppers I have.
May 18th, 2009 at 10:12 pm
I use the stock avrdude that comes with Ubuntu 8.04.
When you use avr-dude with an AVR ISP MKII you don’t need any drivers that I am aware of.
There seemes to be a fairly complete tutorial for working in various flavors of unix for AVR development at Lady Ada
http://www.ladyada.net/library/avrdevtut/setup-unix.html
Also make sure that you get an account at AVR Freaks.
They have lots of tutorials.
http://www.avrfreaks.net/
For determining the types of motors that you have see this page:
http://www.doc.ic.ac.uk/~ih/doc/stepper/others/
August 8th, 2009 at 8:19 am
Nicely done! note that on the Stepping Sequences page that you reference the single coil sequence is often called wave stepping. The 2 coil sequence is often called full stepping. With full stepping you will find that the maximum speeds increase dramatically. Torque will be Way up and vibration will be less. The half step method is also great to reduce vibration.
I love how you integrated several software packages and the hardware. Also I appreciate how well you documented your project.
Thanks
Kirk