Introduction: Train Car Counter-Arduino

Picture of 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

Picture of 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:

  1. One desktop/laptop
  2. One USB cable
  3. One Arduino red board
  4. One breadboard
  5. Matlab 2016
  6. Autodesk Inventor
  7. A 3d Printer with filament
  8. One soldering iron with solder and mounting bracket
  9. Eleven wires
  10. One Piezo Buzzer
  11. One LED
  12. One Flex Sensor
  13. One Photo Resistor
  14. One 10,000 ohm resistor
  15. Two 330 ohm resistors

(Black mounting tray not required)

Step 2: Configuration

Picture of 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)

Picture of 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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

Picture of 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.

Comments

Swansong (author)2017-04-10

I have a friend here that loves to watch the trains go by. He would really like this :)

About This Instructable

330views

1favorite

License:

More by ChaseM35:Train Car Counter-Arduino
Add instructable to: