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)
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.
Learning Outcomes โ Bloom's Taxonomy Mapped
| Bloom's Level | Learning Outcome |
|---|---|
| ๐ต Remember | List the pin configurations of LED, 7-segment display, LCD 16ร2, HC-SR04, DHT22, PIR, and IR sensors |
| ๐ต Understand | Explain how PWM controls LED brightness, how ultrasonic sensors measure distance, and how PIR detects motion |
| ๐ข Apply | Build working circuits on breadboard and write Arduino code to interface each component |
| ๐ข Analyze | Compare DHT11 vs DHT22 specifications; analyze common cathode vs common anode 7-segment displays |
| ๐ Evaluate | Select the most appropriate sensor for a given IoT application and justify the choice |
| ๐ Create | Design a complete IoT security system combining PIR sensor, buzzer, LCD display, and LED indicators |
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
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 Pin | Connects To | Notes |
|---|---|---|
| Arduino Pin 13 | 220ฮฉ Resistor (one end) | Digital output pin |
| Resistor (other end) | LED Anode (+, longer leg) | Current limiting |
| LED Cathode (โ, shorter leg) | Arduino GND | Complete 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 }
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
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 Examples0% 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
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 Pin | Connects To | Notes |
|---|---|---|
| Arduino Pin 9 (~PWM) | 220ฮฉ Resistor (one end) | Must be a PWM-capable pin |
| Resistor (other end) | LED Anode (+) | Current limiting |
| LED Cathode (โ) | Arduino GND | Complete 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); } }
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
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)
| Digit | a | b | c | d | e | f | g | Hex |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0x7E |
| 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0x30 |
| 2 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0x6D |
| 3 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0x79 |
| 4 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0x33 |
| 5 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0x5B |
| 6 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0x5F |
| 7 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0x70 |
| 8 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0x7F |
| 9 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0x7B |
Connection Table โ 7-Segment to Arduino
| 7-Seg Pin | Segment | Arduino Pin | Resistor |
|---|---|---|---|
| Pin 7 | a | 2 | 220ฮฉ |
| Pin 6 | b | 3 | 220ฮฉ |
| Pin 4 | c | 4 | 220ฮฉ |
| Pin 2 | d | 5 | 220ฮฉ |
| Pin 1 | e | 6 | 220ฮฉ |
| Pin 9 | f | 7 | 220ฮฉ |
| Pin 10 | g | 8 | 220ฮฉ |
| Pin 3 or 8 | COM | GND | โ |
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 } }
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 # | Symbol | Function | Connection |
|---|---|---|---|
| 1 | VSS | Ground | GND |
| 2 | VDD | Power Supply (+5V) | 5V |
| 3 | V0 | Contrast Adjustment | Potentiometer wiper (10kฮฉ) |
| 4 | RS | Register Select (0=cmd, 1=data) | Arduino Pin 12 |
| 5 | RW | Read/Write (0=write) | GND (always write) |
| 6 | E | Enable (triggers read/write) | Arduino Pin 11 |
| 7 | D0 | Data Bit 0 | Not connected (4-bit mode) |
| 8 | D1 | Data Bit 1 | Not connected (4-bit mode) |
| 9 | D2 | Data Bit 2 | Not connected (4-bit mode) |
| 10 | D3 | Data Bit 3 | Not connected (4-bit mode) |
| 11 | D4 | Data Bit 4 | Arduino Pin 5 |
| 12 | D5 | Data Bit 5 | Arduino Pin 4 |
| 13 | D6 | Data Bit 6 | Arduino Pin 3 |
| 14 | D7 | Data Bit 7 | Arduino Pin 2 |
| 15 | A | Backlight Anode (+) | 5V (through 220ฮฉ) |
| 16 | K | Backlight 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); }
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 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().
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 CalculationIf Echo pulse duration = 1176 ยตs:
Distance = (1176 ร 0.034) / 2 = 39.984 / 2 = ~20 cm
HC-SR04 Pin Details
| Pin | Function | Description |
|---|---|---|
| VCC | Power Supply | Connect to 5V |
| Trig | Trigger Input | Send 10ยตs pulse to start measurement |
| Echo | Echo Output | Returns pulse proportional to distance |
| GND | Ground | Connect 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 Pin | Arduino Pin | Notes |
|---|---|---|
| VCC | 5V | Power supply |
| Trig | Pin 9 | Trigger โ send pulse |
| Echo | Pin 10 | Echo โ read pulse duration |
| GND | GND | Ground |
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 }
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
| Feature | DHT11 | DHT22 (AM2302) |
|---|---|---|
| Temperature Range | 0ยฐC to 50ยฐC | โ40ยฐC to 80ยฐC |
| Temp Accuracy | ยฑ2ยฐC | ยฑ0.5ยฐC |
| Humidity Range | 20% to 80% RH | 0% to 100% RH |
| Humidity Accuracy | ยฑ5% RH | ยฑ2% RH |
| Sampling Rate | 1 reading/sec | 1 reading/2 sec |
| Price (India) | โน50โโน80 | โน150โโน250 |
| Body Color | Blue | White |
| Best For | Basic indoor projects | Weather 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 Pin | Arduino Pin | Notes |
|---|---|---|
| Pin 1 (VCC) | 5V | Power supply (3.3V also works) |
| Pin 2 (DATA) | Pin 7 | 10kฮฉ pull-up resistor to VCC |
| Pin 3 (NC) | โ | Not connected |
| Pin 4 (GND) | GND | Ground |
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(); }
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
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 FeaturesDetection 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 PotentiometersSensitivity 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
| Pin | Function | Description |
|---|---|---|
| VCC | Power Supply | 5V to 20V (typically 5V) |
| OUT | Signal Output | HIGH when motion detected, LOW otherwise |
| GND | Ground | Connect 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
| Component | Component Pin | Arduino Pin |
|---|---|---|
| PIR Sensor | VCC | 5V |
| PIR Sensor | OUT | Pin 2 |
| PIR Sensor | GND | GND |
| Buzzer | + (Positive) | Pin 8 |
| Buzzer | โ (Negative) | GND |
| Red LED | Anode (through 220ฮฉ) | Pin 13 |
| Red LED | Cathode | GND |
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); }
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
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 ModuleMost 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 OutputDigital: HIGH/LOW only โ object present or not
Analog: Variable voltage proportional to proximity (for advanced distance estimation)
IR Sensor Module Pin Details
| Pin | Function | Description |
|---|---|---|
| VCC | Power Supply | 3.3V to 5V |
| GND | Ground | Connect to GND |
| OUT | Digital Output | LOW = 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 Pin | Arduino Pin | Notes |
|---|---|---|
| VCC | 5V | Power supply |
| OUT | Pin 3 | Digital output (LOW = detected) |
| GND | GND | Ground |
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 IR | Right IR | Robot 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) |
Learn by Doing โ 3-Tier Lab Structure
๐ข Tier 1 โ GUIDED TASK: Build a Traffic Light System
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 Color | Arduino Pin | Resistor |
|---|---|---|
| Red | Pin 4 | 220ฮฉ |
| Yellow | Pin 3 | 220ฮฉ |
| Green | Pin 2 | 220ฮฉ |
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
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:
- Wire the LCD as shown in Section C.4 (RS=12, E=11, D4=5, D5=4, D6=3, D7=2)
- Wire the HC-SR04 to Trig=9, Echo=10 (same as Section C.5)
- Include both libraries:
LiquidCrystal.h - Display format: Line 1: "Distance Meter" | Line 2: "Dist: XX.XX cm"
- Update LCD every 500ms using
lcd.clear()before rewriting - Add a warning: If distance < 10cm, display "TOO CLOSE!" on line 2
beepDelay = distance ร 20 milliseconds.
๐ด Tier 3 โ OPEN CHALLENGE: Complete IoT Security System
The Brief:
Design and build a complete room security system that combines multiple sensors and output devices:
- PIR sensor for motion detection
- Ultrasonic sensor for distance-based proximity alert
- LCD display showing system status ("ARMED", "ALERT!", distance)
- Red + Green LEDs for visual status (Green=safe, Red=alert)
- Buzzer for audio alarm
- 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
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.
| Detail | Info |
|---|---|
| Tools Used Daily | Arduino 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 Hiring | Tata Elxsi, Bosch India, KPIT Technologies, Wipro IoT, L&T Technology Services, Continental, Harman, Samsung R&D |
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
| Platform | Best For | Typical Rate |
|---|---|---|
| Fiverr | Arduino/IoT project gigs globally | $15โ$80/project (โน1,200โโน6,500) |
| Freelancer.com | Embedded systems & IoT projects | $20โ$100/project |
| Internshala | Indian student internships in IoT/embedded | โน3,000โโน10,000/month |
| College WhatsApp Groups | Mini-project help for juniors | โน1,500โโน5,000/project |
| Local Shops/Offices | Custom 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)
MCQ Assessment Bank โ 30 Questions (Bloom's Mapped)
Remember / Identify (Q1โQ5)
The longer leg of an LED is the:
- Cathode (โ)
- Anode (+)
- Ground pin
- Signal pin
The analogWrite() function in Arduino accepts values in the range:
- 0 to 1023
- 0 to 255
- 0 to 100
- 0 to 5
How many segments does a 7-segment display have (excluding the decimal point)?
- 6
- 7
- 8
- 9
The HC-SR04 ultrasonic sensor has how many pins?
- 2 (VCC, GND)
- 3 (VCC, OUT, GND)
- 4 (VCC, Trig, Echo, GND)
- 5 (VCC, Trig, Echo, OUT, GND)
Which Arduino library is used to interface with a 16ร2 LCD?
- Servo.h
- Wire.h
- LiquidCrystal.h
- SPI.h
Understand / Explain (Q6โQ10)
Why is a current-limiting resistor necessary when connecting an LED to an Arduino?
- To increase the LED brightness
- To convert AC to DC
- To limit current and prevent the LED from burning out
- To amplify the signal
What does "duty cycle" mean in the context of PWM?
- The frequency of the PWM signal
- The ratio of ON-time to the total period of the signal
- The maximum voltage output
- The number of PWM pins available
In a Common Cathode 7-segment display, to light up a segment you must:
- Send LOW to the segment pin
- Send HIGH to the segment pin
- Connect the segment to GND
- Disconnect the common pin
Why does the ultrasonic distance formula divide by 2?
- Because the sensor has 2 pins
- Because sound travels at half speed in air
- Because the pulse travels to the object AND back (round trip)
- Because Arduino runs at half clock speed
Why is a PIR sensor called "passive"?
- It requires no power supply
- It only receives infrared radiation and does not emit any
- It works only in passive mode
- It can only detect stationary objects
Apply / Calculate (Q11โQ15)
Calculate the resistor needed for an LED with Vf = 1.8V, If = 15mA, using a 5V Arduino:
- 120ฮฉ
- 213ฮฉ
- 330ฮฉ
- 470ฮฉ
If analogWrite(pin, 191) is used, what is the approximate duty cycle?
- 25%
- 50%
- 75%
- 100%
An HC-SR04 sensor returns an echo pulse of 882 ยตs. What is the distance to the object?
- 10 cm
- 15 cm
- 20 cm
- 30 cm
To display the digit "5" on a Common Cathode 7-segment display, which segments must be ON?
- a, b, c, d, e, f
- a, c, d, f, g
- a, b, d, e, g
- b, c, d, e, f
What Arduino function is used to read the HC-SR04 echo pulse duration?
- digitalRead()
- analogRead()
- pulseIn()
- tone()
Analyze / Compare (Q16โQ20)
Which sensor would be MORE appropriate for detecting a black object on a conveyor belt?
- IR sensor
- Ultrasonic sensor
- PIR sensor
- DHT sensor
Comparing DHT11 and DHT22, which statement is TRUE?
- DHT11 is more accurate than DHT22
- DHT22 can measure negative temperatures, DHT11 cannot
- DHT11 is more expensive than DHT22
- Both have identical specifications
A student's LCD shows all black boxes instead of text. The most likely cause is:
- Wrong Arduino code
- LCD is defective
- Contrast potentiometer (V0) is at the wrong position
- Power supply is insufficient
What is the key difference between a PIR sensor and an IR sensor?
- PIR is analog, IR is digital
- PIR detects changes in infrared radiation (motion), IR emits and receives reflected IR (proximity)
- PIR works outdoors only, IR works indoors only
- There is no difference โ they are the same
In 4-bit mode LCD operation, data is sent as:
- 4 bits at once (half a byte)
- Two 4-bit nibbles sequentially
- 8 bits simultaneously
- Serial data one bit at a time
Evaluate / Justify (Q21โQ25)
For a smart parking system that needs to detect cars in each spot, which sensor is the best choice?
- DHT22 (temperature sensor)
- PIR sensor (motion detector)
- Ultrasonic sensor (distance measurement)
- IR sensor (object detection)
A student uses digitalWrite() instead of analogWrite() for LED dimming. What will happen?
- LED will dim smoothly
- LED will only be fully ON or fully OFF โ no dimming
- Arduino will crash
- LED will blink randomly
Why would you choose a DHT22 over a DHT11 for an outdoor weather station in Shimla (winter temps can reach โ5ยฐC)?
- DHT22 is cheaper
- DHT22 has a wider temperature range (โ40ยฐC to 80ยฐC) while DHT11 only reads 0ยฐC to 50ยฐC
- DHT22 uses fewer pins
- DHT22 doesn't need a pull-up resistor
A PIR-based automatic light system keeps triggering falsely in a room with an AC vent. The most likely cause is:
- The PIR sensor is defective
- Hot air from the AC creates temperature changes that the PIR interprets as motion
- The LED is too bright
- The Arduino code has a bug
For a line-following robot, using 3 IR sensors instead of 2 provides what advantage?
- It uses less power
- It enables the robot to detect intersections and make smoother turns
- It makes the code simpler
- It reduces the cost
Create / Design (Q26โQ30)
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?
- DHT sensor + LCD + Relay module + Arduino
- PIR sensor + 7-segment + Motor driver
- Ultrasonic + LED + Buzzer
- IR sensor + LCD + Servo motor
To create a "smart dustbin" that opens its lid when a hand approaches, which combination is optimal?
- PIR sensor + DC motor
- Ultrasonic sensor + Servo motor
- DHT sensor + Stepper motor
- IR sensor + Relay
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?
- DHT22
- Two IR sensors placed at the doorway
- Single PIR sensor
- Potentiometer
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?
- PIR sensor + single LED
- Ultrasonic sensor + 3 colored LEDs
- DHT sensor + buzzer
- IR sensor + LCD
Design an automated greenhouse monitoring system. Which combination of sensors and outputs would provide the most useful data?
- DHT sensor (temp/humidity) + LCD display + Relay (for water pump) + Soil moisture sensor
- PIR sensor + Buzzer + LED
- Ultrasonic sensor + 7-segment display
- IR sensor + Motor + Potentiometer
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."
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:
| Component | Pin | Arduino Pin |
|---|---|---|
| HC-SR04 | VCC | 5V |
| HC-SR04 | Trig | Pin 9 |
| HC-SR04 | Echo | Pin 10 |
| HC-SR04 | GND | GND |
| LCD | RS | Pin 12 |
| LCD | E | Pin 11 |
| LCD | D4-D7 | Pins 5, 4, 3, 2 |
| LCD | VSS, RW, K | GND |
| LCD | VDD, A | 5V (A through 220ฮฉ) |
| LCD | V0 | 10kฮฉ 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:
| Feature | HC-SR04 (Ultrasonic) | DHT11/DHT22 | PIR | IR Sensor |
|---|---|---|---|---|
| Principle | Sound echo (time-of-flight) | Capacitive humidity + thermistor | Pyroelectric (IR radiation change) | IR reflection (transmit + receive) |
| What it measures | Distance to object | Temperature & humidity | Motion (warm body movement) | Object presence/proximity |
| Output Type | Pulse width (analog duration) | Digital serial (single-wire) | Digital (HIGH/LOW) | Digital (HIGH/LOW) or Analog |
| Range | 2โ400 cm | DHT11: 0โ50ยฐC, DHT22: โ40โ80ยฐC | Up to 7m, 110ยฐ cone | 2โ30 cm (adjustable) |
| Accuracy | ยฑ3mm | DHT11: ยฑ2ยฐC, DHT22: ยฑ0.5ยฐC | N/A (binary) | N/A (binary) |
| Use Cases | Parking sensor, level meter, robot obstacle avoidance | Weather station, greenhouse, HVAC | Security alarm, auto lights, occupancy | Line follower, object counter, edge detect |
| Limitations | Soft/angled surfaces absorb sound; minimum 2cm | Slow sampling (1โ2 sec); humidity-sensitive | False triggers from heat sources; 30s warm-up | Fails 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:
| Component | Component Pin | Arduino Pin |
|---|---|---|
| PIR Sensor | VCC / OUT / GND | 5V / Pin 6 / GND |
| HC-SR04 | VCC / Trig / Echo / GND | 5V / Pin 9 / Pin 10 / GND |
| LCD 16ร2 | RS / E / D4โD7 | Pin 12 / Pin 11 / Pins 5,4,3,2 |
| Green LED | Anode (through 220ฮฉ) | Pin 7 |
| Red LED | Anode (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); }
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.
Earning Checkpoint โ Skills Inventory
| Skill Learned | Tool/Component | Portfolio Piece | Earning Ready? |
|---|---|---|---|
| LED Interfacing & PWM | Arduino, LEDs, Resistors | Traffic Light Controller | โ Yes โ basic Arduino gigs |
| 7-Segment Display | 7-seg, Arduino | Digital Counter Display | โ Yes โ prototype displays |
| LCD Display | 16ร2 LCD, LiquidCrystal | Custom Text Display System | โ Yes โ IoT dashboards |
| Ultrasonic Distance | HC-SR04, Arduino | Smart Distance Meter | โ Yes โ parking sensors, level meters |
| Temperature/Humidity | DHT11/DHT22, DHT Library | Weather Station | โ Yes โ greenhouse monitoring |
| Motion Detection | PIR Sensor | Security Alarm System | โ Yes โ home security projects |
| Object Detection | IR Sensor Module | Object Counter / Line Follower | โ Yes โ robotics competitions |
| System Integration | Multiple sensors + outputs | Smart Home Entry System | โ Yes โ โน5,000โโน20,000 projects |
Quick Reference Card โ Pinouts & Formulas
Essential Formulas
| Formula | Usage | Example |
|---|---|---|
R = (Vs - Vf) / If | LED resistor calculation | (5โ2)/0.02 = 150ฮฉ |
Duty% = (value/255) ร 100 | PWM duty cycle | 127/255 ร 100 = 49.8% |
Dist = (t ร 0.034) / 2 | Ultrasonic distance (cm) | (588ร0.034)/2 = 10cm |
Arduino PWM Pins (Uno)
| PWM Pin | Timer | Frequency |
|---|---|---|
| ~3, ~11 | Timer 2 | 490 Hz |
| ~5, ~6 | Timer 0 | 976 Hz |
| ~9, ~10 | Timer 1 | 490 Hz |
Common LED Forward Voltages
| LED Color | Vf (typical) | Recommended R (at 5V) |
|---|---|---|
| Red | 1.8โ2.0V | 150โ220ฮฉ |
| Yellow | 2.0โ2.2V | 150โ220ฮฉ |
| Green | 2.0โ3.0V | 100โ220ฮฉ |
| Blue | 3.0โ3.4V | 68โ100ฮฉ |
| White | 3.0โ3.4V | 68โ100ฮฉ |
Key Arduino Functions Used in This Unit
| Function | Purpose | Example |
|---|---|---|
pinMode(pin, mode) | Set pin as INPUT or OUTPUT | pinMode(13, OUTPUT) |
digitalWrite(pin, val) | Write HIGH/LOW to digital pin | digitalWrite(13, HIGH) |
digitalRead(pin) | Read HIGH/LOW from digital pin | digitalRead(2) |
analogWrite(pin, val) | PWM output (0โ255) | analogWrite(9, 127) |
pulseIn(pin, val) | Measure pulse duration (ยตs) | pulseIn(10, HIGH) |
delay(ms) | Pause execution | delay(1000) |
delayMicroseconds(ยตs) | Pause in microseconds | delayMicroseconds(10) |
Serial.begin(baud) | Start serial communication | Serial.begin(9600) |
tone(pin, freq, dur) | Generate tone on buzzer | tone(8, 1000, 500) |
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]