Abstract: This project tests the performance of a Light Detecting Resistor for use with a Model Train. The circuit is a voltage divider formed by the LDR and the pull up resistor. The HTML page will basically determine the value of pull up resistor to use to detect the presence of a Model Train.
Audience: Hobbiest who may wish to use an LDR to detect the presence of an object. The HTML page assumes a model train.
Keywords: LDR, Light Detecting Resistor, ESP8266
Required Components: LDR, 1kohm resistor, 10kohm trim pot, Micro-controller- eg Arduino UNO/NANO. ESP8266 used in example
One of my future projects involves monitoring the position of a model train. The first choice was to use IR Detectors. However, since the track was already laid there were difficulties in retrofitting IR detectors so LDRs were chosen. This HTML page is concerned with evaluating the performance/limitations of the LDR.
Future projects will involve using the LDR information to
(i) control trackside signals - the preferred board for that application is the Arduino UNO or NANO, and
(ii) send the position information using WiFi - the preferred board is an ESP8266.
For this project the ESP8266 was used. (other projects have used the UNO/NANO so verified any conclusions)
The test circuit is shown above. A NANO or a UNO could also be used.
For testing purposes the single LDR is wired to A0 as well as D0. The actual analog voltage will be read and displayed using the serial monitor. By measuring the analogue voltage, the margin above or below the threshold can be determined.
The project code is developed using the Arduino Integrated Development Environment (IDE).
In my example the LOIN(WEMOS) D1 Mini Lite was chosen from the Tools : Board Manager option in the IDE.
The program will read the analog and digital voltage every 500 ms and display the results to the screen using the serial monitor.
The train will be moved across the LDR to observe the serial monitor output:
1. During the presence of a train
2. When there is no train
3. When the couplings are across the sensor
The potentiometer will be varied to give different results The ultimate aim is to adjust the potentiometer for optimum readings (i.e. clearly train present or no train present)
The analog to digital conversion is 10 bits so the reading will be between 0 and 1023 Ideally the analog reading will be less than 512 for the LDR in the open - that is no train
The analog reading will be greater than 512 for the LDR covered - train present
The digital output will be "0" for LDR in the open (no train) and "1" for the LDR covered (train detected)
The final code is given below:
To observe that the program is operational the on-board blue LED is flashed every loop.
Note: If the Arduino UNO/NANO is used there is a built in LED (not blue) on pin 13. To flash in line 3 use "int BLUE_LED = 13;"
Readings < 512 imply that the LDR is low resistance - LDR not covered. ie no train present.
Readings >512 implies LDR is in high resistance state - LDR covered. ie train present.
Notes
## 338 implies that there is no train above the LDR - need to adjust Rpot to indicate train present
By experiment Rpot =4.5 K takes reading above threshold (with a margin)
In the final design a 10 Kohm trim pot can be used at about mid range.
The previous results were from "static" tests. In real life the train will be moving across the LDR. Modifying the code to sample every 100ms and printing the digital output gave the following results for two wagons moving across the sensor:
Note there is a "0" in the centre when the couplings were above the sensor. Repeating the experiment sometimes gave a perfect result (solid "1s") but sometimes there were additional "0s". The conclusion must be that it is going to be difficult in practice to include the couplings.
The modified code:
#define DELTA_TIME 100long int start_time; //start time of delay loop1. The LDRs are sensitive to ambient light
2.For my environment I found a 10 kohm potentiometer adjusted to about half way to be the optimum solution.