Lesson 8: Ultrasonic Sensors and Distance Measurement

Measure distance and detect obstacles using ultrasonic sensors

๐Ÿ“ก From output devices to sensors - giving robots spatial awareness

Use Wokwi as the standard simulator for this lesson's ultrasonic experiments so you can verify timing, sensor readings, and obstacle logic before working with real hardware.

๐Ÿ“š Learning Objectives

By the end of this lesson, you will:

  • โ€ข Understand Arduino board components and architecture
  • โ€ข Identify different Arduino models and their capabilities
  • โ€ข Learn about power requirements and connections
  • โ€ข Understand the role of shields and expansion boards

Key Concepts:

  • โ€ข Microcontroller vs microprocessor
  • โ€ข Digital and analog pins overview
  • โ€ข Power management and safety
  • โ€ข Communication interfaces (USB, I2C, SPI)

๐Ÿ” Section 1: Arduino Board Overview

What is Arduino?

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It consists of a physical programmable circuit board (microcontroller) and the Arduino IDE software that runs on your computer.

Arduino Philosophy:

  • Open Source: Hardware designs and software are freely available
  • Easy to Use: Designed for artists, designers, and hobbyists
  • Cross-Platform: Works on Windows, Mac, and Linux
  • Extensible: Can be expanded with shields and libraries

Arduino Uno - The Standard Board

The Arduino Uno is the most popular and widely used Arduino board. It's perfect for learning and most projects. Let's explore its key components:

Technical Specifications:

  • Microcontroller: ATmega328P
  • Operating Voltage: 5V
  • Input Voltage: 7-12V (recommended)
  • Digital I/O Pins: 14 (6 PWM)
  • Analog Input Pins: 6
  • Flash Memory: 32KB
  • SRAM: 2KB
  • EEPROM: 1KB
  • Clock Speed: 16MHz

Key Components:

  • USB Connector: Programming and power
  • Power Jack: External power supply
  • Reset Button: Restart the program
  • Power LED: Indicates board is powered
  • Built-in LED: Connected to pin 13
  • Digital Pins: 0-13 for digital I/O
  • Analog Pins: A0-A5 for analog input
  • Power Pins: 3.3V, 5V, GND, Vin

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Section 2: Arduino Family

Different Arduino Models

While Arduino Uno is the most common, there are many other Arduino boards designed for specific applications. Each has different capabilities, sizes, and features.

Arduino Nano

Use Case: Compact projects, breadboard-friendly
Features: Same functionality as Uno, smaller size, no power jack
Best For: Permanent installations, space-constrained projects

Arduino Mega 2560

Use Case: Complex projects requiring many I/O pins
Features: 54 digital pins, 16 analog pins, 4 serial ports
Best For: Robotics, automation, multi-sensor projects

Arduino Leonardo

Use Case: HID (Human Interface Device) projects
Features: Native USB, can act as keyboard/mouse
Best For: Game controllers, keyboard emulation, USB devices

Choosing the Right Arduino

Selecting the appropriate Arduino depends on your project requirements:

Selection Criteria:

  • Number of I/O pins needed: How many sensors, LEDs, motors?
  • Size constraints: Does it need to fit in a small enclosure?
  • Power requirements: Battery powered or wall adapter?
  • Special features: WiFi, Bluetooth, USB HID capability?
  • Budget: Some boards are more expensive than others

โšก Section 3: Power and Connections

Powering Your Arduino

Arduino can be powered in several ways. Understanding power requirements is crucial for reliable operation and preventing damage to your board or components.

Power Sources:

  • USB Cable: 5V from computer (programming/testing)
  • External Adapter: 7-12V DC (recommended 9V)
  • Battery Pack: 6-9V (4-6 AA batteries)
  • Vin Pin: Direct 7-12V input to voltage regulator

โš ๏ธ Power Safety:

  • Never exceed 12V: Can damage the voltage regulator
  • Check polarity: Wrong polarity can destroy the board
  • Current limits: Each pin can source/sink max 40mA
  • Total current: All pins combined max 200mA

Pin Types and Functions

Understanding the different types of pins and their capabilities is essential for connecting sensors, actuators, and other components to your Arduino.

// Pin Configuration Examples
void setup() {
  // Digital pins can be INPUT or OUTPUT
  pinMode(13, OUTPUT);    // Built-in LED
  pinMode(2, INPUT);      // Button input
  pinMode(3, INPUT_PULLUP); // Button with internal pullup
  
  // Analog pins are INPUT by default
  // No pinMode needed for analogRead()
  
  // PWM pins (marked with ~) can use analogWrite()
  // Pins 3, 5, 6, 9, 10, 11 on Uno
  
  Serial.begin(9600);
  Serial.println("Arduino Hardware Demo");
}

void loop() {
  // Digital output
  digitalWrite(13, HIGH);  // Turn on LED
  delay(500);
  digitalWrite(13, LOW);   // Turn off LED
  delay(500);
  
  // Digital input
  int buttonState = digitalRead(2);
  Serial.print("Button: ");
  Serial.println(buttonState);
  
  // Analog input
  int sensorValue = analogRead(A0);
  Serial.print("Sensor: ");
  Serial.println(sensorValue);
  
  // PWM output
  analogWrite(9, 128);  // 50% brightness/speed
}

๐Ÿ›ก๏ธ Section 4: Shields and Expansion

What are Arduino Shields?

Shields are pre-built circuit boards that plug directly into Arduino, extending its capabilities without requiring breadboards or complex wiring. They stack on top of the Arduino board using the standard pin layout.

Popular Shield Types:

  • Motor Shield: Control multiple motors and servos
  • WiFi Shield: Add wireless internet connectivity
  • LCD Shield: Display information with buttons
  • Sensor Shield: Easy connections for multiple sensors
  • Proto Shield: Prototyping area for custom circuits

Shield Advantages:

  • Plug and Play: No complex wiring required
  • Stackable: Multiple shields can be combined
  • Professional: Reliable connections and circuits
  • Libraries: Often come with software libraries
  • Compact: Saves space compared to breadboard circuits

Alternative Expansion Methods

Besides shields, there are other ways to expand Arduino capabilities:

Breadboards:

  • Temporary prototyping
  • Easy to modify circuits
  • Great for learning
  • No soldering required

Breakout Boards:

  • Individual sensor modules
  • Connect via jumper wires
  • Mix and match components
  • Often include level shifting

๐Ÿ› ๏ธ Hands-On Activity: Arduino Hardware Explorer

Project: Hardware Information System

Create a program that displays comprehensive information about your Arduino hardware, tests different pin types, and demonstrates hardware capabilities through the Serial Monitor.

Activity Instructions

  1. Create a hardware information display program
  2. Show board specifications and pin configurations
  3. Test digital pin capabilities (input/output)
  4. Demonstrate analog pin reading
  5. Test PWM functionality on capable pins
  6. Create an interactive hardware testing menu
  7. Include safety warnings and pin limitations

Expected Features

Hardware Info Display

  • Board type and specifications
  • Available pin counts
  • Memory information
  • Clock speed and voltage

Interactive Testing

  • Pin testing commands
  • Real-time pin monitoring
  • PWM demonstration
  • Safety limit warnings

๐Ÿ“ Assessment & Homework

Quick Check Questions

  1. What is the operating voltage of Arduino Uno?
  2. How many digital I/O pins does Arduino Uno have?
  3. What is the maximum recommended input voltage for Arduino Uno?
  4. Which pins on Arduino Uno support PWM output?
  5. What are the advantages of using shields over breadboard circuits?

Homework Challenges

Challenge 1: Board Comparison

Research and create a comparison chart of Arduino Uno, Nano, and Mega 2560. Include specifications, use cases, and price comparisons.

Challenge 2: Power Calculator

// Your first Arduino program (sketch) that calculates power consumption for different components 
                                and warns if the total exceeds Arduino's current limits.
                            

Challenge 3: Shield Research

Research three Arduino shields that would be useful for robotics projects. Explain their functions, pin usage, and how they would work together.

โ† Previous: Lesson 7
Course Overview
Next: Lesson 9 โ†’