Arduino Tutorial
Introduction
This is the first of a planned series on the Arduino. I plan to walk you through all the basics of using an Arduino from a simple bliking LED to more complicated stuff such as reading and writing seriel data using the I2C protocol. This is my first attempt at wriitng a tutorial of this sort so i apologize if it inst the most polished of tutorials.
What is an Arduino? the Arduino is an open source easily programmable micro-controller and electronic board. the Arduino is popular for hobbyist and for prototyping. It is programmed using an easy to use language which is a basic version of C/C++. One does not need to be an expert in either of those languages to use an Arduino. In this Tutorial I'll show the steps needed to create your first working program using an Arduino.This guide assumes very little in the way of programming knowledge or experience
What will you need
Getting Started
The first project we will make is a simple blinking light. The first step we'll do is to go over the code for it. the following image shows the code and a view which should be close to your own view in the editor you are using. I'll go over what each of these lines does. the complete code can be found here
#define LEDPIN 5
- the #define LEDPIN 5 statement acts as a simple text replacer. #define tells the complier to look everywhere in the code for the word LEDPIN and replace it with the number 5. This is done for the following reasons:
- Makes Programming More Convenient
- enables one to use a word, in this case LEDPIN, rather than remembering a specific number.
- Makes Reading Code easier
- when reading through the code, it is easier to understand a word like LEDPIN rather than just a number such as 5.
- Makes Changes And Updates Easier.
- if you wish to change the code to use a different pin, you simply need to replace the number 5 in the define statement with whatever pin number you are using. no other changes would be needed and now your code works using a different pin.
- all lines which start with '//' are comments. they are not executed as part of the program, they serve as comments and documentation written by the programmer to give information about that specific part of the program.
void setup()
- this is the starting point of every Arduino program. When an Arduino program is run, it starts at the setup function and reads each line, top to bottom, from the opening bracket { until it reaches the closing bracket }.
- void refers to the return type of the function. void simply means that the function does not return data.
- pinMode(LEDPIN,OUTPUT);
- this function, called pinMode, takes in a pin and sets the type of pin, either INPUT or OUTPUT. In this case it sets LEDPIN to OUTPUT.
- INPUT is used when you wish to set a pin on the Arduino in order to receive a signal on that pin.
- OUTPUT is used when you wish to have the Arduino send out a signal on that pin
void loop()
- the loop function is where the Arduino program goes next after finishing the setup function. just like with the setup function, it starts at the opening bracket and runs top to bottom until it reaches the closing bracket.
- The big difference is that when loop reaches the end of the function, it jumps back to the start of the loop function and runs again. The loop function will continue to do this until the Arduino program is stopped.
- digitalWrite(LEDPIN,HIGH);
- this function writes a digital signal to a given pin.
- In this case, it is writting a logic HIGH to pin LEDPIN.
- delay(1000);
- delay function causes the Arduino program to pause for a set number of milliseconds.
- in this case it pauses for 1000 milliseconds.
- digitalWrite(LEDPIN,LOW);
- The call to digitalWrite sets LEDPIN to logic LOW.
Building the circuit
here is a diagram of the circuit you should build
- connect one end of a wire to pin 5 of the Arduino and the other end of that wire to an empty row on the breadboard.
- connect the positive(anode) side of the LED to the row you connected the wire to.
- the anode pin is typically the longer of the two
- connect one end of the resistor to the row with the LED and the other end of the resistor to an empty row on the bread board.
- never connect an LED without a resistor
- connect one end of a wire to the row with the resistor and the other end of that wire to the GND pin of the Arduino
- finally, connect the Arduino to your computer using the USB cable.
here is a picture of what the final circuit should look like. once you have checked to makes sure it is connected correctly you can move on to the next section.
final steps.
Now that you have written the code and connected the circuit correctly, the next step is to compile the code and run it.
- You should first verify the code.
- if you are using the Arduino IDE then the verify code button is the one with the red arrow in the picture below.
- After Verifying the code and getting no errors, you next need to upload the code to the Arduino.
- if you are using the Arduino IDE then the upload code button is the one with the blue arrow in the picture below.
- Once the code is successfully uploaded to your Arduino it should start running within a few seconds. if it doesn't, try pressing the reset button on the Arduino.
congratulations, you have built and programmed your first Arduino creation.
Review
so far in this tutorial we have learned:
- how #define can be used to give meaningful names to things and can be used to make programming a bit easier.
- setup is the function you use to setup and initialize your program and arduino
- loop is a function which loops until the arduino is stopped.
- pinMode can be used to set a pin as either INPUT or OUTPUT
- digitalWrite is a fucntion which allows you to write either logic LOW or logic HIGH to a pin.
- delay is a function which causes the arduino program to pause for a given number of miliseconds.
thank you for checking out my tutorial. check back soon for the next part of this tutorial.