Subscribe via RSS
12May/100

Multiplexing ‘Photodetectors’ to detect train occupancy.

Right, I wasn't impressed whilst using the Sharp distance detectors and so went back to the age-old method of light-detection between the sleepers. As this is N Scale, I didn't want the standard, large and bulky Light Dependent Resistors and went for these smaller 'Photodetectors' found on eBay from a Taiwanese reseller.

Single detector

These were chosen based on the fact that they have a flat lens/front and are clear. They fit nicely between sleepers of Tomix FineTrack and, since I'd already laid and ballasted my main loop, could be retrofitted by drilling up and through the base.

Track wired upRear of detector
Detector between sleepersDetectors between sleepers

Now, since I bought these in bulk, I started going crazy and sticking them everywhere I could. The goal was to put one everywhere that would become a good trigger-point for automation. I started with all of my stabling areas and put one at the start, middle and end of the sidings. I would use the 'trigger' from these to know when to slow to an engine to 50% throttle, 25% throttle and then stop. I then also put some in the tunnel entrances, station/platforms and also where signals should probably be (around points.)

Detector installed and ballastedAnother installed detector

It started dawning on me that I would require one analog input pin on my Arduino per photodetector. This would've gotten very expensive very quickly, but then I remembered that there was a simple tutorial on multiplexing analog inputs on the Arduino Playground (based on the 4051 IC). This IC would save me a lot of time and resources: with a little more wiring it could potentially give me 64 analog inputs for a total of 6 digital pins and one analog.

Here's the basic idea of wiring up a single 4051.
Basic Multiplexer with Photodetectors

Here's how you can use multiple 4051s and reduce pin consumption:
Advanced Multiplexer with Photodetectors

Notes on the options in the above image:

  • Option 1: Take the wires in the first rectangle and wire them to one analog pin and three digital. This will give you a total of 8 detector inputs.
  • Option 2: Take the 8 analog wires and put them into analog input pins. You then also need to connect up the 3 digital pins. For all inputs you'll only ever need 3 digital pins. But for the analog pins you'll need 1 pin for each 8 inputs. (i.e. 8:1, 16:2, 24:3, 32:4, etc... there is no upper limit, as long as you have the analog inputs.)
  • Option 3: Take the single analog pin and then the 6 digital pins. This will give you a total of 64 inputs and will use more digital than analog pins.

As you can see, you can interface with a lot of analog detectors, based on what pins you have available. As you may be aware, analog inputs are more 'expensive' on the Arduino than digital outputs as there are less available.

The process to control the above circuit is to set the digital pins to the desired address and then read the analog pin(s). You then need to set the next address and read the same pin (depending on your setup.) As changing through a lot of inputs and reading can take time, you need to be careful how many detectors you end up implementing. I have no exact numbers; but reading 64 inputs can easily be done in under a second. The goal is to make sure that a train does not pass a detector before it has been read!

So we have our detectors installed and circuitry built; we could now write software to manage it all. The basic idea was to read the value, adjust the min/max of that single detector and then check if it exceeded a threshold. Since these detectors required light to function, they would be effected by the amount of ambient light in the room and therefore the code would need to be smart enough to work out what was 'covered' (i.e. vehicle blocking light) and what was 'open'.

This code was also noted in my previous post where I used the Sharp detectors. These detectors produced a lot of noise and had to be filtered so that my code wouldn't simply trigger when a high/low value broke a threshold.

Here is the basic idea for reading one detector:

 read value of detector
 if (detector value is greater than recorded maximum) then record new maximum value
 if (detector value is lower than recorded minimum) then record new minimum value
 if (either min or max has changed)
  then update range of this detector [max - min]
  adjust threshold [max - (range*0.25)]
 end if
 if (detector value is greater than threshold) then
  report that this detector is 'active'
 else
  report that this detector is 'inactive'
 end if

Right, so the above concept uses a 25% threshold below the maximum-read-value to see if the value read from the detector is 'active'. It is also constantly updating it's valid reading range so that it can adapt to the environmental changes. The main issue with this concept is that if the environment drastically changes (lights are turned on/off, curtains opened/closed, etc...) then this code would not adapt, as it never has a chance to 'retract' the limits. Therefore the following adjustment needs to be made:

 store the last 32 values of detector in circular array
 read value of detector and push last into array, popping off the first value
 find the lowest value in the array and store as the minimum extremity
 find the highest value in the array and store as the maximum extremity
 if (either min or max has changed)
  then update range of this detector [max - min]
  calculate the average from the last 32 read values
  adjust threshold [average + (max-min*0.10)]
 end if
 if (detector value is greater than this threshold) then
  report that this detector is 'active'
 else
  report that this detector is 'inactive'
 end if

Here you can now see that we only care about the last 32 read values (instead of the max and min since the code was running.) We are also using a new threshold calculation: 10% above the average of the last 32 values. This therefore means that we will receive an active notification if the value increases 10% above the 'stable' value of which we have been observing.

Of course, we are always able to introduce new issues; the above code, if run at processor-speed will read 32 values in a under once second and, dependent on environment changes, may well not be able to cope. We therefore need to only test the detector at a specific interval (your mileage (kilometre'age) may vary!) of say, 100ms. This then means that the 32 values are taken over the course of 3.2 seconds. If this doesn't suit, then you can also increase the buffer size or decrease the polling delay.

But I bet you haven't seen the main issue? If a vehicle is stationary on the detector for too long then the range will/should drop to zero and therefore the detector will always be 'active'.
Wait, that would be correct? Wouldn't it?
It would, but it would also then report active for a certain time span until the range had expanded again once the vehicle had moved on. Note this can also be simulated by a long train traversing the detector and blocking the light (even with intermittent gaps of light) for a long period of time.

To prevent this? Adjust the polling delay and the buffer size...

Another good trick for limiting environmental effects is to add lights/LEDs to your layout around the detectors to ensure they always have a good source of UV. That way, when those curtains close, the ranges of your detectors wont drop too low.

What's next... well, what do you want to do with all this new information? You need to read it, pass it to the methods we've described above to filter the data and then act on it. Since we're multiplexing, we need to first tell our 4051 IC(s) which input we want to read and then read it. The following classes operate the multiplexers and detectors:

class DetectorCollection {
	private:
		struct Detector {
			int dValues[32];
			int dMax;
			int dMin;
			int dRange;
			int dAverageValue;
			int dCurrentValue;
			int dThreshold;
			int dCurrentIndex;
			bool dFullArray;
			int dAnalogPin;
			int dBitIndex;
			bool dIsActive;
		} detectors[32];
		int numDetectors;
		int digPins[3];
	public:
        DetectorCollection(int _digPin1, int _digPin2, int _digPin3);
        bool AddDetector(int _aPin, int _bit);
        void UpdateDetector(int detector);
        void UpdateAllDetectors();
        bool IsActive(int detector);
        void DebugInformation(int detector);
        int GetCurrentValue(int detector);
};

DetectorCollection::DetectorCollection(int _digPin1, int _digPin2, int _digPin3) {
	numDetectors = 0;
	digPins[0] = _digPin1;
	digPins[1] = _digPin2;
	digPins[2] = _digPin3;
}

bool DetectorCollection::AddDetector(int _aPin, int _bit) {
	//initialise a detector. the array contains "zero'd" detectors
	//by default
	if (numDetectors < 32) {
		detectors[numDetectors].dAnalogPin = _aPin;
		detectors[numDetectors].dBitIndex = _bit;
		for (int idx = 0; idx < 32; idx++)
			detectors[numDetectors].dValues[idx] = 0;
		detectors[numDetectors].dMax = 0;
		detectors[numDetectors].dMin = 999;
		detectors[numDetectors].dRange = 0;
		detectors[numDetectors].dAverageValue = 0;
		detectors[numDetectors].dThreshold = 0;
		detectors[numDetectors].dCurrentIndex = 0;
		detectors[numDetectors].dFullArray = false;
		detectors[numDetectors].dIsActive = false;
		numDetectors++;
		return true;
	} else return false;
}

void DetectorCollection::UpdateDetector(int detector) {
	//set digital pins
    for (int pin = 0; pin < 3; pin++)
      digitalWrite(digPins[pin],
        ((detectors[detector].dBitIndex >> abs(pin-2)) & 0x01) == true ? HIGH : LOW);

	//read analog pin.
	detectors[detector].dCurrentValue =
        analogRead(detectors[detector].dAnalogPin);
	detectors[detector].dValues[detectors[detector].dCurrentIndex] =
        detectors[detector].dCurrentValue;

	//find the lowest and highest values in the array and store as
	//the minimum and maximum extremities.
	int tempVal, newValue = 0;
	bool extremitiesChanged = false;
	for (int idx = 0; idx < 32; idx++) {
		tempVal = detectors[detector].dValues[idx];
		if (tempVal < detectors[detector].dMin || detectors[detector].dMin == 0) {
			detectors[detector].dMin = tempVal;
			extremitiesChanged = true;
		}
		if (tempVal > detectors[detector].dMax) {
			detectors[detector].dMax = tempVal;
			extremitiesChanged = true;
		}
		//used for average calculated below.
		newValue += tempVal;
	}

		//update range of this detector [max - min]
		detectors[detector].dRange =
		    detectors[detector].dMax - detectors[detector].dMin;
		if (newValue > 0) {
			if (detectors[detector].dFullArray)
                          detectors[detector].dAverageValue = newValue / 32;
			else detectors[detector].dAverageValue =
                          newValue / (detectors[detector].dCurrentIndex + 1);
			//adjust threshold [average + (max-min*0.10)]
			detectors[detector].dThreshold =
                          detectors[detector].dAverageValue +
                          (detectors[detector].dRange * 0.35);
			//adjust active flag:
			detectors[detector].dIsActive =
                          (detectors[detector].dCurrentValue >
                           detectors[detector].dThreshold);
		}

	//finally update the next location to store the next incoming value...
	//we're using a circular buffer, so just point to the start of the
	//array instead of shifting everything along.
	detectors[detector].dCurrentIndex++;
	if (detectors[detector].dCurrentIndex >= 32) {
		detectors[detector].dCurrentIndex = 0;
		//for calculating the average, we need to know once
                //we have a full buffer. Once it's full we will always
                //have a full set of NUM_READINGS values, otherwise
		//we only have as many as dCurrentIndex
		detectors[detector].dFullArray = true;
	}
}

void DetectorCollection::UpdateAllDetectors() {
  for (int d = 0; d < numDetectors; d++) UpdateDetector(d);
}

bool DetectorCollection::IsActive(int detector) {
  return detectors[detector].dIsActive;
}

int DetectorCollection::GetCurrentValue(int detector) {
  return detectors[detector].dCurrentValue;
}

void DetectorCollection::DebugInformation(int detector) {
  Serial.print("Detector: ");
  Serial.print(detector);
  Serial.print(", APin: ");
  Serial.print(detectors[detector].dAnalogPin);
  Serial.print(", DBit: ");
  Serial.print(detectors[detector].dBitIndex);
  Serial.print(", Min: ");
  Serial.print(detectors[detector].dMin);
  Serial.print(", Max: ");
  Serial.print(detectors[detector].dMax);
  Serial.print(", Range: ");
  Serial.print(detectors[detector].dRange);
  Serial.print(", Threshold: ");
  Serial.print(detectors[detector].dThreshold);
  Serial.print(", Average: ");
  Serial.print(detectors[detector].dAverageValue);
  Serial.print(", Current: ");
  Serial.print(detectors[detector].dCurrentValue);
  Serial.print(", FullArray: ");
  Serial.print(detectors[detector].dFullArray);
  Serial.print(", CurrentIndex: ");
  Serial.println(detectors[detector].dCurrentIndex);
  /*for (int idx = 0; idx < 32; idx++) {
    Serial.print("|");
    if (idx == detectors[detector].dCurrentIndex) Serial.print("*");
    Serial.print(detectors[detector].dValues[idx]);
  }
  Serial.println("|");*/
}

And now, use it in your main program. Note I've created custom characters for the Arduino Liquid Crystal library via this website.

#define multiplexerPinBitA  40
#define multiplexerPinBitB  41
#define multiplexerPinBitC  42

#define pwmPin  			2
#define dirPin1 			3
#define dirPin2 			4

#define lcdRSPin			30
#define lcdENPin			31
#define lcdD4Pin			32
#define lcdD5Pin			33
#define lcdD6Pin			34
#define lcdD7Pin			35

#include <LiquidCrystal.h>
LiquidCrystal lcd(lcdRSPin, lcdENPin, lcdD4Pin, lcdD5Pin, lcdD6Pin, lcdD7Pin);

//cool hack! create characters for the LiquidCrystal Library!
//see here: http://icontexto.com/charactercreator/
byte trainCharFrontOn[8] =
{B11111,B10001,B10001,B11111,B10101,B11111,B01010,B11111};
byte emptyChar[8] =
{B00000,B00000,B00000,B00000,B00000,B00000,B11111,B10101};

DetectorCollection dCol = DetectorCollection(multiplexerPinBitA,
    multiplexerPinBitB, multiplexerPinBitC);

void setup() {
  Serial.begin(9600);
  pinMode(multiplexerPinBitA, OUTPUT);
  pinMode(multiplexerPinBitB, OUTPUT);
  pinMode(multiplexerPinBitC, OUTPUT);

  for (int d = 0; d < 24; d++) {
    //analogpin is 0, 1, 2 [so DIV 8].
    //(where 0 is detectors 1-8, 1 is 9-16 and 2 is 17-24)
    //bit is the 0-7 on that analog pin [so MOD 8].
    dCol.AddDetector(d/8, d%8);
  }

  //start a train: direction
  digitalWrite(dirPin2, HIGH);
  digitalWrite(dirPin1, LOW);
  //speed (out of 255 [where ~50 is stopped])
  analogWrite(pwmPin, 85);

  lcd.createChar(0, emptyChar);
  lcd.createChar(1, trainCharFrontOn);
  lcd.begin(16, 2);
}

void loop() {
  //output to the LCD (16x2) the status of all the detectors:
  lcd.clear();
  lcd.setCursor(0, 0);	//top left

  for (int d = 0; d < 16; d++) {
    dCol.UpdateDetector(d);
    lcd.write(dCol.IsActive(d) ? 1 : 0);
  }
  lcd.setCursor(8, 0);
  for (int d = 16; d < 25; d++) {
    dCol.UpdateDetector(d);
    lcd.write(dCol.IsActive(d) ? 1 : 0);
  }

  //we still have 8 characters to draw other stuff... no idea what yet though.
  delay(333);
}

And then the detectors in action. Note it was at night time and I'm surprised the result was this good!
LCD showing train locationAmtrak testing detectors

24Apr/105

Kato Amtrak 13002 (Seibu E851?)

So, I was browsing eBay and saw advertised a Kato Amtrak 4+4+4 Electric Locomotive... For all I'd known Amtrak America had never had any such an engine and therefore clicked the item to investigate... In front of me appeared (what looked like) an EF81 in Amtrak livery!

Kato Amtrak 13002Kato Amtrak 13002Kato Amtrak 13002Kato Amtrak 13002Kato Amtrak 13002Kato Amtrak 13002

It seems that, back in the day, Kato didn't want to put money in to actually designing the models for America and therefore just repainted a (very slightly) remodeled EF81. Of course, it could be an exact copy of another Japanese electric locomotive, but I haven't had the time yet to do further research.

Update:
Toni Babelony of the JNS Forum posted a message in the thread I created on this locomotive that indicated that this is much closer to a Seibu E851. Thanks for pointing this out! Here is the Kato page on the Seibu E851. You'll notice that the Seibu has port holes, and other differences, but is obviously what Kato used as a base for this Amtrak locomotive.

Kato Seibu E851Seibu E851 in JapanKato Seibu E851The real E851 in Japan

Meanwhile, here are some photos I've taken of EF81s in Japan:

EF81EF81EF81
EF81EF81EF81
EF81EF81EF81
EF81EF81EF81
EF81

And, of course, if this locomotive really does exist, then please comment and tell me!

31Mar/102

InfraRed + Arduino revisited

Ok, after failing (more-or-less miserably) with the previous attempt at IR distance detection, I went out and purchased 4 of the Sharp GP2D12 from an eBay Store and put them to work. Finally I had a detection system working, but before this I had also attempted another method using larger IR emitter/detectors that I'd bought from Dip Micro.

Attempt 1: QRD1114, and other smaller IR detectors

See the previous blog post here

Attempt 2: IR Emitter/Detector pairing

Since my previous attempt had failed, I'd decided that if the smaller emitters could not produce enough light to avoid sunlight/roomlight interference, then boosting both the emitter and detector should help. I'd accidently purchased a collection of 10x emitter/collector pairs of IR diodes and so I put these to work in the typical setup and tested them out. These were both 3mm in diameter and, depending on the resistors used, could emit a lot more light (tested via my digital camera.)

3mm IR Diodes on board
3mm IR Diodes in position
Trial spacing
Separator to stop light direct to collector
Completed
Lit



The setup was the same as per usual... mounted horizontally at the end of the tracks to ensure that the light would reflect (as much as possible) off the approaching train.

Working with IR at night-time has it's benefits. Your room is usually lit under artificial light and so the amount of IR in the 'air' is low. I therefore had some pretty good results with this setup, but of course, come daylight, everything went out the window (or in the window, as the case may be.) Since this detector was to be in the back of an engine shed, I'd thought that I could block out the windows and make a little dark room, but my detectors were still too unreliable.

Here's a few more pictures of the experiment... It was, in the end, functional, but not to the degree that I'd wanted and so I therefore opted for the off-the-shelf Sharp detector.

Note: The goal here was to use the pair at the end of the track to sense distance. It now seems that the best method will be to detect 'occupation' and I will again test this method with a series of emitter/detector pairs along the track to sense when a train is approaching.

Attempt 3: Sharp GP2D12

After being concerned about sizing and the minimum distance that these detectors would work from, I decided I'd just bite the bullet and try them out.

Sharp GP2D12
Sharp GP2D12 mounted



I ended up purchasing 4 quite cheaply on eBay and they arrived from the UK in a short amount of time. I then realised that the versions I'd bought were optimised for detection between 10cm and 70cm. This really sucks, as I'd want the range to be much closer to the detector, as 10cm is a long way for a no-detection zone. I then looked at the graphs showing the voltage compared to distance:

GP2D12: Voltage vs Reflection Distance
GP2D12: Voltage vs Detection Distance


It turns out that this datasheet shows you the comparison of the various detectors that Sharp makes. There seems to be one detector better suited for my project, but.. as they say... hindsight is a bitch. Even worse is that Toys Downunder currently has them in stock!

Although the 'optimum' detecting distance for unit is around 10cm, the detector still gives valid voltage results right down to around 3cm. The only issue here is that the voltage difference is not always increasing! I therefore have to take the value and use it in different scenarios (i.e. when cruising, braking, stopped, etc...)

The setup was the same as usual:

Sharp GP2D12 in testing
Sharp GP2D12 in testing


So, to deal with this, I needed to work on the voltage change... in steps of around 0.25v. If you happen to implement this yourself, you'll notice straight away that the voltage returned by this unit is not constant when the vehicle is stopped or approaching... it's perpetually flitting around +-0.5v and this can be a real nightmare. Due to this I only choose to detect larger changes which means only reading the sensor if it is +-25 of the current read value.

The basic idea is to determine the deceleration of the vehicle dependent on it's distance from the wall. The final testing code is listed next. Note that this also contains my multiplexer, thottle and also an off-the-shelf LCD for which you can find tutorials here.

#define MIN_SPEED 50
#define CRAWL_SPEED 85
#define MAX_SPEED 125
 
#include <LiquidCrystal.h>
LiquidCrystal lcd(30, 31, 40, 41, 42, 43);
 
#define STROBE_PIN 50
#define INHIBIT_PIN 51
 
#define BIT1_PIN 21
#define BIT2_PIN 23
#define BIT3_PIN 25
#define BIT4_PIN 27
 
#define DIRECTION1_PIN 52
#define DIRECTION2_PIN 53
 
void initMultiplexer() {
  //set the output mode.
  pinMode(INHIBIT_PIN,OUTPUT);
  pinMode(STROBE_PIN,OUTPUT);
  pinMode(BIT1_PIN,OUTPUT);
  pinMode(BIT2_PIN,OUTPUT);
  pinMode(BIT3_PIN,OUTPUT);
  pinMode(BIT4_PIN,OUTPUT);
  pinMode(DIRECTION1_PIN,OUTPUT);
  pinMode(DIRECTION2_PIN,OUTPUT);
  //initial state
  digitalWrite(INHIBIT_PIN, HIGH); //high = off.
  digitalWrite(STROBE_PIN, LOW); //toggle low -> high -> low to set output.
  digitalWrite(BIT1_PIN, LOW);
  digitalWrite(BIT2_PIN, LOW);
  digitalWrite(BIT3_PIN, LOW);
  digitalWrite(BIT4_PIN, LOW);
}
 
void change(int out) {
  //work out bits
  Serial.print((out >> 3) & 0x01);
  if ((out >> 3) & 0x01) digitalWrite(BIT4_PIN, HIGH);
  else digitalWrite(BIT4_PIN, LOW);
  if ((out >> 2) & 0x01) digitalWrite(BIT3_PIN, HIGH);
  else digitalWrite(BIT3_PIN, LOW);
  Serial.print((out >> 2) & 0x01);
  if ((out >> 1) & 0x01) digitalWrite(BIT2_PIN, HIGH);
  else digitalWrite(BIT2_PIN, LOW);
  Serial.print((out >> 1) & 0x01);
  if ((out) & 0x01) digitalWrite(BIT1_PIN, HIGH);
  else digitalWrite(BIT1_PIN, LOW);
  Serial.println((out >> 0) & 0x01);
  //toggle strobe
  digitalWrite(STROBE_PIN, HIGH);
  delay(50);
  digitalWrite(STROBE_PIN, LOW);
}
 
void setup() {
  initMultiplexer();
  Serial.begin(9600);
  lcd.begin(16, 2);
}
 
void updateSensorValue(int& tgt, int latest)
{
   int threshold = 25;
   if ((latest < (tgt - threshold)) || (latest > (tgt + threshold))) tgt = latest;
}
 
void pulseMultiplexer() {
  //toggle inhibit to low to actually output power
  digitalWrite(INHIBIT_PIN,LOW); 
  delay(125); //25ms is long enough.
  digitalWrite(INHIBIT_PIN,HIGH); 
  delay(1500); //now delay before going to next point.
}
 
int thresholds[4] = {9999,0,9999,0};
 
int t = millis();
int oldT = millis(), dirT = oldT;
int a1;
int a2;
int spd = 0;
int dir = 0;
 
int train_status = 0;
int sensor = 0;
int point = 0;
 
void loop() {
  t = millis();
  
  if ((t - oldT) > 50) {
    updateSensorValue(a1, analogRead(0));
    updateSensorValue(a2, analogRead(1));
    
    sensor = a1;
    if (point == 1) sensor = a2;
 
    switch(train_status) {
      case 0: //accelerating
        if (spd < MIN_SPEED) spd = MIN_SPEED;
        spd += 2;
        if (spd > MAX_SPEED) {
          train_status = 1;
          spd = MAX_SPEED;
        }
        break; 
      case 1: //travelling
        break;
      case 2: //braking
        spd -= 10;
        if (spd < CRAWL_SPEED) spd = CRAWL_SPEED;
        break;
      case 3: //paused
        spd = 0;
        break; 
    }
    
    if (dir == 1) {
      if ((t - dirT > 1250) && (train_status == 0 || train_status == 1)) {
        train_status = 2;
        dirT = t;
      }
      else if ((t - dirT > 2000) && train_status == 2 && spd == CRAWL_SPEED) {
        train_status = 3;
        dirT = t;        
      }
      else if ((t - dirT > 500) && train_status == 3)
      {
        train_status = 0;
        dir = !dir;
        dirT = t;
        //switch point
        change(3);
        if (point == 0) {
          digitalWrite(DIRECTION1_PIN, HIGH);
          digitalWrite(DIRECTION2_PIN, LOW);
          Serial.println(0);
        } else if (point == 1) {
          digitalWrite(DIRECTION1_PIN, LOW);
          digitalWrite(DIRECTION2_PIN, HIGH);      
          Serial.println(1);
        }
        pulseMultiplexer();
        point = !point;
      }
    } 
    else if (dir == 0) 
    {    
      //we should be checking which point we're about to hit first?
      /*if (a1 > 400 && dir == 0) {
        spd = 0;
        dir = !dir;
        train_status = 0;
      } else if (a1 > 300 && dir == 0) spd = 70;
      else if (a1 > 200 && dir == 0) spd = 90;
      else if (a1 > 150 && dir == 0) spd = 110;
      else spd = 125;*/
      if (train_status != 3) {
        if (sensor > 400) {
          train_status = 3;
          dirT = t; 
        } else if (sensor > 300) {
          train_status = 2;
        }
      } else if (t - dirT > 500 && train_status == 3) {
        train_status = 0;
        dir = !dir;
        dirT = t;
      }
    }
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("");
    lcd.setCursor(0, 0);
    lcd.print(a1);
    lcd.setCursor(0, 1);
    lcd.print("");  
    lcd.setCursor(0, 1);
    lcd.print(a2);
 
    lcd.setCursor(5, 0);
    lcd.print(thresholds[0]);
    lcd.setCursor(7, 0);
    lcd.print(thresholds[1]);
  
    lcd.setCursor(5, 1);
    lcd.print(thresholds[2]);
    lcd.setCursor(7, 1);
    lcd.print(thresholds[3]);
  
    lcd.setCursor(11, 0);
    lcd.print(spd);
    lcd.setCursor(11, 1);
    lcd.print(dir);
    lcd.setCursor(15, 1);
    lcd.print(train_status);
 
    oldT = t;
  }
 
  if (a1 < thresholds[0]) thresholds[0] = a1;
  if (a1 > thresholds[1]) thresholds[1] = a1;
  if (a2 < thresholds[2]) thresholds[2] = a2;
  if (a2 > thresholds[3]) thresholds[3] = a2;
  
  if (dir == 0) {
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  } else {
    digitalWrite(4, HIGH);
    digitalWrite(3, LOW);
  }  
  
  analogWrite(2, spd);
}

From the above, we get the following action... note that the whole process here is automated (throttle, detection, point switching):

Conclusion

This sensor worked much better than the previous attempts... but it's still not the best. I think I might now just grab one of the GP2D120s (as it would simply be plug-and-play) and see what happens. Also lighting plays an affect here too, it might just be easier in the end to have a strip of LDRs to work out where the train is... but everything has it's good and bad side!

The other option will be to have a spaced strip of detectors down one side of the track and emitters on the other. This will be like your tandy/radio-shack store that beep-beeps when you trip the beam... we'll see how well it works.


18Mar/104

Controlling lots of LEDs with your Arduino

There's a great article on the MAX7219/MAX7221 LED Drivers on arduino.cc that details how to utilise these chips to control a large amount of LEDs (MAX7219/7221 Datasheet.) The layout I've been working on was always going to have a lot of scenery lighting and I'd decided this time to use LEDs over traditional 12v DC Bulbs as they draw less current and can often be a lot brighter. Also, Japan does use a lot more 'white light' when lighting streets and train/traffic signals, so it fits in close enough to the prototype.

LED Drivers are specific intergrated circuits that are designed to control a large amount of LEDs wired up in a matrix. This means that, in the case of the MAX7219/7221, the LEDs will be in rows and columns of 8 for a total of 64 LEDs per chip. The exact wiring dictates that each column contains 8 cathodes and each row 8 anodes. This therefore means that if you apply power to one row, and then ground a column, you will light the associated LED. Of course, this means that if you power two rows and two columns you will in fact have 4 LEDs lit. This is because the matrix is limited in only being able to light specific LEDs on a row-by-row basis.

Fortunately, the Drivers are fast enough to light row after row (selecting the exact LEDs to light by grounding certain columns) making it look like all LEDs are on at the same time. Therefore, you can specify the exact LEDs to light and, in the case of an 8x8 matrix, you can draw pictures, scroll text, etc...

Of course, you are not limited to controlling a matrix. These chips fundamentally just light up 64 LEDs and, for my layout, this should be enough for my scenery requirements. The main issue is actually building a 'distribution point' for getting the 64 pairs of wires out to the LEDs from the chip.

Note: PLEASE make sure you have a 'clean' power supply and it's connected cleanly and solidly. I just spent a day diagnosing why my LEDs stopped working... it was because my 12v power distribution had a dry solder joint... totally frustrating!

How to wire up a MAX7219 LED Driver

As the chip only has 16 pins (8 rows, 8 columns), one has to split these out into inidividual wires to LEDs.

If you happen to have your LEDs in groups/clusters of 8 then you will have a lot less trouble wiring everything up... otherwise you'll need to tediously create a 'distribution board' like I've done here:

MAX7219 board
Awesome soldering
Close up of 'distribution board'
Plugs to connect LEDs to



So, once this was together I put some plugs on the end of the ribbon wire and then added header pins to my LED wiring. This then meant I could simply plug the LEDs in as no resistors are required. Trying to work out which way around to put them was quite easy, as they seem to get full voltage if you put them the wrong way! So, be careful, if this happens then quickly reverse the LED.

I then wrote some code to turn my well-lit footpath into a flashing mess...

//pins for the MAX7219
#define CLK_PIN 31
#define LOD_PIN 33
#define DIN_PIN 35
 
//pins for my L298N throttle
#define THROTTLE_ENABLE 2
#define PWM1_PIN 3
#define PWM2_PIN 4
 
//library required from:
//http://www.arduino.cc/playground/uploads/Main/LedControl.zip
#include "LedControl.h"
 
//LED CONTROL... 3 pins and then the number of devices
LedControl lcl=LedControl(DIN_PIN,CLK_PIN,LOD_PIN, 1);
 
//currently connected lights:
//ROW 0:   NONE.
//ROW 1: Building, Building, Building, Building,
//       Streetlight, Streetlight, Streetlight, Streetlight
//ROW 2-3: NONE.
//ROW 4: Streetlight, Streetlight, Streetlight, Streetlight,
//       Streetlight, Streetlight, Building
//ROW 4-7: NONE.
 
void setup() {
  //initialise the led driver...
  lcl.shutdown(0, false);   //turn off shutdown..
  lcl.setIntensity(0, 8);   //set the intensity, you can also use the potentiometer.
 
  //set initial throttle, direction and speed
  pinMode(THROTTLE_ENABLE, OUTPUT);
  digitalWrite(PWM2_PIN, HIGH);
  digitalWrite(PWM1_PIN, LOW);
  analogWrite(THROTTLE_ENABLE, 200);
 
  //turn on the building lights.
  for (int i = 0; i < 4; i++) lcl.setLed(0,1,i,true);
  lcl.setLed(0,4,6,true);
}
 
int MS_DELAY = 300; //timing, close enough to train speed.
 
void loop() {
  //the streetlights are in a bit of a jumbled order:
  //ROW 4: 5,4,3,2,1 and then ROW 1: 4,5,6,7.
 
  //turn them all on
  for (int x = 5; x >= 0; x--){
    lcl.setLed(0, 4, x, true);
    delay(MS_DELAY);
  }
  for (int x = 4; x < 8; x++){
    lcl.setLed(0, 1, x, true);
    delay(MS_DELAY);
  }
 
  //now turn them back off
  for (int x = 5; x >= 0; x--){
    lcl.setLed(0,4,x,false);
    delay(MS_DELAY);
  }
  for (int x = 4; x < 8; x++){
    lcl.setLed(0,1,x,false);
    delay(MS_DELAY);
  }
}

And here was the result, with my homemade street lights:

DSC03123
DSC03126
DSC03127




The best thing about these chips is that they can be daisy-chained together to control a total of 512 LEDs off 3 pins. If you think you'll need more than that for your project, then you're crazy... but it can be done. Simply utilise another 3 digital pins of your Arduino to control another 512 LEDs... I shudder to think of something lit that brightly.

24Feb/103

Detecting trains with InfraRed + Arduino

You've probably seen a lot of reversing/stopping circuits around, but the majority of these run on occupancy detection in track blocks. This can work very well but, due to the differences in train engines, you can have issues with how quickly to slow/stop individual locomotives. There are other ways of detecting a train coming to a dead-end, and here I'll show a method using Infrared Light.

Infrared light can't be seen by the human eye, but can be picked up by electronic devices. Due to this, it can be used freely around your layout (with the exception that direct sunlight/room-light can cause interference) for detecting your trains. One of the more common usages is to put an emitter/detector combination in a buffer to stop a train as it comes to the end of the line.

IR Detector



Note that there are two emitter/detector devices! Each 'black box' in that diagram contains both an emitter and a detector. The benefit of using the QRD1114 means that these are neatly packaged in one unit!

In this set up the emitter and detector are both facing towards an approaching vehicle. The detector will receive the infrared light reflected off the vehicle as it approaches and it's internal resistance will rise accordingly. This means we can read the resistance of the detector to know how close the train is getting to the buffer and therefore slow the train down. We can ensure the train does not hit the buffer as long as we check the value of the detector and updating the speed of the train quickly.

Firstly, here's the circuit for wiring up both the 'Infrared Emitters and Detectors' and the Optical Detector / Phototransistor (recommended) from Toys Downunder. And Here's the datasheet for the latter QRD1114.

Note: I happened to test out both setups as I destroyed my first QRD1114 by applying 5v directly to the emitter... You must only apply 1.7v max!

Wiring up the detector


And then the detector set up on the engine shed roads:

Engine shed siding with Infrared
Engine shed siding with Infrared



The two roads above will eventually be located inside an engine shed. Due to the potential interference from one emitter to the other, I will put a separator down the middle of the tracks.

The maxmimum read distance I could achieve was just under 6cm during testing**. This wasn't exactly what I was expecting and would've liked around 10-12cm for this purpose, but I worked around this. It seems that there is a more expensive detector at Toys Downunder: Sharp GP2Y0A21YK that can detect object up to 84cm away. I imagine it would be as simple as obtaining larger IR LEDs to boost the light output and therefore the light reflection.

Once the two detectors were in place, I set about automating a quick shunting process. I'd started noticing that, depending on room light, the readers weren't doing the best job; down to around 20mm distance was all that was being detected. This would not be enough to measure the speed as I wanted to, but I kept going anyway.

Programming the detector was the same as the potentiometer throttle done previously. The detector acted as a variable resistor and the reading would be from +1000 to 0 depending on the reflectivity of the object. It turned out that 0 was when the object was closest to the detector. Unfortunately, this only started changing once the object came within 30mm.

Here's a video of my quick shunt automation in action. First with the QRD1114 and then with the separate emitter/detector:

You can see that the separate emitter/detector doesn't function as well as the train actually hits it:

Results

I ended up stopping progress on this as the detector didn't respond as well as I'd have wanted. After getting a 6cm read distance during test I thought it would feasible, but this dropped to a max of 15mm when actually installed on the track. There would be too much re-adjusting to get either of the types of infrared detectors to work.

The only option from here is to purchase the Sharp detector mentioned above and see if it really can detect 84cm!

18Feb/100

Tracking trains with an Arduino and RFID : Implemented

Right, after telling you all about how the RFID setup works (and how to do it yourself), I thought I'd actually test it on my work-in-progress layout. Just for a recap, this setup uses the Arduino Mega, an ID-12 RFID Reader (plus RFID Button) from Toys Downunder and the code provided in the article listed here.

Full layout so far
Station put back, reader invisible
Thunderbird tests the reader

I knew the ID-12 reader had a 60mm optimal read distance, so I wanted it pretty close to the track for extra reliability. I decided on mounting it inside the platform, as it would be close to the track and I could perform actions/events once a train reached the platform.

First I had to rewire the reader, as previously I'd used component wires (surplus resistor legs, etc...) so that I could easily test it on the Arduino. Now that it was to go through the baseboard I needed lengthy wires:

Re-wiring for cables this time
Fiddly business



And then I mounted it into the station platform:

Station base with area cut for reader
Foam removed for reader
Reader in place
Station setup



And then, it just worked!





What you can't see is the readout on the computer screen of the unique id on the button. This is what will allow me to perform actions (maybe sounds, path settings or signal settings) per each train I have with a button installed.

What's next? I might try some proximity detection; as you can see, I have a 2-road yard just to the right of the station which will be an engine shed and it'll require that trains don't smash through the rear wall :)

17Feb/105

Tracking trains with an Arduino and RFID

So, you're now controlling your trains with your Wii Nunchuck and an Arduino... What if you get tired and want them to run by themselves? This next project will help you with one piece of the puzzle, as long as the trains are still moving :)

Whilst looking for the Wii Nunchuck Adapter at Toys Downunder, I came across RFID 'buttons' and an RFID reader that were compatible with the Arduino. Since I have the Arduino Mega, I knew I'd have pins/serial lines to spare, so I went ahead and added them to my order.

RFID 'Buttons'
RFID Reader ID-12
RFID Reader ID-12 from below



I bought the ID-12 RFID Reader and 2 RFID Buttons. I chose against purchasing the breakout board, as after reading the comments it didn't look like it would fit the ID-12 (only fitting the ID-20.) I've since asked Toys Downunder to correct this comment in case it really does, as soldering these readers is a pain. (Note, they have since updated the notes and the ID-12 does indeed fit. They also offered me a free one! I might act on that next time I go shopping there!)

RFID IC Pinout



Wiring up the reader was going to be pretty simple. I'd see examples of it wired here for a door lock and in the datasheet. I was a little confused as to which 'output' to use; there are two provided and I guessed right the first time (pin 9: Inverted TTL Data.) This output was fed into an RX port on one of the 3 surplus Serial ports on the Arduino Mega (Serial1).

Set up and functional
RFID Reader wiring
Soldering pins



After a quick bit of coding, I had the RFID reader picking up the RFID button reliably. You can find instructions on this from the Arduino site for another type of reader, but below is a quick code listing for getting it to work on the Arduino Mega. I wrote this with guidance from the previous door lock link and the Arduino site as well.

bool newData = false;
int cardData[16];
int idx=0;
void setup() {
  Serial.begin(9600);  // Hardware serial for Monitor 9600bps
  Serial.println("Starting Reader..."); //show some activity
  Serial1.begin(9600); //ID-12 uses 9600!
}
void loop() {
  while (Serial1.available() > 0) {
    cardData[idx] = Serial1.read();
    idx++;
    if (idx == 16) {
      newData = true;
      idx = 0;
    }
  }
  if (newData){
    newData = false;
    Serial.print("Found RFID device: ");
    for (int i = 0; i < 16; i++) Serial.print(cardData[i]);
    Serial.println(".");
  }
} 

I've since read that external antennas can be hooked up, but creating one of these seems to be quite involved. Even though this ID-12 was the smallest reader available on Toys Downunder, there (after reading the datasheet) seems to be a slightly smaller one. It happens to be smaller since it doesn't include an internal antenna. Either way, if you choose to make your own, you may well be able to lay it in the trackbed. Otherwise, you can just shove a reader like this ID-12 into a trackside building or at the back of a engine shed to know which loco has arrived.

Button mounted on carriage
Reader in action through Tomix Finetrack
Reader picking up train with Button



Initial testing showed that the Buttons could be read through my Thunderbird B-Train Shorty with motorised chassis; although the chassis wasn't powered at the time. I'll show the reader installed on a model railway in a future article.

28Oct/094

Bamboo SL Sound Generator

Early this month, I was in Sydney for a weekend and it co-incided with the AMRA Annual Model Railway Exhibition. I was disappointed to not see the usual Japanese Layout by a well known Australian modeller in the Japanese N Scale realm... but found enough goodies in the 2nd-hand junk boxes to satisfy my need for Japanese stock.

An EF81 (missing one panto and other bits) was still for sale from another seller for $80 and I passed on it again as, although I'm sure it'll run fantastically, I don't want to have to spend the extra money (and time search Poppondetta) for all the missing components.

DSC03459
Bamboo Steam Sound Generator

And then... the find of the day... A, and I quote ”BB サウンドシステム SLドラフトN” or translated to: "BB Sound System SL Draft N". [Note: SL stands for Steam Locomotive in Japanese, they've coined the acronym.] When I saw it, I could only guess that it made SL sounds... and should be towed behind an SL. I asked the price, was told $10 and I didn't even ask if it worked, as I just wanted to get it and test it instantly.


DSC03442

On the train back to the city (2 hour ride) I read the instructions... hah... read them like a picture book! I could read the Katakana.. and that hinted something at a 'Power pack' and 'SCR timing pulse'. I thought I'd just bought a lemon that required some magic to get the chuffing happening... boy was I wrong!

If anyone wants to look at this image and give me a proper translation of it then go ahead... I'll post it here. Otherwise, when I get the time, I'll attempt to type it in to Google translator and see what it spits out. I really should've studied Kanji further after Uni :)


DSC03446
DSC03450

Inspecting the Kato WAMU freight car (damn heavy!) I saw that there was a reed switch and a magnet glued to the axel. Primitive technology from Japan... but considering the age of the paper the instructions are on, I'm guessing this whole thing is over 10years old; but i'm yet to actually research it. Anyway, when rotating the axel you could hear the reed-switch clicking... meaning that it would be the 'pulse' required.

DSC03439
DSC03451
DSC03454

Opening it up, very gently, I found a reasonably dated PCB with quite large components.. but everything fitting nicely. There is a standard (what looked like a microphone) speaker mounted downwards and they've also added weights on the inside of the shell.

Finally, tonight, I put some voltage to the unit. I had to turn my Kato Powerpack up to notch '2' to get it hissing... and it sounds good!... I then pushed it along the tracks and the chuffing started... I realised that I could quickly get it to chuff way too fast and sound like a machine gun. After attaching my MicroAce steamer, I realised the main issue; the voltage required to start the sound was so high that the steamer was already flying. At this speed, although it sounded ok, it was still too fast to be enjoyed. When there was no loco on the tracks and the voltage was high, the sounds were great... you could even lock the reed switch open (at the sweet spot) and the chuff would continue forever... as in when an SL releases pressure at the end of a trip.

I then had a closer look at the circuit board to see if I could drop the required voltage to get the sound moving and something dawned on me... The sound worked in both directions... meaning that the circuitry had to work either way the DC voltage was supplied... this meant it had to have a bridge-rectifier in it already... DCC AC Voltage here I come!!!!

Of course, I ran out of time to test it on DCC and I also have no SLs DCC'd up. My MicroAce steamer seems to have a large enough tender... but I love that thing and don't want to hurt it. It also manages to suck power through it's driving wheels and so it'll be a task to convert it.

Videos!

This is the unit running on DCC. I don't have any steamers converted to DCC yet, so I put it in the middle of my 'Aizu Renewal' set. Apart from grotty wheels and tracks, the sound is great.