Introduction to IoT

Unit 4: Visual Output & Sensors

From blinking LEDs to smart security systems โ€” master visual output devices and sensors that give your IoT projects eyes, ears, and voice.

โฑ๏ธ Time to Complete: 10โ€“14 hours  |  ๐Ÿ’ฐ Earning Potential: โ‚น3,000โ€“โ‚น20,000/project  |  ๐Ÿ“ 30 MCQs (Bloom's Mapped)

๐Ÿ’ผ Jobs this unlocks: Embedded Systems Engineer (โ‚น4โ€“8 LPA)  |  IoT Developer (โ‚น5โ€“10 LPA)  |  Arduino Freelancer (โ‚น3,000โ€“โ‚น20,000/project)

Section A

Opening Hook โ€” Making Machines See, Sense, and Respond

๐Ÿข How India's Smart Cities Use Sensors & Displays Every Day

Walk into any metro station in Delhi, Bangalore, or Mumbai โ€” and you're surrounded by IoT. The LCD displays showing next train arrival times? That's a 16ร—2 or OLED display driven by a microcontroller reading data from ultrasonic track sensors. The automatic doors that open when you approach? PIR motion sensors at work. The parking lots with green/red LEDs showing empty/occupied spots? IR sensors + LEDs controlled by Arduino-like boards.

India's Smart Cities Mission (100 cities, โ‚น48,000 crore budget) relies heavily on exactly the components you'll learn in this chapter. Pune's smart streetlights use PIR sensors to dim when no one is around โ€” saving 40% electricity. Jaipur's smart bins use ultrasonic sensors to detect fill levels. Ahmedabad's air quality monitors use DHT sensors for temperature and humidity readings.

What if YOU built these systems? After this chapter, you'll interface LEDs, control displays, read distances with ultrasonic sensors, measure temperature with DHT sensors, detect motion with PIR, and sense objects with IR โ€” the exact building blocks of every smart city project.

๐Ÿ‡ฎ๐Ÿ‡ณ Smart Cities Mission๐Ÿ‡ฎ๐Ÿ‡ณ Tata Elxsi๐Ÿ‡ฎ๐Ÿ‡ณ Bosch India๐Ÿ‡ฎ๐Ÿ‡ณ Wipro IoT๐Ÿ‡ฎ๐Ÿ‡ณ L&T Technology๐Ÿ‡ฎ๐Ÿ‡ณ Stellapps (IoT Dairy)
India's IoT market is projected to reach $15 billion by 2027. Over 2 billion connected devices are expected in India alone. The Indian government's PLI scheme for electronics manufacturing is fuelling demand for engineers who can work with sensors, microcontrollers, and output devices. The skills in this chapter are directly employable.
Section B

Learning Outcomes โ€” Bloom's Taxonomy Mapped

Bloom's LevelLearning Outcome
๐Ÿ”ต RememberList the pin configurations of LED, 7-segment display, LCD 16ร—2, HC-SR04, DHT22, PIR, and IR sensors
๐Ÿ”ต UnderstandExplain how PWM controls LED brightness, how ultrasonic sensors measure distance, and how PIR detects motion
๐ŸŸข ApplyBuild working circuits on breadboard and write Arduino code to interface each component
๐ŸŸข AnalyzeCompare DHT11 vs DHT22 specifications; analyze common cathode vs common anode 7-segment displays
๐ŸŸ  EvaluateSelect the most appropriate sensor for a given IoT application and justify the choice
๐ŸŸ  CreateDesign a complete IoT security system combining PIR sensor, buzzer, LCD display, and LED indicators
Section C

Concept Explanation โ€” Visual Output & Sensors from Scratch

1. LED Interfacing โ€” The Hello World of Hardware

Every electronics journey begins with blinking an LED. An LED (Light Emitting Diode) is a semiconductor device that emits light when current flows through it in the forward direction. Unlike a regular bulb, an LED only allows current in one direction โ€” from the Anode (+) to the Cathode (โˆ’).

โšก LED Circuit Theory โ€” Forward Bias & Resistor Calculation

Forward Bias

An LED works when connected in forward bias โ€” the anode connects to positive voltage and cathode to ground. The typical forward voltage drop (Vf) for a standard red LED is 2V, and it needs about 20mA of current to glow safely.

Why a Resistor?

Arduino outputs 5V, but an LED only needs 2V. Without a resistor, the extra 3V would push too much current through the LED and burn it out. The resistor limits the current to a safe level.

Resistor Calculation (Ohm's Law: V = I ร— R)

R = (Vs โˆ’ Vf) / If

R = (5V โˆ’ 2V) / 0.020A = 3 / 0.020 = 150ฮฉ

In practice, we use a 220ฮฉ resistor (standard value, slightly higher for safety).

ASCII Circuit Diagram โ€” LED with Arduino

Circuit
    Arduino Uno
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚              โ”‚
   โ”‚   Pin 13  โ—โ”€โ”€โ”ผโ”€โ”€โ”€โ”€ [220ฮฉ Resistor] โ”€โ”€โ”€โ”€โ”
   โ”‚              โ”‚                          โ”‚
   โ”‚              โ”‚                     LED (Anode +)
   โ”‚              โ”‚                          โ”‚
   โ”‚              โ”‚                     LED (Cathode -)
   โ”‚              โ”‚                          โ”‚
   โ”‚      GND  โ—โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
   โ”‚              โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   Schematic:
   Pin 13 โ”€โ”€[220ฮฉ]โ”€โ”€โ–บ|โ”€โ”€ GND
                     LED
   (โ–บ| = LED symbol: triangle is anode, line is cathode)

Connection Table

Component PinConnects ToNotes
Arduino Pin 13220ฮฉ Resistor (one end)Digital output pin
Resistor (other end)LED Anode (+, longer leg)Current limiting
LED Cathode (โˆ’, shorter leg)Arduino GNDComplete the circuit

Complete Arduino Code โ€” Blink LED

Arduino
// LED Blink Program โ€” The Hello World of IoT
// Blinks an LED connected to Pin 13 every second

const int ledPin = 13;  // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);  // Set pin 13 as output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn LED ON (5V)
  delay(1000);                  // Wait 1 second
  digitalWrite(ledPin, LOW);   // Turn LED OFF (0V)
  delay(1000);                  // Wait 1 second
}
// Serial Monitor not needed for this program // Observe: LED blinks ON for 1 second, OFF for 1 second, repeating.
Connecting LED without a resistor. This is the #1 beginner error. Without a current-limiting resistor, the LED draws excessive current and burns out within seconds. Always calculate and include the resistor. If unsure, 220ฮฉ is a safe default for most standard LEDs with a 5V supply.
How to identify LED polarity: The longer leg is always the Anode (+). If legs are trimmed, look inside the LED โ€” the smaller metal piece inside is the anode. Also, the LED body has a flat edge on the cathode side.

2. LED Brightness Control with PWM

Digital pins can only output HIGH (5V) or LOW (0V) โ€” so how do we get intermediate brightness? The answer is PWM (Pulse Width Modulation). PWM rapidly switches the pin ON and OFF at varying ratios, creating the illusion of analog voltage.

๐Ÿ“Š Understanding PWM & Duty Cycle

What is PWM?

PWM is a technique where a digital signal is switched ON and OFF very rapidly. The ratio of ON-time to the total period is called the Duty Cycle.

Duty Cycle Examples

0% duty cycle = always OFF โ†’ analogWrite(pin, 0) โ†’ 0V average โ†’ LED off

25% duty cycle = ON 25% of the time โ†’ analogWrite(pin, 64) โ†’ ~1.25V โ†’ dim

50% duty cycle = ON 50% of the time โ†’ analogWrite(pin, 127) โ†’ ~2.5V โ†’ medium

75% duty cycle = ON 75% of the time โ†’ analogWrite(pin, 191) โ†’ ~3.75V โ†’ bright

100% duty cycle = always ON โ†’ analogWrite(pin, 255) โ†’ 5V โ†’ full brightness

The analogWrite() Function

analogWrite(pin, value) โ€” where value ranges from 0 to 255 (8-bit resolution).

On Arduino Uno, PWM is available only on pins marked with ~ : pins 3, 5, 6, 9, 10, 11.

ASCII PWM Waveform Diagram

PWM Waveforms
  25% Duty Cycle (analogWrite = 64):
  5V โ”ค โ–ˆโ–ˆ                          โ–ˆโ–ˆ                          โ–ˆโ–ˆ
     โ”‚ โ–ˆโ–ˆ                          โ–ˆโ–ˆ                          โ–ˆโ–ˆ
  0V โ”ค โ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆ
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Time โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  50% Duty Cycle (analogWrite = 127):
  5V โ”ค โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ              โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
     โ”‚ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ              โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
  0V โ”ค โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Time โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

  75% Duty Cycle (analogWrite = 191):
  5V โ”ค โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
     โ”‚ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
  0V โ”ค โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Time โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

Connection Table

Component PinConnects ToNotes
Arduino Pin 9 (~PWM)220ฮฉ Resistor (one end)Must be a PWM-capable pin
Resistor (other end)LED Anode (+)Current limiting
LED Cathode (โˆ’)Arduino GNDComplete circuit

Complete Arduino Code โ€” Fading LED

Arduino
// LED Fading with PWM โ€” Smooth brightness control
// LED connected to PWM pin 9

const int ledPin = 9;  // PWM pin (marked ~)

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

void loop() {
  // Fade IN: brightness 0 โ†’ 255
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness);
    delay(10);  // 10ms per step = ~2.5 sec total fade
  }

  // Fade OUT: brightness 255 โ†’ 0
  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);
    delay(10);
  }
}
Your smartphone screen brightness uses PWM! When you slide the brightness control on your phone, it's not changing the voltage to the backlight LEDs โ€” it's changing the PWM duty cycle. Some phone screens flicker at frequencies so low that sensitive users report eye strain โ€” this is called "PWM flicker" and is a known issue in OLED displays.

3. 7-Segment Display โ€” Displaying Digits

A 7-segment display is made up of 7 LEDs (segments a through g) arranged in a figure-8 pattern, plus an optional decimal point (dp). By turning specific segments ON/OFF, you can display digits 0โ€“9 and some letters.

๐Ÿ”ข Common Cathode vs Common Anode

Common Cathode (CC)

All cathodes are connected together to GND. To light a segment, send HIGH to its anode pin. (Segment ON = HIGH)

Common Anode (CA)

All anodes are connected together to 5V (VCC). To light a segment, send LOW to its cathode pin. (Segment ON = LOW)

How to Tell?

Connect the common pin to GND and touch 5V (through 220ฮฉ resistor) to a segment pin. If it glows โ†’ Common Cathode. If not, try connecting common to 5V and segment to GND โ†’ Common Anode.

ASCII Pin Diagram โ€” 7-Segment Display

Pin Layout
         โ”€โ”€โ”€ a โ”€โ”€โ”€
        โ”‚         โ”‚
        f         b
        โ”‚         โ”‚
         โ”€โ”€โ”€ g โ”€โ”€โ”€
        โ”‚         โ”‚
        e         c
        โ”‚         โ”‚
         โ”€โ”€โ”€ d โ”€โ”€โ”€   โ— dp

  Physical Pin Layout (top view):
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  a   b   COM  d   e  โ”‚    (Top row: pins 1-5 left to right)
  โ”‚                      โ”‚
  โ”‚  f   g   COM  c   dp โ”‚    (Bottom row: pins 6-10 left to right)
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

  Note: Pin numbering may vary by manufacturer.
  COM = Common pin (connect to GND for CC, 5V for CA)

Segment Truth Table โ€” Digits 0-9 (Common Cathode, HIGH = ON)

DigitabcdefgHex
011111100x7E
101100000x30
211011010x6D
311110010x79
401100110x33
510110110x5B
610111110x5F
711100000x70
811111110x7F
911110110x7B

Connection Table โ€” 7-Segment to Arduino

7-Seg PinSegmentArduino PinResistor
Pin 7a2220ฮฉ
Pin 6b3220ฮฉ
Pin 4c4220ฮฉ
Pin 2d5220ฮฉ
Pin 1e6220ฮฉ
Pin 9f7220ฮฉ
Pin 10g8220ฮฉ
Pin 3 or 8COMGNDโ€”

ASCII Circuit Diagram

Circuit
    Arduino Uno                    7-Segment (Common Cathode)
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚ Pin 2  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค a           โ”‚
   โ”‚ Pin 3  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค b           โ”‚
   โ”‚ Pin 4  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค c           โ”‚
   โ”‚ Pin 5  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค d           โ”‚
   โ”‚ Pin 6  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค e           โ”‚
   โ”‚ Pin 7  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค f           โ”‚
   โ”‚ Pin 8  โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค g           โ”‚
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค COM (GND)   โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Complete Arduino Code โ€” Display 0 to 9

Arduino
// 7-Segment Display: Digits 0-9 (Common Cathode)
// Segments a-g connected to Arduino pins 2-8

const int segPins[] = {2, 3, 4, 5, 6, 7, 8};
//                     a   b   c   d   e   f   g

// Segment patterns for digits 0-9 (1=ON, 0=OFF)
const byte digits[10][7] = {
  {1,1,1,1,1,1,0},  // 0
  {0,1,1,0,0,0,0},  // 1
  {1,1,0,1,1,0,1},  // 2
  {1,1,1,1,0,0,1},  // 3
  {0,1,1,0,0,1,1},  // 4
  {1,0,1,1,0,1,1},  // 5
  {1,0,1,1,1,1,1},  // 6
  {1,1,1,0,0,0,0},  // 7
  {1,1,1,1,1,1,1},  // 8
  {1,1,1,1,0,1,1}   // 9
};

void displayDigit(int digit) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(segPins[i], digits[digit][i]);
  }
}

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(segPins[i], OUTPUT);
  }
}

void loop() {
  for (int d = 0; d <= 9; d++) {
    displayDigit(d);
    delay(1000);  // Show each digit for 1 second
  }
}
Forgetting resistors on each segment. Each segment is an individual LED. Every segment needs its own 220ฮฉ resistor between the Arduino pin and the segment pin. Without resistors, you risk burning out segments and damaging Arduino pins (max 40mA per pin).

4. LCD Display (16ร—2) โ€” Text Output

The 16ร—2 LCD is the most popular text display in embedded systems. It can show 16 characters per line across 2 lines = 32 characters total. It uses the HD44780 controller and communicates via parallel data pins.

LCD Pin Details โ€” All 16 Pins

Pin #SymbolFunctionConnection
1VSSGroundGND
2VDDPower Supply (+5V)5V
3V0Contrast AdjustmentPotentiometer wiper (10kฮฉ)
4RSRegister Select (0=cmd, 1=data)Arduino Pin 12
5RWRead/Write (0=write)GND (always write)
6EEnable (triggers read/write)Arduino Pin 11
7D0Data Bit 0Not connected (4-bit mode)
8D1Data Bit 1Not connected (4-bit mode)
9D2Data Bit 2Not connected (4-bit mode)
10D3Data Bit 3Not connected (4-bit mode)
11D4Data Bit 4Arduino Pin 5
12D5Data Bit 5Arduino Pin 4
13D6Data Bit 6Arduino Pin 3
14D7Data Bit 7Arduino Pin 2
15ABacklight Anode (+)5V (through 220ฮฉ)
16KBacklight Cathode (โˆ’)GND

ASCII Circuit Diagram โ€” LCD with Arduino

Circuit
    Arduino Uno                    LCD 16x2 (HD44780)
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚   5V   โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค VDD (Pin 2)          โ”‚
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค VSS (Pin 1)          โ”‚
   โ”‚          โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค RW  (Pin 5)          โ”‚
   โ”‚          โ”‚  โ”‚               โ”‚                      โ”‚
   โ”‚          โ”‚  โ”‚  โ”Œโ”€[10kฮฉ POT]โ”€โ”ค V0  (Pin 3)          โ”‚
   โ”‚          โ”‚  โ”‚  โ”‚            โ”‚                      โ”‚
   โ”‚ Pin 12 โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค RS  (Pin 4)          โ”‚
   โ”‚ Pin 11 โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค E   (Pin 6)          โ”‚
   โ”‚ Pin  5 โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค D4  (Pin 11)         โ”‚
   โ”‚ Pin  4 โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค D5  (Pin 12)         โ”‚
   โ”‚ Pin  3 โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค D6  (Pin 13)         โ”‚
   โ”‚ Pin  2 โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค D7  (Pin 14)         โ”‚
   โ”‚   5V   โ—โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€โ”€โ”ค A   (Pin 15)         โ”‚
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”˜  โ”‚           โ”ค K   (Pin 16)         โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
              POT: One end โ†’ 5V, Other end โ†’ GND, Wiper โ†’ V0

Complete Arduino Code โ€” LCD Display Text

Arduino
// LCD 16x2 Display โ€” Hello World with LiquidCrystal Library
// Uses 4-bit mode (D4-D7 only, saves Arduino pins)

#include <LiquidCrystal.h>

// Initialize: LiquidCrystal(RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);          // 16 columns, 2 rows
  lcd.setCursor(0, 0);       // Column 0, Row 0 (top line)
  lcd.print("Hello, World!");
  lcd.setCursor(0, 1);       // Column 0, Row 1 (bottom line)
  lcd.print("IoT Unit 4");
}

void loop() {
  // Scrolling text demonstration
  lcd.scrollDisplayLeft();
  delay(500);
}
4-bit vs 8-bit mode: In 4-bit mode, you only connect D4โ€“D7 (saving 4 Arduino pins). Data is sent in two 4-bit "nibbles." This is the standard approach โ€” 8-bit mode is rarely used because it wastes pins with no real speed benefit for text display applications.
Can't see text on LCD? Turn the 10kฮฉ potentiometer slowly. The V0 pin controls contrast. If it's at the wrong position, the screen appears blank or shows solid blocks. This is the most common "my LCD doesn't work" issue โ€” it's almost always the contrast pot.

5. Ultrasonic Sensor HC-SR04 โ€” Distance Measurement

The HC-SR04 ultrasonic sensor measures distance by sending out a sound pulse and timing how long it takes to bounce back โ€” just like a bat uses echolocation. It can measure distances from 2 cm to 400 cm with an accuracy of ยฑ3mm.

๐Ÿ”Š How Ultrasonic Distance Measurement Works

Step-by-Step Process

Step 1: Arduino sends a 10ยตs HIGH pulse to the Trig (Trigger) pin.

Step 2: The sensor emits 8 ultrasonic pulses at 40kHz (inaudible to humans).

Step 3: The pulses hit an object and reflect back.

Step 4: The Echo pin goes HIGH for the duration equal to the round-trip travel time.

Step 5: Arduino reads the Echo pulse duration using pulseIn().

Distance Formula

Speed of sound in air โ‰ˆ 340 m/s = 0.034 cm/ยตs

Distance = (Time ร— 0.034) / 2 cm

We divide by 2 because the pulse travels to the object and back (round trip).

Example Calculation

If Echo pulse duration = 1176 ยตs:

Distance = (1176 ร— 0.034) / 2 = 39.984 / 2 = ~20 cm

HC-SR04 Pin Details

PinFunctionDescription
VCCPower SupplyConnect to 5V
TrigTrigger InputSend 10ยตs pulse to start measurement
EchoEcho OutputReturns pulse proportional to distance
GNDGroundConnect to GND

ASCII Circuit Diagram

Circuit
    Arduino Uno                    HC-SR04
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚    5V  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค VCC        โ”‚
   โ”‚ Pin  9 โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค Trig       โ”‚    )))  โ†’  Object  โ†’  (((
   โ”‚ Pin 10 โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค Echo       โ”‚    Ultrasonic waves
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค GND        โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   Timing Diagram:
   Trig:  _____|โ€พโ€พโ€พ|_____________    (10ยตs pulse)
   Echo:  _________|โ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พ|___    (width = round-trip time)
                   |โ†โ”€โ”€ t โ”€โ”€โ†’|

Connection Table

HC-SR04 PinArduino PinNotes
VCC5VPower supply
TrigPin 9Trigger โ€” send pulse
EchoPin 10Echo โ€” read pulse duration
GNDGNDGround

Complete Arduino Code โ€” Distance Measurement

Arduino
// HC-SR04 Ultrasonic Distance Measurement
// Measures distance and displays on Serial Monitor

const int trigPin = 9;
const int echoPin = 10;

long duration;
float distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  Serial.println("HC-SR04 Distance Sensor Ready");
}

void loop() {
  // Step 1: Send 10ยตs trigger pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Step 2: Read echo pulse duration
  duration = pulseIn(echoPin, HIGH);

  // Step 3: Calculate distance
  distance = (duration * 0.034) / 2;

  // Step 4: Display result
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);  // Measure every 500ms
}
HC-SR04 Distance Sensor Ready Distance: 25.40 cm Distance: 25.38 cm Distance: 12.70 cm Distance: 5.10 cm
Pune's smart parking system uses ultrasonic sensors! Each parking spot has an HC-SR04-like sensor mounted on the ceiling. If the distance reading is less than a threshold (car present), the LED above turns red; otherwise green. The data feeds into a central dashboard showing real-time availability โ€” built with the exact same principles you just learned.
Forgetting the 2ยตs LOW before the trigger pulse. The Trig pin needs a clean LOW-to-HIGH transition. Always set Trig LOW for 2ยตs first, then HIGH for 10ยตs, then LOW again. Skipping the initial LOW can cause inconsistent readings.

6. Temperature & Humidity Sensor โ€” DHT22/DHT11

The DHT series sensors are the most popular temperature and humidity sensors in the IoT world. They're cheap, easy to use, and give both temperature and humidity readings from a single data pin.

DHT11 vs DHT22 โ€” Comparison

FeatureDHT11DHT22 (AM2302)
Temperature Range0ยฐC to 50ยฐCโˆ’40ยฐC to 80ยฐC
Temp Accuracyยฑ2ยฐCยฑ0.5ยฐC
Humidity Range20% to 80% RH0% to 100% RH
Humidity Accuracyยฑ5% RHยฑ2% RH
Sampling Rate1 reading/sec1 reading/2 sec
Price (India)โ‚น50โ€“โ‚น80โ‚น150โ€“โ‚น250
Body ColorBlueWhite
Best ForBasic indoor projectsWeather stations, precision work

Pin Diagram โ€” DHT11/DHT22

Pin Layout
   Front View (grid side facing you):
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘  โ”‚
   โ”‚  โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘  โ”‚
   โ”‚  โ–‘โ–‘ DHT 11  โ–‘โ–‘  โ”‚
   โ”‚  โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘  โ”‚
   โ”‚  โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘  โ”‚
   โ””โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”˜
     โ”‚   โ”‚   โ”‚   โ”‚
    VCC DATA NC  GND
    (1) (2) (3) (4)

   Pin 1: VCC  โ†’ 3.3V or 5V
   Pin 2: DATA โ†’ Arduino digital pin (with 10kฮฉ pull-up to VCC)
   Pin 3: NC   โ†’ Not Connected
   Pin 4: GND  โ†’ GND

ASCII Circuit Diagram

Circuit
    Arduino Uno                      DHT11/DHT22
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚    5V  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค VCC (1) โ”‚
   โ”‚          โ”‚     โ”‚              โ”‚         โ”‚
   โ”‚          โ”‚    [10kฮฉ]          โ”‚         โ”‚
   โ”‚          โ”‚     โ”‚              โ”‚         โ”‚
   โ”‚  Pin 7 โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค DATA(2) โ”‚
   โ”‚          โ”‚                    โ”‚         โ”‚
   โ”‚          โ”‚         (NC)โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค NC  (3) โ”‚
   โ”‚          โ”‚                    โ”‚         โ”‚
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค GND (4) โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   The 10kฮฉ pull-up resistor goes between VCC and DATA.
   It keeps the DATA line HIGH when idle (required for protocol).

Connection Table

DHT PinArduino PinNotes
Pin 1 (VCC)5VPower supply (3.3V also works)
Pin 2 (DATA)Pin 710kฮฉ pull-up resistor to VCC
Pin 3 (NC)โ€”Not connected
Pin 4 (GND)GNDGround

Complete Arduino Code โ€” Weather Station

Arduino
// Weather Station โ€” DHT11/DHT22 Temperature & Humidity
// Install DHT library: Sketch โ†’ Include Library โ†’ Manage โ†’ search "DHT sensor library"

#include <DHT.h>

#define DHTPIN 7       // Data pin connected to Arduino pin 7
#define DHTTYPE DHT11  // Change to DHT22 if using DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("=== IoT Weather Station ===");
  Serial.println("Reading DHT sensor...");
  Serial.println();
}

void loop() {
  delay(2000);  // Wait 2 seconds between readings

  float humidity = dht.readHumidity();
  float tempC = dht.readTemperature();       // Celsius
  float tempF = dht.readTemperature(true);  // Fahrenheit

  // Check if readings are valid
  if (isnan(humidity) || isnan(tempC)) {
    Serial.println("ERROR: Failed to read from DHT sensor!");
    return;
  }

  // Calculate Heat Index
  float heatIndex = dht.computeHeatIndex(tempC, humidity, false);

  Serial.println("--- Weather Report ---");
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print("ยฐC / ");
  Serial.print(tempF);
  Serial.println("ยฐF");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
  Serial.print("Heat Index: ");
  Serial.print(heatIndex);
  Serial.println("ยฐC");
  Serial.println();
}
=== IoT Weather Station === Reading DHT sensor... --- Weather Report --- Temperature: 28.50ยฐC / 83.30ยฐF Humidity: 65.00% Heat Index: 30.12ยฐC --- Weather Report --- Temperature: 28.60ยฐC / 83.48ยฐF Humidity: 64.00% Heat Index: 30.05ยฐC
The 10kฮฉ pull-up resistor is essential. The DHT uses a single-wire protocol where the DATA line needs to be held HIGH when idle. Without the pull-up resistor, you'll get intermittent "Failed to read" errors. Some DHT breakout boards have this resistor built-in โ€” check before adding another one.
Stellapps, a Bangalore-based IoT startup, uses DHT-type temperature sensors in their dairy IoT system called "mooPay." They monitor milk storage temperature in real-time across thousands of dairy farms in Karnataka, Tamil Nadu, and Maharashtra. If temperature rises above the safe threshold, farmers get SMS alerts. This prevents milk spoilage worth โ‚น600+ crore annually.

7. PIR Motion Sensor โ€” Detecting Movement

The PIR (Passive Infrared) sensor detects motion by sensing changes in infrared radiation emitted by warm bodies (humans, animals). It's called "passive" because it doesn't emit anything โ€” it only receives IR radiation.

๐Ÿ‘๏ธ How PIR Sensors Work

The Science

Every warm object emits infrared radiation. The PIR sensor has two pyroelectric elements behind a Fresnel lens. When a person walks past, one element detects a change in IR before the other, creating a differential signal that triggers the output.

Key Features

Detection Range: Up to 7 meters (adjustable)

Detection Angle: ~110ยฐ cone

Output: Digital HIGH when motion detected, LOW when idle

Warm-up Time: ~30-60 seconds after power-on

Adjustment Potentiometers

Sensitivity Pot: Adjust the detection range (clockwise = farther)

Time Delay Pot: How long the output stays HIGH after detection (0.3s to 5 minutes)

PIR Pin Details

PinFunctionDescription
VCCPower Supply5V to 20V (typically 5V)
OUTSignal OutputHIGH when motion detected, LOW otherwise
GNDGroundConnect to GND

ASCII Circuit Diagram โ€” Security Alarm System

Circuit
    Arduino Uno         PIR Sensor          Buzzer        LED (Red)
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚    5V  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค VCC      โ”‚
   โ”‚  Pin 2 โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค OUT      โ”‚
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค GND      โ”‚
   โ”‚          โ”‚        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
   โ”‚          โ”‚
   โ”‚  Pin 8 โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[Buzzer +]
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[Buzzer -]
   โ”‚          โ”‚
   โ”‚ Pin 13 โ—โ”€โ”ผโ”€โ”€[220ฮฉ]โ”€โ”€[LED Anode +]
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[LED Cathode -]
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   PIR Sensor (back view):
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  โŠ• Sensitivity Pot โ”‚
   โ”‚  โŠ• Time Delay Pot  โ”‚
   โ”‚                    โ”‚
   โ”‚  โ”Œโ”€โ”€โ”  โ”Œโ”€โ”€โ”  โ”Œโ”€โ”€โ” โ”‚
   โ”‚  โ”‚VCCโ”‚ โ”‚OUTโ”‚ โ”‚GNDโ”‚ โ”‚
   โ””โ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”˜

Connection Table

ComponentComponent PinArduino Pin
PIR SensorVCC5V
PIR SensorOUTPin 2
PIR SensorGNDGND
Buzzer+ (Positive)Pin 8
Buzzerโˆ’ (Negative)GND
Red LEDAnode (through 220ฮฉ)Pin 13
Red LEDCathodeGND

Complete Arduino Code โ€” Security Alarm Project

Arduino
// PIR Security Alarm System
// Detects motion โ†’ Turns on LED + Buzzer + Serial alert

const int pirPin   = 2;   // PIR sensor output
const int buzzerPin = 8;  // Buzzer
const int ledPin   = 13;  // Red LED

int motionState = 0;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("=== SECURITY SYSTEM ACTIVE ===");
  Serial.println("Warming up PIR sensor (30 sec)...");
  delay(30000);  // PIR needs 30 sec warm-up
  Serial.println("System ARMED. Monitoring...");
}

void loop() {
  motionState = digitalRead(pirPin);

  if (motionState == HIGH) {
    // INTRUDER DETECTED!
    digitalWrite(ledPin, HIGH);    // Red LED ON
    digitalWrite(buzzerPin, HIGH);  // Buzzer ON
    Serial.println("โš ๏ธ ALERT: Motion Detected!");
    delay(3000);  // Keep alarm on for 3 seconds
  } else {
    // No motion โ€” all clear
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
  }

  delay(100);
}
=== SECURITY SYSTEM ACTIVE === Warming up PIR sensor (30 sec)... System ARMED. Monitoring... โš ๏ธ ALERT: Motion Detected! โš ๏ธ ALERT: Motion Detected!
Upgrade Challenge: Modify the security alarm to count how many times motion is detected and display the count on the Serial Monitor. Add a timestamp using millis() to log when each detection occurred. Bonus: Add an LCD to display "INTRUDER ALERT" and the count.

8. IR Sensor โ€” Infrared Object Detection

An IR (Infrared) sensor consists of an IR transmitter (LED) that emits infrared light and an IR receiver (photodiode) that detects the reflected light. When an object is placed in front of the sensor, the IR light bounces back and is detected โ€” triggering a response.

๐Ÿ”ด How IR Object Detection Works

Transmitter & Receiver

The IR LED constantly emits infrared light (invisible to human eyes, wavelength ~940nm). The photodiode is placed next to it. When an object comes close, IR light reflects off the object's surface and hits the photodiode.

Digital IR Sensor Module

Most IR sensor modules (like FC-51) have a built-in comparator (LM393) that gives a clean digital output: LOW when object is detected (reflected IR received), HIGH when no object. A potentiometer on the module adjusts detection range (2โ€“30 cm).

Analog vs Digital Output

Digital: HIGH/LOW only โ€” object present or not

Analog: Variable voltage proportional to proximity (for advanced distance estimation)

IR Sensor Module Pin Details

PinFunctionDescription
VCCPower Supply3.3V to 5V
GNDGroundConnect to GND
OUTDigital OutputLOW = object detected, HIGH = no object

ASCII Circuit Diagram

Circuit
    Arduino Uno               IR Sensor Module (FC-51)
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚    5V  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค VCC              โ”‚
   โ”‚  Pin 3 โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค OUT              โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”
   โ”‚   GND  โ—โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค GND              โ”‚   โ”‚     โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚                  โ”‚   โ”‚ Obj โ”‚
                             โ”‚  [IR TX]โ†’โ†’โ†’โ†’โ†’โ†’โ†’โ†’โ†’โ”ผโ†’โ†’โ†’โ”‚ ect โ”‚
                             โ”‚  [IR RX]โ†โ†โ†โ†โ†โ†โ†โ†โ”ผโ†โ†โ†โ”‚     โ”‚
                             โ”‚                  โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚  โŠ• Range Pot     โ”‚
                             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   IR TX = Infrared transmitter LED (emits IR)
   IR RX = Photodiode receiver (detects reflected IR)

Connection Table

IR Module PinArduino PinNotes
VCC5VPower supply
OUTPin 3Digital output (LOW = detected)
GNDGNDGround

Complete Arduino Code โ€” Object Detection

Arduino
// IR Sensor โ€” Object Detection
// Detects objects and turns on LED indicator

const int irPin  = 3;   // IR sensor output
const int ledPin = 13;  // Indicator LED

void setup() {
  pinMode(irPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("IR Object Detection Ready");
}

void loop() {
  int irValue = digitalRead(irPin);

  if (irValue == LOW) {
    // Object detected (IR reflected back)
    digitalWrite(ledPin, HIGH);
    Serial.println("Object DETECTED!");
  } else {
    // No object
    digitalWrite(ledPin, LOW);
    Serial.println("No object.");
  }

  delay(200);
}

Line Follower Robot โ€” The Classic IR Application

A line follower robot uses 2 or 3 IR sensors pointed downward at the surface. A black line on a white surface absorbs IR (no reflection โ†’ HIGH), while the white surface reflects IR (reflection โ†’ LOW). The robot's logic:

Left IRRight IRRobot Action
Line (HIGH)No line (LOW)Turn LEFT
No line (LOW)Line (HIGH)Turn RIGHT
No line (LOW)No line (LOW)Move FORWARD (line is between sensors)
Line (HIGH)Line (HIGH)STOP (end of track or intersection)
IIT Bombay's e-Yantra competition is one of India's largest robotics contests. Many participating robots use IR sensors for line following, edge detection, and obstacle avoidance. Over 10,000 engineering students from 500+ Indian colleges participate annually. Winning teams often get direct interview calls from Bosch, Tata Elxsi, and KPIT Technologies.
IR sensors don't work well with black objects. Black surfaces absorb IR instead of reflecting it. If your project involves detecting dark objects, consider using an ultrasonic sensor instead. Also, direct sunlight contains IR radiation that can interfere with IR sensor readings โ€” use them in indoor environments for best results.
Section D

Learn by Doing โ€” 3-Tier Lab Structure

๐ŸŸข Tier 1 โ€” GUIDED TASK: Build a Traffic Light System

โฑ๏ธ 45โ€“60 minutesBeginnerZero prior knowledge assumed

Project: 3-LED Traffic Light Controller

Build a traffic light using Red, Yellow, and Green LEDs that cycle automatically.

Components Needed

3ร— LEDs (Red, Yellow, Green) | 3ร— 220ฮฉ resistors | Breadboard | Jumper wires | Arduino Uno

Connection Table

LED ColorArduino PinResistor
RedPin 4220ฮฉ
YellowPin 3220ฮฉ
GreenPin 2220ฮฉ

Step-by-Step Code

Arduino
// Traffic Light Controller โ€” Red โ†’ Yellow โ†’ Green cycle

const int redPin    = 4;
const int yellowPin = 3;
const int greenPin  = 2;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  // RED โ€” Stop (5 seconds)
  digitalWrite(redPin, HIGH);
  digitalWrite(yellowPin, LOW);
  digitalWrite(greenPin, LOW);
  delay(5000);

  // YELLOW โ€” Get Ready (2 seconds)
  digitalWrite(redPin, LOW);
  digitalWrite(yellowPin, HIGH);
  digitalWrite(greenPin, LOW);
  delay(2000);

  // GREEN โ€” Go (5 seconds)
  digitalWrite(redPin, LOW);
  digitalWrite(yellowPin, LOW);
  digitalWrite(greenPin, HIGH);
  delay(5000);

  // YELLOW โ€” Slow Down (2 seconds)
  digitalWrite(redPin, LOW);
  digitalWrite(yellowPin, HIGH);
  digitalWrite(greenPin, LOW);
  delay(2000);
}

๐ŸŽ‰ Upload and observe! The LEDs should cycle just like a real traffic light. Take a video โ€” this is a great portfolio demo.

๐ŸŸก Tier 2 โ€” SEMI-GUIDED TASK: Smart Distance Meter with LCD

โฑ๏ธ 90โ€“120 minutesIntermediateHints provided, you fill the gaps

Your Mission:

Combine the HC-SR04 ultrasonic sensor with a 16ร—2 LCD to build a real-time distance meter that displays measurements on the LCD screen.

Components:

Arduino Uno | HC-SR04 sensor | 16ร—2 LCD | 10kฮฉ potentiometer | Breadboard | Jumper wires

Hints:

  1. Wire the LCD as shown in Section C.4 (RS=12, E=11, D4=5, D5=4, D6=3, D7=2)
  2. Wire the HC-SR04 to Trig=9, Echo=10 (same as Section C.5)
  3. Include both libraries: LiquidCrystal.h
  4. Display format: Line 1: "Distance Meter" | Line 2: "Dist: XX.XX cm"
  5. Update LCD every 500ms using lcd.clear() before rewriting
  6. Add a warning: If distance < 10cm, display "TOO CLOSE!" on line 2
Stretch Goal: Add a buzzer that beeps faster as objects get closer (like a car parking sensor). Use a formula: beepDelay = distance ร— 20 milliseconds.

๐Ÿ”ด Tier 3 โ€” OPEN CHALLENGE: Complete IoT Security System

โฑ๏ธ 2โ€“3 hoursAdvancedNo step-by-step โ€” design it yourself

The Brief:

Design and build a complete room security system that combines multiple sensors and output devices:

  1. PIR sensor for motion detection
  2. Ultrasonic sensor for distance-based proximity alert
  3. LCD display showing system status ("ARMED", "ALERT!", distance)
  4. Red + Green LEDs for visual status (Green=safe, Red=alert)
  5. Buzzer for audio alarm
  6. DHT sensor for ambient temperature display when idle

Requirements:

  • System should have a 30-second warm-up period (for PIR)
  • LCD shows temperature/humidity when no motion detected
  • When PIR detects motion OR ultrasonic reads < 30cm โ†’ trigger alarm
  • Alarm: Red LED blinks + Buzzer beeps + LCD shows "INTRUDER ALERT"
  • Log events to Serial Monitor with timestamps
This project is portfolio-worthy. Film a demo video, push code to GitHub, and write a short project description. Arduino security system projects on Fiverr sell for โ‚น2,000โ€“โ‚น8,000. College mini-projects using this design consistently score 80%+ marks.
Section E

Industry Spotlight โ€” A Day in the Life

๐Ÿ‘จโ€๐Ÿ’ป Rahul Verma, 24 โ€” IoT Engineer at Tata Elxsi, Pune

Background: B.Tech ECE from VIT Vellore. Started with Arduino projects in 2nd year. Built a smart irrigation system for his college campus using soil moisture sensors and relay modules. Won Tata Elxsi hackathon and got a direct PPO (Pre-Placement Offer).

A Typical Day:

9:00 AM โ€” Sprint standup with the automotive IoT team. Discuss progress on the cabin temperature monitoring system for Tata Motors EVs.

10:00 AM โ€” Debug a DHT22 sensor reading issue on the prototype board. Turns out the pull-up resistor value was wrong โ€” replaced 4.7kฮฉ with 10kฮฉ. Fixed.

11:30 AM โ€” Write firmware in C/C++ for the STM32 microcontroller. Implement UART communication between ultrasonic sensors and the main ECU.

1:00 PM โ€” Lunch. Discuss with colleagues about using MQTT protocol for sensor data transmission.

2:00 PM โ€” Test the 7-segment display module for the dashboard instrument cluster. Verify segment timing and brightness under different ambient light conditions.

4:30 PM โ€” Code review for the PIR-based occupancy detection module. Review a junior developer's code for edge cases.

5:30 PM โ€” Learning hour: Explore ESP32 BLE for wireless sensor data. Next sprint includes WiFi-connected sensors.

DetailInfo
Tools Used DailyArduino IDE, STM32CubeIDE, C/C++, Oscilloscope, Logic Analyzer, MQTT, Git
Entry Salary (2024)โ‚น4.5โ€“7 LPA + benefits
Mid-Level (3โ€“5 yrs)โ‚น10โ€“18 LPA
Senior (7+ yrs)โ‚น20โ€“40 LPA
Companies HiringTata Elxsi, Bosch India, KPIT Technologies, Wipro IoT, L&T Technology Services, Continental, Harman, Samsung R&D
Section F

Earn With It โ€” Freelance & Income Roadmap

๐Ÿ’ฐ Your Earning Path After This Chapter

Portfolio Pieces: Traffic Light Controller | Smart Distance Meter | IoT Security System โ€” all with code on GitHub + demo videos.

Beginner Gig Ideas:

โ€ข Arduino project for college students (mini-project help) โ€” โ‚น1,500โ€“โ‚น5,000

โ€ข Home automation prototype (PIR + relay + LED) โ€” โ‚น3,000โ€“โ‚น8,000

โ€ข Smart parking prototype for college tech fest โ€” โ‚น2,000โ€“โ‚น6,000

โ€ข IoT weather station with LCD display โ€” โ‚น2,500โ€“โ‚น7,000

โ€ข Line follower robot for competitions โ€” โ‚น4,000โ€“โ‚น10,000

PlatformBest ForTypical Rate
FiverrArduino/IoT project gigs globally$15โ€“$80/project (โ‚น1,200โ€“โ‚น6,500)
Freelancer.comEmbedded systems & IoT projects$20โ€“$100/project
InternshalaIndian student internships in IoT/embeddedโ‚น3,000โ€“โ‚น10,000/month
College WhatsApp GroupsMini-project help for juniorsโ‚น1,500โ€“โ‚น5,000/project
Local Shops/OfficesCustom automation prototypesโ‚น3,000โ€“โ‚น15,000/project

โฑ๏ธ Time to First Earning: 1โ€“2 weeks (offer to build Arduino mini-projects for juniors in your department)

Document everything. Film a 1-minute demo video of every project you build. Push code to GitHub with a clear README. Add circuit diagrams using Fritzing (free). This portfolio is more valuable than your resume for IoT jobs. Recruiters at Bosch and Tata Elxsi specifically look for GitHub profiles with hardware project repositories.
Section G

MCQ Assessment Bank โ€” 30 Questions (Bloom's Mapped)

Remember / Identify (Q1โ€“Q5)

Q1

The longer leg of an LED is the:

  1. Cathode (โˆ’)
  2. Anode (+)
  3. Ground pin
  4. Signal pin
Remember
โœ… Answer: (B) Anode (+) โ€” The longer leg of an LED is always the anode (positive terminal). The shorter leg is the cathode (negative terminal).
Q2

The analogWrite() function in Arduino accepts values in the range:

  1. 0 to 1023
  2. 0 to 255
  3. 0 to 100
  4. 0 to 5
Remember
โœ… Answer: (B) 0 to 255 โ€” analogWrite() uses 8-bit PWM resolution (2โธ = 256 levels, from 0 to 255). 0 = fully OFF, 255 = fully ON.
Q3

How many segments does a 7-segment display have (excluding the decimal point)?

  1. 6
  2. 7
  3. 8
  4. 9
Remember
โœ… Answer: (B) 7 โ€” The display has 7 segments labeled a, b, c, d, e, f, and g. The decimal point (dp) is an additional 8th LED but is not counted in the "7-segment" name.
Q4

The HC-SR04 ultrasonic sensor has how many pins?

  1. 2 (VCC, GND)
  2. 3 (VCC, OUT, GND)
  3. 4 (VCC, Trig, Echo, GND)
  4. 5 (VCC, Trig, Echo, OUT, GND)
Remember
โœ… Answer: (C) 4 pins โ€” VCC (power), Trig (trigger input), Echo (echo output), and GND (ground). Trig sends the pulse, Echo returns the timing.
Q5

Which Arduino library is used to interface with a 16ร—2 LCD?

  1. Servo.h
  2. Wire.h
  3. LiquidCrystal.h
  4. SPI.h
Remember
โœ… Answer: (C) LiquidCrystal.h โ€” This is the standard library for parallel-interface HD44780-based LCDs. For I2C LCDs, you would use LiquidCrystal_I2C.h.

Understand / Explain (Q6โ€“Q10)

Q6

Why is a current-limiting resistor necessary when connecting an LED to an Arduino?

  1. To increase the LED brightness
  2. To convert AC to DC
  3. To limit current and prevent the LED from burning out
  4. To amplify the signal
Understand
โœ… Answer: (C) โ€” Arduino outputs 5V, but a standard LED only needs ~2V and 20mA. Without a resistor, excessive current flows through the LED, causing it to overheat and burn out. The resistor drops the extra voltage safely.
Q7

What does "duty cycle" mean in the context of PWM?

  1. The frequency of the PWM signal
  2. The ratio of ON-time to the total period of the signal
  3. The maximum voltage output
  4. The number of PWM pins available
Understand
โœ… Answer: (B) โ€” Duty cycle is the percentage of time the signal is HIGH (ON) compared to the total period. A 50% duty cycle means the signal is ON half the time and OFF half the time, resulting in an average voltage of 2.5V from a 5V signal.
Q8

In a Common Cathode 7-segment display, to light up a segment you must:

  1. Send LOW to the segment pin
  2. Send HIGH to the segment pin
  3. Connect the segment to GND
  4. Disconnect the common pin
Understand
โœ… Answer: (B) โ€” In a Common Cathode display, all cathodes are tied to GND. To light a segment, you send HIGH (5V) to its anode pin, completing the circuit and allowing current to flow through the LED segment.
Q9

Why does the ultrasonic distance formula divide by 2?

  1. Because the sensor has 2 pins
  2. Because sound travels at half speed in air
  3. Because the pulse travels to the object AND back (round trip)
  4. Because Arduino runs at half clock speed
Understand
โœ… Answer: (C) โ€” The ultrasonic pulse travels from the sensor to the object and then reflects back. The measured time is the round-trip time, so we divide by 2 to get the one-way distance to the object.
Q10

Why is a PIR sensor called "passive"?

  1. It requires no power supply
  2. It only receives infrared radiation and does not emit any
  3. It works only in passive mode
  4. It can only detect stationary objects
Understand
โœ… Answer: (B) โ€” The word "passive" means the sensor does not emit any infrared signal. It passively detects the infrared radiation naturally emitted by warm bodies (humans, animals). Compare this to an "active" IR sensor that emits and receives IR.

Apply / Calculate (Q11โ€“Q15)

Q11

Calculate the resistor needed for an LED with Vf = 1.8V, If = 15mA, using a 5V Arduino:

  1. 120ฮฉ
  2. 213ฮฉ
  3. 330ฮฉ
  4. 470ฮฉ
Apply
โœ… Answer: (B) 213ฮฉ โ€” R = (5V โˆ’ 1.8V) / 0.015A = 3.2 / 0.015 = 213.3ฮฉ. In practice, you'd use the next standard value: 220ฮฉ.
Q12

If analogWrite(pin, 191) is used, what is the approximate duty cycle?

  1. 25%
  2. 50%
  3. 75%
  4. 100%
Apply
โœ… Answer: (C) 75% โ€” Duty cycle = (191/255) ร— 100 = 74.9% โ‰ˆ 75%. The LED would be at approximately 75% brightness.
Q13

An HC-SR04 sensor returns an echo pulse of 882 ยตs. What is the distance to the object?

  1. 10 cm
  2. 15 cm
  3. 20 cm
  4. 30 cm
Apply
โœ… Answer: (B) 15 cm โ€” Distance = (882 ร— 0.034) / 2 = 29.988 / 2 = 14.994 โ‰ˆ 15 cm.
Q14

To display the digit "5" on a Common Cathode 7-segment display, which segments must be ON?

  1. a, b, c, d, e, f
  2. a, c, d, f, g
  3. a, b, d, e, g
  4. b, c, d, e, f
Apply
โœ… Answer: (B) a, c, d, f, g โ€” Digit 5 lights segments a (top), c (bottom-right), d (bottom), f (top-left), and g (middle). Segments b and e are OFF.
Q15

What Arduino function is used to read the HC-SR04 echo pulse duration?

  1. digitalRead()
  2. analogRead()
  3. pulseIn()
  4. tone()
Apply
โœ… Answer: (C) pulseIn() โ€” The pulseIn(echoPin, HIGH) function measures how long the Echo pin stays HIGH in microseconds, which corresponds to the round-trip time of the ultrasonic pulse.

Analyze / Compare (Q16โ€“Q20)

Q16

Which sensor would be MORE appropriate for detecting a black object on a conveyor belt?

  1. IR sensor
  2. Ultrasonic sensor
  3. PIR sensor
  4. DHT sensor
Analyze
โœ… Answer: (B) Ultrasonic sensor โ€” Black objects absorb infrared light, so IR sensors cannot detect them reliably. Ultrasonic sensors use sound waves which reflect off objects regardless of color. PIR detects motion/heat, and DHT measures temperature โ€” both irrelevant here.
Q17

Comparing DHT11 and DHT22, which statement is TRUE?

  1. DHT11 is more accurate than DHT22
  2. DHT22 can measure negative temperatures, DHT11 cannot
  3. DHT11 is more expensive than DHT22
  4. Both have identical specifications
Analyze
โœ… Answer: (B) โ€” DHT22 measures โˆ’40ยฐC to 80ยฐC (covers negative temps), while DHT11 only measures 0ยฐC to 50ยฐC. DHT22 is also more accurate (ยฑ0.5ยฐC vs ยฑ2ยฐC) but costs more (โ‚น150โ€“250 vs โ‚น50โ€“80).
Q18

A student's LCD shows all black boxes instead of text. The most likely cause is:

  1. Wrong Arduino code
  2. LCD is defective
  3. Contrast potentiometer (V0) is at the wrong position
  4. Power supply is insufficient
Analyze
โœ… Answer: (C) โ€” When the V0 (contrast) pin receives too much voltage, all pixel blocks appear dark. Slowly adjusting the 10kฮฉ potentiometer should reveal the text. This is the most common LCD troubleshooting issue.
Q19

What is the key difference between a PIR sensor and an IR sensor?

  1. PIR is analog, IR is digital
  2. PIR detects changes in infrared radiation (motion), IR emits and receives reflected IR (proximity)
  3. PIR works outdoors only, IR works indoors only
  4. There is no difference โ€” they are the same
Analyze
โœ… Answer: (B) โ€” PIR is passive (only receives IR changes caused by moving warm bodies) and detects motion. An active IR sensor has a transmitter and receiver โ€” it emits IR and detects the reflection, used for proximity/object detection.
Q20

In 4-bit mode LCD operation, data is sent as:

  1. 4 bits at once (half a byte)
  2. Two 4-bit nibbles sequentially
  3. 8 bits simultaneously
  4. Serial data one bit at a time
Analyze
โœ… Answer: (B) โ€” In 4-bit mode, each byte of data is split into two 4-bit "nibbles" and sent sequentially over D4โ€“D7. This uses only 4 data pins instead of 8, saving Arduino pins at the cost of slightly slower communication.

Evaluate / Justify (Q21โ€“Q25)

Q21

For a smart parking system that needs to detect cars in each spot, which sensor is the best choice?

  1. DHT22 (temperature sensor)
  2. PIR sensor (motion detector)
  3. Ultrasonic sensor (distance measurement)
  4. IR sensor (object detection)
Evaluate
โœ… Answer: (C) Ultrasonic sensor โ€” Cars are stationary once parked (PIR won't work as it needs motion). Cars can be any color (IR may fail with dark cars). Temperature is irrelevant (DHT22 no use). Ultrasonic sensors reliably detect presence by measuring distance regardless of color or lighting conditions.
Q22

A student uses digitalWrite() instead of analogWrite() for LED dimming. What will happen?

  1. LED will dim smoothly
  2. LED will only be fully ON or fully OFF โ€” no dimming
  3. Arduino will crash
  4. LED will blink randomly
Evaluate
โœ… Answer: (B) โ€” digitalWrite() can only output HIGH (5V) or LOW (0V). It cannot produce intermediate values needed for dimming. You must use analogWrite() on a PWM-capable pin (~3, ~5, ~6, ~9, ~10, ~11) for brightness control.
Q23

Why would you choose a DHT22 over a DHT11 for an outdoor weather station in Shimla (winter temps can reach โˆ’5ยฐC)?

  1. DHT22 is cheaper
  2. DHT22 has a wider temperature range (โˆ’40ยฐC to 80ยฐC) while DHT11 only reads 0ยฐC to 50ยฐC
  3. DHT22 uses fewer pins
  4. DHT22 doesn't need a pull-up resistor
Evaluate
โœ… Answer: (B) โ€” DHT11's range starts at 0ยฐC, so it cannot measure the โˆ’5ยฐC temperatures in Shimla winters. DHT22 measures down to โˆ’40ยฐC with better accuracy (ยฑ0.5ยฐC vs ยฑ2ยฐC), making it the correct choice for cold-climate outdoor applications.
Q24

A PIR-based automatic light system keeps triggering falsely in a room with an AC vent. The most likely cause is:

  1. The PIR sensor is defective
  2. Hot air from the AC creates temperature changes that the PIR interprets as motion
  3. The LED is too bright
  4. The Arduino code has a bug
Evaluate
โœ… Answer: (B) โ€” PIR sensors detect changes in infrared (heat) radiation. An AC vent blowing hot/cold air creates moving thermal gradients that the PIR interprets as motion. Solutions: reposition the PIR away from the vent, or reduce sensitivity using the potentiometer.
Q25

For a line-following robot, using 3 IR sensors instead of 2 provides what advantage?

  1. It uses less power
  2. It enables the robot to detect intersections and make smoother turns
  3. It makes the code simpler
  4. It reduces the cost
Evaluate
โœ… Answer: (B) โ€” A 3rd (center) sensor allows the robot to detect when it's perfectly centered on the line. It also enables detection of T-intersections and cross-junctions. Two sensors only know "too far left" or "too far right" but can't confirm "perfectly centered."

Create / Design (Q26โ€“Q30)

Q26

You need to build a system that displays room temperature on an LCD and turns on a fan (via relay) if temp exceeds 30ยฐC. Which components do you need?

  1. DHT sensor + LCD + Relay module + Arduino
  2. PIR sensor + 7-segment + Motor driver
  3. Ultrasonic + LED + Buzzer
  4. IR sensor + LCD + Servo motor
Create
โœ… Answer: (A) โ€” DHT sensor reads temperature, LCD displays it, and a relay module switches the fan ON/OFF based on the temperature threshold. This is a classic IoT thermostat design.
Q27

To create a "smart dustbin" that opens its lid when a hand approaches, which combination is optimal?

  1. PIR sensor + DC motor
  2. Ultrasonic sensor + Servo motor
  3. DHT sensor + Stepper motor
  4. IR sensor + Relay
Create
โœ… Answer: (B) โ€” Ultrasonic sensor detects the hand approaching within a specific range (e.g., <15 cm). Servo motor provides precise angular control to open/close the lid. PIR might work but gives less precise distance control. DHT is irrelevant.
Q28

Design a visitor counter for a shop doorway. When a person enters, a counter increments on the LCD. Which sensor is best for detecting a person passing through?

  1. DHT22
  2. Two IR sensors placed at the doorway
  3. Single PIR sensor
  4. Potentiometer
Create
โœ… Answer: (B) โ€” Two IR sensors placed a few centimeters apart can detect direction of movement (which sensor triggers first determines entry vs exit). A single PIR would detect motion but can't differentiate between entry and exit or count accurately.
Q29

You're designing a "garage parking assistant" that shows distance on LEDs (Green=far, Yellow=close, Red=stop). Which sensor and output combination would you use?

  1. PIR sensor + single LED
  2. Ultrasonic sensor + 3 colored LEDs
  3. DHT sensor + buzzer
  4. IR sensor + LCD
Create
โœ… Answer: (B) โ€” Ultrasonic sensor continuously measures distance to the approaching car. Code logic: if distance >100cm โ†’ Green LED, if 30โ€“100cm โ†’ Yellow LED, if <30cm โ†’ Red LED (+ optional buzzer). This replicates commercial parking assist systems.
Q30

Design an automated greenhouse monitoring system. Which combination of sensors and outputs would provide the most useful data?

  1. DHT sensor (temp/humidity) + LCD display + Relay (for water pump) + Soil moisture sensor
  2. PIR sensor + Buzzer + LED
  3. Ultrasonic sensor + 7-segment display
  4. IR sensor + Motor + Potentiometer
Create
โœ… Answer: (A) โ€” A greenhouse needs temperature/humidity monitoring (DHT), soil moisture sensing (soil moisture sensor), visual status display (LCD), and automated watering control (relay + pump). This is a complete IoT agriculture system.
Section H

Short Answer Questions (8 Questions)

Q1. What is forward bias in an LED? Why does an LED need it to work?

Model Answer: Forward bias occurs when the anode (+) of an LED is connected to a higher voltage than the cathode (โˆ’). An LED is a diode โ€” it only conducts current in one direction. In forward bias, the potential barrier across the P-N junction is reduced, allowing current to flow and the LED to emit light. In reverse bias (cathode to positive), the barrier increases and no current flows, so the LED remains off. The minimum voltage needed to achieve forward bias is called the forward voltage (Vf), typically 1.8Vโ€“3.3V depending on color.

Q2. Calculate the resistance needed for a blue LED (Vf = 3.2V, If = 20mA) connected to a 5V Arduino pin.

Model Answer: Using Ohm's law: R = (Vs โˆ’ Vf) / If = (5V โˆ’ 3.2V) / 0.020A = 1.8 / 0.020 = 90ฮฉ. The nearest standard resistor value is 100ฮฉ. Using 100ฮฉ gives a current of 1.8/100 = 18mA, which is safe for the LED and within the Arduino's 40mA per-pin limit.

Q3. Explain the difference between Common Cathode and Common Anode 7-segment displays.

Model Answer: In a Common Cathode (CC) display, all cathodes of the 7 LED segments are connected together and tied to GND. To turn on a segment, you send HIGH (5V) to its anode pin. In a Common Anode (CA) display, all anodes are connected together and tied to VCC (5V). To turn on a segment, you send LOW (0V) to its cathode pin. The code logic is inverted: CC uses HIGH=ON, CA uses LOW=ON. CC displays are more common in Arduino projects because the logic matches Arduino's HIGH/LOW convention intuitively.

Q4. What is the purpose of the V0 pin on a 16ร—2 LCD, and how is it controlled?

Model Answer: The V0 pin controls the contrast of the LCD display. It determines how dark or light the text pixels appear against the background. V0 is connected to the wiper (middle terminal) of a 10kฮฉ potentiometer, with the other two terminals connected to VCC (5V) and GND. Turning the potentiometer varies the voltage on V0 between 0V and 5V, adjusting contrast. If contrast is wrong, the display may appear blank or show solid black blocks. Optimal contrast is usually around 0.3Vโ€“0.7V on V0.

Q5. Write the complete distance calculation formula for the HC-SR04 sensor and explain each component.

Model Answer: Distance (cm) = (Duration ร— 0.034) / 2. Where: Duration = time in microseconds measured by pulseIn(echoPin, HIGH) โ€” the duration the Echo pin stays HIGH. 0.034 = speed of sound in cm/ยตs (340 m/s = 34,000 cm/s = 0.034 cm/ยตs). รท 2 = because the ultrasonic pulse travels to the object AND reflects back, so the measured duration is the round-trip time. We divide by 2 to get the one-way distance. Example: Duration = 588ยตs โ†’ Distance = (588 ร— 0.034) / 2 = 19.99 / 2 โ‰ˆ 10 cm.

Q6. Why does a DHT sensor need a 10kฮฉ pull-up resistor on its DATA pin?

Model Answer: The DHT sensor communicates using a single-wire bidirectional protocol. Both the sensor and the Arduino take turns pulling the DATA line LOW to send bits. When neither is actively driving the line, it needs to return to HIGH โ€” this is the "idle" state. The 10kฮฉ pull-up resistor connected between VCC and DATA ensures the line defaults to HIGH when not actively driven LOW. Without it, the DATA line would "float" at an undefined voltage, causing communication errors and intermittent "Failed to read" errors. Some breakout boards have this resistor built-in.

Q7. How does a PIR sensor differentiate between a stationary warm object and a moving person?

Model Answer: A PIR sensor has two pyroelectric sensing elements side by side, covered by a Fresnel lens that divides the field of view into multiple zones. A stationary warm object (like a radiator) produces a constant IR level on both elements โ€” no change, no trigger. When a person moves across the sensor's field of view, one element detects a change in IR level before the other, creating a differential voltage spike. This change is what triggers the output. This is why PIR sensors detect movement, not just heat presence. The Fresnel lens amplifies this effect by creating alternating sensitive and blind zones.

Q8. Explain why IR sensors are unsuitable for detecting black objects, and suggest an alternative.

Model Answer: IR sensors work by emitting infrared light and detecting the reflection. Black surfaces absorb most of the IR light instead of reflecting it. Since very little IR returns to the receiver, the sensor fails to detect the object โ€” it "sees through" black objects as if they aren't there. This is a fundamental limitation based on the absorption/reflection properties of the surface. Alternative: Use an ultrasonic sensor (HC-SR04), which uses sound waves instead of light. Sound reflects off objects regardless of their color, surface texture, or material (as long as the surface isn't sound-absorbent like foam). For line-following robots on dark lines, this is actually an advantage โ€” the IR sensor detects the black line as "no reflection."

Section I

Long Answer Questions (3 Questions)

Q1. Explain the working principle, circuit diagram, connection table, and complete Arduino code for interfacing an HC-SR04 ultrasonic sensor to measure distance and display it on a 16ร—2 LCD. (10 marks)

Model Answer:

Working Principle: The HC-SR04 ultrasonic sensor works on the principle of echolocation. A 10ยตs HIGH pulse is sent to the Trig pin, which causes the sensor to emit 8 ultrasonic pulses at 40kHz. These sound waves travel through the air, hit an object, and reflect back to the sensor. The Echo pin goes HIGH for a duration proportional to the round-trip travel time. Using the speed of sound (340 m/s), we calculate distance: Distance = (Time ร— 0.034) / 2 cm.

Connection Table:

ComponentPinArduino Pin
HC-SR04VCC5V
HC-SR04TrigPin 9
HC-SR04EchoPin 10
HC-SR04GNDGND
LCDRSPin 12
LCDEPin 11
LCDD4-D7Pins 5, 4, 3, 2
LCDVSS, RW, KGND
LCDVDD, A5V (A through 220ฮฉ)
LCDV010kฮฉ pot wiper
Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int trigPin = 9;
const int echoPin = 10;
long duration;
float distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lcd.begin(16, 2);
  lcd.print("Distance Meter");
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034) / 2;

  lcd.setCursor(0, 1);
  lcd.print("Dist: ");
  lcd.print(distance);
  lcd.print(" cm   ");  // Extra spaces clear old chars

  delay(500);
}

Q2. Compare and contrast the four sensors covered in this unit (Ultrasonic HC-SR04, DHT11/DHT22, PIR, and IR) in terms of working principle, output type, detection range, use cases, and limitations. Present in tabular form. (10 marks)

Model Answer:

FeatureHC-SR04 (Ultrasonic)DHT11/DHT22PIRIR Sensor
PrincipleSound echo (time-of-flight)Capacitive humidity + thermistorPyroelectric (IR radiation change)IR reflection (transmit + receive)
What it measuresDistance to objectTemperature & humidityMotion (warm body movement)Object presence/proximity
Output TypePulse width (analog duration)Digital serial (single-wire)Digital (HIGH/LOW)Digital (HIGH/LOW) or Analog
Range2โ€“400 cmDHT11: 0โ€“50ยฐC, DHT22: โˆ’40โ€“80ยฐCUp to 7m, 110ยฐ cone2โ€“30 cm (adjustable)
Accuracyยฑ3mmDHT11: ยฑ2ยฐC, DHT22: ยฑ0.5ยฐCN/A (binary)N/A (binary)
Use CasesParking sensor, level meter, robot obstacle avoidanceWeather station, greenhouse, HVACSecurity alarm, auto lights, occupancyLine follower, object counter, edge detect
LimitationsSoft/angled surfaces absorb sound; minimum 2cmSlow sampling (1โ€“2 sec); humidity-sensitiveFalse triggers from heat sources; 30s warm-upFails with black objects; sunlight interference
Price (India)โ‚น40โ€“โ‚น80โ‚น50โ€“โ‚น250โ‚น50โ€“โ‚น100โ‚น30โ€“โ‚น60

Key Insight: No single sensor is "best" โ€” each excels in specific scenarios. Good IoT system design often combines multiple sensors. For example, a smart home security system might use PIR for room occupancy, ultrasonic for proximity-based triggers, and IR for doorway counting. Understanding the strengths and limitations of each sensor is critical for selecting the right tool for the job.

Q3. Design a complete "Smart Home Entry System" that uses a PIR sensor to detect someone approaching the door, an ultrasonic sensor to measure their distance, an LCD to display a welcome message, a green LED for "approach" status, a red LED for "too close" warning, and a buzzer for doorbell function. Draw the circuit diagram, provide the complete connection table, and write the full Arduino code. (15 marks)

Model Answer:

System Design:

  • PIR detects person approaching โ†’ LCD shows "Welcome!"
  • Ultrasonic measures distance: >50cm โ†’ Green LED | <50cm โ†’ Red LED + Buzzer rings (doorbell)
  • When no one is around โ†’ LCD shows "Smart Home" on line 1, "System Ready" on line 2

Connection Table:

ComponentComponent PinArduino Pin
PIR SensorVCC / OUT / GND5V / Pin 6 / GND
HC-SR04VCC / Trig / Echo / GND5V / Pin 9 / Pin 10 / GND
LCD 16ร—2RS / E / D4โ€“D7Pin 12 / Pin 11 / Pins 5,4,3,2
Green LEDAnode (through 220ฮฉ)Pin 7
Red LEDAnode (through 220ฮฉ)Pin 8
Buzzer+ / โˆ’Pin 13 / GND
Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int pirPin     = 6;
const int trigPin    = 9;
const int echoPin    = 10;
const int greenLED   = 7;
const int redLED     = 8;
const int buzzerPin  = 13;

long duration;
float distance;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  lcd.begin(16, 2);
  lcd.print("Smart Home");
  lcd.setCursor(0, 1);
  lcd.print("Initializing...");
  delay(30000);  // PIR warm-up
  lcd.clear();
  lcd.print("System Ready");
}

float getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  return (duration * 0.034) / 2;
}

void loop() {
  int motion = digitalRead(pirPin);

  if (motion == HIGH) {
    distance = getDistance();

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome!");
    lcd.setCursor(0, 1);
    lcd.print("Dist: ");
    lcd.print(distance);
    lcd.print("cm");

    if (distance < 50) {
      // Person at the door โ€” ring doorbell
      digitalWrite(redLED, HIGH);
      digitalWrite(greenLED, LOW);
      tone(buzzerPin, 1000, 500);  // Doorbell tone
    } else {
      // Person approaching
      digitalWrite(greenLED, HIGH);
      digitalWrite(redLED, LOW);
      noTone(buzzerPin);
    }
  } else {
    // No one around โ€” idle state
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, LOW);
    noTone(buzzerPin);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Smart Home");
    lcd.setCursor(0, 1);
    lcd.print("System Ready");
  }

  delay(300);
}
Section J

Chapter Summary

๐Ÿ“‹ Unit 4 โ€” Key Takeaways

Visual Output Devices:

  • LED Interfacing: LEDs need forward bias and a current-limiting resistor (R = (Vsโˆ’Vf)/If). Always use 220ฮฉ for standard LEDs on 5V Arduino.
  • PWM Brightness: analogWrite(pin, 0-255) controls brightness via duty cycle. Only works on PWM pins (~3, ~5, ~6, ~9, ~10, ~11).
  • 7-Segment Display: 7 LED segments (a-g) display digits 0-9. Common Cathode = HIGH turns ON segments. Each segment needs its own 220ฮฉ resistor.
  • LCD 16ร—2: 16 pins, uses HD44780 controller. LiquidCrystal library in 4-bit mode. Contrast controlled by 10kฮฉ potentiometer on V0.

Sensors:

  • HC-SR04 Ultrasonic: Measures distance (2โ€“400cm) using sound echo. Distance = (Time ร— 0.034)/2 cm. Uses Trig and Echo pins.
  • DHT11/DHT22: Temperature + humidity sensor. DHT22 is more accurate and has wider range. Needs 10kฮฉ pull-up resistor on DATA pin.
  • PIR Sensor: Passive infrared โ€” detects warm body motion. Needs 30-second warm-up. Great for security systems and automatic lighting.
  • IR Sensor: Emits and receives IR for proximity/object detection. Cannot detect black objects. Used in line-follower robots and object counters.

Key Skills Acquired: Circuit building, Ohm's law calculation, Arduino coding, sensor interfacing, data reading, debugging hardware projects.

Section K

Earning Checkpoint โ€” Skills Inventory

Skill LearnedTool/ComponentPortfolio PieceEarning Ready?
LED Interfacing & PWMArduino, LEDs, ResistorsTraffic Light Controllerโœ… Yes โ€” basic Arduino gigs
7-Segment Display7-seg, ArduinoDigital Counter Displayโœ… Yes โ€” prototype displays
LCD Display16ร—2 LCD, LiquidCrystalCustom Text Display Systemโœ… Yes โ€” IoT dashboards
Ultrasonic DistanceHC-SR04, ArduinoSmart Distance Meterโœ… Yes โ€” parking sensors, level meters
Temperature/HumidityDHT11/DHT22, DHT LibraryWeather Stationโœ… Yes โ€” greenhouse monitoring
Motion DetectionPIR SensorSecurity Alarm Systemโœ… Yes โ€” home security projects
Object DetectionIR Sensor ModuleObject Counter / Line Followerโœ… Yes โ€” robotics competitions
System IntegrationMultiple sensors + outputsSmart Home Entry Systemโœ… Yes โ€” โ‚น5,000โ€“โ‚น20,000 projects
Minimum Viable Earning Setup after this chapter: An Arduino Uno (โ‚น500) + sensor kit (โ‚น400) + GitHub portfolio with 3โ€“4 project videos = you can earn โ‚น3,000โ€“โ‚น15,000/project helping college juniors with mini-projects, or build custom IoT prototypes for local businesses.
Section L

Quick Reference Card โ€” Pinouts & Formulas

Essential Formulas

FormulaUsageExample
R = (Vs - Vf) / IfLED resistor calculation(5โˆ’2)/0.02 = 150ฮฉ
Duty% = (value/255) ร— 100PWM duty cycle127/255 ร— 100 = 49.8%
Dist = (t ร— 0.034) / 2Ultrasonic distance (cm)(588ร—0.034)/2 = 10cm

Arduino PWM Pins (Uno)

PWM PinTimerFrequency
~3, ~11Timer 2490 Hz
~5, ~6Timer 0976 Hz
~9, ~10Timer 1490 Hz

Common LED Forward Voltages

LED ColorVf (typical)Recommended R (at 5V)
Red1.8โ€“2.0V150โ€“220ฮฉ
Yellow2.0โ€“2.2V150โ€“220ฮฉ
Green2.0โ€“3.0V100โ€“220ฮฉ
Blue3.0โ€“3.4V68โ€“100ฮฉ
White3.0โ€“3.4V68โ€“100ฮฉ

Key Arduino Functions Used in This Unit

FunctionPurposeExample
pinMode(pin, mode)Set pin as INPUT or OUTPUTpinMode(13, OUTPUT)
digitalWrite(pin, val)Write HIGH/LOW to digital pindigitalWrite(13, HIGH)
digitalRead(pin)Read HIGH/LOW from digital pindigitalRead(2)
analogWrite(pin, val)PWM output (0โ€“255)analogWrite(9, 127)
pulseIn(pin, val)Measure pulse duration (ยตs)pulseIn(10, HIGH)
delay(ms)Pause executiondelay(1000)
delayMicroseconds(ยตs)Pause in microsecondsdelayMicroseconds(10)
Serial.begin(baud)Start serial communicationSerial.begin(9600)
tone(pin, freq, dur)Generate tone on buzzertone(8, 1000, 500)
Section M

What's Next โ€” Unit 5 Preview

๐Ÿš€ Unit 5: Communication Protocols & IoT Connectivity

You've built circuits that sense and display data โ€” but how do you send this data to the cloud? In Unit 5, you'll learn about UART, SPI, I2C communication protocols, connect your Arduino to WiFi using ESP8266/ESP32, and send sensor data to ThingSpeak, Blynk, and MQTT brokers. You'll build your first truly connected IoT device โ€” a weather station that uploads data to the internet in real-time.

Key Topics: Serial communication (UART) | SPI protocol | I2C (Wire library) | ESP8266 WiFi module | MQTT protocol | Cloud platforms (ThingSpeak, Blynk) | REST APIs for IoT

โœ… Unit 4 complete. Ready for Unit 5: Communication & IoT Connectivity!

[QR: Link to EduArtha video tutorial โ€” Visual Output & Sensors]