🚀
ArkyEdu BLOG TIC
RO Open App
Arduino Robotics & IoT Grades 7–8 11 min read

Arduino Uno Smart Home School Project (Grades 7–8): PIR, HC-SR04 Ultrasonic, MQ-2 & Non-Blocking millis()

Robotics and IoT units in Grades 7 and 8 bring C++ code into the physical world—switching on street lights at dusk, lifting parking gates for approaching vehicles, and sounding buzzers on gas leaks. This hands-on guide breaks down pin mapping, why Ohm’s Law protects your LEDs, and how non-blocking millis() turns hobby scripts into real-time systems.

💬 WhatsApp ← ICT Blog & Guides
Quick Answer (TL;DR)

A reliable middle school Arduino Uno Smart Home reads digital sensors (PIR motion, HC-SR04 ultrasonic) and analog A0–A5 inputs (LDR photoresistor, MQ-2 gas sensor) to drive actuators (220 Ω resistor-protected LEDs, PWM ~ SG90 servo gates). Replace blocking delay() calls with millis() timers so emergency alarms always trigger immediately.

1. Arduino Uno Pin Map: Digital, Analog A0–A5, PWM (~) & the 220 Ω Resistor Rule

An Arduino Uno operates at 5V with digital pins (D0–D13, strictly HIGH/5V or LOW/0V) and analog input pins (A0–A5, converting voltage into 0–1023 steps via a 10-bit ADC). Digital pins marked with a tilde (~: 3, 5, 6, 9, 10, 11) output PWM signals to dim LEDs or position SG90 servo motors.

Critical hardware rule (Ohm’s Law, I = V / R): Never wire an LED directly between 5V and GND without a series resistor (typically 220 Ω to 330 Ω). Without it, current surges above the pin’s 40 mA safety limit and damages both the LED and the microcontroller.

Smart Home / Smart City Hardware Component Reference
ComponentArduino Pin TypeRole in the System
LDR PhotoresistorAnalog (e.g., A0)Reads ambient light (0–1023) to switch street lights on at dusk.
MQ-2 Gas SensorAnalog (e.g., A1)Detects smoke or combustible gas; triggers alarm buzzer.
PIR Motion SensorDigital INPUT (e.g., D2)Detects warm movement via infrared changes (HIGH/LOW).
HC-SR04 UltrasonicDigital TRIG / ECHO (D4, D5)Measures distance in cm via sound echo (duration * 0.034 / 2).
SG90 Servo MotorDigital PWM (~9)Rotates a parking gate arm smoothly between 0° and 90°.
LCD 1602 I2C DisplayI2C Bus (SDA=A4, SCL=A5)Displays sensor telemetry using only two data wires.

2. Why delay() Breaks Smart Systems—and How millis() Fixes Multitasking

Calling delay(5000) to hold a gate open freezes the entire Arduino chip for five seconds. If the MQ-2 sensor detects gas during that pause, the alarm will not fire until delay() finishes!

Using millis()—which counts milliseconds since boot—lets you check elapsed time non-blockingly (`if (now - previousTime >= interval)`), keeping gas sensors responsive thousands of times per second.

Non-Blocking Arduino Code: Smart Streetlight + Instant Gas Alarm
const int PIN_LDR = A0;
const int PIN_GAS = A1;
const int PIN_STREET_LED = 6; // PWM (~) pin
const int PIN_BUZZER = 8;

unsigned long lastLightCheck = 0;
const unsigned long LIGHT_INTERVAL = 250; // check every 250 ms without blocking

void setup() {
  pinMode(PIN_STREET_LED, OUTPUT);
  pinMode(PIN_BUZZER, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  unsigned long now = millis();

  // 1. Instant non-blocking safety check: MQ-2 gas alarm
  int gasLevel = analogRead(PIN_GAS);
  if (gasLevel > 450) {
    tone(PIN_BUZZER, 1200);
  } else {
    noTone(PIN_BUZZER);
  }

  // 2. Periodic PWM smart lighting using millis() (zero delay!)
  if (now - lastLightCheck >= LIGHT_INTERVAL) {
    lastLightCheck = now;
    int light = analogRead(PIN_LDR);
    int pwm = (light < 400) ? map(light, 0, 400, 255, 60) : 0;
    analogWrite(PIN_STREET_LED, pwm);
  }
}
Always store millis() timestamps in unsigned long variables and avoid delay() in multi-sensor loops.

3. Fast Hardware Debugging Checklist When a Circuit Doesn’t Respond

In school labs, four out of five circuit issues come from breadboard wiring rather than C++ syntax:

  1. Check Common Ground (GND): Every sensor, LED, and external motor battery pack must share a common GND rail with the Arduino.
  2. Verify LED Polarity: Long leg (Anode +) connects toward the resistor and signal pin; short leg / flat edge (Cathode -) goes to GND.
  3. Use Serial Monitor (`Serial.begin(9600)`): Print raw LDR and MQ-2 readings to calibrate thresholds to actual classroom lighting.
  4. I2C LCD Lit Blue With No Text? Gently turn the small contrast potentiometer on the back of the I2C backpack and verify address 0x27 vs 0x3F.
Never Power Heavy DC Motors Directly From Arduino Pins

Use an L298N H-Bridge driver with a dedicated battery supply and shared GND so motor current spikes never brown out your microcontroller.

✅ Smart Home Lab Pre-Demo Checklist

  • Every LED has a 220 Ω–330 Ω resistor in series.
  • All grounds (GND) tie together on a common breadboard rail.
  • Timed actions use millis() instead of blocking delay() calls.
  • Sensor trigger thresholds are verified in Serial Monitor under real room lighting.

❓ Frequently Asked Questions

Why does an LED need a 220 Ω resistor with Arduino Uno?
Standard LEDs drop around 2V at 20 mA, while Arduino pins output 5V. By Ohm’s Law, omitting a resistor causes excessive current that can burn out both the LED and the microcontroller pin.
What is the difference between digital pins and analog pins A0–A5?
Digital pins read or write only two states (HIGH = 5V, LOW = 0V), ideal for buttons or PIR sensors. Analog pins A0–A5 measure continuous voltage across 0–1023 steps for LDR, temperature, or MQ-2 sensors.
Why should students use millis() instead of delay()?
delay() halts all code execution until the timer ends, making critical alarms unresponsive. millis() checks elapsed time without blocking the loop so multiple sensors run simultaneously.
How do you convert HC-SR04 echo time to centimeters?
Trigger a 10 µs pulse on TRIG, read duration with pulseIn(ECHO, HIGH), and compute distance in cm as duration * 0.034 / 2.
Where can students practice Arduino circuits and Smart City IoT online for free?
ArkyEdu includes interactive Grade 8 Arduino Robotics and Smart Home IoT labs, the Arkedo Smart City simulator (/oras/), Circuit Master 80s in the Arcade, and 1v1 Circuit Clash duels.

More Study Guides

← Back to Blog Index