Model trains are the center of one of the largest community of hobbyists on the internet. One of the most important aspects of the model train is its ability to move at varied speeds, unfortunately most model train remote controls do not allow you to set a precise speed. Enter the Arduino micro-controller! With some simple circuitry and Matlab code you too can achieve acme of model train variable speed control.
Step 1: Parts
- 1x: SparkFun RedBoard - Programmed with Arduino
- 1x: Breadboard
- 1x: USB to microUSB cable
- 1x: Photo Resistor
- 3x:10k ohm resistor
- 1x: Diode
- 1x: Red LED
- 1x: Transistor
- 1x: Track connector wire
- 17x: Jumpers
- Fun 3D printed sign (Optional)
Step 2: Circuit Set Up - Voltage Control
This setup is similar to a circuit that would run a DC motor, because it essentially is. The DC motor is located in the model train itself, so all this circuit has to do is to run voltage to the rails the train is sitting on.
Step 3: Circuit Setup - Photo Resistor
This setup is basic. All it needs to do is run current though the photo resistor and flow back to the arduino. Make sure you plug the wires into the correct pins. The LED setup is also basic.
Step 4: The Code
there are a few components to this:
1. the arduino command calls the arduino itself and stores it in the a variable. You will need to find the right com port to use (e.g. com1, com2, etc.). To do this, input the function 'fopen(serial('nada'))' into the command window; this will tell you open com ports. After the arduino is called successfully, comment out this line to save the variable without trying to reconnect to the arduino.
2. a graphic user interface function 'TrainController' that opens another window so you can vary the voltage being supplied to the rails while also giving feedback on actual and scaled speed. It is important to have the GUI file in the same folder as the MATLAB code your are calling it in.
3. the voltage function 'writePWMVoltage' rewrites the default voltage output for a pin (D5 in this case; can be changed). The default is 5 to start with.
the point of the photo resistor is to resist voltage when light detected decreases. Using the 'readVoltage' command, we can see the amount of voltage resisted by the photo resistor at any time. When the photo resistor reads less than 2.75 volts, the elseif portion of the script is activated and the light begins to flash. Make sure to use the correct pins, D9 in our case.