Printed from www.nzmeccano.com
The Little Tram
(Author: David Couch)
An introductory Arduino project
The Little Tram
An introductory project for Meccano and Arduino
by David Couch
I have previously written, and published on the NZ Meccano web site, three documents giving an introduction to the Arduino microcontroller, a detailed description of how to buy and build a recommended system, and a programming reference. In this document I describe a model which is an example of control of a Meccano model by an Arduino. I have kept the model and program as simple as possible, but I add some suggestions for ways in which you might add extra detail once you have got the basic model working.
The model represents a tram running on rails, which can be as long as you wish, and a shelter which houses the Arduino system. The tram runs back and forth along the track, pausing at each end.
You do not need any prior knowledge of either electronics or Arduino programming to build this model. You do need to have installed the Arduino and motor shield software on your computer and got to the point of loading and running the "blink" example sketch, as described in my document.
Components you will need, in addition to standard Meccano parts, are:
• The Arduino system described in my document, consisting of an Arduino Uno R3 or Mega 2560, a motor shield and a terminal shield
• A suitably cut and drilled piece of Perspex, thin ply or thick card to mount the Arduino
• A small electric motor (see below)
• Two small magnetic switches of the kind used on windows and doors as part of a security system, such as this
• A 4mm plastic knitting needle (UK no. 8, US no. 6)
• M3 and M4 screws, nuts and washers, including nylon ones
• Hookup wire, cable ties, a rubber band and a spring from a ball-point pen.
Building the tram chassis
The tram uses a two-rail power supply, which means that the rails and wheels
The chassis of the tram is shown in figs. 2, 3 and 4. It consists of two 5½" zinc-plated Strips connected by a Double Bracket at either end. The Double Brackets are placed with the centre section upward. On the grounded side standard Bolts are used, and two Washers are inserted between the Strip and the Bracket on
The tram is driven by a small electric motor mounted above the chassis. I used an NZMecc no. 1 400 rpm geared motor from Stan Baker. It is held between a Double Bent Strip and a 1½" Flat Girder. A Bolt held by a Nut in the centre hole of the Flat Girder clamps the motor in
The magnet part of one of the magnetic switches is attached by an M3 screw and nut to an Angle Bracket, which is bolted to the grounded side chassis Strip and spaced with Washers so that the magnet is positioned under the centre of the chassis.
The switch part of a magnetic switch is attached to the base, centred between the rails, a short distance from each end of the rails. The magnet on the chassis should just clear the switches.
At this point the chassis can be tested by connecting a suitable power supply to the two rails and placing the chassis on the rails. If it runs erratically, clean the top edges of the rails and the rims of the Wheels with a piece of fine emery paper or sandpaper. Also check that the small springs are lightly compressed between the Wheels and the chassis to make good contact.
The tram body
The shelter and Arduino
The Arduino, without the shields, is attached to a suitably cut and drilled piece of Pespex, thin ply or thick card with M3 screws and nuts and 38B Spacers. This mounting is in turn attached to the Flat Plate with Bolts spaced by two Washers, and positioned so that cables can be plugged into the USB and power sockets. The shields can then be inserted into the Arduino.
The construction of the rest of the shelter should be evident from figs. 1, 7 and 8. Note the slot to allow the cables to be plugged into the Arduino.
Loading the Arduino program
To obtain the "sketch" or program, please send me an email message at
firstname.lastname@example.org. I will send you a file named tram.txt. Save this file to your hard drive, and then open it with Notepad or another editor. (It does not matter that the text is not correctly formatted in Notepad.) Press Ctrl and A to select the entire text, then press Ctrl and C to copy it to the clipboard. Run the Arduino program, which will open with just the minimum code to allow you to create a new sketch. Press Ctrl and A, and then Delete, to delete all the code in the code space. Press Ctrl and V to paste the new sketch in. In the tool bar click the first icon (a tick mark in a circle) to check that the code compiles correctly. You will be prompted to enter a name for the new sketch. Type the name Tram in the File name space and click Save. In future, when you run the Arduino program you can load the Tram sketch by selecting Sketchbook in the File menu and then Tram in the sub-menu.
With the Arduino connected via the USB cable, click the Upload icon (the second in the tool bar, a right arrow in a circle). If the sketch uploads successfully it will then be running in the Arduino. It will also be saved in the Arduino's flash memory, so you do not need to upload it again unless you modify it.
Connect the power to the motor shield and put the tram on the rails. The tram should soon start to move, and stop when it reaches the switch. If it goes right past the switch it may mean that the sketch is checking the wrong switch. Try swapping the two connections to the rails.
If nothing happens at all you can see what the sketch is doing by running the serial monitor. In the Tools menu select Serial Monitor. At the bottom right of the monitor screen check whether the speed is set to 115200 baud. If it is not, select 115200 from the drop-down list. You should then see the messages from the sketch appearing at intervals. If necessary reset the Arduino, or close and restart the serial monitor. To test the switches, hold the spare magnet next to each switch.
Modifying the model and sketch
Once you have got the basic model working you may like to add some more features to the model and modify the sketch to make them work. Here are some suggestions. My reference document tells you how to control the additional devices.
1. Make the tram wait for a random time at each stop. (See the random() function in the Arduino reference.)
2. Add another stop with another switch and make the tram stop there - sometimes.
3. Add a set of traffic lights using bright LEDs, and make them change regularly. Then make the tram obey them.
4. Add a barrier or lifting bridge, powered by a motor or servo, which is raised and lowered as the tram approaches and leaves.
I would really like to hear from you if you build this model, and will do my best to help if you have problems. Please send email to email@example.com
If you have any interesting information or comments about this page, please add them here: