Arduino UNO Tutorial For Beginners
Arduino UNO Tutorial For Beginners
Beginners
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:
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.
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.
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.
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.
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!
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.
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?
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.
void setup() {
void loop() {
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.
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.
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.
mode: To what you will define the pin (e.g. INPUT or OUTPUT)
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() {
pinMode(13, OUTPUT);
void loop() {
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.
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() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
}
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.
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.
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)
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() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
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.
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
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.
pinMode(LED_BUILTIN, OUTPUT);
to this:
pinMode(LED_BUILTIN, OUTPUT)
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.
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.
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.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)
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.
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.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
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.
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() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
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
And the LED keeps blinking since the loop runs over and over again.