2

Lesson 2: Digital Output, Variables, and Serial Output

A slower look at how Arduino programs control pins and report what they are doing

32-Lesson Course • Semester 1

Lesson 1 introduced the Arduino program structure and simple counting. In this lesson, the focus shifts to what pins do, how outputs are controlled, how messages appear in the Serial Monitor, and how variables change over time.

Learning Objectives

By the end of this lesson, you will:

  • • Explain the difference between an input pin and an output pin
  • • Use pinMode() to set up a pin correctly
  • • Control an LED or simulated output with digitalWrite()
  • • Use Serial.print() and Serial.println() correctly
  • • Identify simple variable types and update variables with ++ and --

Skills You'll Develop:

  • • Reading code in small, understandable chunks
  • • Connecting code to visible hardware behavior
  • • Using the Serial Monitor as a debugging tool
  • • Tracking how values change while a program runs

Section 1: Understanding Pins and Digital Output

Inputs and outputs

Arduino pins are where code meets the real world. Some pins bring information in, and some pins send information out.

Input Pin

  • • Reads information
  • • Common examples: buttons, switches, sensors
  • • The Arduino listens and records what it sees

Output Pin

  • • Sends information out
  • • Common examples: LEDs, buzzers, motors
  • • The Arduino tells another device what to do

Setting up the mode of a pin

A pin does not automatically know whether it should behave as an input or an output. We usually set that job in setup() using pinMode().

  • pinMode(13, OUTPUT); means pin 13 will control something
  • pinMode(2, INPUT); means pin 2 will read something
  • pinMode(2, INPUT_PULLUP); is often used with buttons

A simple digital output program

int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

What this code does

  • int ledPin = 13; stores the pin number in a variable
  • pinMode(ledPin, OUTPUT); prepares the pin to send signals out
  • digitalWrite(ledPin, HIGH); turns the output on
  • digitalWrite(ledPin, LOW); turns the output off
  • delay(1000); waits 1 second

Section 2: Serial Output and Variables

What is the difference between Serial.print() and Serial.println()?

  • Serial.print() writes text and stays on the same line
  • Serial.println() writes text and then moves to the next line
  • • Using both together makes the Serial Monitor easier to read
void setup() {
  Serial.begin(9600);

  Serial.print("Count = ");
  Serial.println(5);
  Serial.print("Ready");
  Serial.print("...");
  Serial.println(" go!");
}

void loop() {
}

Basic variable types

Variables are named storage boxes inside a program. Different variable types are used for different kinds of data.

int

Whole numbers like 0, 7, and 100

float

Decimal numbers like 3.14 or 22.5

char

A single character like 'A'

bool

A true-or-false value

int blinkCount = 0;
float waitTime = 1.5;
char grade = 'A';
bool lightOn = false;

Increment and decrement

  • count++; adds 1 to a variable
  • count--; subtracts 1 from a variable
  • • These are useful for counters, timers, scores, and tracking events
int count = 3;

void setup() {
  Serial.begin(9600);
  Serial.println(count);
  count++;
  Serial.println(count);
  count--;
  Serial.println(count);
}

void loop() {
}

Conditional statements: if and else

Sometimes a program should only do something when a condition is true. That is what a conditional statement does.

  • if means “only do this when the condition is true”
  • else means “otherwise, do this instead”
  • • Conditions often compare values with symbols like ==, <, or >

Important: = is not the same as ==

  • x = 0 means “store 0 in x
  • x == 0 means “check whether x is equal to 0”
  • • In an if statement, you usually want == because you are testing a value

What do () and do?

  • • Parentheses () hold the condition or information being passed in
  • • Braces hold the block of code that belongs together
  • • In if (score == 7) { ... }, the parentheses contain the test, and the braces contain the code to run if the test is true

What does a semicolon ; do?

  • • A semicolon usually marks the end of one complete statement
  • • Many lines like int score = 7; or Serial.begin(9600); need semicolons
  • • Lines that open a block, like if (...) or void setup(), do not use a semicolon before the opening brace

What are comments?

  • • Comments are notes for people reading the code
  • • The computer ignores comments when it runs the program
  • • A single-line comment starts with //
int score = 7;

void setup() {
  // Start serial communication
  Serial.begin(9600);

  if (score == 7) {
    Serial.println("Score is 7");
  } else {
    Serial.println("Score is not 7");
  }
}

void loop() {
}
  • score == 7 asks whether the value in score is equal to 7
  • • If that is true, the first message prints
  • • If that is false, the else message prints instead
  • • Writing if (score = 7) would mean something very different, so be careful
  • • The parentheses check the condition, and the braces group the lines that belong to that choice
  • • The semicolons end the regular statements inside the program
  • // Start serial communication is a comment for the human reader, not for the computer

Section 3: Putting the Ideas Together

Blink and count example

This example combines digital output, Serial Monitor messages, and a variable that changes each time the loop runs.

int ledPin = 13;
int blinkCount = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Blink program starting");
}

void loop() {
  digitalWrite(ledPin, HIGH);
  Serial.print("Blink number: ");
  Serial.println(blinkCount);
  delay(500);

  digitalWrite(ledPin, LOW);
  delay(500);

  blinkCount++;

  if (blinkCount == 10) {
    Serial.println("Reached 10 blinks. Resetting to 0.");
    blinkCount = 0;
  }
}

What to look for

  • setup() prepares the pin and starts serial communication
  • loop() repeats the blinking pattern again and again
  • blinkCount keeps track of how many times the loop has blinked
  • Serial.print() and Serial.println() make a readable message
  • • The if statement checks when the count reaches 10

Section 4: Hands-On Activity

Modify the blink-and-count program

  1. Build the blink-and-count program in Wokwi.
  2. Change the delay from 500 to 1000.
  3. Change the reset value from 10 to 5.
  4. Add a new Serial.println() message when the LED turns off.
  5. Replace blinkCount++; with blinkCount = blinkCount + 1; and explain why both work.

Challenge extension

Challenge 1

Add a second variable called flashTotal that keeps counting even when blinkCount resets.

Challenge 2

Create a short countdown using count--; and print each number to the Serial Monitor.

Questions to discuss

  • • Why is pinMode() usually placed in setup()?
  • • What is the difference between turning a pin on and printing a message?
  • • Which variables change in the program, and which values stay fixed?

📝 Assessment & Homework

📊 Quick Assessment

  • • Explain the difference between an input pin and an output pin
  • • Use pinMode() and digitalWrite() correctly
  • • Demonstrate the difference between Serial.print() and Serial.println()
  • • Show how a variable changes with ++ or --

🏠 Homework Assignment

  • • Create a blink program that counts up to 5 and then resets
  • • Write one example using Serial.print() and one using Serial.println()
  • • List three variable types and write one example of each
  • • Add comments that explain the main parts of your code

Homework Assignments

Assignment 1: Blink Program Enhancement

Take your blink-and-count program from class and add these features:

  • • Add a startup message with your name
  • • Add a second variable that tracks total flashes
  • • Print a message when the count resets
  • • Change the blink speed and describe the result

Assignment 2: Serial Monitor Exploration

Experiment with the Serial Monitor and document:

  • • One example using Serial.print()
  • • One example using Serial.println()
  • • One variable that counts up with ++
  • • One variable that counts down with --
← Previous: Lesson 1
📝 Take Quiz Next: Lesson 3 →