Ethernet Controlled Graduated ND Filter

Noah Allen bio photo By Noah Allen

Problem:

So, after the excitement of fixing the shutter jitter mentioned in the ‘Reverse Engineering the D750 Shutter for Remote Control’ project, I was able to take a nice looking SSPC signal from 3 samples but I noticed something interesting. Ideally, the SSPC signal below should only show slope increases when the incident energy is enough to excite trapped charge into the conduction band or out of the valence band but it seemed that the signal could be perturbed by some of the peaks in my 350W mercury source. In order to publish the data this needs to be fixed so that I’m not trying to make conclusions based on faulty data.

Solution:

As always, I checked for an existing solution that may be within financial reach but once again found they were much too expensive. The only thing I needed to do was control how much light was able to pass into the end of a fiber optic and quickly found that Newport and ThorLabs sold a circular and rectangular graduated ND filter. If possible I would have liked to use the circular one since attaching it to the end of a stepper motor would have been very easy but they were VERY expensive (>$1K) compared to the rectangular ones (~$114). Since the optics had been selected I only needed to figure out how to slide it in front of the light path. Having read a lot about 3D printers and how open source they have become, I decided to invest in a cheap ($75) stepper controlled linear actuator which I could mount the rectangular ND filter on and then slide in front of the optical path. Below is the linear actuator with the ND filter mounted.

Once I had the ND filter mounted it was time to setup the hardware so that it could easily be controlled by a LabVIEW program I had running on our main characterization computer. To control the stepper motor I first tried to use a generic L298N board I bought from eBay but quickly learned that an H-bridge wasn’t the most efficient way to control a stepper mainly because there was no circuitry to limit the current through each of the stepper coils. This resulted in saturating the coils with a 12V supply when the motor wasn’t moving. After a little research the DRV8825 Polulu clone from Amazon would be the best solution as far as price and usability. An Arduino seemed like the most sensible choice for quick prototyping but I wasn’t comfortable running a long (>30ft) serial cable across the lab so I decided to acquire a generic ENC28J60 board for Ethernet communication. Below is the circuit I used to interface all three boards boards along with the stepper motor. NOTE: The ENC28J60 is 5V tolerant but requires its own 3.3V power supply. I used two Buck converters to transform the 12V/2A wall wart to 3.3V and 5V.

The Arduino was programmed to act like a TCP server (using the UIPethernet library) which could be connected to by a LabVIEW running a TCP client program. The client connects and sends a short string which the Arduino server would interpret and either move or return requested data (Arduino program attached at the end). The next step was to check the range of attenuation I could achieve with my new apparatus. I set the wavelength to 435nm (one of the larger peaks in an Hg lamp spectrum) and moved the ND filter while taking optical intensity measurements. The intensity vs. motor step is shown below.

Finally, I created the algorithm to calculate the motor steps away from the limit switch which would give the correct attenuation value and once again measured the spectrum of the Hg lamp. As you can see below the photon flux at each wavelength is much more consistent but a lot of light power is lost.



Arduino Code

The Arduino code is long but pretty simple. I decided that the speed of moving the ND filter did not matter as much as the resolution so I chose to micro-step at 1/32. The first piece of code I wrote was to reset the stage to a known location. To accomplish this I quickly stepped the motor until I hit the limit switch at which point I SLOWLY micro-stepped off of it until it was no longer depressed and set the current location to ZERO. Next, I manually moved the stage to a few steps prior to hitting the far side and then I would run the ResetMotor() function to check how many steps it would take and found that 138,000 steps was the maximum I could go before hitting the other side. When testing for backlash and motor slippage I found that if I moved the motor <500 steps at full speed it would miss a few so I wrote a check in the MoveSteps() function that would move the motor a little slower for small motor movements. As for the Ethernet code, I basically used the UIPEthernet TCPserver example to setup the ENC28J60 to receive TCP packets and parse the data portion into a String. From there I just setup a few command strings to be recognized (GT, CS, RS, and EN) and then parsed accordingly (ex. GT120000(CR) would go to step 120,000). NOTE: The only way I could get LabVIEW to connect to the ENC28J60 was if I gave it an IP address VERY close to the computer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Definition of all of the Ethernet Code
 *  ***********************************************************************
*/
#include <UIPEthernet.h>

EthernetServer server = EthernetServer(1000);
/*
 *  ***********************************************************************
*/


/* Definition of all the Stepper Code

*/
const int SLEEP = 2;

const int MODE0 = 3;
const int MODE1 = 4;
const int MODE2 = 5;

const int DIR = 7;
const int STEP = 6;

const int LIMIT = 8;

int stepMode = 0;
long currentStep = 0;

long MaxStep = 138000;
long MinStep = 0;

/*
 *  ***********************************************************************
*/

String MessageStr = "";


void setup()
{
  Serial.begin(9600);
  /*
   *  ***********************************************************************
  */

  Serial.println("Serial Begin");
  uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
  IPAddress myIP(169, 254, 237, 145);

  Ethernet.begin(mac, myIP);
  Serial.print("localIP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: ");
  Serial.println(Ethernet.dnsServerIP());
  server.begin();
  Serial.println("Server Begin");
  /*
   *  ***********************************************************************
  */
  pinMode(MODE0, OUTPUT);
  pinMode(MODE1, OUTPUT);
  pinMode(MODE2, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(STEP, OUTPUT);
  pinMode(LIMIT, INPUT);

  digitalWrite(DIR, LOW);
  digitalWrite(STEP, LOW);
  digitalWrite(SLEEP, HIGH);
  stepMode = 5; // 1/32 Steps
  SetMode(stepMode); // 1/32 Steps
  Serial.println("Starting Motor Control");
  ResetMotor();
  /*
   *  ***********************************************************************
  */


}

void loop()
{
  size_t size;
  String MessageStr2;

  if (EthernetClient client = server.available())
  {
    while ((size = client.available()) > 0)
    {
      char* msg = (char*)malloc(size);
      size = client.read(msg, size);
      for(int i=0;i<size;i++){MessageStr = String(MessageStr + String(*(msg+i)));}
      free(msg);
    }
    Serial.println("Going to Parser");
    Serial.println(MessageStr);
    ParseString();
    client.println(MessageStr);
    Serial.println(MessageStr);
    client.stop();
    MessageStr = "";
  }else if(Serial.available()!=0){
    MessageStr = Serial.readStringUntil('\n');
    ParseString();
    Serial.println(MessageStr);
    MessageStr = "";
  }

}

void ParseString(void) {
  String Message = MessageStr;
  //Serial.println("In Parser");
  //Serial.println(MessageStr);
  int StrLen = Message.length();
  String strType = Message.substring(0, 2);
  String strInfo;
  if(StrLen>3){strInfo=Message.substring(2, StrLen - 1);}else{strInfo=String("Null");}
  Serial.println(String("Input Length:"+String(StrLen)));
  Serial.println(String("Input Type:"+strType));
  Serial.println(String("Input Info:"+strInfo));

  if (strType == "GT") {
    //GT stands for GoTo step
    Serial.println("GoTo Step");
    currentStep = GotoStep(strInfo.toInt());
    //Serial.println(strInfo.toInt());
    MessageStr = String("CS" + String(currentStep));
    return;
  } else if (strType == "RS") {
    //RS stands for reset the motor position
    Serial.println("Resetting Motor");
    MessageStr = String("RS" + String(ResetMotor()));
    return;
  } else if (strType == "EN") {
    //EN Stands for enabling and disabling the motor
    Serial.println("Setting Motor Enable");
    if (strInfo.toInt()) {
      digitalWrite(SLEEP, HIGH);
      MessageStr = "ENABLED";
      return;
    } else {
      digitalWrite(SLEEP, LOW);
      MessageStr = "DISABLED";
      return;
    }
  } else if (strType == "CS") {
    Serial.println("Motor Position");
    //CS stands for current step
    MessageStr = String("CS" + String(currentStep));
    return;
  }else if (strType == "??") {
    MessageStr = String(F("\nList of Commands:\nGT------(CR) | goto step where '-' is a number between 0 and 138000\nRS(CR)       | Reset Motor [returns # of steps from previous location]\nEN-(CR)      | where '-' is 0 or 1 for disable or enable\nCS(CR)       | Returns the current step [CS------]"));
    return;
  }
}

/* All of the Code to run the stepper
 *  ***********************************************************************
*/

void SetMode(int MODE) {
  switch (MODE) {
    case 0:// Full Step
      digitalWrite(MODE0, LOW);
      digitalWrite(MODE1, LOW);
      digitalWrite(MODE2, LOW);
      break;
    case 1:// Half Step
      digitalWrite(MODE0, HIGH);
      digitalWrite(MODE1, LOW);
      digitalWrite(MODE2, LOW);
      break;
    case 2:// 1/4 Step
      digitalWrite(MODE0, LOW);
      digitalWrite(MODE1, HIGH);
      digitalWrite(MODE2, LOW);
      break;
    case 3:// 1/8 Step
      digitalWrite(MODE0, HIGH);
      digitalWrite(MODE1, HIGH);
      digitalWrite(MODE2, LOW);
      break;
    case 4:// 1/16 Step
      digitalWrite(MODE0, LOW);
      digitalWrite(MODE1, LOW);
      digitalWrite(MODE2, HIGH);
      break;
    case 5:// 1/32 Step
      digitalWrite(MODE0, HIGH);
      digitalWrite(MODE1, LOW);
      digitalWrite(MODE2, HIGH);
      break;
    default:// Full Step
      digitalWrite(MODE0, LOW);
      digitalWrite(MODE1, LOW);
      digitalWrite(MODE2, LOW);
  }
}//END SetMode

long ResetMotor(void) {
  Serial.println("Inside Motor Reset");
  long i = 0;
  digitalWrite(DIR, LOW);
  delay(500);
  while (digitalRead(LIMIT)) {
    digitalWrite(STEP, LOW);
    delayMicroseconds(3);
    digitalWrite(STEP, HIGH);
    delayMicroseconds(5);
    digitalWrite(STEP, LOW);
    delayMicroseconds(3);
    i++;
  }
  digitalWrite(DIR, HIGH);
  delay(500);
  while (!digitalRead(LIMIT)) {
    digitalWrite(STEP, LOW);
    delayMicroseconds(3);
    digitalWrite(STEP, HIGH);
    delayMicroseconds(5);
    digitalWrite(STEP, LOW);
    delayMicroseconds(3);
    delay(1);
    i--;
  }
  //Serial.print("BackFrom: "); Serial.println(i);
  currentStep = 0;
  return i;
}//End ResetMotor

void MoveSteps(long numSteps) {
  int Delay=0;
  if(abs(numSteps)>500){Delay=0;}
  else{Delay=2;}
  if (digitalRead(LIMIT) && (numSteps < 0)) {
    digitalWrite(DIR, LOW);
    for (long i = 0; i < abs(numSteps); i++) {
      digitalWrite(STEP, LOW);
      delayMicroseconds(3);
      digitalWrite(STEP, HIGH);
      delayMicroseconds(5);
      digitalWrite(STEP, LOW);
      delayMicroseconds(3);
      delay(Delay);
    }
  } else if ((numSteps >= 0)) {
    digitalWrite(DIR, HIGH);
    for (long i = 0; i < abs(numSteps); i++) {
      digitalWrite(STEP, LOW);
      delayMicroseconds(3);
      digitalWrite(STEP, HIGH);
      delayMicroseconds(5);
      digitalWrite(STEP, LOW);
      delayMicroseconds(3);
      delay(Delay);
    }
  } else {
    currentStep = 0;
    return;
  }
  delay(500);

}//End MoveSteps

long GotoStep(long Step) {
  if (Step > MaxStep) {
    Step = MaxStep;
  }
  if (Step < MinStep) {
    Step = MinStep;
  }

  MoveSteps(Step - currentStep);
  currentStep = Step;
  return Step;
}