This page will enhance the LDR_Sensor_Class and LDR_Overload_Class to adjust the signal switching threshold when the sensor is attached to one of the analog pins. Basically when the system is reset the voltage at the LDR pin will be read and this will be the reference for an uncovered sensor. A margin is added to this voltage and a reading above this value it will be assumed that the sensor is covered. Note the system will not be suitable if the ambient light level changes.
Include file: <LDR_Analog_Class>
Constructor: LDR_Analog_Class (pin) : Pin where LDR is wired
LDR_Analog_Class (pin,filter): Filter time in seconds. Default 1 second
Public Methods: begin( ) Required (sets up for analog vs digital)
train_over_sensor( ) Returns 1 for train present
Abstract: This page enhances the C++ Class/Library developed in the pages LDR_Sensor_Class and LDR_Overload_Class. If an analog pin is specified the code at reset will assumed the LDR is uncovered and test the light level to determine a reference voltage to decide when an object is detected (the LDR is covered)
Target Audience: The target audience is model train enthusiasts who may wish to detect their trains in different (but constant) light environments.
Keywords: Analog pins and analog voltages, LDR, Arduino IDE, Arduino NANO, C++ Classes
Required Components: Project was tested with a NANO, LDR and 4K7 resistor. The library was then used in more advance projects.
Required Library: LDR_Overload_Class: To be enhanced.
---------------------------------------------
Previous projects developed
1. LDR_Sensor_Class. This project had a heavy emphasis on developing C++ libraries for Arduino projects.
2. LDR_Overload_Class: This project also had a heavy emphasis on the C++ aspects (Introduction to overloading)
Both classes have been extensively used in a number of model train projects. The biggest limitation has been the variable light level at different locations in the model train layout. This project at reset will measure the light level and use this as a reference for later measurements. Note the project does assume that the light level is constant at each LDR. All room lighting should be ON when the project is reset/powered up.
Most of the projects presented are "how to" and go into significant detail as to the development of the code. This page will simply present how to change the existing libraries.
------------------------
The test hardware is shown below. The test circuit used a 5V supply but 3.3V could be used.
For starters the analog voltage should be continuously read.
The LDR should be moved to various locations where it might be used and the un-covered (no train present) and covered (train present) analog values read. The 4K7 pull up resistor should be adjusted to bring the readings into the range 100 to 900**. If the voltage readings are too low a smaller resistor should be tried. Alternatively if they are two high a larger resistor will be required++
** With the Arduino the ADC is 10 bits so the reading will be in the range 0 to 1023.
++ I found 4K7 to be the best compromise in my environment. If the LDRs are to be used in a dark environment (inside a tunnel?) a higher resistance might be required. Projects using multiple instances of this class will not place any restriction on all pull up resistors being the same.
------------------------------
The first step is to create the LDR_Analog_Class project.
1. In the Arduino IDE create a new file LDR_Analog_Class. By default this file will have the extension *.ino
2. Use the inverted triangle in the Arduino IDE to create two new files LDR_Analog_Class.cpp and LDR_Analog_Class.h
3. Copy the code from the LDR_Analog_Class_code page.
4. This code should compile and run. (It might be necessary to correct any characters embedded in the given code)
For testing purposes the results are displayed to both the serial port and the LED on pin 13. Also the code as given has a 5 second filter so once the covering of the LDR is removed it will be 5 seconds before the serial output displays a "0" and the LED turns off.
---------------------------------------
The LDR_Analog_Class will be building on the LDR_Sensor_Class. The LDR_Sensor_Class includes an extensive description of developing C++ libraries for Arduino based micro-controllers. The LDR_Sensor_Class develops a public method "train_over_sensor( )" that returns the state of the sensor. This method is based on a state machine explained on the page.
This page also placed heavy emphasis on developing C++ code for the Arduino. In this case the example was overloading the constructor to modify the state machine to give filter times other than the default one second of the LDR_Sensor_Class. There can now be two possible objects/instances
1. sensor(A0) where the sensor object is attached to pin A0. The instance has a one second filter, or
2. sensor(A0,5) where the second parameter defines the filter time in seconds. (5 seconds shown)
The constructor specifies which pin the object will be attached to. If this pin is one of the analog pins A0..A5 for UNO the begin method will
1. Read and print the analog voltage reading. This will be the refeerence voltage with no trains present (the LDR uncovered).
2. Initialise a variable that will be the reference for a train present (LDR covered)
3. Initialise a variable to specify how the remainder of the code will handle the LDR reading.
The new code introduces three new variables
bool its_analog that is set to true if one of the input pins is A0 through A5
int No_train the analog reading with no train present at reset.
int Train_present the analog reading that defines when a train is present
There is also a constant DELTA that specifies the minimum difference between the no train and train present readings. This is currently set to 75. This value should be reviewed in light of experiments as per the section Test_Software.
----------------------------------
Currently the variable train_detected is determined by a digital read of the pin. If the pin is an analog pin the additional code performs an analogRead and the variable train_detected is over ridden.
The project was tested using various indoor light settings. For example in dark hallways and outside with 8/8th cloud. It did not work in a model railway tunnel although with a bit of experimentaion it probably could be made to work. (Increase 4K7 pull up). For each light setting the project needed to be reset. When used as an instance/object in larger projects the lighting should be constant before the code starts. Alternatively if people are going to be turning lights on and off there might need to be provision for reseting especialy if the final circuitry is difficult to access. For example beneath a railway layout.