Beginner’s Guide to Arduino

Arduino Uno Featured Image Beginner's Guide

Whether you’re an embedded systems vet, high school scientist, or a curious person, you need to start your electronic explorations somewhere. If the manual isn’t enough, then check out this beginner’s guide to Arduino.

Also read: 6 of the Best Raspberry Pi Alternatives

What’s an Arduino?

Arduino is a brand of open-source microcontroller boards developed by the Arduino company.

When you’re talking about the Arduino, it’s usually about the board. Arduino boards are special because they’re durable. They’re meant to be used by students (or anyone) who could mess things up and break something.

The other thing that’s special about them is that they’re open-source. The datasheets for all Arduino boards and shields are available online. You can make your own Arduino board if you have the technical know-how.

What Does This Mean to Me?

For a beginner, this means:

  1. You don’t need to make your own circuits to study microcontrollers.
  2. These boards come with safety features to keep you from breaking them accidentally.
  3. The open-source hardware community has made a wide array of boards for different purposes – there’s even one meant for putting electronics on clothes!

Anatomy of an Arduino Board

There are many kinds of Arduino boards. Some are as tiny as a battery, while others are as big as a digital camera, but they all have a few parts in common:

  1. Microcontroller unit
  2. Header pins
  3. Power and USB ports
  4. Indicator LEDs

There are also other third-party, Arduino-based boards that are built by their own manufacturers, but normally, they have these things in common.

Microcontroller Unit (MCU)

The microcontroller unit, aka MCU, reads and controls all the inputs and outputs on the board. It also stores the user-made code that makes it do stuff.

The Arduino Uno R3 has a special removable MCU chip. This way, you can replace that part once it’s either broken or worn down. Other boards have their MCU chips soldered to the board itself. The drawback there is obvious, but they’re usually made to be much smaller and faster than their non-soldered counterparts.

Header Pins

To the sides, you should see some raised pieces of plastic with holes on top. They are female header pins. You’re supposed to put wires or male jumper pins in them.

Arduino Uno Pinout
Arduino Uno pinout

There are two kinds of pins: GPIO and power pins. GPIO pins let you process inputs and outputs. By default, all Arduino pins are inputs. On the other hand, power pins are meant for moving electricity around the board. 5V and 3.3V always emit as much voltage as their name says. GND stands for “ground” and Vin lets you power the board through that pin.

Also read: How to Make Blinking LEDs With the Raspberry Pi

Power and USB Ports

All Arduino boards typically have two types of ports: a USB port and DC barrel jack, but some don’t have a DC barrel jack. They usually get their power from either the USB port or the power pins.

DC barrel jacks are typically sized as 2.1×5.5mm with the inside as positive and outside as negative. They’re designed to accept anything between 7 and 20 volts, but you’re better off sticking to 9 volts whenever possible.

USB ports are different, depending on the model. They may use Type-A, Type-B, USBmicro, or Type-C. You can power the board through these, and they also serve as communication ports.

Indicator LEDs

Arduino Leds With Labels
Left (top to bottom): L, TX, and RX LEDs. Right: ON LED.

Lastly, there are typically three indicator LEDs that let you see the state of the board.

  1. RX (Receive)
  2. TX (Transmit)
  3. L (LED Built in)
  4. ON (Power On)

The L and ON pins are self-explanatory. One is a built-in LED that you can control, while the other turns on whenever electricity passes through the board. The first two, on the other hand, turn on whenever the Arduino receives or transmits information over serial communication.

Arduino IDE

Beginners should start with the Arduino IDE before moving to any other program, like PlatformIO, partly because it’s easy, with everything you need there. You’re also less likely to mess things up if you use this. It’s made for Arduino boards, after all.

The Arduino IDE has three important functions:

  1. Editing
  2. Building
  3. Uploading

Normally, the building and uploading functions work hand-in-hand. As soon as you finish typing and editing your code, you can build it then upload everything straight to your board. But there are times when you only have to build it and don’t have to upload.

The Arduino IDE can be downloaded through the Arduino website.

Also read: How to Make Blinking LEDs With the Raspberry Pi

Blinking the built-in LED is the Arduino version of a “Hello World” script and is a simple way to test whether the Arduino is working. I’m demonstrating all the different ways to make it blink, including telling the computer that it’s already blinking.

We’re starting with the basic blink script. You’ll just have to turn on the built-in LED for 0.5 seconds, then turn it off for another 0.5 seconds.

  1. Open your Arduino IDE.
Arduino Ide New Sketch
  1. You should be greeted with a new sketch as soon as you open the IDE. If not, click “File -> New” or press Ctrl + N.
Arduino Ide File New Sketch
  1. Replace the code on the IDE with the following code:
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);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}
  1. Go to “Sketch -> Upload” or press Ctrl + U to upload your code to Arduino.
Arduino Ide Upload Code

Arduino code is usually divided into two parts: setup() and loop().

When you see setup(), you’re supposed to define your outputs and inputs. In this case, we are using pinMode() to turn the LED_BUILTIN pin into an output pin.

The LED_BUILTIN pin is pin 13 and is directly connected to the L LED. It will turn it on whenever it receives enough electricity to crank it to HIGH. You can even replace all instances of LED_BUILTIN with 13, and it will still work.

Arduino Raspberry Pi Led Internal 3

setup() is discussed more in the next example. Here, everything starts from the first line within loop(), and the Arduino executes that line, then does the second one, then third, and so on, until it reaches the last line. It will then go back to the first line after that. That’s why it’s called a loop().

In this example, we used digitalWrite() to power LED_BUILTIN to HIGH. 5V of electricity passes through here, then delay(500) will temporarily stop the Arduino from reading more code for 500 milliseconds. After that, the Arduino runs the next line, which bringsLED_BUILTIN down to LOW.

It’s actually a loop of turning it on, waiting for half a second, turning it off, waiting for another half of a second, then going back to the starting line again. It keeps lopping for as long as it’s connected to a power source.

Also read: 10 Useful Python One-Liners You Must Know

This time, we’re playing around with setup() so that we can make the built-in LED quickly blink three times before blinking slowly every 0.5 seconds.

  1. On your current code, move the cursor to pinMode(LED_BUILTIN, OUTPUT); make a new line under void setup().
Arduino Ide Three Blinks New Line
  1. Paste the following code on that line:
 digitalWrite(LED_BUILTIN, HIGH);
 delay(1000);
 digitalWrite(LED_BUILTIN, LOW);
 delay(250);
 digitalWrite(LED_BUILTIN, HIGH);
 delay(1000);
 digitalWrite(LED_BUILTIN, LOW);
 delay(250);
 digitalWrite(LED_BUILTIN, HIGH);
 delay(1000);
 digitalWrite(LED_BUILTIN, LOW);
 delay(250);
  1. Your code should look like the following:
void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}
  1. Upload your code, then look at your Arduino go! Remember, it’s either “Sketch -> Upload” or Ctrl + U to send your code to your board.

The previous example talked about loop() and how it works. Let’s go back a bit and expand on setup().

Everything that you place in setup() runs once, just as the pre-written comment says. With this, you can make your Arduino do something to tell you that it’s going to start working. If you have an LCD, OLED, or AMOLED screen, you could even give it a fake loading animation!

QAPASS LCD Front And Back View
A pair of QAPASS 16×2 LCDs. Left to right: front and back views.

This is also where you’re supposed to start up special commands, like establishing communication through serial.

Example 3: Using Serial Communication

In contrast to a parallel communication, a serial communication sends data from the source to the destination one bit at a time.

This time, Arduino will send some messages over serial to give you status updates.

  1. On setup(), make a new line before pinMode(LED_BUILTIN, OUTPUT); and type Serial.begin(9600);. Make a new line underneath to look a little neater.
Arduino Ide Serial Begin
  1. Make a new line after pinMode(LED_BUILTIN, OUTPUT);, then type Serial.println("Arduino, starting up!"); in that new line. Leave a new line between these two.
Arduino Ide Serial Print Arduino Starting Up
  1. Go to loop(). Make a new line under digitalWrite(LED_BUILTIN, HIGH); and type Serial.println("LED on");. Do the same for digitalWrite(LED_BUILTIN, LOW); but type Serial.println("LED off"); instead.
Arduino Ide Loop Serial Print
  1. (Optional) You can leave a new line between the first delay(500) and digitalWrite(LED_BUILTIN, LOW);.
Arduino Ide Loop Serial Print Optional
  1. The full code should look like the following:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
 
  pinMode(LED_BUILTIN, OUTPUT);
 
  Serial.println("Arduino, starting up!");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("LED on");
  delay(500);
 
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("LED off");
  delay(500);
}
  1. Upload your code using “Sketch -> Upload” or Ctrl + U.
  2. Open the Serial Monitor by either going to “Tools -> Serial Monitor” or pressing Ctrl + Shift + M on the keyboard. A rectangular screen should show up and print all the serial outputs that you programmed your Arduino with.
Arduino Ide Serial Monitor
Arduino Ide Serial Monitor Output

As software uses a console for debugging, you’re supposed to use Serial in hardware for the same thing.

In setup(), you added the line Serial.begin(9600);. This tells Arduino to open a channel with the computer for serial communication. The number next to it shows how fast it should send data.

The 9600 refers to 9600 baud. This means it’s going to send 9600 bits per second. Baud rates range from 300 to more than 2 million baud for the Arduino IDE. Smaller rates are better for longer wires (think telephone wires for distance), while larger ones are for shorter wires. 2 million bauds is overkill, though. In many cases, even 19200 bauds is more than enough for even the most sensitive hobbyist sensors.

Arduino Ide Serial Monitor Baud Rate Drop Down

With that out of the way, let’s talk about Serial.println(); next. This command stands for “print, line new” and makes a new line after it prints what’s inside the () area.

Serial.println() accepts either strings or variables that contain strings. It behaves oddly when you add numbers in them, especially if it starts with 0.

Arduino Ide Serial Monitor Output Zero Zero Zero
Serial.println(000); prints out a single zero instead of three zeroes.

If you look at your serial monitor’s results, you should also notice that it prints “Arduino, starting up!” only once. Meanwhile, “LED on” and “LED off” are repeatedly printed out every blink cycle. That’s the big difference between setup() and loop().

In fact, you can even put your code from setup() into loop(), but that’s usually bad practice. You may end up turning an input pin into an output pin, potentially breaking your microcontroller chip.

Also read: Python While Loop: Intro and Explanation

Example 4: Blink with For-Loops

This helps very much. A for-loop lets you do a mini loop() that ends after a given number of times.

Let’s try making complex blinking patterns with multiple for-loops. On first load, the LED should blink 13 times, on for 500ms and off for 250ms, and just once. Then it should loop, blink four times, on for 1000ms and off for 1000ms, then blink two more times, on for 500ms and off for 250ms.

  1. On setup(), remove the block of code that turns LED_BUILTIN to HIGH or LOW. Also, delete the delay() functions.
Arduino Ide Delete Part
  1. Replace that code with the following:
for (int i = 0; i < 12; i++) {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
}
  1. At loop(), remove everything and replace it with the following code that turns the LED on for 1000ms and off for 1000ms, each for four times:
for (int i = 0; i < 4; i++) {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  Serial.println("LED on for 1 second");
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  Serial.println("LED off for 1 second");
}
  1. Make a new line and add the following code that turns the LED on for 500ms and off for 250ms, each for two times:
for (int i = 0; i < 2; i++) {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  Serial.println("LED on for 0.5 seconds");
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  Serial.println("LED off for 0.25 seconds");
}
  1. Upload your code, then open the serial monitor. It should start counting up to 13 LED blinks before doing the two different mini loops.
Arduino Ide Serial Monitor Complex Blink
The numbers show how many times the LED has blinked.

The for-loop is a simple, yet easy-to-learn tool. It has three parts: for, (int i = 0; i < x; i++), and whatever’s inside the { }.

  • for tells the Arduino IDE that it’s a for-loop function. There are many others, like while, if, and switch.
  • (int i = 0; i < x; i++) is a condition. It says that while the integer i, which is 0, is below the value of x, then do i++ and execute the function. i++ is shorthand for i + 1. You can even replace it with i + x where “x” is any number or make it count down using the “-” operator.
  • The { } contains everything the loop will do. It doesn’t need a ; in the end. You just leave it as is.

Also read: How to Control your Raspberry Pi through Windows via SSH

Conclusion

With this simple beginner’s guide to Arduino, you can start making your own custom light effects or maybe a fancy Christmas light.

There’s more to learn, of course, but you’re better off learning as you go. You could try out motors, sensors, relays, and a bunch more! If you’re up for the challenge, you may want to program an Arduino using a Raspberry Pi.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Terenz Jomar Dela Cruz

Terenz is a hobbyist roboticist trying to build the most awesome robot the world has ever seen. He could have done that already if he wasn't so busy burning through LEDs as a second hobby.