Lesson 2: Core Programming Basics – Variables, Data Types, and Operators

Core Programming Basics: Variables, Data Types, and Operators

Now that you can upload a program, this lesson gives you the building blocks every program is made of: variables to hold data, data types to describe it, constants for fixed values, and operators to compute and compare. You will also use the Serial Monitor to see your program's values live, and practice with a potentiometer, a photoresistor, and a button.

Variables: containers for data

A variable is a named container that stores a value your program can read and change. You create one by stating its type and name, then giving it a value, such as int myNumber = 10. From then on you can use myNumber wherever you need that value, and you can update it as the program runs. Variables are what let a program remember and work with information instead of using fixed numbers everywhere.

Data types: describing the value

Every variable has a type that tells the board what kind of value it holds and how much memory to set aside. The common ones are int for whole numbers like 10 or -5, float for decimals like 3.14, boolean for true or false, and char for a single character like the letter A. Choosing the right type matters: storing a decimal in an int drops the fractional part, and a boolean is the natural fit for something that is only ever on or off.

Constants: values that never change

Some values stay fixed for the whole program, like the pin an LED is wired to. Marking them as constants makes your intent clear and prevents accidental changes. There are two common ways: const int ledPin = 13 creates a typed constant, and #define PI 3.14 defines a fixed value the compiler substitutes in. Both keep important values in one place, so changing a pin means editing one line.

Operators: doing things with values

Operators are the symbols that compute and compare. Arithmetic operators do math: plus, minus, times, divide, and the modulus percent sign, which gives the remainder of a division. Comparison operators ask true-or-false questions: double equals for equal, the not-equal sign, and the less-than and greater-than family. Assignment operators change a variable: a single equals sets it, and shorthands like plus-equals add to it in place, so a += 5 increases a by five. These are the verbs of your program.

Serial communication: seeing inside your program

The Serial Monitor is a window that shows text your program prints over the USB cable. You start it with Serial.begin(9600) in setup, then use Serial.println to send a value or message. This is invaluable: you can watch a sensor reading change in real time, confirm a variable holds what you expect, and catch mistakes early. It is your first and most useful debugging tool.

Working through it

Declare a few variables and constants. Practice with int, float, boolean, and char variables, and set a pin as a const int. Notice how the type matches the kind of value you store.

Start Serial output. Call Serial.begin(9600) in setup so you can print values. Open the Serial Monitor in the editor to see them.

Read a potentiometer and print it. Wire a potentiometer to A0. Read it with analogRead into an int and Serial.println the value. Turn the knob and watch the number sweep from 0 to 1023.

Read a photoresistor. Wire a photoresistor with a voltage divider to another analog pin and print its value. Cover it and shine light on it to see the reading respond to brightness.

Read a button. Wire a pushbutton to a digital pin, read it with digitalRead, and print whether it is pressed. This shows the difference between a two-state digital input and the ranged analog inputs above.

Declaring variables, constants, and operators

int myNumber = 10;      // whole number
float myDecimal = 3.14; // decimal number
boolean isOn = true;    // true or false
char myLetter = 'A';    // single character

const int ledPin = 13;  // a constant pin number
#define PI 3.14         // a defined constant

void setup() {
  Serial.begin(9600);

  int a = 10;
  int b = 20;
  int sum = a + b;            // arithmetic: 30
  boolean isEqual = (a == b); // comparison: false
  a += 5;                     // assignment shorthand: a is now 15

  Serial.println(sum);
  Serial.println(isEqual);
  Serial.println(a);
}

void loop() {}

This shows each kind of variable, both ways to make a constant, and the three operator families. Watch the printed values in the Serial Monitor to confirm the math and comparisons.

Reading and printing a potentiometer

const int potPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int potValue = analogRead(potPin); // 0 to 1023
  Serial.println(potValue);
  delay(200);
}