Sei sulla pagina 1di 24

Arduino UNO Tutorial for

Beginners

Chapter 1: Start Learning with the Arduino UNO


Board
Welcome to the first chapter of my Arduino Tutorial for Beginners. In this chapter, you will
learn the very basics of the Arduino platform so that you can start programming. I will teach
you how to set up your computer and write your first program for the Arduino Uno board.

What better place to start learning about the Arduino platform than with the Arduino
Uno board. For PLC programmers it can often be an advantage to know microcontrollers.
They are very similar in many aspects, and knowing about microcontrollers can help your
understanding of how the PLC works.
If you’re in Europe the Arduino recently changed its name to Genuino.

The Arduino Uno or Genuino Uno board is a simple open source electronics prototyping
platform. It has an Atmel ATmega328P microcontroller which makes if perfect for learning
about embedded programming and microcontrollers.

There are several reasons the Arduino Uno is perfect for beginners:

 Simple connection to computer


The Arduino Uno board has a USB port and can therefore easily be connected to
a computer. All the drivers are even included in the Arduino IDE so that’s the only
thing you’ll have to install.
 Easy wiring
All the pins on the ATmega328P microcontroller are connected to headers on the
side. In that way you can wire it to a breadboard without any hassle.
 Simplified programming language
When you are programming the Arduino Uno you will be using a simplified
programming language. Actually it is simplified C programming language.
 Lots of support
The whole Arduino platform is very popular and has millions of users. This
means that there’s a lot of help to get when you are having problems. You will be
able to Google many of your problems and find many solutions.

Since it is so good for beginners why don’t we just go ahead and start the Arduino Uno
tutorial by setting up your computer.

Arduino Uno Software


First of all you want to install some software for the Arduino Uno.

There are several IDE’s (integrated development editors) available to program the Arduino
boards. In this tutorial we will be using the Arduino IDE which is a free open source IDE by
Arduino.

Arduino IDE is a very simple piece of software to program Arduino boards. The download
and installation process is very simple.

Download the Arduino IDE here.


When you have downloaded the IDE you have to install it. Installing it is simple and you
don’t have to do much else than accepting terms of conditions and select the components
you want to install.

Here are some instructions to install Arduino IDE on the different platforms:

Windows

Linux

MacOS

After the installation you can now start the Arduino IDE.

Most of the IDE will be a while field where your code goes, menus in the top and a terminal
in the bottom.

Next step is to connect your Arduino Uno to your computer.

Connecting the Arduino Uno


Before you connect the Arduino Uno for the first time you should close the Arduino IDE.

To connect the board to your computer you will need a USB 2.0 A/B cable.

On the side of the Arduino Uno there is a USB type B plug. Plug the cable into that and the
other end to one of the USB ports on your computer. The board will automatically be
powered up via the USB and you will see the ON LED light up green.

If you’re using Windows you will have to find the drivers the first time you connect your
Arduino. In the pop-up window you should choose to look up the drivers on your computer.
When you click browse go to the folder where you’ve installed Arduino and choose the
folder called Drivers.
After that you can start the Arduino IDE.

Let’s check if the connection to your Arduino Uno works.

Go to the menu Sketch -> Upload or press Ctrl+U.

This should start uploading the code written in the white box to your Arduino. The code
doesn’t do anything or have any functions. It is just to check if you have a connection to the
board.

If the upload is successful you should see the message “Done uploading.” in a blue bar just
above the black terminal window in the bottom of the IDE.

You might see the bar is orange instead of blue with the message: “An error occurred when
uploading the sketch.”

This is most likely because you haven’t chosen the right port.

Go to the menu Tools -> Port and then choose the COM port where (Arduino/Genuino Uno)
is included in the name.
Choosing the right COM port in Arduino IDE

Now try again to upload the code (Sketch -> Upload or Ctrl+U) and it should work.

Congratulations! You are now ready to start programming with the Arduino Uno!

Your First Arduino Uno Program


Before we go ahead there’s one new word you have to learn. In the Arduino IDE the code is
not called a program but a sketch.

Since we are using the Arduino IDE I will be using the word sketch for our code because it
makes it easier to navigate in the IDE.

Let’s start with our first sketch!


The only things you need for this is the Arduino Uno board and the USB cable. The Arduino
Uno board has a build-in LED (light emitting diode) connected to one of the digital
input/outputs.

Arduino Uno Digital Pins


If you look at a pinout for the Arduino Uno board you will find that all the header connectors
in the sides are connected to the microcontroller.

There are both pins for powering and communicating with the Arduino but what we need to
look at now is the digital inputs/outputs.

At the top of the Arduino Uno board there are headers counting from 0 to 13 followed by
one called GND. These are all digital inputs or outputs and the last one is the ground. You
can see on the Arduino board that they are all marked as “DIGITAL”.

So what are all these digital pins? And what do I mean when I say they are both inputs and
outputs?

Digital Inputs or Outputs


All the digital pins from 0 to 13 can be set to be either inputs or outputs. You will do that in
the code.

We call these pins digital because they have two states.

HIGH and LOW.

These two states are the voltage level. For example if you set one of the digital pins to be
an output (as we’re going to do soon) you can set the output to be either HIGH or LOW in
the code.
If the output is LOW the pin will be at 0 volts. But if it’s set to HIGH the voltage level be 5
volts.

This is very useful for us. If you know a little bit about electronics you will quickly realize that
we can use this changing between 0 and 5 volts to control things.

Let’s go back to the code and see how we can setup the digital pins.

Arduino Minimum Code


When you open the Arduino IDE you will typically see a piece of code that looks like this:

void setup() {

// put your setup code here, to run once:

void loop() {

// put your main code here, to run repeatedly:

This is actually the minimum code your Arduino Uno board needs.

Setup and loop are both functions in C. C is the programming language used for Arduinos.
In fact, it’s a simplified version of C, but the syntax is the same. Programming
microcontrollers with C can quickly become complicated because you have to deal with
memory and so on. The fact that you don’t have to is one of the biggest advantages of the
Arduino platform.
If you have never seen the C programming language before I would recommend you to take
the first chapter in this C tutorial.

First, take a look at the setup function. Inside you will see one line of code. This is not really
code but rather a comment. Everything on a line after two slashes is a comment and will be
ignored by the Arduino.

Arduino Setup Function


As the comment says, this function will only run once. It happens when you power up the
Arduino or when you press the reset button.

This can come in very handy. You can use it as a function to setup variables and so on in
your program. That’s why the function is called setup. The Arduino will simply run this code
one time and then go to the main function.

Remember that the digital pins on an Arduino can be set to either inputs or outputs. The
setup function is the perfect place to define that since you only need to do that one time.

Defining Output with pinMode()


In order to define whether a digital pin on your Arduino Uno board should be an input or an
output, you have to use another function. This is also a standard Arduino function. We will
be using the function pinMode().

Arduino uses simplified C with a lot of built-in functions. As described before, this will make
it a lot easier to configure and program the board. The standard function for defining the
digital pins is the pinMode() function.

If you look in the reference for the pinMode() function you will see this:

pinMode(pin, mode)
This is the syntax of the function. You can see that the function takes 2 parameters: pin and
mode.

pin: The number of the pin you want to set

mode: To what you will define the pin (e.g. INPUT or OUTPUT)

With that in mind, let’s go back to our first program.

On the Arduino Uno board there’s a built-in LED. This LED is connected to pin # 13.
Remember that this is an LED, so we want to set that pin to an output. This is how we will
do it:

void setup() {

// put your setup code here, to run once:

pinMode(13, OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

You can see that the pinMode() function is placed in the setup() function. This is because
we only need to set the pin one time.

Be aware that the default state of the Arduino digital pins is input. So if you want to use
them as inputs you don’t even need the pinMode() function.
Built in LED and Constants
There’s another way to set the pin number 13. The pin is special because it has the built in
LED connected to it. So instead of just using the number 13 in our function, we can use
something called a constant.

Like the functions Arduino also has some predefined constants.

A constant is a predefined expression. Just take a look at the reference site for
constants provided by Arduino. You will see some constants like the pin levels (HIGH and
LOW), pin modes (INPUT, OUTPUT and INPUT_PULLUP). But if you scroll down a little
further you will find a constant called LED_BUILTIN.

This constant always refers to the pin where the built in LED is connected. On most Arduino
boards it is connected to pin # 13. So instead of just using 13 in the pinMode() function we
could use LED_BUILTIN:

void setup() {

// put your setup code here, to run once:

pinMode(LED_BUILTIN, OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

}
In this way our code becomes a little more generic. You can now use this code on any
Arduino board and not just the Arduino Uno.

Arduino Loop Function


Next step is to look at the loop() function. Because now that we’ve setup the pin we need
it’s time to do something with that pin.

After running the setup() function the Arduino will go on to the loop() function and run that.
But this function is a little different than the setup() function. The loop() function as the name
says will run over and over. Like an infinite loop.

The loop is where the real fun happens. It’s here you will write the behavior of the program.

Since we already defined pin # 13 (LED_BUILTIN) to an output, let’s start by turning on the
output. This will turn on the built in LED on the Arduino board. Because what we will be
doing is setting the state of the output to HIGH. As said before the state of HIGH will set the
voltage level of the pin to 5V.

Setting Output with digitalWrite()


To change the state of an output guess how?

Again, Arduino has a predefined function for doing so. Just like the pinMode() function, this
function also takes parameters. Here is how the syntax looks in the official reference from
Arduino:

digitalWrite(pin, value)

The function also takes two parameters: pin and value.

pin: The number of the pin you want to write to

value: The value you want to give this pin


The values we’re going to use are HIGH and LOW. These are the predefined constants that
will set the voltage level of the pin.

Since we want to turn on the LED we have to use HIGH. Because when we apply 5 volts
the LED will turn on. The function we have to write in the loop will look like this:

void setup() {

// put your setup code here, to run once:

pinMode(LED_BUILTIN, OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

Take note that I use the constant LED_BUILTIN instead of just number 13. You can use this
constant every time you want to work with this pin. It makes your code much easier to read
and understand. You will always see that this is the pin where the built in LED is connected.

You can also see that I’ve added a comment after the line of code. This is also to make the
code easier to read and understand. Especially when you are learning it’s very important to
comment your code.

It is now time to put this code into the Arduino and run it!
Compiling and Uploading the
Sketch
I already showed you how to upload the sketch (program) to the Arduino board. But there is
one thing you always should do before uploading. That is to verify your code. We have
written some lines of code, and we want to make sure that they work. Because things can
go wrong if you upload a piece of code with errors to the Arduino board.

Verify the Arduino Code


With all the above code in the editor go to Sketch -> Verify/Compile or press Ctrl+R.

This will not upload the code to your Arduino. It will verify your code. Checking the code for
any errors and make sure it will work on your board.

If your code has no errors the verification will look like this:
Verify Arduino Code with Sucess

Good job! You’ve written Arduino code that works!

But what will happen if there is some errors in the Arduino code?

For the sake of learning, let’s make a syntax error on purpose. After every expression there
is a semicolon to end the expression. In this way the compiler will know when an expression
ends and a new one begins. Forgetting to put the semicolon is one of the most common
programming mistakes. Even for experienced programmers. Let’s make that mistake and
see what happens.

Change line 4 from this:

pinMode(LED_BUILTIN, OUTPUT);

to this:

pinMode(LED_BUILTIN, OUTPUT)

Now, go to Sketch -> Verify/Compile or press Crtl+R

The verification will now look a bit different:


Verify Arduino Code with Syntax Error

What you will see is that the status bar turns orange. At the same time an error message is
shown in the status bar and one line turns red. This is extremely helpful because you will
both see what the error is and where it is.

The error message is:

expected ‘;’ before ‘}’ token


What this message means is that the compiler needs to see a semicolon before the bracket.

Line 4 is an expression. But without the semicolon the expression never ends. The compiler
will just continue to read the code expecting the expression to continue or end. When the
compiler meets a bracket sign it gets confused. Because the bracket will end the setup
function before the expression ends.

To fix this, simply put the semicolon back on line 4.

Uploading Sketch to Arduino


Now that you have a code without errors it’s time to upload it to the Arduino board.

As described before this is done in the menu Sketch -> Upload or just by pressing Ctrl+U.
Upload Arduino Code to Board

If you get the message: “Done uploading.” you have successfully compiled and uploaded
your sketch to the Arduino board.

To those of you who have good eye you might have noticed something before the upload.
Before uploading the Arduino IDE will compile your sketch. Compiling means that your code
will be translated into code that the Arduino (AVR microcontroller) can understand. A
microcontroller is not as intelligent as we are. In fact, the only thing it can understand are 0’s
and 1’s. All your code will be translated into 0’s and 1’s when compiling.
With the code now in the Arduino, take a look at the board.

You should see that the LED on the board (close to pin 13) called L1 is now on. This is
because we’ve changed the state of the pin to HIGH. Pin 13 now has a voltage level of 5
volts.

As a little experiment, we will change the code. Instead of setting the pin state to HIGH we
will set it to LOW. This should set the voltage level to 0 volts (ground) and turn of the LED.

The code should look like this:

void setup() {

// put your setup code here, to run once:

pinMode(LED_BUILTIN, OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)

Look closely at the Arduino board while uploading this code.

You will see that two other LED’s start to blink. They are called “TX” and “RX”. These two
LED’s are the indicators of communication. Every time they blink communication to the
Arduino Uno board is going on. Communication happens when you as here upload a sketch
to the board.
As soon as the uploading process is done the LED’s will turn off. The built in LED will also
turn off due to our new code.

Working with the Loop()


Let’s dig a little deeper into this loop() function. Again, this loop runs over and over again.
We can use this to make our program a little more fun.

Right now we only have one line of code in the loop() function. This line of code will be
executed over and over. Pin # 13 will be set to the same state again and again. The result
will be that the LED will always be either on or off.

What if we write two lines of code in the loop. One that turns the LED on and one that turns
it off. This should in theory make the LED blink.

Here is how out code will look now:

void setup() {

// put your setup code here, to run once:

pinMode(LED_BUILTIN, OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)

}
The loop will now run these two lines of code over and over. First, turning the LED on and
then off, then on, then off…

Try to upload the code and see what happens at the Arduino board. What happens now is
something that you might not have expected. The LED does not turn on and off, but rather
shines vaguely.

Arduino UNO LED lights with half brightness


This is because the Arduino is fast. Very fast. Faster than the human eye. As a matter of
fact the LED actually blinks. It blinks so fast that you will neither see the LED on or off. You
will see the avarage of this. Since the LED is on 50% of the time and off 50% of the time
you will see the LED shining with 50% intensity.

Slow Down the Code with Delay()


What we want to do now is to slow down the code a bit. Slow it down so we can see the
LED blink.

This is done by putting in delays. So that after the LED is turned on we want to wait a bit.
The same after we have turned it off. In that way we will have time enough to see the LED
blink.

Here again we are lucky to use the Arduino platform. Because the Arduino has a predefined
function for putting in delays in our program. This function is simply called delay() and takes
one parameter. This is how the syntax looks like in the Arduino reference:

delay(ms)

The parameter in this function is ms. Here goes the amount of time you want the delay to
be. The delay is set in milliseconds. In one second there are 1000 milliseconds (ms).

Let’s put these delays into our code. Half a second should be enough to see the LED blink.
We will therefore put 500ms as a parameter in the delay functions:

void setup() {

// put your setup code here, to run once:

pinMode(LED_BUILTIN, OUTPUT);

}
void loop() {

// put your main code here, to run repeatedly:

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(500); // Wait 500 milliseconds

digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)

delay(500);

Take note that I put the delay() function both after turning the LED on and off. By doing so
the LED will be on for 500ms and then off for 500ms.

Try to upload this code and see how the LED now behaves.
Arduino UNO blinking builtin LED

You now got a blinking LED!

And the LED keeps blinking since the loop runs over and over again.

Potrebbero piacerti anche