Arduino MASTERCLASS | How to Use Variables

Introduction

Are you ready to dive into the basics of Arduino programming? In this tutorial, we’ll walk through how to control electronics with code—no prior programming experience required! Here’s what we’re going to cover:

  • How every Arduino program is laid out.
  • How to use variables to store information.
  • How to use control structures to build logic.
  • Key Arduino-specific functions you need to know to get started.

Let’s jump into it by using real Arduino programs so you can see everything in action.

Programming Electronics Academy members, learn about the Arduino IDE toolchain in the Familiarization section of the Arduino Course for Absolute Beginners.

Not a member yet?  Sign up here.

1. Opening the Arduino IDE and Starting a New Sketch

To start, open the Arduino IDE and create a new sketch.

In every Arduino program, you’ll see two key functions: setup() and loop(). Here’s what each of them does:

  1. setup(): This function runs once when the program starts. It’s used to set up anything that only needs to be done once, like initializing pins or sensors.
  2. loop(): This function runs repeatedly after setup(). It contains the main logic of your program that keeps executing over and over.

When you look at the basic structure of an Arduino sketch, it’ll look something like this:

void setup() {
  // Put setup code here, to run once
}

void loop() {
  // Put main code here, to run repeatedly
}

Anything you write in setup() will be executed once, while code in loop() will run continuously until the Arduino is powered off.

2. Understanding Code Execution in Arduino

Code execution happens sequentially, meaning it runs from the top to the bottom of each function. For example:

void setup() {
  // Code 1
  // Code 2
}

Here, the Arduino will execute Code 1 first, then Code 2. After the setup is complete, the Arduino will move to the loop and start executing the code there. The loop repeats endlessly, running from top to bottom and then starting again.

3. Introducing Variables

Variables are containers that store data you can use in your program. Think of them like labeled buckets that can hold information. You can store numbers, characters, and more.

To declare a variable, you need four things:

  • Type: What kind of data you’re storing (e.g., integer, float, etc.).
  • Name: The variable’s identifier.
  • Assignment operator (=): Used to store a value in the variable.
  • Value: The actual value to store.

For example:

int temperature = 25;

In this case, int is the type (short for integer), temperature is the name, and 25 is the value.

4. Common Data Types in Arduino

Here’s a quick rundown of the most commonly used data types:

  • Boolean: Stores true or false values.
  • Byte: Stores small numbers (0-255).
  • Int: Stores larger integers (-32,768 to 32,767).
  • Long: Stores even larger numbers (-2 billion to 2 billion).
  • Float: Stores numbers with decimal points.
  • Char: Stores single characters like 'A', 'B', etc.
  • Character Array: Stores a series of characters (used to store strings).

You can see how you might use some of these types in a program. For instance:

boolean isLEDOn = false;
int sensorValue = 1023;
char letter = 'A';

5. Using Variables in Your Program

You can declare and initialize variables separately or together:

int ledPin; // Declaration
ledPin = 9; // Initialization

int brightness = 255; // Declaration and initialization

Once a variable is declared, you can refer to it by name throughout your code. For example, to set the brightness of an LED, you could use:

analogWrite(ledPin, brightness);

6. Exploring a Real Example: Fading an LED

Let’s take a look at an example program that fades an LED in and out using pulse-width modulation (PWM). You can find this in the File > Examples > Basics > Fade example.

At the top of this sketch, there are three global variables:

int led = 9;          // The PWM pin the LED is connected to
int brightness = 0;    // Initial brightness of the LED
int fadeAmount = 5;    // How much to change the brightness each time

Here’s what happens in the program:

  1. The setup function sets the led pin as an output using pinMode():
    cpp pinMode(led, OUTPUT);
  2. The loop function adjusts the LED’s brightness using analogWrite():
    cpp analogWrite(led, brightness);
  3. The brightness is adjusted by adding or subtracting fadeAmount:
    cpp brightness = brightness + fadeAmount;

If the brightness gets too high or too low, the direction of the fade is reversed:

if (brightness <= 0 || brightness >= 255) {
  fadeAmount = -fadeAmount;
}

7. Key Arduino Functions

Here are a couple of Arduino-specific functions that you’ll frequently use:

  • pinMode(pin, mode): Sets a pin to be either an INPUT or OUTPUT.
  • analogWrite(pin, value): Writes an analog value (PWM) to a pin.

These are essential functions that allow you to interact with the Arduino’s hardware. We used pinMode to set the LED pin as an output, and analogWrite to adjust the brightness of the LED via PWM.

8. Conclusion

In this tutorial, we learned the basics of Arduino programming, how programs are structured, how to declare and use variables, and we explored some key Arduino-specific functions like pinMode() and analogWrite().

In our next lesson, we’ll dive into control structures and how they help us build logic in our programs.

If you have any questions, feel free to leave a comment below.

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?

2 Comments

  1. George on September 21, 2024 at 10:36 am

    A dumb question is one thought of and not asked. So here goes, not just for here but other places as well: I keep seeing references tp “subscribe to a Youtube Channel”. How is never explained and is not obvious. So how do I subscribe?
    Thanks

    • Michael Cheich on September 23, 2024 at 10:37 am

      Hi George – great question! If you click the YouTube button on the bottom right of the video player, it will take you to YouTube, and you will see a subscribe button to our channel on the bottom right below the video there.

      Thanks for watching!

Leave a Comment