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  
  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...