Programming IoT with Arduino

Complete IoT Textbook โ€” 10 Chapters

From blinking LEDs to cloud-connected smart systems โ€” master Arduino, sensors, actuators, wireless communication, and IoT cloud platforms in one book.

โฑ๏ธ 50+ hrs total  |  ๐ŸŽฏ Arduino + ESP8266 + Cloud  |  ๐Ÿ’ฐ โ‚น6โ€“20 LPA Embedded/IoT  |  ๐Ÿ–ฅ๏ธ Smart India Hackathon Ready

๐Ÿ’ผ Jobs this unlocks: Embedded Developer (โ‚น6โ€“10 LPA)  |  IoT Engineer (โ‚น8โ€“15 LPA)  |  Firmware Developer (โ‚น10โ€“20 LPA)

Chapter 1

LCD Interfacing with Arduino Uno

๐Ÿ–ฅ๏ธ Every Display Around You Started with an LCD

ATM machines, railway ticket counters, petrol pump meters, weighing scales at kirana shops โ€” they all use LCD displays. The 16ร—2 LCD is like the "sabse pehla output device" for Arduino. Think of it as a TV remote's display โ€” small, simple, but powerful enough to show you what matters.

LCD = Jugaad Display! With just โ‚น80 and 6 wires, you can make Arduino talk to you. By the end of this chapter, you'll display custom messages, scrolling text, and even create your own characters like โค๏ธ on a tiny screen.

๐Ÿ‡ฎ๐Ÿ‡ณ Indian Railways๐Ÿ‡ฎ๐Ÿ‡ณ Petrol Pumps๐Ÿ‡ฎ๐Ÿ‡ณ ATM Machines๐Ÿ‡ฎ๐Ÿ‡ณ Weighing Scales

1.1 LCD Pin Details โ€” All 16 Pins Explained

A standard 16ร—2 LCD module (based on the HD44780 controller) has 16 pins. Understanding each pin is crucial before wiring.

Pin #NameFunction
1VSSGround (0V)
2VDDPower Supply (+5V)
3V0Contrast Adjustment (connect to potentiometer)
4RSRegister Select: 0=Command, 1=Data
5RWRead/Write: 0=Write, 1=Read (usually GND)
6ENEnable: Latches data on falling edge
7D0Data Bit 0 (not used in 4-bit mode)
8D1Data Bit 1 (not used in 4-bit mode)
9D2Data Bit 2 (not used in 4-bit mode)
10D3Data Bit 3 (not used in 4-bit mode)
11D4Data Bit 4
12D5Data Bit 5
13D6Data Bit 6
14D7Data Bit 7
15A (LED+)Backlight Anode (+5V through 220ฮฉ resistor)
16K (LED-)Backlight Cathode (GND)
16x2 LCD Pin Diagram (Front View) โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ H e l l o W o r l d ! โ”‚ โ”‚ โ† Row 0 (16 chars) โ”‚ โ”‚ A r d u i n o I o T โ”‚ โ”‚ โ† Row 1 (16 chars) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ | | | | | | | | | | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 V V V R R E D D D D D D D D A K S D 0 S W N 0 1 2 3 4 5 6 7 + - S D โ†“ โ†“ โ†“ โ†“ โ†“ โ†“ โ†“ GND 5V POT Used in 4-bit mode

1.2 Interfacing 16ร—2 LCD with Arduino (4-bit Mode)

In 4-bit mode, we only use pins D4โ€“D7, saving 4 Arduino pins. This is the most common wiring method.

Arduino Uno โ†โ†’ 16x2 LCD Wiring (4-bit Mode) ARDUINO UNO 16x2 LCD โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ D12 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ RS (4) โ”‚ โ”‚ D11 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ EN (6) โ”‚ โ”‚ D5 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ D4 (11)โ”‚ โ”‚ D4 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ D5 (12)โ”‚ โ”‚ D3 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ D6 (13)โ”‚ โ”‚ D2 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ D7 (14)โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ 5V โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ VDD (2)โ”‚ โ”‚ GND โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ VSS (1)โ”‚ โ”‚ GND โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ RW (5)โ”‚ โ”‚ 5V โ”€โ”€โ”€โ”€โ”ผโ”€โ”€220ฮฉโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ A (15)โ”‚ โ”‚ GND โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ K (16)โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 10kฮฉ POT โ”Œโ”€โ”€โ”€โ”คโ”œโ”€โ”€โ”€โ” 5V โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€ GND โ”‚ V0 (3)

Connection Table

LCD PinArduino PinPurpose
VSS (1)GNDGround
VDD (2)5VPower
V0 (3)Potentiometer WiperContrast control
RS (4)D12Register Select
RW (5)GNDWrite mode (always)
EN (6)D11Enable pulse
D4 (11)D5Data bit 4
D5 (12)D4Data bit 5
D6 (13)D3Data bit 6
D7 (14)D2Data bit 7
A (15)5V via 220ฮฉBacklight ON
K (16)GNDBacklight GND

1.3 LiquidCrystal Library Functions

FunctionDescriptionExample
LiquidCrystal(rs,en,d4,d5,d6,d7)Constructor โ€” define pinsLiquidCrystal lcd(12,11,5,4,3,2)
lcd.begin(cols, rows)Initialize LCD sizelcd.begin(16, 2)
lcd.print(data)Print text/number at cursorlcd.print("Hello")
lcd.setCursor(col, row)Move cursor (0-indexed)lcd.setCursor(0, 1)
lcd.clear()Clear screen, cursor to 0,0lcd.clear()
lcd.scrollDisplayLeft()Scroll entire display leftUse in loop for marquee
lcd.scrollDisplayRight()Scroll entire display rightReverse marquee
lcd.blink()Blinking block cursorShows cursor position
lcd.noBlink()Stop blinking cursorHide cursor
lcd.createChar(num, data)Create custom 5ร—8 charMax 8 custom chars (0โ€“7)

1.4 Arduino Code โ€” Hello World on LCD

Arduino
// Program: Display "Hello World!" on 16x2 LCD
// Board: Arduino Uno
// Connection: 4-bit mode (RS=12, EN=11, D4=5, D5=4, D6=3, D7=2)

#include <LiquidCrystal.h>

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

void setup() {
  lcd.begin(16, 2);       // Set LCD to 16 columns, 2 rows
  lcd.setCursor(0, 0);    // Move cursor to column 0, row 0
  lcd.print("Hello World!"); // Print on first line
  lcd.setCursor(0, 1);    // Move cursor to second line
  lcd.print("Arduino IoT");  // Print on second line
}

void loop() {
  // Nothing to repeat โ€” static display
}
LCD Display Output: โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚Hello World! โ”‚ โ”‚Arduino IoT โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

1.5 Custom Character Display โ€” createChar()

The LCD can store up to 8 custom characters (numbered 0โ€“7). Each character is a 5ร—8 pixel grid defined by a byte array.

5x8 Pixel Grid for Heart Character (โ™ฅ) Column: 0 1 2 3 4 Row 0: . # . # . โ†’ 0b01010 โ†’ 0x0A Row 1: # # # # # โ†’ 0b11111 โ†’ 0x1F Row 2: # # # # # โ†’ 0b11111 โ†’ 0x1F Row 3: # # # # # โ†’ 0b11111 โ†’ 0x1F Row 4: . # # # . โ†’ 0b01110 โ†’ 0x0E Row 5: . . # . . โ†’ 0b00100 โ†’ 0x04 Row 6: . . . . . โ†’ 0b00000 โ†’ 0x00 Row 7: . . . . . โ†’ 0b00000 โ†’ 0x00
Arduino
// Program: Display custom heart character on LCD
#include <LiquidCrystal.h>

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

// Define heart character as byte array
byte heart[8] = {
  0b01010,  // Row 0: .#.#.
  0b11111,  // Row 1: #####
  0b11111,  // Row 2: #####
  0b11111,  // Row 3: #####
  0b01110,  // Row 4: .###.
  0b00100,  // Row 5: ..#..
  0b00000,  // Row 6: .....(blank)
  0b00000   // Row 7: .....(blank)
};

void setup() {
  lcd.begin(16, 2);
  lcd.createChar(0, heart);   // Store heart at position 0
  lcd.setCursor(0, 0);
  lcd.print("I ");
  lcd.write(byte(0));        // Display custom character 0 (heart)
  lcd.print(" Arduino!");
}

void loop() {}

1.6 Worked Examples

Example 1: Display Your Name

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.print("Name: Rahul");
  lcd.setCursor(0, 1);
  lcd.print("Roll: 101");
}
void loop() {}

Example 2: Counter 0โ€“99

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() { lcd.begin(16, 2); }
void loop() {
  for (int i = 0; i < 100; i++) {
    lcd.clear();
    lcd.print("Count: ");
    lcd.print(i);
    delay(500);
  }
}

Example 3: Scrolling Marquee

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.print("  Welcome to EduArtha IoT Lab!  ");
}
void loop() {
  lcd.scrollDisplayLeft();  // Shift text left by 1 position
  delay(300);              // Speed of scrolling
}

Example 4: Temperature Display Format

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte degreeSymbol[8] = {0x06,0x09,0x09,0x06,0x00,0x00,0x00,0x00};
void setup() {
  lcd.begin(16, 2);
  lcd.createChar(0, degreeSymbol);
  lcd.print("Temp: 32");
  lcd.write(byte(0));
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity: 65%");
}
void loop() {}

Example 5: Custom Heart + Smiley

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte heart[8] = {0x00,0x0A,0x1F,0x1F,0x0E,0x04,0x00,0x00};
byte smiley[8] = {0x00,0x0A,0x0A,0x00,0x11,0x0E,0x00,0x00};
void setup() {
  lcd.begin(16, 2);
  lcd.createChar(0, heart);
  lcd.createChar(1, smiley);
  lcd.write(byte(0));
  lcd.print(" I love IoT ");
  lcd.write(byte(1));
}
void loop() {}

Example 6: Blinking Cursor

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.print("Enter PIN:");
  lcd.setCursor(0, 1);
  lcd.blink();  // Show blinking block cursor
}
void loop() {}

Example 7: Two-Line Message

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(2, 0);
  lcd.print("Jai Hind!");
  lcd.setCursor(0, 1);
  lcd.print("Bharat Mata Ki");
}
void loop() {}

Example 8: Right-to-Left Scroll

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Scrolling Right");
}
void loop() {
  lcd.scrollDisplayRight();
  delay(400);
}

Example 9: Display Analog Reading

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
}
void loop() {
  int val = analogRead(A0);  // Read pot/sensor on A0
  lcd.clear();
  lcd.print("Analog: ");
  lcd.print(val);              // 0-1023
  lcd.setCursor(0, 1);
  lcd.print("Volts: ");
  lcd.print(val * 5.0 / 1023, 2); // Convert to voltage
  delay(500);
}

Example 10: Simple LCD Menu

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int btnPin = 7;
int menu = 0;
void setup() {
  lcd.begin(16, 2);
  pinMode(btnPin, INPUT_PULLUP);
  showMenu();
}
void loop() {
  if (digitalRead(btnPin) == LOW) {
    menu = (menu + 1) % 3;
    showMenu();
    delay(300); // Debounce
  }
}
void showMenu() {
  lcd.clear();
  lcd.print("> Menu Option:");
  lcd.setCursor(0, 1);
  if (menu == 0) lcd.print("1. Read Temp");
  else if (menu == 1) lcd.print("2. Read Light");
  else lcd.print("3. Motor ON");
}
LCD shows blank or garbled text? Most common causes: (1) V0 contrast not adjusted โ€” turn potentiometer slowly. (2) Wrong pin connections โ€” double-check RS, EN, D4-D7. (3) Missing lcd.begin(16,2) in setup. (4) Backlight not connected โ€” check A and K pins.

1.7 MCQs โ€” Chapter 1 (20 Questions)

Q1

How many pins does a standard 16ร—2 LCD module have?

  1. 8
  2. 14
  3. 16
  4. 20
Remember
โœ… Answer: (C) 16 โ€” The standard HD44780-based 16ร—2 LCD has 16 pins including VSS, VDD, V0, RS, RW, EN, D0-D7, A, and K.
Q2

What is the function of pin V0 on the LCD?

  1. Power supply
  2. Contrast adjustment
  3. Data transmission
  4. Backlight control
Remember
โœ… Answer: (B) Contrast adjustment โ€” V0 is connected to a potentiometer to control the display contrast.
Q3

In 4-bit mode, which data pins of the LCD are used?

  1. D0โ€“D3
  2. D4โ€“D7
  3. D0โ€“D7
  4. D2โ€“D5
Remember
โœ… Answer: (B) D4โ€“D7 โ€” In 4-bit mode, only the upper nibble pins (D4-D7) are used, saving 4 Arduino pins.
Q4

What does the RS pin on the LCD control?

  1. Reset the display
  2. Select between command and data mode
  3. Control the refresh speed
  4. Enable the backlight
Understand
โœ… Answer: (B) โ€” RS (Register Select): When LOW, it sends commands (like clear, cursor move). When HIGH, it sends data (characters to display).
Q5

Which function initializes the LCD dimensions?

  1. lcd.init(16,2)
  2. lcd.start(16,2)
  3. lcd.begin(16,2)
  4. lcd.setup(16,2)
Remember
โœ… Answer: (C) lcd.begin(16,2) โ€” This function sets the LCD to 16 columns and 2 rows.
Q6

What is the maximum number of custom characters you can create on a 16ร—2 LCD?

  1. 4
  2. 8
  3. 16
  4. 32
Remember
โœ… Answer: (B) 8 โ€” The HD44780 controller supports 8 custom characters stored at positions 0โ€“7 in CGRAM.
Q7

What is the pixel grid size for each custom character on a 16ร—2 LCD?

  1. 8ร—8
  2. 5ร—7
  3. 5ร—8
  4. 7ร—5
Remember
โœ… Answer: (C) 5ร—8 โ€” Each character cell is 5 pixels wide and 8 pixels tall.
Q8

Why is the RW pin usually connected to GND?

  1. To save power
  2. Because we only write to the LCD, never read from it
  3. It doesn't work otherwise
  4. To increase speed
Understand
โœ… Answer: (B) โ€” In most Arduino projects, we only send data/commands TO the LCD. Tying RW to GND keeps it in write-only mode permanently.
Q9

Which function moves the cursor to column 5, row 1?

  1. lcd.setCursor(1, 5)
  2. lcd.setCursor(5, 1)
  3. lcd.moveTo(5, 1)
  4. lcd.goto(5, 1)
Apply
โœ… Answer: (B) lcd.setCursor(5, 1) โ€” The format is setCursor(column, row), both 0-indexed.
Q10

What happens when you call lcd.clear()?

  1. Only the first line is cleared
  2. The backlight turns off
  3. All text is erased and cursor moves to position (0,0)
  4. The LCD resets completely
Understand
โœ… Answer: (C) โ€” lcd.clear() erases all displayed text and returns the cursor to the top-left corner (0,0).
Q11

How many Arduino digital pins are needed for LCD in 4-bit mode (excluding power)?

  1. 4
  2. 6
  3. 8
  4. 10
Apply
โœ… Answer: (B) 6 โ€” RS, EN, D4, D5, D6, D7 = 6 digital pins.
Q12

What is the purpose of the EN (Enable) pin?

  1. Enables the backlight
  2. Latches data on the falling edge of a pulse
  3. Enables write mode
  4. Enables the power supply
Understand
โœ… Answer: (B) โ€” The Enable pin latches data into the LCD's registers on the falling edge (HIGHโ†’LOW transition) of the enable pulse.
Q13

Which resistor value is typically used with the LCD backlight (pin A)?

  1. 10ฮฉ
  2. 100ฮฉ
  3. 220ฮฉ
  4. 10kฮฉ
Remember
โœ… Answer: (C) 220ฮฉ โ€” A 220ฮฉ current-limiting resistor protects the backlight LED from excessive current.
Q14

Which library is used for basic LCD control in Arduino?

  1. LCD.h
  2. LiquidCrystal.h
  3. Display.h
  4. HD44780.h
Remember
โœ… Answer: (B) LiquidCrystal.h โ€” This is Arduino's built-in library for HD44780-based LCD displays.
Q15

What controller chip does the standard 16ร—2 LCD use?

  1. ATmega328P
  2. HD44780
  3. ESP8266
  4. MAX7219
Remember
โœ… Answer: (B) HD44780 โ€” The Hitachi HD44780 is the standard controller IC for character LCD modules.
Q16

To display the byte stored at custom character position 0, which function is used?

  1. lcd.print(0)
  2. lcd.display(0)
  3. lcd.write(byte(0))
  4. lcd.show(0)
Apply
โœ… Answer: (C) lcd.write(byte(0)) โ€” lcd.write() sends raw bytes, while lcd.print() would print the number "0" as text.
Q17

What does lcd.scrollDisplayLeft() do?

  1. Moves the cursor left
  2. Shifts the entire display content one position to the left
  3. Erases the leftmost character
  4. Rotates the display 90 degrees
Understand
โœ… Answer: (B) โ€” It shifts ALL displayed text one position to the left, creating a scrolling/marquee effect when called repeatedly.
Q18

How many total characters can a 16ร—2 LCD display at once?

  1. 16
  2. 24
  3. 32
  4. 64
Remember
โœ… Answer: (C) 32 โ€” 16 columns ร— 2 rows = 32 characters visible at any time.
Q19

If you want to display text starting from the 3rd column of the 2nd row, which call do you use?

  1. lcd.setCursor(3, 2)
  2. lcd.setCursor(2, 1)
  3. lcd.setCursor(3, 1)
  4. lcd.setCursor(2, 2)
Apply
โœ… Answer: (B) lcd.setCursor(2, 1) โ€” Columns and rows are 0-indexed. 3rd column = index 2, 2nd row = index 1.
Q20

What advantage does 4-bit mode have over 8-bit mode?

  1. Faster data transfer
  2. Uses fewer Arduino pins (6 instead of 10)
  3. Better contrast
  4. Supports more characters
Understand
โœ… Answer: (B) โ€” 4-bit mode uses only 6 digital pins (RS, EN, D4-D7) instead of 10 (RS, EN, D0-D7), freeing 4 pins for other components. Speed difference is negligible.
Chapter 2

LDR, Ultrasonic & IR Sensor Interfacing

๐Ÿ‘๏ธ Sensors = Arduino Ki Aankhein Aur Kaan

Without sensors, Arduino is blind and deaf โ€” it can't sense the world. An LDR detects light (street lights that auto-ON at night), an ultrasonic sensor measures distance (parking sensors in cars), and an IR sensor detects objects (automatic doors at malls). These three sensors are the foundation of every IoT project.

Analogy: Think of sensors as your body's senses โ€” LDR is your eyes (light), ultrasonic is your ears (echo/sonar), IR is your touch (proximity detection).

๐Ÿš— Parking Sensors๐Ÿ  Smart Street Lights๐Ÿšช Auto Doors๐Ÿค– Line Follower Robots

2.1 LDR (Light Dependent Resistor)

An LDR's resistance changes with light: bright light โ†’ low resistance (~1kฮฉ), dark โ†’ high resistance (~10Mฮฉ). We use a voltage divider circuit to convert this resistance change into a voltage that Arduino can read via analogRead().

LDR Voltage Divider Circuit +5V โ”‚ โ”Œโ”ดโ” โ”‚ โ”‚ LDR (resistance varies with light) โ”‚ โ”‚ โ””โ”ฌโ”˜ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Arduino A0 โ”Œโ”ดโ” โ”‚ โ”‚ 10kฮฉ (fixed resistor) โ”‚ โ”‚ โ””โ”ฌโ”˜ โ”‚ GND In DARK: LDR resistance HIGH โ†’ A0 voltage LOW โ†’ analogRead LOW In LIGHT: LDR resistance LOW โ†’ A0 voltage HIGH โ†’ analogRead HIGH

Street Light Automation โ€” Full Code

Arduino
// Program: Automatic Street Light using LDR
// When dark (LDR value < threshold) โ†’ LED ON
// When bright (LDR value >= threshold) โ†’ LED OFF

int ldrPin = A0;     // LDR connected to analog pin A0
int ledPin = 13;     // LED connected to digital pin 13
int threshold = 500; // Adjust based on your environment

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

void loop() {
  int ldrValue = analogRead(ldrPin);  // Read LDR (0-1023)
  Serial.print("LDR Value: ");
  Serial.println(ldrValue);

  if (ldrValue < threshold) {   // Dark condition
    digitalWrite(ledPin, HIGH); // Turn ON street light
    Serial.println("STATUS: Dark โ†’ LED ON");
  } else {                       // Bright condition
    digitalWrite(ledPin, LOW);  // Turn OFF street light
    Serial.println("STATUS: Bright โ†’ LED OFF");
  }
  delay(500);
}
LDR Value: 234 STATUS: Dark โ†’ LED ON LDR Value: 756 STATUS: Bright โ†’ LED OFF

2.2 Ultrasonic Sensor (HC-SR04)

The HC-SR04 sends an ultrasonic pulse (40kHz) and measures the time it takes for the echo to return. Distance is calculated using: distance = (time ร— 0.034) / 2 (speed of sound โ‰ˆ 340 m/s).

PinFunctionArduino Connection
VCCPower (+5V)5V
TrigTrigger pulse inputD9
EchoEcho pulse outputD10
GNDGroundGND
HC-SR04 Ultrasonic Sensor Circuit ARDUINO UNO HC-SR04 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ 5V โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ VCC โ”‚ โ”‚ D9 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ Trig โ”‚ โ† Send pulse โ”‚ D10 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ Echo โ”‚ โ† Receive echo โ”‚ GND โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ GND โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” Ultrasonic wave: โ”‚ )))) โ†’ OBJECT โ†’ (((( โ”‚ โ”‚ Trig sends Echo receives โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ Distance = (Time ร— 0.034) / 2 cm

Distance Measurement โ€” Full Code

Arduino
// Program: Measure distance using HC-SR04 Ultrasonic Sensor
// Formula: distance = (duration * 0.034) / 2

const int trigPin = 9;   // Trigger pin
const int echoPin = 10;  // Echo pin
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: Measure 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);
}
HC-SR04 Distance Sensor Ready Distance: 25.34 cm Distance: 12.67 cm Distance: 5.11 cm

Parking Sensor with Buzzer

Arduino
// Parking sensor: buzzer beeps faster as object gets closer
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 8;
long duration;
float distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034) / 2;

  if (distance < 10) {
    tone(buzzerPin, 1000); // Continuous beep โ€” very close!
  } else if (distance < 30) {
    tone(buzzerPin, 1000, 100); // Short beep
    delay(distance * 10);       // Beep faster when closer
  } else {
    noTone(buzzerPin); // No beep โ€” safe distance
  }
  delay(100);
}

2.3 IR Sensor โ€” Object Detection

An IR sensor module has an IR LED (emitter) and a photodiode (receiver). When an object is close, IR light reflects back and the output goes LOW. It gives a digital output โ€” HIGH (no object) or LOW (object detected).

IR Sensor Module Circuit ARDUINO UNO IR SENSOR MODULE โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ 5V โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ VCC โ”‚ โ”‚ D7 โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ OUT (Signal)โ”‚ โ”‚ GND โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€ GND โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ Object Present: IR reflects โ†’ OUT = LOW (0) No Object: IR passes โ†’ OUT = HIGH (1)
Arduino
// Program: Object detection using IR Sensor
int irPin = 7;
int ledPin = 13;

void setup() {
  pinMode(irPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int irValue = digitalRead(irPin);
  if (irValue == LOW) {  // Object detected
    digitalWrite(ledPin, HIGH);
    Serial.println("Object Detected!");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No Object");
  }
  delay(200);
}
Indian traffic lights use IR sensors for vehicle detection at signals. Many Smart City projects under the Indian government's Smart Cities Mission use ultrasonic + IR sensor combinations for traffic management and smart parking systems.

2.4 Worked Examples

Example 1: LDR with LED brightness (PWM)

Arduino
int ldrPin = A0;
int ledPin = 9; // PWM pin
void setup() { pinMode(ledPin, OUTPUT); }
void loop() {
  int val = analogRead(ldrPin);
  int brightness = map(val, 0, 1023, 255, 0); // Dark=bright LED
  analogWrite(ledPin, brightness);
  delay(100);
}

Example 2: Ultrasonic + LCD display

Arduino
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trig = 9, echo = 10;
void setup() { lcd.begin(16,2); pinMode(trig,OUTPUT); pinMode(echo,INPUT); }
void loop() {
  digitalWrite(trig,LOW); delayMicroseconds(2);
  digitalWrite(trig,HIGH); delayMicroseconds(10);
  digitalWrite(trig,LOW);
  float d = (pulseIn(echo,HIGH) * 0.034) / 2;
  lcd.clear(); lcd.print("Distance:");
  lcd.setCursor(0,1); lcd.print(d); lcd.print(" cm");
  delay(300);
}

Example 3: IR counter (count objects passing)

Arduino
int irPin = 7;
int count = 0;
bool lastState = HIGH;
void setup() { pinMode(irPin,INPUT); Serial.begin(9600); }
void loop() {
  bool curr = digitalRead(irPin);
  if (lastState == HIGH && curr == LOW) {
    count++;
    Serial.print("Count: "); Serial.println(count);
  }
  lastState = curr;
  delay(50);
}

Example 4: Multi-zone parking (3 ultrasonic sensors)

Arduino
int trig[] = {2, 4, 6};
int echo[] = {3, 5, 7};
void setup() {
  Serial.begin(9600);
  for(int i=0;i<3;i++) { pinMode(trig[i],OUTPUT); pinMode(echo[i],INPUT); }
}
float getDistance(int t, int e) {
  digitalWrite(t,LOW); delayMicroseconds(2);
  digitalWrite(t,HIGH); delayMicroseconds(10);
  digitalWrite(t,LOW);
  return (pulseIn(e,HIGH) * 0.034) / 2;
}
void loop() {
  for(int i=0;i<3;i++) {
    float d = getDistance(trig[i],echo[i]);
    Serial.print("Zone "); Serial.print(i+1);
    Serial.print(": "); Serial.print(d);
    Serial.println(d<20 ? " OCCUPIED" : " EMPTY");
  }
  Serial.println("---"); delay(1000);
}

Example 5: LDR night lamp with Serial plotter

Arduino
void setup() { Serial.begin(9600); }
void loop() {
  Serial.println(analogRead(A0)); // Open Serial Plotter to see graph
  delay(100);
}

Example 6: Ultrasonic + RGB LED (distance โ†’ color)

Arduino
const int trig=9, echo=10, redPin=3, greenPin=5, bluePin=6;
void setup() {
  pinMode(trig,OUTPUT); pinMode(echo,INPUT);
  pinMode(redPin,OUTPUT); pinMode(greenPin,OUTPUT); pinMode(bluePin,OUTPUT);
}
void loop() {
  digitalWrite(trig,LOW); delayMicroseconds(2);
  digitalWrite(trig,HIGH); delayMicroseconds(10);
  digitalWrite(trig,LOW);
  float d = (pulseIn(echo,HIGH)*0.034)/2;
  if(d<10){analogWrite(redPin,255);analogWrite(greenPin,0);analogWrite(bluePin,0);}
  else if(d<30){analogWrite(redPin,255);analogWrite(greenPin,255);analogWrite(bluePin,0);}
  else{analogWrite(redPin,0);analogWrite(greenPin,255);analogWrite(bluePin,0);}
  delay(200);
}

Example 7: IR-based line follower (2 sensors)

Arduino
int leftIR = 6, rightIR = 7;
int motorL = 9, motorR = 10;
void setup() {
  pinMode(leftIR,INPUT); pinMode(rightIR,INPUT);
  pinMode(motorL,OUTPUT); pinMode(motorR,OUTPUT);
}
void loop() {
  int L = digitalRead(leftIR), R = digitalRead(rightIR);
  if(L==LOW && R==LOW) { analogWrite(motorL,200); analogWrite(motorR,200); } // Forward
  else if(L==LOW) { analogWrite(motorL,0); analogWrite(motorR,200); } // Turn left
  else if(R==LOW) { analogWrite(motorL,200); analogWrite(motorR,0); } // Turn right
  else { analogWrite(motorL,0); analogWrite(motorR,0); } // Stop
}

Example 8: LDR alarm (buzzer when too dark)

Arduino
void setup() { pinMode(8,OUTPUT); }
void loop() {
  if(analogRead(A0) < 200) tone(8,1000); else noTone(8);
  delay(100);
}

Example 9: Ultrasonic water level monitor

Arduino
const int trig=9,echo=10;
const float tankHeight = 30.0; // cm
void setup() { pinMode(trig,OUTPUT); pinMode(echo,INPUT); Serial.begin(9600); }
void loop() {
  digitalWrite(trig,LOW); delayMicroseconds(2);
  digitalWrite(trig,HIGH); delayMicroseconds(10);
  digitalWrite(trig,LOW);
  float d = (pulseIn(echo,HIGH)*0.034)/2;
  float level = tankHeight - d;
  float pct = (level/tankHeight)*100;
  Serial.print("Water Level: "); Serial.print(pct); Serial.println("%");
  delay(1000);
}

Example 10: Combined sensor dashboard (LDR+Ultrasonic+IR on Serial)

Arduino
const int trig=9,echo=10,irPin=7;
void setup() {
  pinMode(trig,OUTPUT);pinMode(echo,INPUT);pinMode(irPin,INPUT);
  Serial.begin(9600);
  Serial.println("=== IoT Sensor Dashboard ===");
}
void loop() {
  int ldr = analogRead(A0);
  digitalWrite(trig,LOW);delayMicroseconds(2);
  digitalWrite(trig,HIGH);delayMicroseconds(10);
  digitalWrite(trig,LOW);
  float dist = (pulseIn(echo,HIGH)*0.034)/2;
  int ir = digitalRead(irPin);
  Serial.print("Light:"); Serial.print(ldr);
  Serial.print(" | Dist:"); Serial.print(dist);
  Serial.print("cm | Object:"); Serial.println(ir==LOW?"YES":"NO");
  delay(500);
}
Ultrasonic reads 0 cm? Check: (1) Trig and Echo pins not swapped. (2) Object too close (<2cm) or too far (>400cm). (3) Missing 10ยตs trigger pulse. (4) Object surface is too soft/angled (absorbs sound).

2.5 MCQs โ€” Chapter 2 (20 Questions)

Q1

What happens to LDR resistance in darkness?

  1. Decreases
  2. Increases
  3. Stays same
  4. Becomes zero
Remember
โœ… Answer: (B) โ€” In darkness, LDR resistance increases to ~10Mฮฉ.
Q2

Which Arduino function reads analog voltage?

  1. digitalRead()
  2. analogRead()
  3. voltageRead()
  4. sensorRead()
Remember
โœ… Answer: (B) analogRead() โ€” Returns a value 0โ€“1023 representing 0โ€“5V.
Q3

The HC-SR04 ultrasonic sensor operates at what frequency?

  1. 20 kHz
  2. 40 kHz
  3. 100 kHz
  4. 1 MHz
Remember
โœ… Answer: (B) 40 kHz โ€” This is in the ultrasonic range, above human hearing.
Q4

What is the formula for distance using HC-SR04?

  1. distance = time ร— 340
  2. distance = (time ร— 0.034) / 2
  3. distance = time / 340
  4. distance = time ร— 0.034
Understand
โœ… Answer: (B) โ€” We divide by 2 because the sound travels TO the object AND back. Speed of sound โ‰ˆ 0.034 cm/ยตs.
Q5

What does pulseIn(echoPin, HIGH) return?

  1. Voltage in volts
  2. Time in microseconds the pin was HIGH
  3. Distance in cm
  4. Frequency in Hz
Understand
โœ… Answer: (B) โ€” pulseIn() measures the duration (in ยตs) that the specified pin is at the given state (HIGH).
Q6

What is the range of HC-SR04 sensor?

  1. 0โ€“10 cm
  2. 2โ€“400 cm
  3. 1โ€“1000 cm
  4. 10โ€“200 cm
Remember
โœ… Answer: (B) 2โ€“400 cm โ€” Objects closer than 2cm or farther than 400cm give unreliable readings.
Q7

An IR sensor output is LOW when:

  1. No object is present
  2. An object is detected
  3. Power is off
  4. Sensor is broken
Remember
โœ… Answer: (B) โ€” Most IR modules output LOW (active low) when an object reflects IR light back.
Q8

In an LDR voltage divider, the fixed resistor is typically:

  1. 100ฮฉ
  2. 1kฮฉ
  3. 10kฮฉ
  4. 1Mฮฉ
Remember
โœ… Answer: (C) 10kฮฉ โ€” This value gives a good range of voltage variation for the LDR's typical resistance range.
Q9

analogRead() returns a value in the range:

  1. 0โ€“255
  2. 0โ€“1023
  3. 0โ€“4095
  4. 0โ€“65535
Remember
โœ… Answer: (B) 0โ€“1023 โ€” Arduino Uno has a 10-bit ADC (2^10 = 1024 levels).
Q10

Which trigger pulse duration is needed for HC-SR04?

  1. 2 ยตs
  2. 10 ยตs
  3. 100 ยตs
  4. 1 ms
Remember
โœ… Answer: (B) 10 ยตs โ€” The trigger pin must be held HIGH for at least 10 microseconds to initiate measurement.
Q11

What type of output does an IR obstacle sensor give?

  1. Analog
  2. Digital
  3. PWM
  4. Serial
Remember
โœ… Answer: (B) Digital โ€” IR obstacle sensors output HIGH or LOW only.
Q12

map(val, 0, 1023, 0, 255) converts analog reading to:

  1. Voltage
  2. PWM duty cycle range
  3. Temperature
  4. Distance
Understand
โœ… Answer: (B) โ€” The map() function scales the 10-bit ADC range (0โ€“1023) to 8-bit PWM range (0โ€“255).
Q13

Speed of sound used in ultrasonic calculation is approximately:

  1. 340 m/s
  2. 3400 m/s
  3. 34 m/s
  4. 34000 m/s
Remember
โœ… Answer: (A) 340 m/s โ€” At room temperature, sound travels at approximately 340 meters per second in air.
Q14

Which application uses LDR sensors in India?

  1. Automatic street lights
  2. Motor speed control
  3. Temperature measurement
  4. RFID reading
Understand
โœ… Answer: (A) โ€” Automatic street lights that turn ON at dusk and OFF at dawn use LDRs for light detection.
Q15

How many pins does the HC-SR04 module have?

  1. 2
  2. 3
  3. 4
  4. 6
Remember
โœ… Answer: (C) 4 โ€” VCC, Trig, Echo, GND.
Q16

For a line follower robot, which sensor is used?

  1. LDR
  2. Ultrasonic
  3. IR sensor
  4. Temperature sensor
Understand
โœ… Answer: (C) โ€” IR sensors detect the contrast between black line and white surface for line following.
Q17

What is the ADC resolution of Arduino Uno?

  1. 8-bit
  2. 10-bit
  3. 12-bit
  4. 16-bit
Remember
โœ… Answer: (B) 10-bit โ€” Provides 1024 discrete levels (0โ€“1023).
Q18

Which function generates a square wave on a pin for a buzzer?

  1. analogWrite()
  2. tone()
  3. buzz()
  4. sound()
Remember
โœ… Answer: (B) tone() โ€” tone(pin, frequency) generates a square wave at the specified frequency.
Q19

Why do we divide the ultrasonic time by 2?

  1. To convert units
  2. Sound travels to object and back (round trip)
  3. Sensor has 2 pins
  4. Arduino clock is 2x faster
Understand
โœ… Answer: (B) โ€” The measured duration is the round-trip time (to object + back), so we halve it for one-way distance.
Q20

What component is paired with LDR in a voltage divider?

  1. Capacitor
  2. LED
  3. Fixed resistor
  4. Transistor
Understand
โœ… Answer: (C) โ€” A fixed resistor creates a voltage divider with the LDR, producing a variable voltage at the junction that Arduino reads.