Introduction: Train Car Counter-Arduino
This instructable will teach you how to create a train car counter using a Matlab program and a complementary arduino red board with various sensors/components.
Step 1: Supplies
Pictures of each of these items are placed in the order in which they are listed in this step.
In order to recreate this project, you will need the following:
- One desktop/laptop
- One USB cable
- One Arduino red board
- One breadboard
- Matlab 2016
- Autodesk Inventor
- A 3d Printer with filament
- One soldering iron with solder and mounting bracket
- Eleven wires
- One Piezo Buzzer
- One LED
- One Flex Sensor
- One Photo Resistor
- One 10,000 ohm resistor
Two 330 ohm resistors
(Black mounting tray not required)
Step 2: Configuration
Pictures of the configuration at several angles have been placed in this step. The last photo is a frits layout.
Once you have acquired the listed materials, configure them as follows (the lowercase letters refer to slots on the breadboard, while the capital letters refer to the Arduino board):
Wires connected between
- a1 and negative 1
- a5 and A1
- a6 positive 2
- a11 and 9 on the Arduino
- i20 and negative 14
- f15 and 10 on the Arduino
- f13 and GND on the Arduino
- j21 and A0 on the Arduino
- h24 and positive 21
- h21 and left component on flex sensor (soldered;sensor bars facing up)
- h20 and right component on flex sensor (soldered;sensor bars facing up)
LED placed at
- c11 and c12
Piezo Buzzer at
- i13 and i15
Photo resistor at
- e5 and e6
10,000 ohm resistor at
- i21 and i24
- b2 and b5
330 ohm resistor at
- a12 and negative 10
Step 3: 3D Print Block (flex Sensor Mount)
There are 10 photos placed in this step. The first 6 are what the sketch of your mount should look like after completing each step listed in the instructions for creating the sketch. The next 3 are shots of several angles of the completed sketch, and the last picture is of the block after printing.
In order to mount the flex sensor, this design used the Autodesk Inventor program draft sketches for a cube on dimensions 1 in. x 1 in. x 1 in. with a slit in the center of dimensions (.28 in x .0625 in. x 1 in.). The flex sensor will be placed through the slit as pictured so that it can be place in front of the tracks for the train cars to hit. The sketch should take about 31 minutes to print depending on your printer's print speed, infill used and resolution desired. Once it is finished, simply slip the flex sensor through the slit.
Instructions for creating the sketch:
- Open the Autodesk Inventor program and click "new part". Click on the "Rectangle" tab, then click on the point that is 4 units to the left and 3 units up from the origin, then the point 4 units to the right and 3 units down from the origin. Your sketch should now look like the first picture.
- Click on each side of the rectangle, click the "Dimension" tab, and enter in "1 in." Your sketch should now look like the second picture.
- Click on the "3D Model" tab, and click on the "Extrude" tab. Once the extrude options menu pops up, simply click "OK". Your sketch should now look like the third picture.
- Click on the "Sketch" tab, and click on the "Rectangle" tab again. This time we will draw a rectangle for the slit that the flex sensor will be mounted in. Click on the point that is 1 unit above and .5 units to the left of the origin, then 1 unit below and .5 units to the right of the origin. Your sketch should now look like the fourth picture.
- Repeat the instructions described in step two, but this time, make the width .0625 in. and the height .28 in. Your sketch should now look like the fifth picture.
- Repeat the instructions described in step three, but this time make sure you select the option that extrudes the shape inwards. Your sketch should now be the completed form shown in pictures six through nine.
Now that your sketch is completed, print the file on your 3D printer. You may need to get help from a 3D printing expert for this step, as we cannot provide information specific to everyone's 3D printer.
Step 4: Code, Logic, and Algorithms
The picture in this step is a flow chart outlining the logic of the program.
The code for this project is in the Matlab language using Matlab 2016.
It was developed using a new feature in Matlab called App Designer.
by typing into the command window appdesigner, you open up a toolbox that helps create a GUI (graphical user interface). You can assign callback functions to various buttons in the GUI, for ours we simply added a connect to Arduino button, which assumes the Arduino will connect to comport 4, though on some computers or even with different Arduinos you may need to change which comport it is connected to (usually COM3).
The code is displayed below:
classdef Train_Car_Counter < matlab.apps.AppBase
% Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure StartButton_2 matlab.ui.control.Button TrainCarCounterLabel matlab.ui.control.Label NumberofTrainCarsGaugeLabel matlab.ui.control.Label NumberofTrainCarsGauge matlab.ui.control.LinearGauge end
methods (Access = private)
% Button pushed function: StartButton_2 function connect(app, event) a=arduino('COM4','UNO') n=0; fsflag=0; while true voltage = readVoltage(a,'A0'); if voltage>4 && fsflag==0 writeDigitalPin(a,'D9',1) fsflag=1; n=n+fsflag playTone(a,'D10',440,.1) elseif voltage<3.9 && fsflag==1 writeDigitalPin(a,'D9',0) fsflag=0; end app.NumberofTrainCarsGauge.Value=n end end end
% App initialization and construction methods (Access = private)
% Create UIFigure and components function createComponents(app)
% Create UIFigure app.UIFigure = uifigure; app.UIFigure.Position = [100 100 602 189]; app.UIFigure.Name = 'UI Figure'; setAutoResize(app, app.UIFigure, true)
% Create StartButton_2 app.StartButton_2 = uibutton(app.UIFigure, 'push'); app.StartButton_2.ButtonPushedFcn = createCallbackFcn(app, @connect, true); app.StartButton_2.Position = [424.5 69 119 22]; app.StartButton_2.Text = 'Start';
% Create TrainCarCounterLabel app.TrainCarCounterLabel = uilabel(app.UIFigure); app.TrainCarCounterLabel.FontSize = 36; app.TrainCarCounterLabel.Position = [175 120 291 47]; app.TrainCarCounterLabel.Text = 'Train Car Counter';
% Create NumberofTrainCarsGaugeLabel app.NumberofTrainCarsGaugeLabel = uilabel(app.UIFigure); app.NumberofTrainCarsGaugeLabel.HorizontalAlignment = 'center'; app.NumberofTrainCarsGaugeLabel.Position = [137 45 124 15]; app.NumberofTrainCarsGaugeLabel.Text = 'Number of Train Cars';
% Create NumberofTrainCarsGauge app.NumberofTrainCarsGauge = uigauge(app.UIFigure, 'linear'); app.NumberofTrainCarsGauge.Limits = [0 10]; app.NumberofTrainCarsGauge.MinorTicks = ; app.NumberofTrainCarsGauge.Position = [38 75 321 40]; end end
methods (Access = public)
% Construct app function app = Train_Car_Counter()
% Create and configure components createComponents(app)
% Register the app with App Designer registerApp(app, app.UIFigure)
if nargout == 0 clear app end end
% Code that executes before app deletion function delete(app)
% Delete UIFigure when app is deleted delete(app.UIFigure) end end end
The code reads the voltage on the input pin and detects when the flex sensor is bent by measuring a pre-determined change in voltage. When this change in voltage is detected, the LED will light up, the piezo buzzer will play a quick tone, and the GUI will display the number of times the flex sensor has been triggered on a number line with a range of values from 1 to 10.
Step 5: Conclusion:Connect to Your Model Train Set, and Enjoy!
You now have your program, and your arduino configured. Connect the arduino to your train set, run the program, and behold your new train car counter.
We have a be nice policy.
Please be positive and constructive.