# Introduction to Arduino part 2 - Installing the software



## Shdwdrgn (Dec 23, 2014)

Before you begin experimenting with circuits on your arduino, you must first install the software on your desktop computer that allows you to write your programs to the arduino. Since more experienced people than myself have already written up excellent guides, I will point you to those to get started...

Windows
Mac OSX
Linux

The hardest step to get working is the blink example (note the linux guide does not have this, but you can follow along in one of the other guides). The blink example is similar to "hello world" in that this is the first step you take to see if your desktop is communicating with your arduino, and get something to actually happen -- in this case blinking an LED. If you have any trouble getting blink to work, feel free to ask questions below! Something to keep in mind is that whatever program you write to the arduino will _always_ be there, until you write a new program. So if you unhook your arduino from the desktop computer and plug it into a different power supply, you will see that it still starts running the blink program every time you power it on. When you write a new program to the arduino, it will erase the old program, so only the newest code will exist.

So let's assume you have an LED blinking -- what does all that code really mean? Starting with the simple structure of the program, you will have two sections that begin with "void setup()" and "void loop()".

"Setup" is where you put your initial instructions, such as telling the arduino that you want a specific pin to be either an input or output. The items in this section only get run one time, when the arduino is first powered on, so you only prepare things here while the real action happens later. For example, if you hook your arduino to the motor of your locomotive, you might want to initially make sure the motor is turned off, or perhaps quickly flash a cab light as a visual indicator that the arduino is working.

The "loop" section is where you handle anything that will change. In the case of the blink example, we are turning the LED on and off. "Loop" will run continuously until the arduino is turned off. So what are we doing in the blink program? Well first we turn on the LED with the digitalWrite command, setting the output to HIGH (on). Next, we use a delay command to wait 1000 milliseconds (1 full second). After one second has passed, we use digitWrite again to set the output LOW (off), and then another delay to wait one more second. Now we've reached the end of our commands, but remember that "loop" keeps running indefinitely, so it will start back at the beginning and do the same thing all over again, continuously turning the LED on and off.

Hopefully that was all pretty simple, and it gives you a taste of what programming looks like. A program will follow a specific set of commands and runs very much like a train -- you give it a path to follow, and sometimes you give it alternate choices, like a turnout, to change the path of the program, but no matter what happens the code will always follow the exact path that you give it.

Lets touch on one more subject this week. When you were setting up the arduino software and selected the board you have, you may have noticed there is quite a list of different boards this software will work with. _But wait, there's more!!!_ You are not limited to just these boards, and in fact a number of developers have created add-ons to allow you to program other computer boards in the same way. Your desktop arduino software has made it easy to load support, and in a future project I will be using an ATtiny85 board as an example of keeping things as small as possible.

If you want to see a list of microprocessor chips that are compatible, take a look at this page: http://playground.arduino.cc/Main/ArduinoOnOtherAtmelChips. Near the top you will see an entry for ATtiny44/45/84/85. Right underneath than is a link for the Boards Manager URL. You want to copy that link (usually something like right-clicking the link and selecting "copy link location"). Now within the arduino program, click on *File* -> *Preferences*. On the line for Additional Boards, paste the URL from the link above. Note that if you are adding multiple boards, you can click the icon to the right of the text box and a new window will open to enter multiple links. Now click OK. The last step will be clicking *Tools* -> *Board* -> *Boards Manager*. If you scroll to the bottom of the list, you should see "attiny". Click on that line, then click the install button. This should only take a couple seconds, then you can close the window. Now if you once again click on *Tools* -> *Boards*, you will see ATtiny at the bottom of the list of boards that your system can work with. Congratulations, you now have support for a completely new line of processors!

Coming up next: We will start using the breadboard and wires to hook up a switch and two LEDs, simulating a crossing signal.


----------



## ogaugenut (Dec 27, 2012)

I've been following along. I got a Uno starter kit to learn this. I understand that I will want smaller boards for actual projects. The Uno seemed worth it for learning.

The manual I got stated that most Arduino boards now come with blink loaded and mine did. It suggested changing the blink rate in the code to confirm that the process of loading a program is working (which I did).

Thanks
Bill


----------



## Shdwdrgn (Dec 23, 2014)

That is true, every arduino I've picked up has come with the blink code already installed. There's another board I really like called the "Teensy" which can also be programmed with the arduino software, and it too came with blink installed. That's a good point to just change the blink rate to confirm that your updates are working. Hopefully I provided enough info to make it obvious how you would do that.

I ordered the breadboard listed in the first article, and it just came in yesterday. Good timing, because I need to start working on the circuit for what I am planning to show (not that I don't already have other breadboards  ). Even though the circuit may be obvious to some, I think pictures help others to understand what is going on. I'm expecting that at least some people who find these articles may have no previous experience with electronics, while others may be twiddling their thumbs wondering when I'm going to get to the good stuff. My experience has always been that getting your initial set of tools up and running is always the most time-consuming part of a project, whether you are working on computers, metal, gardening, or anything else, and I want to make sure everyone has those tools available to follow along here.


----------



## 2869ral (Dec 15, 2015)

Im just waiting for my usb cable to arrive, the one that came with my kit was too big. I just received the stepper motors that some of us ordered from China. They are a lot smaller than i thought, but should still work ok.


----------



## Shdwdrgn (Dec 23, 2014)

I think I ordered my steppers just a few days after you did, so hopefully mine arrive soon. I think the body on them was only 7mm diameter? Really tiny, but great for hobby use! I did finally receive the new voltage regulators last night, and they're looking very promising (and very tiny!). I still have five more orders I'm waiting on from China.

[EDIT] *Holy crap, you weren't kidding!!!!* I mean, I knew the dimensions of those motors, but actually _seeing_ them, they are smaller than I imagined. Awesome, now the gears in my head are turning towards what all I could embed these in...


----------



## 2869ral (Dec 15, 2015)

Shdwdrgn said:


> I think I ordered my steppers just a few days after you did, so hopefully mine arrive soon. I think the body on them was only 7mm diameter? Really tiny, but great for hobby use! I did finally receive the new voltage regulators last night, and they're looking very promising (and very tiny!). I still have five more orders I'm waiting on from China.
> 
> [EDIT] *Holy crap, you weren't kidding!!!!* I mean, I knew the dimensions of those motors, but actually _seeing_ them, they are smaller than I imagined. Awesome, now the gears in my head are turning towards what all I could embed these in...


Oh boy! We have created a monster! LOL


----------



## 2869ral (Dec 15, 2015)

Im actually thinking about placing a couple of these in my rail rebuilders building on some stands, because they look like lathes.


----------



## Shdwdrgn (Dec 23, 2014)

Well hook them up to an ATtiny board so you can actually make them spin. Gotta have that fine-detail action!


----------



## TomW2034 (Jun 2, 2015)

I just got an Arduino Nano, installed the software, and modified the Blink sketch to leave the LED HIGH for three seconds, and LOW for a half second. Uploaded it, and it worked :thumbsup:









I can tell this is going to be a lot of fun at a reasonable cost.

Tom


----------



## fcwilt (Sep 27, 2013)

You can do a lot with these little boards.

Good 3rd party support and many fine support groups.


----------



## Shdwdrgn (Dec 23, 2014)

Glad everyone is having fun with this subject! Unfortunately it looks like there's no way to get out a new article every week, so I'm just going to have to play it by ear and write when I am able. And as we move into the next parts, I will need to build and test circuits as I go. I'll also be covering more programming concepts, so after the next article everyone should have the tools to really start going crazy.

For what it's worth, I haven't even had time to play with the arduino in my 0-4-0 for over a week.


----------



## gunrunnerjohn (Nov 10, 2010)

The Arduino puts software control in the reach for most folks, it's one of the simplest platforms to crank out basic building blocks with. I use the PIC because of the size, but I also have the Arduino here and some boards that I've used for simple quick and easy projects. I just couldn't build some of the stuff I'm doing with the Arduino, my latest PCB only 0.4" x 1.3" in total.


----------



## johnfl68 (Feb 1, 2015)

gunrunnerjohn said:


> I use the PIC because of the size....


Gunner, have you seen the Adafruit Trinket and Trinket Pro (ATmega328 Arduino compatible)?

Trinket 5V (there is also a 3.3V version available):
https://www.adafruit.com/products/1501

Trinket Pro 5V (there is also a 3.3V version available):
https://www.adafruit.com/products/2000


I use the Trinket to control the different Color Looks of the Neopixel LED's on my Warren truss bridge, so I can change them with different holidays and seasons.

They are great little chips.

Lots of great stuff and tutorials over at Adafruit for those interested.


----------



## gunrunnerjohn (Nov 10, 2010)

I've briefly looked at Adafruit stuff, but I'm really mostly using the PIC. Since I actually design and build the PCB with all the stuff I need for an application, the canned stuff doesn't work for a lot of my projects. I do use commercially available modules at times, for instance this sound board uses a plug-in MP3 player and a plug-in RF receiver. The motherboard has the PIC processor that ties it all together and decodes the commands from the RF remote. It then manages the MP3 player, the two servo outputs, lighting outputs, and GP inputs.


----------



## Shdwdrgn (Dec 23, 2014)

John, I've seen some of the arduinos in raw DIP form, I think they were 28-pin 0.3" centers. Of course most are in the more compact solder-on formats like a PLCC, but the options _are_ there.

I've seen those SD carriers, been thinking of grabbing one in case I need to store some raw data or maybe sounds files for a loco. What's your RF board there, one of the 433Mhz variety?


----------



## gunrunnerjohn (Nov 10, 2010)

I actually have a couple of the Arduino processors bare, I bought them with the thought of a custom board. Then I get involved with the PIC, and figured it was probably a better choice size-wise. One issue I have is making sure that I can build prototypes of small quantities of any board I design. That limits me from the really small pitch parts that are almost impossible to solder without an IR station. I made the mistake of using some TSOP parts on a layout, what a raindance getting them soldered!  I currently stick with 603 or larger components and the SOIC packages for integrated circuits. None of these components are that difficult to solder by hand.

Yes, the board is half of the 4-button remote control set, you can get them for around $3 on eBay.

The micro-SD equipped board is an MP3 player with a serial control interface. Sadly, you can't really use it for data, though that's an interesting idea.  You can look it up by the number on the top, BY8001-16P.


----------



## Shdwdrgn (Dec 23, 2014)

Ah, didn't realize that SD card carrier was part of another board. If you look on ebay, you can get SD boards for around 72 cents that use an SPI interface, and there is code available for the arduino to read/write directly to the cards. And I see they have some micro-sd carriers as well, that would fit a lot better into a loco.


----------



## gunrunnerjohn (Nov 10, 2010)

Yep, that SD holder is for the sounds. I've seen the micro-SD boards, they would be useful in some situations. In my case, I didn't want to do all the work of a software MP3 conversion, so I let someone else do it.


----------



## TomW2034 (Jun 2, 2015)

*Stand by - I just got an Arduino Nano*



gunrunnerjohn said:


> ... I use the PIC because of the size, but I also have the Arduino here and some boards that I've used for simple quick and easy projects...


I agree with your sentiment - I am thrilled with both my crossing bell, and crossing lights sequenced by independent LM555 chips.

Here's a short clip taken today of my 2029 waiting till everyone knows it's about to pull freight on the T&K Railway (the lights flash red in real life):






But I am now finding out what an Arduino Nano will do for me and am looking forward to the challenge.

Stay tuned! 
Tom


----------



## gunrunnerjohn (Nov 10, 2010)

I think I'd want an on/off switch on that bell if the train is continuously running!


----------



## TomW2034 (Jun 2, 2015)

*Parts have been ordered*



gunrunnerjohn said:


> I think I'd want an on/off switch on that bell if the train is continuously running!


An IR detector is on a boat from China as I type just for that purpose.

Tom


----------



## Lemonhawk (Sep 24, 2013)

Nice looking boards GRJ!


----------



## TomW2034 (Jun 2, 2015)

*RTC module*

I am getting ready to experiment with a Real Time Clock, and have seen the Arduino paired with both the DS3231, and DS3234 modules. While neither is that expensive, the DS3231 is half the cost.

Is there a good reason to choose one or the other (or maybe a different RTC module altogether)?

Tom


----------



## fcwilt (Sep 27, 2013)

TomW2034 said:


> I am getting ready to experiment with a Real Time Clock, and have seen the Arduino paired with both the DS3231, and DS3234 modules. While neither is that expensive, the DS3231 is half the cost.
> 
> Is there a good reason to choose one or the other (or maybe a different RTC module altogether)?
> 
> Tom


As to the two units mentioned it would seem to depend solely on which communication bus you would prefer to use, I2C on the DS3231 or SPI on the DS3234.


----------



## Shdwdrgn (Dec 23, 2014)

For a small project there is probably no difference at all between the two modules, however you do have to load a library to talk to each bus type. If you were doing a project with a lot of I/O devices, you could save programming space by having all of those devices use the same interface, and thus the same communication library.

Sorry for the long delay in the next article of this series... My refrigerator took a dive on Monday and I've been trying to troubleshoot. Failing that, I have someone coming to look at it today. It's been one of those weeks...


----------



## DonR (Oct 18, 2012)

Refrigerator on the Fritz? Not Cool!

Don


----------



## rva1945 (Feb 11, 2016)

I 've just written a sketch or program that will control my train. It accelerates the train until it passes near an IR sensor, then it will gradually decrease the speed until it stops (at the station); after a while, it accelerates again and so. So far I'm debugging it using push buttons but I'm in the process of picking the right IR sensors.


----------



## Shdwdrgn (Dec 23, 2014)

My first train program would slowly ramp up to 3/4 speed, drop back down to a stop, then build up to half speed in reverse. Now I've added WIFI to it, and have it connecting to a server for commands, which are provided through a web interface.

Eventually I want to be able to have the trains control themselves, but to do this requires a fair number of sensors around the layout to tell each train its current position and warn of any obstacles. For this, I've been considering using LED trackside signals... something that shows if the track ahead is clear, but actually sends a signal to the loco with information such as position. I could still get certain information over wifi such as loads waiting for pickup, but the first challenge is just to get the trains wandering randomly around the layout without running into each other. (Well ok, the FIRST challenge is getting the layout built...)


----------



## rva1945 (Feb 11, 2016)

Hi:

Seems as if you are (or will be) using digital control, that is not my case. I'm still a newbie starting with a very basic kit.


----------



## fcwilt (Sep 27, 2013)

DonR said:


> Refrigerator on the Fritz? Not Cool!
> 
> Don


Well at least not cold enough.


----------



## fcwilt (Sep 27, 2013)

Shdwdrgn said:


> Eventually I want to be able to have the trains control themselves


If you are doing this just for fun that's grand BUT coding a program to handle this task in a comprehensive way is a very, very significant project.

If you are really wanting your layout to be automated save your self a whole lot of time and trouble and use...

http://www.freiwald.com/pages/traincontroller.htm

You can download any of them and try them out as it has a simulator feature so you can test things.

Trying it out like this for a while would tell you a lot about the complexity of such a project.

The trial version can even control actual hardware for a short span of time (10-15 minutes) before it shuts down and says thanks for playing.

If no hardware is connected it will not timeout.

Good luck.


----------



## rva1945 (Feb 11, 2016)

Very interesting, maybe I will give it a try, thanks.


----------



## Shdwdrgn (Dec 23, 2014)

Yep, the fridge is dying. Compressor is on its way out, nothing to do but look for a new one. Unfortunately 'new' is probably not better, I find a model I like and then find lots of complaints about them barely getting cold enough. *sigh* I already have that problem, I'm not going to put up with it in a brand new unit.

@rva1945 - Are you using the arduino to control the DC voltage to the track, or using it onboard the loco to directly control the motor? What I'm working on is probably similar to DCC, in that I expect a full 18v signal on the tracks at all times, however I'm sending my commands via WIFI. SO yes, I'm using a digital signal to control the speed of the motor by pulses, which is actually a better technique than controlling speed by voltage (pulses of full voltage allow the motor to run at low speeds but still have a lot of pulling power).


----------



## TomW2034 (Jun 2, 2015)

*Pipe Organ Parts Seem Like a Reasonable Pursuit*

One of the projects I have in mind for my Arduino is to control a PVC, 8-chime whistle to warn traffic crossings on the T&K Railway of heavy metal on the way.

The fun today consisted of both determining about 9 inches of water pressure was optimum, and that the blower needs to be in another room.

I am currently pinging pipe organ parts houses in a quest to find air-valves appropriate for the effort.

Any insights or leads to electro-mechanical hardware which could replace my hand/finger in this video would be much appreciated.












​

Tom


----------



## gunrunnerjohn (Nov 10, 2010)

You should make this a bit harder. 

DC-12V-1-2-New-Electric-Solenoid-Valve-Magnetic-Water-Air-N-C-Normally-Closed


----------



## TomW2034 (Jun 2, 2015)

gunrunnerjohn said:


> You should make this a bit harder.


I tried a water valve from a sprinkler system with no joy.

Water valves need a back-pressure measured in the range of many psi's instead of inches water.

Thanks for the suggestion, though.
Tom


----------



## gunrunnerjohn (Nov 10, 2010)

Tom, this one specifically mentions air and water. Since solenoid is opening the valve, are you sure this isn't going to work? I don't see how it could need any pressure to operate, I suspect it's just a gate valve. I don't see any reason it wouldn't work, and it's cheap to try.


----------



## Lemonhawk (Sep 24, 2013)

Reminds me of when I was a kid living in Columbus Ohio. We visited an OSU music professor and he had a pipe organ in his house. It was a small house but the basement rafters were filled with the organ pipes! The whole house much have vibrated when he was playing it! Great experiment Tom. Organs use big bellows, which reduces the noise of air production rather than compressors. Probably all sorts of advice on organ building on the internet


----------



## Shdwdrgn (Dec 23, 2014)

Are you actually getting tones from pure PVC pipes, or is there something inserted to act as the reed?

Just a quick update, I have the net article written, I just need to test the arduino circuit to make sure it works, and make up some diagrams. Funny thing, I realized my only actual arduino is soldered into a loco now, so I'll have to test with one of the other models and hope for the best. (I did get a couple more of the pro-micros ordered though)... So hopefully I'll have something to post this weekend.


----------



## TomW2034 (Jun 2, 2015)

*The Former*



Shdwdrgn said:


> Are you actually getting tones from pure PVC pipes, or is there something inserted to act as the reed?...


Each pipe has a wooden stopper with a chord removed to direct air to the beveled outlet.

USPS delivered two, different goody bags today of stuff to audition for getting-Tom's-fingers-out-of-controlling-air-flow: solenoid valves, and a servo with higher torque than the Arduino starter kit (to possibly operate a gate or butterfly valve).









It is shaping up to be a weekend of fun in my Shop!

Tom


----------



## gunrunnerjohn (Nov 10, 2010)

TomW2034 said:


> Each pipe has a wooden stopper with a chord removed to direct air to the beveled outlet.


Can you clarify what this means?


----------



## Shdwdrgn (Dec 23, 2014)

If it's similar to other pipes I've seen, it probably means the wooden plug has an angled slice out of one end that matches the angle you see cut in the pipe, plus I think the top edge is shaved so that some air still passes through the length of the pipe? I was never very good with that sort of thing, but I had a short interest and saw how some bamboo pipes were made (many many years ago).


----------



## TomW2034 (Jun 2, 2015)

gunrunnerjohn said:


> Can you clarify what this means?


The information at this link describes the process quite well.

Tom


----------



## TomW2034 (Jun 2, 2015)

*Proportional Whistle Control*



Wikipedia said:


> The older steam whistles were almost always actuated with a pull cord (or sometimes a lever) that permitted proportional (tracker) action, so that some form of "expression" could be put into the sound. Many locomotive operators would have their own style of blowing the whistle, and it was often apparent who was operating the locomotive by the sound...





TomW2034 said:


> One of the projects I have in mind for my Arduino is to control a PVC, 8-chime whistle to warn traffic crossings on the T&K Railway of heavy metal on the way...


Here is a short clip of an Arduino-controlled device I built today to modulate the airflow going to my whistle.






The next step is to incorporate it into the blower motor's enclosure. If all goes well, I will pursue some form of spring-loaded potentiometer to which a pull cord can be attached.

I must say that this little micro-processor has been lotsa fun.

Tom


----------



## gunrunnerjohn (Nov 10, 2010)

I use model airplane servos with the PWM outputs of the PIC for stuff like this. Is that what you have there?


----------



## TomW2034 (Jun 2, 2015)

The servo is an HS-311 made by HiTEC. I imagine it is used in a lot of hobbies.

Tom


----------



## gunrunnerjohn (Nov 10, 2010)

Yep, that's what I thought. Those are pretty "standard" servos, I have a couple in my parts box. I tried to find an assortment of all sizes from small to medium size to fit various uses. I used one of those servos in my Tie-Jector command upgrade.


----------



## TomW2034 (Jun 2, 2015)

gunrunnerjohn said:


> ... I used one of those servos in my Tie-Jector command upgrade...


What function is the servo providing in your application?

Tom


----------



## gunrunnerjohn (Nov 10, 2010)

Tom, the Tie-Jector in it's original form used trackside trippers to start and stop the action. Clearly, for command operation, that was not an option, very uncool.  Now I use the smoke control on the TMCC/Legacy remote to turn on and off the dropping of ties. The servo moves that control back and forth to engage the tie dropping mechanism.

Here's the old instructions for the original PW, the remake I converted worked the same way.


----------



## TomW2034 (Jun 2, 2015)

gunrunnerjohn said:


> ... I use the smoke control on the TMCC/Legacy remote to turn on and off the dropping of ties. The servo moves that control back and forth to engage the tie dropping mechanism...




That's neat - I like your adaptation.

Tom


----------

