Arduino MASTERCLASS | Full Programming Workshop

ARDUINO PROGRAMMING WORKSHOP

Have you ever seen a martial arts movie where the hero needs to gain ninja-like fighting prowess? (Reasons vary, but they usually involve a town bully and/or gremlins.) But to attain said mastery, the hero will first need to trek up a mountain. There they’ll hang out with a couple of monks and undergo a series of tests – each more demanding than the last. A few cool music montages later, the steely-eyed protagonist returns from the mountain, suddenly proficient in an assortment of roundhouse kicks and equipped with endless throwing stars, a grappling hook, and a sachet of magical ninja herbs.

This page? It’s that mountain.

This blog post and its accompanying video are your one-stop shop for gaining a basic but very real understanding of Arduino programming. (Or, if you prefer, a solid yellow belt proficiency in Arduino-Fu.)
So, throw on some sensible shorts and pack a few granola bars, because your journey to Arduino mastery begins …

… here.

Here’s what you’ll learn

  • Arduino basics: You’ll learn about what an Arduino can do, how you can make it happen, and why Arduino-Fu.
  • Hardware and software essentials: This’ll include what boards and other equipment to buy, the various components that live on an Arduino board, what software you need, and how to use it.
  • The anatomy of an Arduino sketch: Time to get down to some tinkering — yep, this journey gets hands-on real quick. You’ll learn how to read Arduino code and the essential parts of any sketch. Then you’ll tackle a sketch in action where you’ll get to see variables and control structures in action.
  • Unlocking the vast potential of libraries: To cap it off, you’ll learn about how libraries can have you performing sophisticated tasks in a fraction of the time.

 

Arduino basics

Welcome to your Arduino mountain base camp. Let’s start by tackling the big questions — what an Arduino actually is, how the magic happens, and why Arduino programming is such a cool skill to learn.

 

What can Arduino do?

It’s helpful to think of an Arduino microcontroller as a tool. It’s a device that helps you control other electronic devices with code. There are two main categories of things that you can manipulate with an Arduino microcontroller: Inputs and outputs.

  • Inputs are devices that gather information. For example, a temperature sensor is an input. It gathers information about heat and can send that as data to an Arduino microcontroller. You can use a multitude of different sensors with an Arduino board. A few other common inputs you’ll likely work with include:
  • Outputs are devices that “do things.” They perform some useful physical action out in the world. An obvious example here (and one you’ll probably mess around with first) is a light-emitting diode, commonly known as an LED. Here are just a few examples of other outputs:

In your mind’s eye, grab one of each of the inputs and outputs listed above and imagine a device that uses both. For example, picture a device that activates a servo motor to wiggle a Halloween skeleton’s eyeballs around whenever it detects motion using an infrared sensor. That’s a basic example of the kind of stuff you can do — and surprisingly easily! — using an Arduino microcontroller.

 

How do you make an Arduino do that cool stuff?

It’s time to introduce you to what we call the Arduino trifecta. There are three basic elements to every Arduino project you’ll ever work on. Learning how these three elements interact is key to gaining mastery over the whole Arduino shebang.

  • Arduino hardware: This is probably what you think of first when you think about the Arduino skillset. The hardware is the physical Arduino circuit board and all the physical bits and pieces that live on it. We’ll look at that board in a lot more detail in just a moment.
  • Arduino IDE: This is the software you’ll use to write code and then load it onto the Arduino board. Just for future reference, IDE stands for integrated development environment, and that’s basically what it is — a software package that integrates all the development tools you need to work with the Arduino hardware. How much would you expect to pay for such a wondrous piece of software? It’s totally free (and doesn’t come with a free set of steak knives, sorry).
  • Arduino code: And this techno-trinity is completed by code. Code is the set of instructions that tells your Arduino how to behave. In the world of Arduino, that code is called a sketch, and you get no prize if you predicted we’ll be looking at sketches in detail later on too.

 

Why is it a great thing to learn?

We’re not too proud to lead with the obvious. Having skills with Arduino means you can do great stuff. Arduino programming is one of those skills that you can apply to a multitude of other activities.

  • Into gardening? Use an Arduino to automate watering your plants.
  • Got an interest in home security? An Arduino can set up sensors around your house.
  • Unhealthily obsessed with Halloween? Use an Arduino to animate a zombie and successfully terrorize your whole neighborhood.

You get the idea. Arduino opens doors.

There’s a practical upside too. As you learn to write sketches, you’ll be working with a pared-down version of two powerful and ubiquitous programming languages: C and C++. These languages are everywhere, and once you know them, you’ll be well-positioned to learn a bunch of adjacent programming languages.

Ready for another ninja movie metaphor? Climb this knowledge mountain and other cool mountains out there will be much easier to climb.

 

Interesting factoid 1: C and C++ are used on things like mission-critical code on spaceships to GPUs running machine learning algorithms.

 

 Understanding the hardware and software

Right! So turn around and take a moment to enjoy the awesome mountain view. You’ve actually come quite a ways already. You may even have befriended a friendly raccoon who shall aid you on your righteous quest. Or not. Raccoon companions are optional on this adventure.

The next step is to get a lot more granular about the hardware and software you’ll use.

 

What Arduino board should you buy?

You might have searched around already for Arduino boards. You may even have purchased one already. If you have, you probably noticed there are a ton of different products out there. The merits of each board are the subject for another adventure (a sequel, perhaps?), but for now, you only really need to know two things about the many types of boards at your disposal:

  1. Pretty much any board will do. In other words, if you already bought an Arduino board, it’ll almost certainly be just fine as you learn.
  2. If you haven’t bought a board yet, buy the Arduino UNO Rev 3. Here’s a link. Why this board in particular?
    • It’s popular, which means it’s well supported, and most basic Arduino tutorials will assume you’re working with this model.
    • It has plenty of stuff on it for most beginner applications: We won’t go into what that stuff is just yet. But let’s just say the Arduino UNO can easily handle any beginner-level stuff you’ll want to do.
    • It has a garden variety standard layout: Nothing about the Arduino UNO Rev 3 is unusual. It’ll work with the vast majority of Arduino accessories on the market today.

 

Oh no! I have a different board. Am I on the wrong quest?

Nope. You don’t have to leave this mountain, and any friendly raccoons you’ve befriended on your quest thus far will still heed your bidding. You’ll be fine. Arduino hardware and software are open-source, which means that other companies are free to use, copy and market new products without changing the underlying architecture. That open-source approach is one of the wonderful things about Arduino.

Whatever board variant you own, there’s a 99.3685% chance it’ll function perfectly as a learner’s board. If you’re in doubt, however, drop us an email. We’ll be happy to talk you through the quirks (if there are any) of the board you’re using.

 

Interesting factoid 2: You’re not locked into one board either. The code that you write for one board will usually work on another board with only slight modifications.

 

What other stuff should you get?

No ninja can perform feats of daring with just a beginner set of nunchucks, a flatulent raccoon, a pocketful of ninja stars, and a smattering of enigmatically encouraging proverbs. Mastery requires tools. While you don’t need much to get started with Arduino, there are some essential bits and pieces you’ll need as well as a few optional extras that’ll make your life a whole lot easier.

Must haves:

Good to have:

  • Tinkercad Arduino simulator: Build, test, and code using a software simulation of Arduino hardware. This is a free tool and a great way to tinker on new projects without investing in more boards.
  • Jumper cables: These make it easier to connect your Arduino board to electronic components.
  • Breadboard: A breadboard makes it easier to organize your components without having to mess about with more permanent solder connections.

 

What’s on an Arduino board?

Next, let’s look at an Arduino board and what it’s made up of.

Circuit board

First thing first, the whole board together is a circuit board, and here’s what it looks like.

Microcontroller

The most important thing on an Arduino circuit board is this black oblong with metal prongs sticking out of it. This is the microcontroller. It’s smaller on some boards, larger on others, but the function is always the same. This is the Arduino board’s brain. It stores the code you upload to the board and uses that “sketch” (remember, Arduino code is called a sketch) to control inputs and outputs.

DC jack

A 2.1-millimeter center-positive plug you can use to power the board. You can connect 5 AA batteries or a wall wart like this one to the DC jack. The jack can take around 7 to 20 volts, but it’s best to stick to around 12 volts. You can also power the Arduino board via a power pin or USB. See the section below on pins for more information.

Reset button

Press this button to reset the board. Think of it like turning your laptop or smartphone off and on again. The button is useful for testing and troubleshooting. You can also reset the board using a reset pin. See the section below on pins for more information.

Pins on the microcontroller

See the circled pins in the pic below? These are known as general purpose input-output (GPIO) pins. The Arduino reads the voltage at each of these pins. If it detects voltage, its state is referred to as HIGH, or on. If it doesn’t detect voltage, its state is LOW, or off. If “HIGH” and “LOW” sound weird in this context, don’t worry. We’ll talk about that more in a moment.

Pin headers

Now check out these plastic columns with holes in them. Each one of the holes makes an electrical connection to one of the pins that we were just talking about on the microcontroller. The beauty of pin headers is that you can make a quick and temporary electrical connection without needing to solder. Just stick a jumper cable into one of these pin headers, attach it to an input or output device, and you’re good to go.

 

Understanding the pin headers and how they work.

Whew! You’re about halfway up the mountain now. Congratulations. We’ve covered all the hardware basics. Before we look at the Arduino IDE software, it’s worth taking a closer look at those pin headers. Knowing how the pin headers work (and what they’re for) is essential to getting the most out of Arduino.

Take a close look at those headers. You’ll notice different acronyms and numbers. These markings shed important insight into the unique functions of individual pin headers.

Binary inputs/outputs

On one side, we have the digital pins labeled 0 through 13.

These pins can be used for reading on/off type inputs. Think of a button. It has precisely two states, right? On and Off. A button is a perfect example of an input you’d connect to one of these digital pins.

What “on” or “off” states do these pins detect?

It’s important to understand that this state is just the presence or absence of an electrical current. For a pin to be “on” it needs to have 5 volts applied to it. That state is also called “HIGH.” The opposite applies too. For a pin to be “off” it needs to have low, or zero volts applied. That state is also called “LOW.”
As you get more used to working with Arduino, you’ll probably find thinking in terms of “HIGH” and “LOW” a lot more intuitive than you do now. But don’t sweat it too much starting out. Just keep the terminology tucked away at the back of your mind.

Pins 0 and 1 are a bit different.

They’re still digital, but they’re used for communication with the USB port. You’ll see that these pins are marked with a TX for transmit and an RX for receive. There are also two tiny LEDs on the board marked TX and RX. Those LEDs will flash on and off when signals are being transmitted on those two pins.

Generally speaking, it’s a good idea NOT to use these pins if you can help it. Sometimes having these pins attached to other circuitry can interfere with you uploading code to your board – a source of serious angst!

Some pins also have a little squiggly line next to them.

Those little squiggles mean that the pin is capable of pulse-width modulation, or PWM. Now, this capability gets easier to understand once you start playing with devices that operate at a range of different intensities, like LEDs and servo motors. For now, the main thing to remember is that PWM pins can switch from high to low at varying speeds.

So what, you might ask? PWM is useful because it means you’re not just stuck with on or off. You can use PWM to fine-tune the apparent brightness of an LED, for example, or how fast a motor runs.

Analog inputs/outputs

The Analog In section has six holes. These are places where you can connect and read analog inputs.

Think of an analog input as a continuous signal. For example, let’s say you wanted to measure ambient heat in a room with a temperature sensor. In this situation, a simple binary result (think on or off, high or low) isn’t going to tell you much. You need an analog signal to track continual changes in temperature along a continuum of possible results.

Power pins

Next to the analog in section, you’ll find the power pins. You have ground (GND); that’s the lowest voltage on the board. You have 5V and 3.3V pins. These supply 5 volts and 3.3 volts respectively to connected components.

You’ll see a pin marked Vin. You can use this pin to hook up an external voltage source instead of via a USB cable or DC power jack.

If you really want to learn the ins and outs of these pins, we have a course that deep dives into powering projects with Arduino in our training academy.

Reset pin

An easy one! If you apply a low voltage to this pin, it will reset the Arduino board, just as the reset button does.

 

Downloading the Arduino IDE

This brings us to software! As you’ll recall, working with Arduino requires knowledge of three different systems: the hardware, the software, and the code. The software part of that equation is called the Arduino IDE.

You’ll find the software on the Arduino website. Just click on the software tab, and you’ll be taken to the site’s download page.

 

 

You actually have three different options on this website for what IDE you want to use.

  • Arduino IDE (IDE 1): The original Arduino IDE software package. This version has all the core features you need, with the added benefit of being easy to use. This is a great option for beginners because it’s simple, uncluttered, and forgiving.
  • Arduino IDE (IDE 2): This is the most recent version of the IDE software. It has additional features, none of which you’ll need starting out. But if you want to start your Arduino journey with the software you’ll end up using when you become a card-carrying Arduino-Fu black belt, no one’s gonna stop ya.
  • Web editor: If you’re on a Chromebook or a locked-down computer, this one’s for you. This is a fully browser-based version that you can use on any web-enabled computer.

Once you’ve made your decision, hit download, double click the install file and follow all the generic prompts.

 

Interesting factoid 3: We mentioned before that Arduino is absolutely free, which is crazy when you think about how much time, passion and dedication has gone into making this product. So when you hit download, consider showing your support with a donation.

 

Familiarizing yourself with the Arduino IDE

You’ll spend a lot of time in Arduino IDE, and it’ll become as familiar to you as the back of your hand. Here’s a quick guided tour to get you started (you can follow along with the instructional video too):

  1. Open the IDE: Double-click the shortcut on your desktop or in your app folder.
  2. Find your sketch and library locations: Open up your documents folder and look for a sub-folder called Arduino. This is where your sketches and libraries will be stored (more on libraries later).
  3. Open a sketch: To open a sketch, just go to File>Examples>01.Basics>Blink.
  4. Connect your board: While we’re here, let’s look at this Blink sketch in action! Grab your Arduino board and plug it into your computer with your USB cable.
  5. Test your board is connected: Click on Tools>Board. Now you can select your board by name. Now hit Tools>Port. Simply select the port that shows the name of your board.
  6. Verify a sketch: Let’s check your sketch for any errors by hitting the checkmark at the top left of your sketch. You’ll see the status of your sketch outlined in the black notification box at the bottom of your screen.
  7. Upload a sketch: Now hit the little right-pointing arrow to upload. If you look closely at the Arduino board during this step, you’ll see the TX and RX lights blinking.
  8. Play with the sketch: Once the Blink sketch is uploaded, the built-in LED on your board will start blinking.

 

To dig a bit deeper into the Arduino IDE, head over to this part of the video. You’ll learn how to:

  • Play with other scripts
  • Install IDE 2.0
  • Identify and troubleshoot ports
  • Install the web editor
  • Experiment a little with the Blink sketch.

 

Mapping out an Arduino Sketch

Now it gets real. Those granola bars you packed when you set out on this adventure are now but a distant memory. If you crane your neck, you can see the top of this mountain not too far ahead. Keep going!

Here’s what a sketch looks like and how it works

We’re about to look at sketches. These might look treacherous and slippery at first glance, but with the right map and a potion of fortitude at the ready (AKA coffee), we’re confident you’ll emerge unscathed.

Let’s start with the map. Here are the core elements you need to understand in order to successfully navigate a sketch.

Code execution

Note that the sketch lines are numbered.

When this sketch executes (or runs), the Arduino microcontroller will read the code, line by line, from top to bottom. No exceptions. It doesn’t read like a human who might check for context or jump up and down the document as commonsense demands. Nope. The Arduino will always methodically follow each instruction in order from top to bottom. It’s important to remember the methodical ways of your Arduino as you learn to write your own sketches.

Functions

See those opening and closing brackets?

This is a function. These are your basic building blocks for an Arduino sketch. As the Arduino microcontroller executes a sketch it’ll look for these functions and treat them as precise instructions.

Setup

Here’s the setup portion of this sketch.

Everything in this area is only run once. If it’s code that only needs to execute once at the beginning of a program, this is the place to have it.

Loop

The loop portion of your sketch runs repeatedly. It just repeats over and over until you either remove power from the Arduino board or the universe collapses in on itself — whichever comes first.

 

Interesting factoid 4: Setup and loop need to be in every single sketch. If you accidentally remove these, Arduino IDE will warn you when you verify the sketch. Try it out.

 

These are just the bare basics. Watch the video here to:

  • Learn more about the kinds of things you can do with a loop
  • See what happens if you leave loop out and try to verify the sketch

 

How to use variables

A variable is like a bucket that allows you to store information in it. For example, let’s say you want to measure readings from a temperature sensor. You would store those temperature values as a variable.

A variable has four ingredients:

  • Type: The kind of information that you’re actually going to store in the variable (see the link below to learn about each type).
  • Name: The name of the variable. You can use any name you want, but it’s smart to keep your names clear, simple, and descriptive. Variable names can contain letters, numbers, and underscores, but they can’t start with a number.
  • Assignment operator: When you initialize a variable, you’ll see an equals sign to signal that the type and name apply to whatever information the sketch includes next.
  • Value: And finally, there’s the value you assign to the variable.

 

When you’re ready for more, build on your understanding of variables by looking at:

 

How to use control structures

Up until now, we’ve looked at how you can write a sketch to control inputs and outputs. Read and write enough sketches, and all of that syntax will become second nature. But how do you bring all of those lines of code together into a fluid and smoothly-functioning project?

To make that happen, you’ll need to build logic into your Arduino program, and that’ll require you to wrap your cerebral cortex around control structures.

What is a control structure?

About half a cup of coffee that way ^^^ you learned about code execution. We talked about how your Arduino’s brain reads code, line by line, from start to finish. That is true, BUT… a sketch can also contain specific instructions to do something else — to jump forward, go back, and repeat bits of code, to name just a few examples.

Long story short, control structures allow you to adjust the flow of code, and that paves the way for your Arduino to produce more sophisticated and complex outcomes. That sounds a bit abstract, so let’s dig into an example.

Imagine you’re designing an Arduino project for a lab experiment. When a test subject presses button A, an enormous speaker plays a song intended to convince an unnamed third party of the deep intensity of the singer’s romantic devotion, and a small hopper administers a peanut.

When the test subject presses button B, they hear a different song instructing the listener to exercise extreme mental acuity in order to achieve victory conditions in any kind of conflict setting. They do not get a peanut.

If the test subject does nothing for ten seconds, the Arduino uses an attached relay to deliver a small — but not that small — electric shock to the test subject’s nose.
In this perfectly innocent example of a garden variety Arduino project, all these potential outcomes could be controlled with a single control structure. The sketch would tell the Arduino to perform actions W and X if the (softly sobbing) test subject hits button A, action Y if they hit button B, else action Z (for zap) if neither button is pressed after a certain length of time.

To get you started, we will look at three functions: if, else, and switch case. You can continue your learning via the links at the end of this section.

If

If statements tell the Arduino to check to see if a certain condition is true or false and to perform different actions based on the result. If the condition evaluates to true, then the code inside the curly braces gets run. If the condition evaluates to false, then that code just gets skipped over.

You could use an IF control structure to make a LED gradually brighten and dim, for example.

Else

The else statement comes at the end of an if statement. If the first part of the if condition evaluates to false, then the else condition provides an alternative action for the Arduino to perform.

Here’s an example of the else statement being used inside the button sketch.

Switch case

This control structure will compare in input value with a series of “cases.” If the input value matches the case value, then the code specific to that case will get executed. For example, the Arduino might determine the ambient temperature from a temperature sensor and then report Cold, Medium, and Hot to the serial monitor based on a defined temperature range.

Here’s an example of how a switch case can be used to report a potentiometer setting to the serial monitor.

 

More things you can learn:

 

Unlocking the next level

You’re at the top of the mountain! It’s time to meet the crazy monks who can usher you on your way to Arduino ninjadom. Libraries help you hit the ground running, leveraging the hard work and expertise of an amazing group of programmers from all over the world.

An Arduino Library is code that’s developed for a specific purpose and is packaged in a way that allows you to access complex functionality without necessarily having to understand all the dirty details.

 

What you need to know about libraries

  • There is a vast number to choose from. You’ll usually find multiple libraries to control just about any piece of hardware out there.
  • Most libraries are free. If you want evidence that humans can be good people, this is it.
  • Libraries can save you vast amounts of time.

Arduino programming can be incredibly versatile, but for all those options, the learning curve is usually nice and gentle, thanks to the hard work of other coders in the Arduino community. If you have some change to spare, consider donating to support Arduino’s ongoing good work.

 

How to use Arduino libraries

  • To get started, take a look at the default libraries that came with the Arduino IDE. To find those libraries, go to Sketch, then Include Library.
  • To install a new library:
    • Either select Sketch>Include Library>Manage Libraries.
    • Or you can go to Tools>Manage Libraries>Library Manager.

 

Interesting Factoid 5: Finding the right library can be tricky because there are so many, but a bit of judicious early Googling can narrow down your search. See the link at the end of this section for pointers.

 

More things you can learn:

 

Cue the closing credits

You did it! You ate your way through a Costco-sized box of granola bars, befriended a raccoon, and received a codex of ultimate wisdom from the crazy monks at the top of the mountain. (Playing a surging 80’s success soundtrack at this juncture is entirely optional though highly recommended).

Now that you’re armed with a solid foundation of Arduino knowledge, it’s a good idea to consolidate what you know (revisit the video often!) and experiment with everything you’ve already learned.

And of course, when you’re ready to dig deeper, you can always enroll in our Free Arduino Crash Course.

AppLab Bricks open in background with actual brick

Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?

Arduino Ventuno single board computer - top side

New Ventuno Q Dual Brain Single Board Computer

AppLab Pip Install

How to Add Python Packages in Arduino AppLab (No pip install needed)

Arduino Power Section Schematic

Kit-on-a-Shield Schematic Review

Just how random is the ESP32 random number generator?

Just how random is the ESP32 random number generator?

Leave a Comment