Tuesday 11 September 2012

How to Control an A/C motor

A new post after looong silence....
What happened in the meantime?

Our big question was: Can we control the A/C motor of this sewing machine?

Who is 'we'? I have had the most amazing help from an electronics expert, Bernd, in this project.

To be able to 'hack this sewing machine' (make it do what we want), we need to be able to do three things electronically:

  • Sense the needle position
  • Sense/know/estimate the motor speed
  • Control the motor speed, switch the motor on and off.
We have a proof of concept that the sensing might work; for the past weeks, we have been puzzling over the big question: Can we control this motor?

... So... we actually disassembled the whole machine. The electronic circuitry is in a plastic box underneath the motor jammed tightly into the frame of the machine, obviously never to be disassembled, with good reason: Inside the box, we have mains power (240V A/C) oscillating.

So, if you undertake this, be sure to play it absolutely safe and disconnect all power sources:
And then,... and if you manage to open a plastic box that doesn't want to be opened without breaking it, then... you may find something like this:

On the board there are plenty of capacitors and resistors, a potentiometer, a diode, and more... and the foot control (not shown here) is a simple variable resistor, a potentiometer. All this electronic circuitry somehow makes the motor start, stop, and go slow or fast, depending on how the foot control is pressed.

So, we decided we have to put our controlling element where the foot control is.
We soldered some additional wires in... at the back, where all the soldering is done:
PCB with extra wires attached, from top
PCB with extra wires attached, from bottom
... and what did we attach at the other end? How can we control 240V A/C with 5V D/C from an Arduino?
It's this little thing:

an optotriac MOC3021, generously donated by Bernd. Inside, a LED activates a triac: when the LED is on, a the triac switches on. It allows to switch a high-power A/C current on or off with a low-power 5-V connection.
This is how we connect it:
... although in our first experiment, we didn't connect an Arduino up yet, just an arbitrary 5V power source.

Here is its datasheet. It tells us that the "static forward voltage" V(F) will be 1.2V at current I(F) = 10 mA -- under these conditions, the LED will switch fully on and activate the triac. The Arduino will send in 5V. We have to dimension the resistor so that, when switched on, the current in the LED's circuit will be 10 mA. Voltage is 5V-1.2V = 3.8V, R = V/I, and we find that we need a 380 Ohm resistor -- 390 is the closest we had at hand.

Again: CAUTION. Mains power is running on the one side of this circuit! We wrapped everything up tightly, like this, to run our test:
Sewing machine frame sideways, motor up, control board in its case, optocoupler circuit tightly wrapped in cardboard.
We connected a 5V power source, and... the motor started! We turned it off again, and the motor stopped! Concept proved!

So... what's next? To do this with an Arduino, and to switch the motor off over small intervals within its A/C power cycle interval to make the motor go fast... or slow. This is called Pulse Width Modulation (PWM). Can we do this using an Arduino?

Sunday 15 July 2012

First Arduino Connection

What we did with an LED, we wanted to do with an Arduino next. I used an Arduino Mega and wrote a little program to sense and estimate the machine sewing speed, based upon using the machine as a switch. The program is based on the "Button" example that turns on an LED when a button is pressed. It specifies the set-up as follows:

* LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.

This is the circuit for it. In my case, the sewing machine would constitute the switch... I won't insert that picture here without a big warning sign because Andy (thanks Andy) showed me why I should not do it like this.

Naive use of "Button" example circuit substituting the sewing machine for the switch
The sewing machine just makes a simple switch with its upper thread lever and the newly inserted PCB, but putting it into the same circuit like a normal switch is dangerous, because it isn't just a switch... it is itself an electrical instrument connected to mains power and, when so, most likely to ground. In case something goes wrong, and the lever is up/switch is closed, we might have a direct connection from the power source on the Arduino to the ground, with no resistor in between... a recipe for trouble.
We opted to put the (probably ground-connected)  machine on the safe side -- where the 'ground' connection of the Arduino is -- like this:

Circuit connecting the sewing machine as a switch to the Arduino

 When the lever is down/switch is open, the voltage on pin 2 is 5V. When the lever is up/switch is closed, the voltage on pin 2 goes down to 0V. The 'Ground'  connection on the Arduino and the possibly ground-connected machine are connected, but between them and the 5V power source is a resistor, avoiding a short circuit.

The Arduino program to sense the machine speed from this is a very simple modification of the Button example. It doesn't use interrupts to know when the lever position has changed, it just runs in a loop. It uses a very simple heuristic to try and eliminate noise:We know that the minimum length between stitches is 75ms, so it's making the assumption that any two "needle ups" that were sensed less than 40 ms apart are noise, and ignores those.

  State change frequency detection/sewing machine speed detection (modified from State change detection (edge detection) at http://arduino.cc/it/Tutorial/Button)  
  Application: sewing machine, assuming that signal "off" means "needle (or thread takeup lever) is up" and "on" means "needle is down"
  including a simple noise elimination algorithm that assumes a stitch must at least be 40ms in length, or else, the variation is just noise.
 modified Jul 2012
 by barbara

// this constant won't change:
const int leverPin = 2;     //the pin that the machine lever switch is attached to
const int ledPin = 13;      // the pin that the LED is attached to

const unsigned long minTimeBetweenStitchesMs = 40000; // minimum interval between stitches -- 40 ms

// Variables that will change:
unsigned long lastLeverUpTime;  // last time the lever was up
float stitchTime;               // length of current stitch

int leverState = 0;          // current state of the lever
int lastLeverState = 0;      // previous state of the lever

void setup() {
  // initialize the lever pin as a input:
  pinMode(leverPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:

void loop() {
  // read the input pin:
  leverState = digitalRead(leverPin);
  // compare the leverState to its previous state
  if (leverState != lastLeverState) {
    digitalWrite(ledPin, ! leverState);
    unsigned long changeTime = micros();
    // if the state has changed, increment the counter
    if (leverState == LOW) {
      // if the current state is LOW then the lever went from high to low
      unsigned long diffTime = changeTime - lastLeverUpTime;
      if (diffTime > minTimeBetweenStitchesMs)
        lastLeverUpTime = changeTime;
        stitchTime = diffTime/1000.0;
        Serial.print("stitchTime:  ");
        Serial.print(" ms, stitch frequency:  ");
        Serial.print(" Hz, sewing speed: ");
        Serial.println(" stitches per minute");
  // save the current state as the last state, 
  //for next time through the loop
  lastLeverState = leverState;

Here is some example output:
... as can be seen, the stitch length estimated by the program varies quite a bit -- actually, upon every other stitch -- whereas we know from a look to the oscilloscope that the stitches are quite uniform in length and frequency. This is probably due to the lever bouncing a few times on and off as it hits the PCB during each stitch. We will either have to improve the smoothing algorithm or the input signal.

Sunday 8 July 2012

First Success: Needle Position Sensed!

In my post on the 30th of June I said: The most interesting position of the entire machine cycle we want to know about is when the needle is up and the upper thread is drawn out of the fabric as much as possible. This would be when the thread take-up lever is highest. Again, let's have a look at the arm that guides this lever:
Thread take-up lever up -- optimal position to sense (and later stop the machine in) The needle shaft is up but the thread take-up lever is down -- so even if we could sense this position, it wouldn't help us much.
As it turns out, all metal parts of the machine are connected, including the metal frame and the lever. We used a recycled piece of scrap PCB board just above the lever mounted on a bolt with a few nuts and washers to make the entire machine a simple electric switch.We soldered some of the exposed contacts of the board together to make a larger contact area.
For the firs experiment, we just connected an LED that will light up whenever the lever is "up".

A little bit of scrap PCB, a bit of soldering lead, an LED, a resistor, a power source....
The lever is down, it doesn't touch the board, and the LED is off.
The lever is up and touches the board, closes a circuit and... voilà ! The LED is on!

And this is the entire set-up in motion:

Simple circuit for the very first
machine rotation sensing solution.
The machine itself constitutes the switch.
 We also connected an oscilloscope and noticed two things:
  • At maximum speed, the signals occur 75 ms apart. This means the maximum speed of the machine is 13.333 Hz (not 10 as I had estimated), so it can do 800 stitches per minute.
  • We get vibration effects which get worse at high speeds: As the lever hits the board, it bounces off a few times. This, e.g., distorted the frequency estimated by the oscilloscope and will be a problem for any electronic equipment we will want to connect. Bernd suggested using a capacitor to alleviate this problem, but I will have to learn how...

Saturday 30 June 2012

Where to Start

The first step will be: Just to fit a sensor somewhere, then hook it up and see whether we can actually sense the position of the needle or the position within the rotation cycle of the machine.

Then, feed that measurement into an Arduino.

Today I've taken some better pictures.
And I have had a lot of help -- thanks Bernd. Thanks everyone for their input. Thanks Unknown, for the suggestion of the light or magnetic sensor.
You said: I could put a sensor in the middle of the bed shaft.

It sounded a good idea, but when I opened the machine today, I noticed that it won't be possible, because the (rotating) bed shaft is too tighly nudged into a corner of the cast-metal frame of the machine. There is just no space around it. The flat band in the foreground is reasonably free, but it is not the shaft, it is part of the control mechanism for the feed dogs.

Thanks to Bernd who took a lot of time with me to evaluate different options.
We thought for a long time to attach a sensor that would sense the up-and-down movement of a lever somehwere in the actual vicinity of the needle movement.
These are two close-up pictures of the different phases of the needle movement. Points A, B, and C were all discussed as anchor points for attaching a sensor -- or a magnet which is sensed by a magnetic sensor.
Admittedly I have the feeling it's much too crowded there, and there is hardly any space to put anything in that wouldn't interfere with the existing mechanism.

But I have found out that not any old "up"  position of the needle will do. The needle is high enough "up" over easily 1/4 of the whole rotation of the shaft, but over most of this, the upper needle thread is still caught somewhere within the guts of the bobbin mechanism, making a turn around the bobbin to wrap itself around the bobbin thread and create a stitch. In this position, if a seam has just been sewn, the fabric cannot be pulled out of the machine because the threads are still caught in the mechanism. That means that the motor has to stop at indeed one very particular point of the cycle, which appears to be the time when point A is highest.

Unfortunately, then, I can't see how we would be able to sense both the "up" and the "down" position of the needle -- because both are needed variously.

So far we mostly thought about two candidates for machine rotation sensing solutions:

Sensing Solution 1

What Unknown suggested might actually work on the (top) arm shaft:

Please apologise the very bad drawing.
There is a little bit of space within the frame around this shaft.
  • Fix a disk with one or more gaps in it around the arm shaft. It can probably not be slid on, but will probably have to be made in two parts and glued together around the shaft.
  • Fix a light sensor to the frame that reaches around the disk and can sense the gap going past. This would probably be a photo interrupter like this or this.
  • The radius of the disk would have to be small, and so, a relatively wide angle out of the whole rotation would be considered as "signal"/"up". 
  • The gap in these sensors is only 3 mm wide, so the disk would have to run very true, even though it has to be fixed in 2 parts around the shaft.

Sensing solution 2

At the back end of the machine, where the big drive wheel picks up the rotation from the motor, there is a little bit of space that might be of interest:

  • Attach some reflective material to the smaller drive wheel, best at the spot which is at the top when the needle is in the optimum "up" position. It is of advantage that the wheel is originally black.
  • Fix a bracket to the frame above the top and attach a light sensor like this that senses reflected light.
  • A second bracket with sensor could be fixed underneath the wheel and sense when the needle is in the "down" position.
Main disadvantage: The brackets -- they'd have to have very particular, exact shapes to hold the sensors at the right places. Here is a picture of the same thing from the top:

Motor Control

Different to what I wrote before, the machine easily does 10 stitches per second, so 600 per minute, which would mean the motor then probably turns 8 times as fast, at 4500-4800 per minute.

I was told that it would be difficult to tell a conventional motor to stop that precisely at a certain point. I'll take my chances and try...

We had a look the foot control of the machine and found it contains a potentiometer that seems to range between 0 and 100 kΩ.

Thanks very very much to Bernd for all his help, assessments, and suggestions.

Wednesday 27 June 2012

Hacking my sewing machine

The Machine

This is my sewing machine.

It's quite a nice one, if a bit old (I have had it for 16+ years). The plastic is a bit worn, and of course, these days there are much better machines around -- but it still has some interesting features, e.g.adjustable foot pressure. It is called "electronic", and the claim is that it somehow has an electronic sensor that senses material thickness. It's certainly true that it can sew through very bulky fabrics with no problem.

My hand wheel needed replacing, the hand wheel on this photo is black because it was printed with a 3d printer (thanks Dave!) (I think it was Dave).

But I had never seen another one exactly like this around until I walked into the hackspace the other day, where someone has donated one of exactly the same model!

How does a sewing machine sew? If you are not familiar with sewing machines -- before I waste a lot of space here, there is an excellent Wikipedia article here.

This machine here works with an (I think) potentiometer inside the variable-speed foot controller: You leave it alone, and the machine stops; you press it, and the motor starts to run and the machine sews, the more you press it down, the faster.

Maximum speed on this one is about, oh, say, 300 stitches per minute or 5 Hertz, but it could also be twice that or half that. I had said "300 RPM of the motor", but Holger has pointed out that of course the drive belt introduces a translation between the motor and the rotary wheel, so the motor would be running faster, at maybe 2000-2500RPM (assuming the ratio is 1:7 or 1:8).

The machine sews forward by walking the fabric forward with the feed dogs against the presser foot. This one has two different modes for the feed dogs: in one, they just make longer or smaller steps, and the stitch length is adjustable from 0 to about 6 mm, or can even go backwards. This creates straight seams or, if the sideways position of the needle is varied, different patterns, like zig-zag and more.  In the other mode, the feed dogs always go 2 mm forward - 2 mm forward - 2 mm backwards (repeat). This creates elastic seams, good for elastic fabrics.

Andy invited me to open up the Hackspace machine to look what's inside... so here it is!

(Please excuse the lousy picture quality, this was taken with my age-old dumbphone).

A pulley and drive belt takes the rotation from the motor, and a system of shafts, gears with different translations, cams, and more pulleys moves the feed dog mechanism, the needle and thread take-up lever motions, and the stitch cams in unison. (The stitch patterns are of course done with cams. There is a better picture of those below).

The Problem


Why does the machine need a hand wheel at all?

As you press the foot controller, the machine starts sewing. As you release it, it stops. And the needle will stop in whatever position it just is.

And that is a problem. More often than not, the needle will be in a downward or half-way position somewhere, and the fabric can not be pulled out. So in most cases, what you want is for the needle to be in an "up" position when the machine stands still.

Also, that's the only position where the needle can be threaded, of course.

But sometimes, you particularly want to lock the fabric in place with the needle, e.g. to lift the presser foot and turn the fabric, to sew corners. I that case, the needle should stop on a "down" position.
Very good (and expensive) modern machines have such a function, where you can define what position the needle should be in when sewing has stopped.

So, I imagine a function like this: the current electricity supply needs to be altered. When the foot control is pressed, it should work like before and let the motor run faster or slower according to the amount of pressure on the foot control. But when the foot control is released, it should not immediately stop the motor, but turn it just ever so little further until the needle is either in the "up" or "down" position.

The interface for that could be two buttons, one for "up" and one for "down" -- possibly on the cable that leads into the machine, but most elegantly, of course, incorporated into the body of the machine.

But when I've got that, I have the feeling that I wouldn't want to stop there -- if I already could control the motor rotations independently of the foot control, then I might also want to do something like: "Sew exactly 3 stitches, then stop" (if sewn in place, this, e.g., would make a knot, so the thread can then be cut off neatly and the seam won't come apart again). Or, to sew a buttonhole: "sew exactly 20 stitches, then stop". (I haven't thought about the interface for that yet).

Here are two pictures of the rear end of the machine. The electric circuitry is just in a little black box at the bottom which I haven't opened yet. One cable goes to the light and just supplies electricity whenever the machine is switched on. The other cable goes to the motor, but here, the flow of electricity is obviously controlled by the foot control.

It could be a second issue to replace the light with an LED light, and then the machine wouldn't draw 25W any more when switched on, but maybe 2 or 5 or so only.

So what will I need?
  • Something to sense whether the needle is up or down.
  • Something to control the electricity supply accordingly.
  • Some kind of interface for that.
  • possibly, a LED light and socket of the right size, and possibly a transformer for it.
... and all of that has to be  in a size so it fits into the very narrow space inside the machine...
... and all of that has to be affixed somehow to existing parts of the machine.

And here I am inviting some brainstorming!

Andy has suggested installing a sensor with a light barrier at the top pf the needle shaft to sense whether it's up or down.

Here is a sequence of  pictures of the different phases the needle shaft goes through with the rotation of the motor.

The problem I see there is: This would need two light bariers, one at the very top of the highest point of the needle shaft, to only fire when the needle is really high up, and another just above the top of the needle shaft at its lowest point, to fire when the needle is really, really down.

Others have suggested a rotation sensor.
I figure one could be put in at the end of the bed shaft.

I can show you how to sew ballgowns and T-Shirts, but I haven't done much like this yet. I don't know where to get either a light-barrier sensor or a rotation-count sensor and how to get one that fits in that space... and how to attach it.

I have no idea what kind of control mechanism to put in! a chip? an Arduino?  And could that fit into the machine?
I know we have a drill press, a lathe, a laser cutter and a 3d mill in the hackspace, not to mention several 3d printers...

Any comments, suggestions and ideas are welcome.

Thank you for reading this far!

Tuesday 26 June 2012


Of course I have more dreams for the future...
like this:
... attaching an embroidery frame to the machine, then controlling that with two or more stepper motors like the mill, like the laser cutter, coordinating that with the stitch control that was already put in, and  this way, make it an embroidery machine...
Look what these guys have done. (They have made a quilting machine, not an embroidery machine). (They are moving the machine, not the frame).
But these are just dreams for the future...