So your ready to jump into the vastly complex world of electrical engineering. Weather you have a specific goal in mind or just think Microchips are cool in general I aim to assist you in your learning experience by posting several articles based around the Arduino platform. I will be using a mixture of the Duemilanove, Uno, Mega, & Ardweeny as the main platform. First thing is ordering your unit which I already covered in a previous article located here, It is also extremely important to get organized and order some methods of storage for your Micro’s and electronic components. I cover storage in the previous article but I don’t cover what type of components you will be storing, these will be a mixture of capacitors, resistors, diodes, transistors and other useful parts. You will learn as you go along that is good to keep a stock of cheap parts on hand because once you realize you need the part to finish a project or just to finish something you are tinkering with, you will be really bummed out when you have to put the project on hold for a few weeks while you wait for the parts to come in the mail. Alternatively you can buy most small parts locally at an inflated price of 2-10x the normal amount, no worries I will go over some great locations to stock up on parts when the time is right.
Before we completely get started I want to touch on the meaning of the Arduino, why it exists and why it is so successful in my own interpretation. To me the Arduino is very much like the programming language AutoIT, the reason I say this is picture a person or group of people who have an extreme love for software design or hardware design and they want to share this love and help others to experience the same level of interest and creativeness as they do. The problem is that in order to teach someone C++ or quickly demonstrate to them how cool the language is rather difficult and its much easier teaching a new language like French compared to teaching C++ to a non-technical person. Because of this the language AutoIT was developed as a programming language that was extremely capable but also too the amount of code required to complete similar projects and reduced it by a considerable amount. This is also very true when speaking about the Arduino, consider trying to learn a standard Atmel in comparison to an Arduino and the amount of code and training required to program each. While its true of both C++ and programing the Atmel that they are much more powerful in comparison to AutoIT and the Arduino respectively but the advantage comes from the amount of time it takes to teach someone how to do similar things on each platform. There are several reasons I believe these platforms have been so successful, one of which is how much easier the platform is in comparison to its alternative, the second being how powerful the platform is in comparison to its alternative, the third being they are both free.
You should have your Micro in hand at this point, Arduino software downloaded and installed and have read through the instructions on how to connect and program your Arduino. I won’t be covering any of that so I suggest practicing connecting to your Arduino and verify its working by selecting the proper COM port and sending some example code to your Arduino.
The first thing we are going to do is blink the on-board LED on the Duemilanove and go over a quick run down of the code. I am going to refrain from any electronic component explanations in this guide and I don’t plan on ever touching on the bare basics of electrical engineering so I suggest grabbing some external guides to make sure you are up to date on the basics of AC, DC, Voltage, Current, Resistance, Capacitance & OHM’s law. I will likely go over some basic things later like pull up / down resistors, voltage dividers, RC filters and other basic stuff. I want to focus on the code itself and how to interface components so I assume if you are reading this you are familiar with the components of OHM’s law in the least.
The Basic Blink Sketch
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off delay(1000); // wait for a second }
Lets jump right into the code and I will explain each line so you fully understand what is going on. One great thing you will notice is that I have the Code Syntax highlighted which makes reading the code much easier. I recommend opening any code from this point forward in the Arduino IDE so your code will be highlighted as well. I know the colors are not the same as you will see in the Arduino IDE and that is because I am using C++ as the code “template” to interpret the code. Maybe later on I will make a custom template just for Arduino code and then the examples will be highlighted the same colors. Until then it should be easy enough to see the differences in the code.
Comments or Commented Code, This code is Grey in color in the Arduino IDE and every language has a Comment character or set of characters. These are characters that can be placed before, after or around some text and from that point on that text is ignored by the program. Comments are only there for humans, they are there for the author and others that they share their code with. In the Arduino / Wiring language you will see 2 ways to comment code. The first is a simple // double forward slash, the second is a /* forward slash asterisk and then at the end of the comment an asterisk forward slash */. Never try to decipher code online until you have copy and pasted the code into the Arduino IDE unless you fully understand the Wiring language. If I find example code online I will always copy it and then paste it into a fresh Arduino window so I can really see whats going on. The syntax highlighting really helps you focus on the code and see past all of the comments and other garbage.
Before we go further please note the Case sensitivity in the Wiring Language. You will find the Wiring language and C are very similar and they are both case sensitive. If your code doesn’t work it is always a good idea to check your case. Another issue I have ran into is using a function that is already defined by the Arduino language and I thought I just made it up, if this happens you can just change the case and it will be a completely different function.
After the big comment blurb at the top we have the first significant line of code and that is the setup function. Just about anytime you see a word followed by () this indicates it is a function of sorts. The reason the word void is before setup and later the word loop is because the word void specifies that nothing is to be returned from this function. Other functions are in place because it is there job to do something and then give the result back, but because we don’t want anything back we specify void.
Next we have pinMode which is a function and what this function does is specify how to use one of the Arduino pins. In this example the pin is number 13 and we are telling the Arduino it will be an Output. Earlier I said when you see a word followed by () it is usually a function, I want to add that inside of the () is the function parameters, in other words they tell the function what to do.
If you are using the Arduino IDE and put your cursor after the } sign you will notice it will highlight its partner {. This is helpful because unlike Basic type languages you don’t have EndIF, EndFunc of anything like that so it helps to keep track of the scope of the function you are highlighting.
After pinMode(13, OUTPUT) you will see the first } sign which is connected to the { sign after void setup. This means everything from void setup() to the } sign it is partnered with are all sub routines within the setup() function. Yes setup is just another function just like the ones you or I create, the only difference is that this function is already defined in the wiring language so we don’t need to define it. The setup function is in place to specify parameters to the Arduino before the main loop function is executed.
Next up we have the main Arduino function and that is the loop() function. This is another void function so it isn’t meant to return any data, its primary role is to continually execute the code within itself over and over until the power is removed from the device.
digitalWrite is another function like pinMode in that it is already defined by the Arduino / Wiring language but we need to give it some extra direction. We need to tell digitalWrite what pin to “write” something to and what to do with that pin. First we tell it pin 13 since that is the pin we are working with, we then tell it to write HIGH. Remember in electronics when you see HIGH it means positive and when you see LOW is means negative. This is the part of the code that turns the LED on by sending power to the pin.
After the LED has been turned on we need to wait a second before turning it back off, this is where the delay() function comes in. We can specify the amount of time to wait in between the () symbols but the trick is that the time is in milliseconds much like any other software language. Milliseconds is easy to work with, just multiple by 1000, so 1 second is 1000, 5 seconds is 5000 and so on.
We now write the digitalWrite function again but this time turn the LED off, we follow that up by adding another delay before the LED is turned back on. All of this is wrapped up with a } symbol which specifies we are finished adding functions inside of the loop() function.
I hope this guide has helped you in understanding the bare basics of Arduino and Wiring. I will be posting a new guide soon with more LED fun-ness.