8
Lesson 8: Introduction to Arduino and Hardware
Transitioning from software to hardware programming
Learning Objectives
By the end of this lesson, students will:
- Understand what microcontrollers are and how they work
- Set up Arduino IDE and connect to hardware
- Write basic Arduino programs (sketches)
- Understand digital vs analog signals
Skills Developed:
- Hardware-software integration
- Embedded programming basics
- Problem-solving with constraints
Lesson Content
1 What is Arduino? (15 minutes)
Arduino is an open-source microcontroller platform that makes it easy to create interactive electronic projects. Think of it as a tiny computer that can control lights, motors, sensors, and other hardware components.
What makes Arduino special:
- • Easy to learn programming language
- • Affordable hardware ($10-30)
- • Large community support
- • Thousands of tutorials and examples
Compared to regular computers:
- • Much smaller and simpler
- • Designed for specific tasks
- • Direct hardware control
- • No operating system needed
2 Arduino IDE Setup (20 minutes)
Installation Steps:
- Download Arduino IDE from arduino.cc
- Install the software (Windows/Mac/Linux)
- Connect Arduino board via USB cable
- Select correct board type (Arduino Uno)
- Select correct COM port
// Your first Arduino program (sketch)
// setup() runs once when Arduino starts
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Arduino started!");
}
// loop() runs repeatedly forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
} 3 Arduino Programming Basics (30 minutes)
Key Differences from Python:
Python (what you know):
- • No variable types needed
- • Indentation matters
- • No semicolons
- • print() for output
Arduino C++ (new):
- • Must declare variable types
- • Curly braces for blocks
- • Semicolons required
- • Serial.println() for output
// Robot Decision Making Example
int batteryLevel = 100;
int missionStep = 0;
bool obstacleDetected = false;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("🤖 Robot Brain Online!");
}
void loop() {
// Simulate sensor readings
int sensorReading = random(0, 100);
obstacleDetected = (sensorReading < 20);
// Robot decision making
Serial.print("Step ");
Serial.print(missionStep);
Serial.print(" - Battery: ");
Serial.print(batteryLevel);
Serial.println("%");
if (batteryLevel < 20) {
Serial.println("🔋 Low battery! Returning...");
digitalWrite(LED_BUILTIN, HIGH);
} else if (obstacleDetected) {
Serial.println("🛑 Obstacle detected!");
digitalWrite(LED_BUILTIN, HIGH);
} else {
Serial.println("🚀 Path clear, continuing...");
digitalWrite(LED_BUILTIN, LOW);
batteryLevel -= 2;
}
missionStep++;
delay(2000);
} 4 Digital vs Analog Signals (20 minutes)
Understanding Signal Types:
Digital Signals:
- • Binary data (0s and 1s)
- • Discrete values
- • Easy to process and store
Analog Signals:
- • Continuous data (any value)
- • Range of values
- • More complex to process and store
5 Hands-On Activity (30 minutes)
Activity Instructions:
- Connect your Arduino board to the computer
- Open the Arduino IDE and create a new project
- Write a simple program to blink an LED
- Upload the program to the Arduino board
- Observe the LED blinking
Arduino Programming Examples
// Example 1: Battery level check
int batteryLevel = 85;
if (batteryLevel < 20) {
Serial.println("Low battery warning!");
digitalWrite(LED_BUILTIN, HIGH);
} else if (batteryLevel > 80) {
Serial.println("Battery level good");
digitalWrite(LED_BUILTIN, LOW);
}
// Example 2: Sensor reading
int sensorValue = analogRead(A0);
if (sensorValue > 500) {
Serial.println("High sensor reading");
}
// Example 3: Loop with comparison
for (int i = 0; i < 10; i++) {
Serial.print("Count: ");
Serial.println(i);
delay(100);
} Assessment & Homework
Quick Check:
- ✓ Can you explain what Arduino is?
- ✓ Do you understand setup() vs loop()?
- ✓ Can you identify digital vs analog signals?
- ✓ Can you write a simple if statement in Arduino?
Homework:
- • Download and install Arduino IDE
- • Try the robot brain simulator code
- • Modify the battery drain rate
- • Research: What sensors could miniAuto use?