Arduino basics
From STAB Resources
Contents |
Introduction
What is ARDUINO?
ARDUINO is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. Arduino produces highly user friendly application boards for uC(Microcontroller) programming and application, with loads of features packed in. Plus it has its own IDE(Integrated Development Environment).
Why ARDUINO?
Arduino is best for dummies. If you have the dreaded "What is a uC?" question in your mind... Arduino is the thing for you. The Arduino IDE is a very user friendly environment with embedded examples in all the common domains of uC coding. The best part about Arduino is, it has inbuilt libraries for almost all uC applications from ADC to to LCD displays to Streaming and so on. So unlike AVR, you now dont need to keep setting the registers for every bit of code. Arduino has already done it for you.
Getting started
Hardware
Get the Arduino board. Connect it to the USB port of the computer through the USB cable. The USB cable is like the USBprinter cable. Fit the uC in the uC slot of the Arduino board. It is already present on the board when you bought it. In case of UNO its the ATMEGA 328P. And there you are! Setting up the hardware is done.
Software
Installation
Arduino is available for Windows,Linux and Mac OS. Arduino software download is available on the Arduino website. For the Uno board you need version 0021 atleast on all platforms. Open this link to get the software.
Windows
Download the latest version for windows from the above link. Unzip the downloaded zip file to the desired location. This is a step by step guide from installing Arduino and burning and running your first code on the uC. We have already completed the first 3 steps. Just install the drivers as specified in step 4 and you are good to go. This completes the installation of Arduino in Windows.
Mac OS X
This is the step by step guide to install Arduino on Mac.
Linux
This is the guide for linux. Installing Arduino on Linux is a bit painful as compared to the above two. You need to get a few more things. Open the Synaptic package manager, and install the following packages :
sun-java6-jre
gcc-avr(version 4.3.2 or later.)-- You can check the version by typing the command : avr-gcc --version in the terminal.
avr-libc
First code!
So now that you are done with the installation, lets burn our first code. Pin 13(pin numbers are written beside the pin slots on the board) of the Arduino board, has an LED connected onboard. Connect the board to the computer.
Open Arduino
Click on :
'file' -> 'Examples' -> 'Basics' -> 'Blink'
Click on the 'Verify' button to check the correctness of your code in the new window that opens.
Click on the 'upload' button.
There you go! Your led starts blinking at the rate of 1 blink per 2 secs. Now check the code for validity of your observation. You can try out other example codes given.
Features
No need of a USBasp
Arduino uses serial communication to burn the code onto the uC. So, unlike AVR, you dont need a separate USBasp to do that. But one thing... the uC should have a Bootloader initially burnt into it. The uC in any Arduino board, has the Bootloader already burnt into it. The name of the Bootloader for UNO is Optiboot.
But what is a Bootloader?
Bootloader can be considered as the OS of the uC. So when you burn a code on to the uC using a USBasp, its like working with DOS. But with the Bootloader, you get to burn the code directly... something like using windows.
Examples
As you saw above, there is an arsenal of examples in various domains at hand, in Arduino. You can study the examples specific to your requirement.
Serial monitor
The Arduino IDE has an embedded serial monitor. So you don't need to have a separate Hyperterminal, to check the serial output of your uC. Just open the serial monitor, and you get the output in it.Upload this code in the uC. Open the Serial monitor and you can see 'Hello World' getting printed every 2 secs.
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
Serial.print("Hello World");
delay(2);
}
Its that easy!
C like syntax
Arduino language has a very intuitive C like syntax. So you don't need to learn this language separately. All features like functions, for & while loops, switch cases, arrays are similar to C.
Terminal output
There is a terminal output at the bottom. This output displays the state, errors, code size and other such information(The serial output is not displayed here).
Hardware info
Pins
Not all the pins on the board can be used for all purposes. Only the pins with names Ax where x is a number from 0 to max, can be used for Analog input and output. Other pins with just numbers can be used as digital pins i.e. with digital high and low states. Pins with '~' before the number are PWM(Pulse Width Modulation)pins...but they can be used as digital pins as well.
Software Info
Reference
There is an exhaustive reference section in Arduino. This section contains all the information about every inbuilt function. You can find this in the reference folder inside the arduino-0020 folder. You can also get it by opening the :
help -> Reference
via Arduino. This reference is in html format.
Libraries
For using specific functions, like EEPROM or LCD, you need to #include<that specific library> as the first line of your code, just like in C or C++. Arduino has many other libraries which are not per-included in the software download. You can get these libraries from [http//:www.arduino.cc/en/Reference/Libraries here]. To install this library:
First launch Arduino
Open 'Sketch' -> 'Show Sketch folder'
Create a folder named 'libraries' in it
Download the zip file from the above link.
Extract the zip file to the 'libraries' folder.
Restart Arduino
Now you should see the new library in the list of 'Import Library'.
Caution
- The pin numbers of the uC and the pin numbers in the example codes of Arduino are different. The pin numbers used in the code are same as the pin numbers written on the "Arduino board". Following is the actual pin mapping of the uCs :
- Never give the voltage higher than the specification to the pins. For the 5V pin, even a voltage in range of 4.5V to 5V will do. Similarly, for a 3.3 V pin, a voltage in range of 2.8V to 3.3V will work. But not above the specification.