Introduction to IoT
Unit 2: Getting Started with Arduino & Hardware
From unboxing an Arduino UNO to blinking your first LED โ master the hardware foundations, understand the ATMega328 microcontroller, and write your first embedded C sketch.
โฑ๏ธ Time to Complete: 6โ8 hours | ๐ฐ Earning Potential: โน3,000โโน12,000/project | ๐ 30 MCQs (Bloom's Mapped)
๐ผ Jobs this unlocks: IoT Developer (โน4โ8 LPA) | Embedded Systems Engineer (โน5โ10 LPA) | Hardware Prototyper (โน3โ6 LPA)
Opening Hook โ The โน500 Board That Powers a Billion-Dollar Industry
๐ How a Tiny Blue Board Changed Everything
In 2005, a group of Italian engineers at the Interaction Design Institute Ivrea created a small, inexpensive circuit board for their design students. They called it Arduino โ named after a local bar. That โน500 board has since sold over 10 million units and sparked a global maker revolution that powers smart homes, industrial IoT, agricultural automation, and even space experiments.
In India alone, Arduino-based projects are driving Smart Agriculture (automated irrigation in Rajasthan), Smart Cities (Pune's IoT traffic management), and Healthcare (low-cost pulse oximeters built during COVID-19 by engineering students). Companies like Tata Elxsi, Wipro IoT, and Bosch India use Arduino for rapid prototyping before scaling to production hardware.
What if YOU could build this? What if you could take a โน500 board, connect a few wires, write 10 lines of code, and make a physical LED blink โ your first step towards building real-world IoT products? That's exactly what this chapter teaches you.
Learning Outcomes โ Bloom's Taxonomy Mapped
| Bloom's Level | Learning Outcome |
|---|---|
| ๐ต Remember | List the key components of an Arduino UNO board and recall the ATMega328 specifications (32 KB Flash, 2 KB SRAM, 16 MHz) |
| ๐ต Understand | Explain how the setup() and loop() functions work together in an Arduino sketch, and describe the role of each AVR pin |
| ๐ข Apply | Write and upload the Blink sketch to an Arduino UNO, correctly selecting board and COM port in the IDE |
| ๐ข Analyze | Compare digital vs analog pins, differentiate PWM-capable pins, and trace the pin mapping between Arduino labels and ATMega328 physical pins |
| ๐ Evaluate | Diagnose common Arduino upload errors (COM port not found, driver issues, wrong board selected) and apply fixes |
| ๐ Create | Design and implement a multi-LED pattern sketch using digitalWrite(), analogWrite(), and timing functions |
Concept Explanation โ Setting Up & First Sketch
1. Setting Up the Arduino Board
Think of the Arduino UNO as a tiny, programmable computer. It doesn't have a screen or keyboard โ instead, it has pins that connect to LEDs, sensors, motors, and other electronic components. Your laptop talks to it through a USB cable.
๐ Connecting Your Arduino UNO
Step 1 โ Unbox: You'll find the Arduino UNO board, a USB Type-B cable (the same type used for printers), and sometimes a small breadboard.
Step 2 โ Connect via USB: Plug the USB-B end into the Arduino and the USB-A end into your laptop. The board's green ON LED should light up immediately โ this confirms the board is receiving power (5V via USB).
Step 3 โ Check Power: The Arduino UNO can be powered in three ways:
| Power Source | Voltage | Best For |
|---|---|---|
| USB Cable | 5V from laptop | Development & uploading code |
| DC Barrel Jack | 7โ12V external adapter | Standalone projects (no laptop) |
| Vin Pin | 7โ12V from battery | Portable/field projects |
Step 4 โ Observe: When freshly purchased, most Arduino boards come pre-loaded with the Blink sketch. The built-in LED (pin 13) should be blinking on and off every 1 second. If it is โ your board is working!
2. Arduino IDE โ Your Programming Environment
Analogy: If the Arduino board is a car's engine, the Arduino IDE is the steering wheel and dashboard. It's where you write instructions (code), check for errors, and send the code to the board.
๐ป Installing & Configuring the Arduino IDE
Step 1 โ Download: Go to arduino.cc/en/software โ Download Arduino IDE 2.x (recommended) for your OS (Windows / macOS / Linux).
Step 2 โ Install: Run the installer. On Windows, click "Yes" on all driver installation prompts โ these install the USB-to-Serial drivers that let your laptop communicate with the board.
Step 3 โ Open IDE: Launch Arduino IDE. You'll see a blank code editor with two default functions: setup() and loop().
Step 4 โ Select Board: Go to Tools โ Board โ Arduino AVR Boards โ Arduino Uno.
Step 5 โ Select Port: Go to Tools โ Port โ Select the COM port that says "Arduino Uno" next to it (e.g., COM3 (Arduino Uno) on Windows or /dev/ttyACM0 on Linux).
Step 6 โ Test: Click the โ (Verify) button. If no errors appear, your IDE is ready!
| IDE Feature | Shortcut | What It Does |
|---|---|---|
| Verify (โ) | Ctrl+R | Compiles your code and checks for syntax errors โ does NOT upload |
| Upload (โ) | Ctrl+U | Compiles AND sends the code to the Arduino board via USB |
| Serial Monitor | Ctrl+Shift+M | Opens a console to send/receive text data from the board |
| Serial Plotter | Ctrl+Shift+L | Graphically plots numeric data from the board in real-time |
3. Preparing a Sketch โ setup() and loop()
In Arduino, a program is called a "sketch". Every sketch has exactly two mandatory functions:
๐ The Two Pillars of Every Arduino Sketch
void setup() โ Runs once when the board powers on or resets. Use it for one-time initialisation: setting pin modes, starting serial communication, configuring libraries.
Analogy: Like the morning routine before you start working โ brush teeth, get dressed, eat breakfast. You do it once, then you're ready.
void loop() โ Runs continuously, forever, in an infinite loop. This is where your main logic lives: read sensors, make decisions, control outputs.
Analogy: Like a security guard who checks the gate, walks the perimeter, checks the gate again โ on repeat, 24/7, until the power is cut.
Arduino C // Minimal Sketch Structure void setup() { // Runs ONCE at startup // Initialize pins, serial, etc. } void loop() { // Runs FOREVER in a loop // Read sensors, control LEDs, etc. }
setup() and loop() are called from a hidden main() function.
4. The Blink Sketch โ Your "Hello World" of Hardware
In software programming, the first program is always "Hello World." In hardware/embedded programming, the first program is always Blink โ making an LED turn ON and OFF repeatedly. It proves your board works, your IDE is configured correctly, and you can write + upload code.
๐ก Step-by-Step: Blink the Built-in LED (Pin 13)
Step 1: Open Arduino IDE โ File โ Examples โ 01.Basics โ Blink (or type the code below manually).
Step 2: Understand the code โ read every line and its comment below.
Step 3: Click Verify (โ) โ you should see "Done compiling." at the bottom.
Step 4: Click Upload (โ) โ you should see "Done uploading." The TX/RX LEDs on the board will flash rapidly during upload.
Step 5: Observe โ the small orange LED labelled "L" on the board (connected to pin 13) should now blink: ON for 1 second, OFF for 1 second, repeat.
Arduino C โ Blink Sketch /* * Blink Sketch โ The "Hello World" of Arduino * Turns the built-in LED (pin 13) ON for 1 second, * then OFF for 1 second, repeatedly. * * No external components needed โ uses the onboard LED. */ void setup() { // Initialize digital pin 13 as an OUTPUT pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); // Turn LED ON (5V on pin 13) delay(1000); // Wait 1000 ms (1 second) digitalWrite(13, LOW); // Turn LED OFF (0V on pin 13) delay(1000); // Wait 1000 ms (1 second) }
| Function | What It Does | Parameters |
|---|---|---|
pinMode(pin, mode) | Configures a pin as INPUT or OUTPUT | pin: 0โ13 or A0โA5; mode: INPUT, OUTPUT, INPUT_PULLUP |
digitalWrite(pin, value) | Sets a digital pin to HIGH (5V) or LOW (0V) | pin: the pin number; value: HIGH or LOW |
delay(ms) | Pauses the program for a given number of milliseconds | ms: time in milliseconds (1000 = 1 second) |
delay(1000) to delay(200). What happens? Now try delay(50). Can you make the LED appear to be "always on" by going fast enough? (Hint: This is the principle behind PWM โ Pulse Width Modulation, which we'll study later.)
5. Troubleshooting โ When Things Go Wrong
Welcome to the real world of hardware. Things will go wrong. Here's a troubleshooting guide for the most common issues:
| Problem | Cause | Fix |
|---|---|---|
| "COM port not found" | Driver not installed or cable is charge-only | Install CH340 driver (for clones) from sparks.gogo.co.nz/ch340.html. Use a data-capable USB cable. |
| "avrdude: stk500_recv(): programmer is not responding" | Wrong board/port selected, or board is bricked | Check Tools โ Board = "Arduino Uno". Check Tools โ Port. Try pressing Reset button on board right before upload. |
| "Board at COM3 is not available" | USB cable disconnected or port in use by another app | Reconnect USB. Close Serial Monitor/Plotter in another IDE window. Close PuTTY/Tera Term. |
| LED doesn't blink | Code has errors, or wrong pin number | Verify code compiles. Ensure you're using pin 13 (or LED_BUILTIN constant). |
| "Access denied" on COM port | Port locked by another application | On Linux: add user to dialout group. On Windows: check Device Manager for conflicts. |
| Board not detected at all | Defective board or USB port issue | Try a different USB port. Try a different USB cable. Test on another computer. |
Concept Explanation โ Architecture & Internals
6. Arduino UNO Architecture โ Block Diagram
The Arduino UNO is more than just a microcontroller. It's a complete development board with voltage regulation, USB communication, clock oscillator, and pin headers โ all designed so you can focus on programming without worrying about circuit design.
๐๏ธ Arduino UNO Block Diagram
| Component | Part Number | Function |
|---|---|---|
| Main MCU | ATMega328P | Runs your Arduino sketch; contains Flash, SRAM, EEPROM, ADC, timers |
| USB-to-Serial | ATMega16U2 (official) / CH340G (clone) | Converts USB signals to Serial (UART) for communication with the MCU |
| Voltage Regulator | AMS1117 (5V & 3.3V) | Steps down input voltage (7โ12V from DC jack) to stable 5V/3.3V for the MCU |
| Crystal Oscillator | 16 MHz ceramic resonator | Provides the clock signal โ the "heartbeat" that synchronises all operations |
| Reset Button | Tactile switch | Restarts the MCU โ re-runs setup() then loop() |
7. AVR ATMega328P โ The Brain of Arduino UNO
The ATMega328P is an 8-bit AVR microcontroller made by Microchip (formerly Atmel). It's the chip that actually executes your code. Understanding its pin layout is essential for advanced projects.
๐ง ATMega328P โ Key Specifications
| Specification | Value | What It Means |
|---|---|---|
| Architecture | 8-bit AVR (RISC) | Simple, fast instruction set; one instruction per clock cycle |
| Clock Speed | 16 MHz | Executes 16 million instructions per second |
| Flash Memory | 32 KB (0.5 KB bootloader) | Stores your program (sketch). Non-volatile โ survives power off |
| SRAM | 2 KB | Working memory for variables during execution. Volatile โ lost on power off |
| EEPROM | 1 KB | Non-volatile storage for settings/data that persist across resets |
| Digital I/O Pins | 23 (14 on Arduino headers) | Can be set as INPUT or OUTPUT; read/write digital HIGH/LOW |
| Analog Input Channels | 6 (A0โA5) | 10-bit ADC: converts analog voltage (0โ5V) to digital value (0โ1023) |
| PWM Channels | 6 (pins 3, 5, 6, 9, 10, 11) | Simulates analog output using rapid ON/OFF switching |
| Operating Voltage | 5V | Logic HIGH = 5V, Logic LOW = 0V |
| Package | 28-pin DIP | Through-hole package; easy to replace if damaged |
ATMega328P โ 28-Pin DIP Diagram
8. Pin Mapping โ Arduino โ AVR ATMega328
Arduino uses simplified pin names (D0, D1, A0, etc.) that map to specific physical pins on the ATMega328P chip. Understanding this mapping is crucial when you read datasheets or design custom PCBs.
Digital Pins (D0โD13)
| Arduino Pin | ATMega328 Pin | Port/Bit | PWM? | Special Function |
|---|---|---|---|---|
| D0 | Pin 2 | PD0 | โ | RX (Serial Receive) |
| D1 | Pin 3 | PD1 | โ | TX (Serial Transmit) |
| D2 | Pin 4 | PD2 | โ | INT0 (External Interrupt 0) |
| D3 | Pin 5 | PD3 | โ ~ | INT1 (External Interrupt 1) |
| D4 | Pin 6 | PD4 | โ | T0 (Timer 0 external) |
| D5 | Pin 11 | PD5 | โ ~ | T1 (Timer 1 external) |
| D6 | Pin 12 | PD6 | โ ~ | AIN0 (Analog Comparator +) |
| D7 | Pin 13 | PD7 | โ | AIN1 (Analog Comparator โ) |
| D8 | Pin 14 | PB0 | โ | ICP1 (Input Capture) |
| D9 | Pin 15 | PB1 | โ ~ | OC1A (Timer 1 output) |
| D10 | Pin 16 | PB2 | โ ~ | SS (SPI Slave Select) |
| D11 | Pin 17 | PB3 | โ ~ | MOSI (SPI Master Out) |
| D12 | Pin 18 | PB4 | โ | MISO (SPI Master In) |
| D13 | Pin 19 | PB5 | โ | SCK (SPI Clock) + Built-in LED |
Analog Pins (A0โA5)
| Arduino Pin | ATMega328 Pin | Port/Bit | ADC Channel | Special Function |
|---|---|---|---|---|
| A0 | Pin 23 | PC0 | ADC0 | โ |
| A1 | Pin 24 | PC1 | ADC1 | โ |
| A2 | Pin 25 | PC2 | ADC2 | โ |
| A3 | Pin 26 | PC3 | ADC3 | โ |
| A4 | Pin 27 | PC4 | ADC4 | SDA (I2C Data) |
| A5 | Pin 28 | PC5 | ADC5 | SCL (I2C Clock) |
pinMode(A0, OUTPUT) and digitalWrite(A0, HIGH) on them. This gives you up to 20 digital I/O pins total (D0โD13 + A0โA5). This is documented but often overlooked by beginners.
PWM Pins Summary
PWM (Pulse Width Modulation) pins are marked with a tilde (~) on the Arduino board. They can simulate analog output by rapidly switching between HIGH and LOW.
| PWM Pin | Timer Used | Frequency | Notes |
|---|---|---|---|
| D3, D11 | Timer 2 (8-bit) | ~490 Hz | Shared timer โ changing one affects the other |
| D5, D6 | Timer 0 (8-bit) | ~980 Hz | โ ๏ธ Also used by delay() and millis() |
| D9, D10 | Timer 1 (16-bit) | ~490 Hz | Best for precise timing and Servo control |
9. Data Types & Essential Functions
Arduino uses C/C++ data types. Here are the most important ones you'll use in IoT projects:
Core Data Types
| Type | Size | Range | Use Case |
|---|---|---|---|
int | 2 bytes | โ32,768 to 32,767 | Pin numbers, counters, sensor values |
unsigned int | 2 bytes | 0 to 65,535 | Only positive values; larger range |
long | 4 bytes | โ2,147,483,648 to 2,147,483,647 | Large numbers, millis() return type |
float | 4 bytes | ยฑ3.4ร10ยณโธ (6โ7 decimal digits) | Temperature readings, voltage calculations |
char | 1 byte | โ128 to 127 (or ASCII character) | Single characters, serial data |
bool / boolean | 1 byte | true (1) or false (0) | Flags, ON/OFF states |
byte | 1 byte | 0 to 255 | Raw data, register values |
String | Variable | Dynamic character array | Text messages, LCD display strings |
Essential Arduino Functions
| Function | Category | Syntax | Description |
|---|---|---|---|
pinMode() | Digital I/O | pinMode(pin, mode) | Set pin as INPUT, OUTPUT, or INPUT_PULLUP |
digitalWrite() | Digital I/O | digitalWrite(pin, val) | Write HIGH (5V) or LOW (0V) to a pin |
digitalRead() | Digital I/O | digitalRead(pin) | Returns HIGH or LOW โ reads button/switch state |
analogRead() | Analog I/O | analogRead(pin) | Returns 0โ1023 (10-bit ADC) from analog pin |
analogWrite() | Analog I/O | analogWrite(pin, val) | PWM output: val = 0โ255 (0% to 100% duty cycle) |
delay() | Timing | delay(ms) | Pause execution for ms milliseconds |
millis() | Timing | millis() | Returns ms since board started โ non-blocking timing |
Serial.begin() | Communication | Serial.begin(baud) | Start serial at given baud rate (usually 9600) |
Serial.println() | Communication | Serial.println(data) | Send data to Serial Monitor with newline |
map() | Math | map(val,fromL,fromH,toL,toH) | Re-maps a number from one range to another |
Arduino C โ Reading a Sensor // Example: Read a temperature sensor on A0 and print to Serial Monitor int sensorPin = A0; // Analog pin connected to sensor int sensorValue = 0; // Variable to store the reading float voltage = 0.0; // Converted voltage value float temperatureC = 0.0; // Temperature in Celsius void setup() { Serial.begin(9600); // Start serial communication at 9600 baud } void loop() { sensorValue = analogRead(sensorPin); // Read analog value (0-1023) voltage = sensorValue * (5.0 / 1023.0); // Convert to voltage (0-5V) temperatureC = voltage * 100.0; // LM35: 10mV per ยฐC Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" ยฐC"); delay(1000); // Read every 1 second }
float on Arduino is slow and memory-heavy. The ATMega328 has no hardware floating-point unit (FPU), so every float operation is done in software โ using ~10ร more clock cycles than int operations. For IoT projects where speed matters, use integer math and fixed-point arithmetic. Only use float when you need decimal precision (like temperature calculations).
millis() instead of delay() for real IoT projects. delay() blocks the entire program โ nothing else can run during the wait. millis() lets you check elapsed time without blocking, so your code can multitask (read sensors AND control LEDs simultaneously). This is called non-blocking programming โ essential for IoT.
Learn by Doing โ 3-Tier Lab Structure
๐ข Tier 1 โ GUIDED TASK: Blink an External LED on a Breadboard
Components Needed:
- Arduino UNO + USB cable
- 1ร Red LED
- 1ร 220ฮฉ resistor (Red-Red-Brown-Gold)
- Breadboard + 2 jumper wires
Step 1: Identify LED Polarity
An LED has two legs: the longer leg is the Anode (+) and the shorter leg is the Cathode (โ). Current flows from Anode to Cathode.
Step 2: Build the Circuit
- Insert the LED into the breadboard
- Connect a 220ฮฉ resistor from the Anode (long leg) to Arduino pin 8 using a jumper wire
- Connect the Cathode (short leg) to Arduino GND using a jumper wire
Step 3: Write the Code
Arduino C int ledPin = 8; // External LED connected to pin 8 void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); }
Step 4: Upload & Observe
Upload the code. Your external LED should blink ON/OFF every 0.5 seconds.
๐ Congratulations! You've built your first real hardware circuit. This is the foundation of every IoT project.
๐ก Tier 2 โ SEMI-GUIDED: Traffic Light Simulation (3 LEDs)
Your Mission:
Build a traffic light using 3 LEDs (Red, Yellow, Green) that cycles through the standard traffic sequence.
Hints:
- Connections: Red LED โ pin 4, Yellow LED โ pin 3, Green LED โ pin 2. Each with a 220ฮฉ resistor to GND.
- Sequence: Green ON for 5 sec โ Yellow ON for 2 sec โ Red ON for 5 sec โ repeat.
- Key functions:
pinMode(),digitalWrite(),delay(). - Remember: Turn OFF the previous LED before turning ON the next one.
INPUT_PULLUP). When pressed, the traffic light should go Red immediately for 10 seconds to let pedestrians cross. Use digitalRead().
๐ด Tier 3 โ OPEN CHALLENGE: Smart Night Light with LDR Sensor
The Brief:
Build a smart night light that automatically turns ON an LED when it gets dark and turns it OFF when there's enough light. This mimics real-world street lighting IoT systems.
- Use an LDR (Light Dependent Resistor) connected to analog pin A0
- Use a voltage divider circuit with a 10Kฮฉ resistor
- Read the LDR value using
analogRead() - Set a threshold (experiment to find the right value)
- Turn ON an LED when the reading is below the threshold (dark)
- Print the sensor value to Serial Monitor for debugging
- Bonus: Use
analogWrite()to make the LED brightness proportional to darkness (PWM dimming)
Industry Spotlight โ A Day in the Life
๐จโ๐ป Rahul Verma, 24 โ IoT Prototyping Engineer at Tata Elxsi, Bangalore
Background: B.Tech ECE from NIT Bhopal. Built Arduino projects since 2nd year. Won Smart India Hackathon 2024 with an IoT-based water quality monitoring system. Got placed at Tata Elxsi through campus recruitment.
A Typical Day:
9:00 AM โ Team standup. Review progress on a smart agriculture PoC (Proof of Concept) for a government project in Maharashtra.
10:00 AM โ Test soil moisture sensor integration with Arduino Mega. Calibrate readings against lab-grade moisture meter. Document threshold values.
11:30 AM โ Write firmware in Arduino IDE. Implement non-blocking sensor reading using millis(). Add Wi-Fi data transmission via ESP32 module.
1:00 PM โ Lunch. Discuss with the cloud team about AWS IoT Core integration for the sensor data pipeline.
2:00 PM โ Debug a relay switching circuit. The motor pump isn't activating when soil is dry. Issue: GPIO pin set to INPUT instead of OUTPUT. Classic!
4:00 PM โ Prepare a demo for the client โ show real-time sensor data on a Grafana dashboard. Arduino โ ESP32 โ MQTT โ AWS โ Grafana.
5:30 PM โ Learning hour: studying STM32 microcontrollers for production-grade designs (the next step beyond Arduino prototyping).
| Detail | Info |
|---|---|
| Tools Used Daily | Arduino IDE, VS Code + PlatformIO, Multimeter, Logic Analyser, AWS IoT Core |
| Entry Salary (2024) | โน5โ8 LPA + benefits |
| Mid-Level (3โ5 yrs) | โน10โ18 LPA |
| Senior (7+ yrs) | โน20โ40 LPA |
| Companies Hiring | Tata Elxsi, Wipro IoT, Bosch India, L&T Smart World, Continental, Samsung R&D, Qualcomm India, Honeywell |
Earn With It โ Freelance & Income Roadmap
๐ฐ Your Earning Path After This Chapter
Portfolio Piece: "Arduino-Based Smart Night Light with LDR" โ a working hardware project with code on GitHub, circuit diagram, and a demo video.
Beginner Gig Ideas:
โข Arduino-based attendance system for schools/colleges โ โน3,000โโน8,000
โข Smart plant watering system for homes/offices โ โน2,000โโน5,000
โข LED display projects for local shops โ โน2,000โโน6,000
โข IoT prototype for B.Tech/M.Tech final year projects โ โน3,000โโน10,000
| Platform | Best For | Typical Rate |
|---|---|---|
| Freelancer.com | Arduino/IoT prototype projects | $50โ$300/project (โน4,000โโน25,000) |
| Fiverr | Quick Arduino code/circuit gigs | $20โ$100/gig (โน1,600โโน8,000) |
| Internshala | Indian student internships in IoT/embedded | โน5,000โโน15,000/month |
| College Projects | Final year project help for other students | โน3,000โโน12,000/project |
| Local Shops | Automation projects for small businesses | โน5,000โโน20,000/project |
โฑ๏ธ Time to First Earning: 3โ4 weeks (complete all labs, build one portfolio project, list on Fiverr/Freelancer)
MCQ Assessment Bank โ 30 Questions (Bloom's Mapped)
Remember / Identify (Q1โQ6)
What microcontroller chip is used on the Arduino UNO board?
- ATMega2560
- ATMega328P
- ESP8266
- STM32F103
How much Flash memory does the ATMega328P have?
- 8 KB
- 16 KB
- 32 KB
- 64 KB
What is the clock speed of the Arduino UNO?
- 8 MHz
- 12 MHz
- 16 MHz
- 20 MHz
How much SRAM does the ATMega328P have?
- 512 bytes
- 1 KB
- 2 KB
- 4 KB
Which function in an Arduino sketch runs only once at startup?
loop()main()setup()init()
setup() โ The setup() function runs once when the board powers on or is reset. It's used for one-time initialisation like pinMode() and Serial.begin().Which pin on the Arduino UNO has a built-in LED?
- Pin 0
- Pin 7
- Pin 11
- Pin 13
LED_BUILTIN.Understand / Explain (Q7โQ12)
What is the purpose of the pinMode() function?
- Sets the voltage level of a pin
- Configures a pin as INPUT or OUTPUT
- Reads the analog value on a pin
- Starts serial communication
pinMode(pin, mode) configures whether a pin will be used to read inputs (INPUT) or drive outputs (OUTPUT). This must be done in setup() before using digitalRead() or digitalWrite().Why does analogRead() return values between 0 and 1023?
- Because the ADC has 8-bit resolution
- Because the ADC has 10-bit resolution (2ยนโฐ = 1024 levels)
- Because the maximum voltage is 10.23V
- Because there are 1023 analog pins
What does the delay(1000) function call do?
- Delays for 1 microsecond
- Delays for 100 milliseconds
- Delays for 1000 milliseconds (1 second)
- Delays for 1000 seconds
delay() takes milliseconds as its argument. 1000 ms = 1 second. During this time, the processor is blocked and cannot execute other code.What is the role of the USB-to-Serial converter (ATMega16U2 or CH340) on the Arduino UNO?
- It runs the user's sketch code
- It converts USB signals to UART serial for communication with the main MCU
- It regulates voltage from 12V to 5V
- It stores the bootloader firmware
Why is a 220ฮฉ resistor used in series with an LED connected to an Arduino pin?
- To increase the LED brightness
- To limit current and prevent the LED from burning out
- To convert analog signal to digital
- To filter noise from the power supply
What happens if you upload code with the wrong board selected in the Arduino IDE?
- The code uploads successfully but runs slower
- The IDE automatically detects the correct board
- The upload may fail with "programmer not responding" or produce unpredictable behavior
- Nothing happens โ the board ignores incorrect code
Apply / Implement (Q13โQ18)
To make an LED on pin 9 fade smoothly, which function should you use?
digitalWrite(9, 128)analogWrite(9, 128)analogRead(9)digitalRead(9)
analogWrite(pin, value) generates a PWM signal. Pin 9 is PWM-capable (~). A value of 128 gives a 50% duty cycle, making the LED glow at half brightness. digitalWrite() only accepts HIGH/LOW, not intermediate values.What is the correct way to read a push button connected to pin 7 with an internal pull-up resistor?
pinMode(7, OUTPUT); int val = digitalRead(7);pinMode(7, INPUT_PULLUP); int val = digitalRead(7);pinMode(7, INPUT); int val = analogRead(7);analogWrite(7, HIGH);
INPUT_PULLUP enables the ATMega328's internal pull-up resistor (~20Kฮฉ to VCC). The pin reads HIGH when the button is open and LOW when pressed. This eliminates the need for an external pull-up resistor.To convert an analogRead() value (0โ1023) to voltage (0.0โ5.0V), which formula is correct?
voltage = analogRead(A0) * 1023.0 / 5.0;voltage = analogRead(A0) * 5.0 / 1023.0;voltage = analogRead(A0) / 5.0;voltage = analogRead(A0) + 5.0;
value * (5.0 / 1023.0) maps the 10-bit ADC range (0โ1023) to the voltage range (0Vโ5V). Each ADC unit = 5.0/1023 โ 0.00488V.You want to send sensor data from Arduino to a computer. What should be in setup()?
Serial.println(9600);Serial.begin(9600);Serial.read();Serial.write(9600);
Serial.begin(9600) initialises serial communication at 9600 baud (bits per second). This must be called in setup() before using Serial.print(), Serial.println(), or Serial.read().Which of these pins can be used for PWM output on the Arduino UNO?
- Pins 2, 4, 7, 8
- Pins 3, 5, 6, 9, 10, 11
- Pins A0, A1, A2, A3
- All digital pins (0โ13)
What value does digitalRead() return when a button connected with INPUT_PULLUP is pressed?
HIGHLOW1023255
INPUT_PULLUP, the pin is pulled HIGH by default (via internal resistor). When the button is pressed, it connects the pin to GND, making it read LOW. This is called "active LOW" logic.Analyze / Compare (Q19โQ23)
What is the key difference between delay() and millis() for timing?
delay()is more accurate thanmillis()delay()blocks execution;millis()allows non-blocking timingmillis()can only measure up to 1 second- They are functionally identical
delay() halts the entire program for the specified duration. millis() returns the time elapsed since board startup, allowing you to check time without blocking โ enabling multitasking in IoT applications.Comparing digitalWrite() and analogWrite(): which statement is correct?
digitalWrite()outputs 0โ255 levels;analogWrite()outputs HIGH/LOW onlydigitalWrite()outputs only HIGH (5V) or LOW (0V);analogWrite()outputs 0โ255 duty cycle levels via PWM- Both functions work on all 20 pins of the UNO
analogWrite()reads sensor values;digitalWrite()sends data
digitalWrite() is binary (ON/OFF). analogWrite() uses PWM to simulate analog output with 256 brightness/speed levels (0 = always OFF, 255 = always ON). analogWrite() only works on PWM pins (~).Why do PWM pins 5 and 6 on Arduino UNO run at ~980 Hz while others run at ~490 Hz?
- They use a faster crystal oscillator
- They share Timer 0, which has a different prescaler setting because it's also used by
delay()andmillis() - They have dedicated hardware for higher frequency
- It's a manufacturing defect
delay() and millis(). This results in a ~980 Hz PWM frequency, while Timer 1 and Timer 2 (other PWM pins) use a prescaler giving ~490 Hz.An Arduino UNO is connected to a laptop via USB but the COM port doesn't appear. Which factor is LEAST likely to cause this?
- The USB cable is charge-only (no data wires)
- The CH340 driver is not installed (for clone boards)
- The Arduino sketch has a syntax error
- The USB port on the laptop is malfunctioning
What is the advantage of using the LED_BUILTIN constant instead of the number 13?
LED_BUILTINmakes the LED brighterLED_BUILTINis portable โ it maps to the correct pin on any Arduino board, not just UNOLED_BUILTINenables PWM on pin 13- There is no difference; they are always the same
LED_BUILTIN is a board-specific constant defined in the Arduino core. On UNO it's 13, but on other boards (e.g., Arduino Mega, Leonardo) the built-in LED may be on a different pin. Using the constant makes your code portable.Evaluate / Judge (Q24โQ27)
A student's LED blinks correctly on Tinkercad simulator but doesn't work on the real Arduino UNO. What is the MOST likely cause?
- The simulator uses different code syntax
- A hardware issue: incorrect wiring, missing resistor, or wrong pin connection
- The ATMega328P chip is incompatible with LED circuits
- Tinkercad uses a different programming language
A project needs to read 8 analog sensors simultaneously. Is the Arduino UNO suitable?
- Yes โ it has 8 analog pins
- No โ it only has 6 analog input pins (A0โA5), so you'd need a multiplexer or a different board
- Yes โ you can use digital pins for analog reading
- No โ Arduino cannot read analog sensors at all
Evaluate this code: int x = 40000; โ What problem will this cause on Arduino UNO?
- No problem โ
intcan store 40000 - Integer overflow โ
inton AVR is 16-bit (max 32,767), so 40000 will wrap around to a negative value - Compilation error โ 40000 is not a valid number
- The program will crash immediately
int is 16-bit (range: โ32,768 to 32,767). The value 40,000 exceeds this range and wraps around due to two's complement overflow, storing โ25,536. Use unsigned int (0โ65,535) or long instead.A student uses delay(60000) to wait 1 minute between sensor readings. Why is this a poor design choice for an IoT system?
delay()cannot accept values larger than 10000- The entire MCU is blocked for 60 seconds โ it cannot respond to button presses, sensor alerts, or incoming serial data during that time
- It will drain the battery faster
delay()is not accurate enough for 60 seconds
delay() is a blocking function. During 60 seconds of delay, the Arduino cannot read sensors, respond to button interrupts, or process serial commands. IoT systems need responsiveness. Use millis()-based timing for non-blocking waits.Create / Design (Q28โQ30)
You need to design a circuit where an LED's brightness varies with a potentiometer. Which combination of functions is correct?
digitalRead()on potentiometer pin +digitalWrite()on LED pinanalogRead()on potentiometer pin +analogWrite()on LED pin (with value mapped from 0โ1023 to 0โ255)analogRead()on both pinsSerial.read()on potentiometer pin +Serial.write()on LED pin
analogRead() (returns 0โ1023), then use map(val, 0, 1023, 0, 255) to scale the value, and analogWrite() to control LED brightness via PWM. The LED must be on a PWM pin (~).To build an Arduino-based temperature alarm that buzzes when temperature exceeds 40ยฐC, which sensor + output combination is most appropriate?
- Push button (digital input) + LED (digital output)
- LM35 temperature sensor (analog input on A0) + Piezo buzzer (digital output on pin 8)
- Potentiometer (analog input) + Servo motor (PWM output)
- Photoresistor (analog input) + Relay (digital output)
analogRead(). Convert to temperature using temp = (analogRead(A0) * 5.0 / 1023.0) * 100. If temp > 40, activate the buzzer using digitalWrite(8, HIGH).You want to create a data logger that records temperature every 5 seconds and stores 1000 readings. Where should the data be stored on Arduino UNO?
- Flash memory (32 KB) โ it has enough space
- SRAM (2 KB) โ use an array of 1000 floats
- EEPROM (1 KB) โ write each reading with
EEPROM.write() - External SD card module โ 1000 float values (4 KB) exceed EEPROM capacity, and SRAM can't persist data
Short Answer Questions (8 Questions)
๐ Q1: Explain the difference between setup() and loop() in an Arduino sketch. (3 marks)
Answer:
setup() is called once when the Arduino is powered on or reset. It is used for one-time initialisation tasks such as setting pin modes (pinMode()), starting serial communication (Serial.begin()), and initialising libraries or variables.
loop() is called repeatedly after setup() finishes. It runs continuously in an infinite loop for the entire runtime of the program. All main logic โ reading sensors, controlling outputs, making decisions โ goes here.
Analogy: setup() is like setting up a shop before opening (arranging shelves, switching on lights). loop() is like running the shop all day โ serving customers, restocking, until you close.
๐ Q2: List the three power supply options for the Arduino UNO and state the voltage range for each. (3 marks)
Answer:
1. USB Cable: Provides 5V directly from the laptop/computer. Best for development and code uploading.
2. DC Barrel Jack: Accepts 7โ12V from an external DC adapter. The onboard voltage regulator (AMS1117) steps it down to 5V. Best for standalone projects without a laptop.
3. Vin Pin: Accepts 7โ12V from an external battery or power source connected directly to the Vin header pin. The voltage regulator handles the conversion. Best for portable or field-deployed projects.
Note: Input below 7V may cause unstable operation. Input above 12V may overheat the voltage regulator.
๐ Q3: What is the function of the CH340 chip found on Arduino UNO clones? Why do clones require a separate driver? (3 marks)
Answer:
The CH340 (or CH341) is a USB-to-Serial converter chip used on low-cost Arduino clone boards. It converts USB signals from the computer into UART serial signals (TX/RX) that the ATMega328P microcontroller can understand. This enables code uploading and Serial Monitor communication.
Official Arduino boards use an ATMega16U2 or FTDI chip for this purpose, whose drivers come pre-installed with Windows and macOS. However, the CH340 is a third-party Chinese chip that is not included in the standard OS driver packages, so users must manually download and install the CH340 driver from the manufacturer's website. Without this driver, the board appears as "Unknown Device" in Device Manager and no COM port is assigned.
๐ Q4: What is PWM? Name all PWM-capable pins on the Arduino UNO. (3 marks)
Answer:
PWM (Pulse Width Modulation) is a technique that simulates analog output using digital pins. The pin rapidly switches between HIGH (5V) and LOW (0V) at a fixed frequency. The ratio of ON-time to total cycle time is called the duty cycle.
- 0% duty cycle (value 0) = always OFF = 0V average
- 50% duty cycle (value 128) = ON half the time = ~2.5V average
- 100% duty cycle (value 255) = always ON = 5V average
PWM-capable pins on Arduino UNO: 3, 5, 6, 9, 10, 11 โ marked with a tilde (~) on the board.
Use cases: LED dimming, motor speed control, servo positioning, audio tone generation.
๐ Q5: Describe the three types of memory in the ATMega328P and state their sizes. (4 marks)
Answer:
| Memory Type | Size | Volatile? | Purpose |
|---|---|---|---|
| Flash | 32 KB | Non-volatile | Stores the compiled sketch (program code). Survives power cycles. 0.5 KB reserved for bootloader. |
| SRAM | 2 KB | Volatile | Working memory for variables, arrays, stack, and heap during program execution. Data is lost when power is removed. |
| EEPROM | 1 KB | Non-volatile | Long-term storage for settings and calibration data that must persist across power cycles. Limited write cycles (~100,000). |
Analogy: Flash = a textbook (stores instructions permanently). SRAM = a whiteboard (used during class, erased after). EEPROM = a locker (stores personal items that persist even when you leave the room).
๐ Q6: What is the role of the 16 MHz crystal oscillator on the Arduino UNO? (2 marks)
Answer:
The 16 MHz crystal oscillator provides the clock signal โ the precise timing reference that synchronises all operations inside the ATMega328P. It generates 16 million pulses per second, and each pulse triggers the execution of one instruction.
Without the crystal, the microcontroller would have no sense of timing โ it wouldn't know when to execute the next instruction, how long a delay() should last, or at what speed to send serial data. The crystal is essentially the "heartbeat" of the processor.
๐ Q7: Explain the difference between analogRead() and analogWrite(). Can they be used on the same pins? (3 marks)
Answer:
analogRead(pin) reads an analog voltage (0โ5V) from an analog input pin (A0โA5) and converts it to a digital value (0โ1023) using the built-in 10-bit ADC. Used for reading sensors like temperature, light, and potentiometers.
analogWrite(pin, value) generates a PWM output signal on specific digital pins (3, 5, 6, 9, 10, 11) with a duty cycle from 0 (0%) to 255 (100%). Used for controlling LED brightness and motor speed.
They cannot be used on the same pins. analogRead() only works on A0โA5 (analog input pins). analogWrite() only works on PWM-capable digital pins (3, 5, 6, 9, 10, 11). Despite the similar names, they are entirely different operations โ one reads an analog value, the other generates a PWM digital output.
๐ Q8: A student connects an LED directly to pin 13 and GND without a resistor. What could happen and why? (3 marks)
Answer:
Without a current-limiting resistor, the LED would attempt to draw excessive current from the Arduino pin. Each GPIO pin can supply a maximum of 40 mA (absolute max), and the recommended operating current is 20 mA.
Possible consequences:
- The LED may burn out โ its maximum forward current (typically 20 mA) will be exceeded
- The GPIO pin on the ATMega328P could be damaged due to overcurrent
- In severe cases, the entire microcontroller could be permanently damaged
Note: Pin 13 on the Arduino UNO has a built-in resistor (1 Kฮฉ) connected to the onboard LED, which provides some protection. However, this is specific to pin 13 only โ all other pins require an external resistor (typically 220ฮฉโ330ฮฉ).
Formula: R = (V_supply โ V_LED) / I_LED = (5V โ 2V) / 0.015A = 200ฮฉ โ use 220ฮฉ (standard value).
Long Answer Questions (3 Questions)
๐ Q1: Describe the complete architecture of the Arduino UNO board. Explain the role of each major component with a block diagram. (10 marks)
Answer:
The Arduino UNO is an open-source microcontroller development board based on the ATMega328P. It provides a complete development environment with power management, USB communication, clock generation, and accessible I/O pins.
Major Components & Their Roles:
1. ATMega328P (Main Microcontroller): The brain of the Arduino UNO. An 8-bit AVR RISC microcontroller running at 16 MHz. Contains 32 KB Flash (program storage), 2 KB SRAM (runtime variables), 1 KB EEPROM (persistent data), a 10-bit ADC (6 channels), 3 timers, SPI, I2C, and UART interfaces. It executes the setup() and loop() functions that constitute your sketch.
2. USB-to-Serial Converter (ATMega16U2 / CH340G): Acts as a bridge between the USB port on your laptop and the UART serial interface (pins 0/TX and 1/RX) on the ATMega328P. Enables code uploading from the Arduino IDE and two-way serial communication via Serial Monitor. Official boards use ATMega16U2; clones typically use CH340G (requires separate driver installation).
3. Voltage Regulators (AMS1117): Two regulators provide stable 5V and 3.3V outputs from the 7โ12V DC jack input. The 5V line powers the ATMega328P and digital I/O. The 3.3V line is available on a header pin for 3.3V peripherals (e.g., some sensors and modules). When powered via USB, the 5V comes directly from the USB bus.
4. 16 MHz Crystal Oscillator: Provides the precise clock signal for the ATMega328P. Every instruction execution, timer tick, ADC conversion, and serial baud rate calculation depends on this 16 MHz reference. Connected to XTAL1 and XTAL2 pins of the MCU.
5. Digital I/O Pins (D0โD13): 14 digital pins that can be configured as INPUT or OUTPUT using pinMode(). 6 of these (3, 5, 6, 9, 10, 11) support PWM output. Pins D0 and D1 double as hardware serial (TX/RX). Pins D2 and D3 support external hardware interrupts (INT0, INT1).
6. Analog Input Pins (A0โA5): 6 analog input channels connected to the 10-bit ADC. Convert analog voltages (0โ5V) to digital values (0โ1023). Pins A4 and A5 double as I2C bus (SDA/SCL). All analog pins can also be used as digital I/O if needed.
7. Power Pins: Include 5V output, 3.3V output, GND (3 pins), Vin (raw input voltage), and IOREF. The RESET pin can be used to externally reset the board.
8. Status LEDs: ON LED (power indicator), L LED (connected to pin 13), TX LED (blinks during data transmission), RX LED (blinks during data reception).
9. Reset Button: Pulls the RESET pin of the ATMega328P low, restarting the program from setup(). Useful during debugging or when the program hangs.
10. ICSP Header: 6-pin header for In-Circuit Serial Programming. Used to program the ATMega328P directly (bypassing the bootloader) using an external programmer. Also used to reflash the ATMega16U2 firmware.
๐ Q2: Explain the complete pin mapping between Arduino UNO labels (D0โD13, A0โA5) and the ATMega328P physical pins. Include port registers, PWM capabilities, and special functions. (10 marks)
Answer:
The Arduino UNO simplifies pin naming by using labels like D0, D1, A0, etc. However, internally, these map to specific physical pins on the 28-pin ATMega328P DIP package. Each pin belongs to one of three 8-bit I/O ports: Port B (PB0โPB5), Port C (PC0โPC5), and Port D (PD0โPD7).
Digital Pins Mapping:
| Arduino | ATMega Pin | Port | PWM | Special Function |
|---|---|---|---|---|
| D0 | Pin 2 | PD0 | โ | Hardware Serial RX |
| D1 | Pin 3 | PD1 | โ | Hardware Serial TX |
| D2 | Pin 4 | PD2 | โ | External Interrupt INT0 |
| D3 | Pin 5 | PD3 | ~ | External Interrupt INT1, Timer2 OC2B |
| D4 | Pin 6 | PD4 | โ | Timer0 external T0 |
| D5 | Pin 11 | PD5 | ~ | Timer0 OC0B |
| D6 | Pin 12 | PD6 | ~ | Timer0 OC0A |
| D7 | Pin 13 | PD7 | โ | โ |
| D8 | Pin 14 | PB0 | โ | Timer1 Input Capture ICP1 |
| D9 | Pin 15 | PB1 | ~ | Timer1 OC1A |
| D10 | Pin 16 | PB2 | ~ | SPI Slave Select (SS) |
| D11 | Pin 17 | PB3 | ~ | SPI MOSI, Timer2 OC2A |
| D12 | Pin 18 | PB4 | โ | SPI MISO |
| D13 | Pin 19 | PB5 | โ | SPI SCK, Built-in LED |
Analog Pins Mapping:
| Arduino | ATMega Pin | Port | ADC Channel | Special Function |
|---|---|---|---|---|
| A0 | Pin 23 | PC0 | ADC0 | Also usable as Digital I/O |
| A1 | Pin 24 | PC1 | ADC1 | Also usable as Digital I/O |
| A2 | Pin 25 | PC2 | ADC2 | Also usable as Digital I/O |
| A3 | Pin 26 | PC3 | ADC3 | Also usable as Digital I/O |
| A4 | Pin 27 | PC4 | ADC4 | I2C SDA (Data line) |
| A5 | Pin 28 | PC5 | ADC5 | I2C SCL (Clock line) |
Port Register Architecture:
Each port has three 8-bit registers:
- DDRx (Data Direction Register) โ Sets each bit as INPUT (0) or OUTPUT (1). Equivalent to
pinMode(). - PORTx (Output Register) โ Sets the output value. Writing 1 = HIGH, 0 = LOW. Equivalent to
digitalWrite(). - PINx (Input Register) โ Reads the current pin state. Equivalent to
digitalRead().
Direct port manipulation (PORTD |= B00100000;) is approximately 50ร faster than digitalWrite() because it bypasses the Arduino library's pin lookup and safety checks. This is critical in time-sensitive IoT applications like driving LED matrices or reading high-speed sensors.
PWM Capabilities:
The 6 PWM pins are driven by 3 hardware timers:
- Timer 0 (8-bit): Drives pins 5 and 6 at ~980 Hz. Also used by
delay()andmillis(). - Timer 1 (16-bit): Drives pins 9 and 10 at ~490 Hz. Best for precise timing applications.
- Timer 2 (8-bit): Drives pins 3 and 11 at ~490 Hz. Used by the
tone()function.
๐ Q3: Design a complete Arduino project: "Smart Temperature Monitor with Alert System." Provide the circuit description, complete code, and explain each section. (10 marks)
Answer:
Project Overview:
Build a temperature monitoring system that reads an LM35 temperature sensor, displays the temperature on Serial Monitor, and activates a buzzer + red LED when temperature exceeds a threshold (e.g., 35ยฐC). A green LED stays ON when temperature is normal.
Components Required:
- Arduino UNO + USB cable
- LM35 temperature sensor
- 1ร Red LED + 220ฮฉ resistor
- 1ร Green LED + 220ฮฉ resistor
- 1ร Piezo buzzer
- Breadboard + jumper wires
Circuit Connections:
| Component | Pin Connection |
|---|---|
| LM35 Vcc | Arduino 5V |
| LM35 Output | Arduino A0 |
| LM35 GND | Arduino GND |
| Green LED (Anode) | Pin 3 via 220ฮฉ resistor |
| Red LED (Anode) | Pin 5 via 220ฮฉ resistor |
| Buzzer (+) | Pin 8 |
| All GNDs | Arduino GND (common ground) |
Complete Code:
Arduino C โ Smart Temperature Monitor /* * Smart Temperature Monitor with Alert System * Reads LM35 sensor, displays temp on Serial Monitor. * Green LED = normal. Red LED + Buzzer = overheat alert. */ // Pin definitions const int sensorPin = A0; // LM35 output connected to A0 const int greenLED = 3; // Green LED on pin 3 (PWM) const int redLED = 5; // Red LED on pin 5 (PWM) const int buzzerPin = 8; // Buzzer on pin 8 // Configuration const float THRESHOLD = 35.0; // Alert temperature in ยฐC // Variables int sensorValue = 0; float voltage = 0.0; float temperatureC = 0.0; void setup() { // Configure pin modes pinMode(greenLED, OUTPUT); pinMode(redLED, OUTPUT); pinMode(buzzerPin, OUTPUT); // Start serial communication Serial.begin(9600); Serial.println("=== Smart Temperature Monitor ==="); Serial.print("Alert Threshold: "); Serial.print(THRESHOLD); Serial.println(" ยฐC"); Serial.println("--------------------------------"); } void loop() { // Step 1: Read sensor sensorValue = analogRead(sensorPin); // Step 2: Convert to voltage (0-5V) voltage = sensorValue * (5.0 / 1023.0); // Step 3: Convert to temperature (LM35: 10mV per ยฐC) temperatureC = voltage * 100.0; // Step 4: Display on Serial Monitor Serial.print("Temp: "); Serial.print(temperatureC, 1); // 1 decimal place Serial.print(" ยฐC | ADC: "); Serial.print(sensorValue); // Step 5: Check threshold and activate alerts if (temperatureC >= THRESHOLD) { // ALERT MODE: Red LED ON, Green OFF, Buzzer ON digitalWrite(redLED, HIGH); digitalWrite(greenLED, LOW); digitalWrite(buzzerPin, HIGH); Serial.println(" โ ๏ธ ALERT! OVERHEAT!"); } else { // NORMAL MODE: Green LED ON, Red OFF, Buzzer OFF digitalWrite(greenLED, HIGH); digitalWrite(redLED, LOW); digitalWrite(buzzerPin, LOW); Serial.println(" โ Normal"); } // Step 6: Wait before next reading delay(2000); // Read every 2 seconds }
Code Explanation:
1. Pin Definitions: Using const int makes pin assignments readable and easy to change. Storing pin numbers in constants is best practice โ avoids "magic numbers" in code.
2. setup(): Configures all output pins and starts serial at 9600 baud. Prints a header to Serial Monitor so the user knows the system is running and what the threshold is.
3. ADC Reading: analogRead(A0) returns 0โ1023. The formula value ร (5.0 / 1023.0) converts this to voltage (0โ5V).
4. Temperature Conversion: The LM35 outputs 10 mV per ยฐC. So voltage ร 100 gives temperature in Celsius. At 25ยฐC, the LM35 outputs 250 mV (0.25V), and analogRead() returns approximately 51.
5. Decision Logic: A simple if-else compares the temperature against the threshold. In alert mode, the red LED and buzzer activate. In normal mode, only the green LED is on.
6. Timing: delay(2000) ensures the sensor is read every 2 seconds. For production use, this should be replaced with millis()-based non-blocking timing.
Expected Serial Monitor Output:
Chapter Summary
๐ Key Takeaways from Unit 2
โ The Arduino UNO is a development board based on the ATMega328P microcontroller (8-bit, 16 MHz, 32 KB Flash, 2 KB SRAM, 1 KB EEPROM).
โ
Every Arduino sketch has two mandatory functions: setup() (runs once) and loop() (runs forever).
โ
The Blink sketch is the "Hello World" of hardware โ uses pinMode(), digitalWrite(), and delay().
โ The UNO has 14 digital pins (D0โD13), 6 analog pins (A0โA5), and 6 PWM pins (3, 5, 6, 9, 10, 11).
โ
analogRead() converts 0โ5V to 0โ1023 (10-bit ADC). analogWrite() outputs PWM 0โ255 on ~ pins.
โ Common issues: wrong COM port, charge-only USB cables, missing CH340 drivers (for clone boards).
โ The ATMega328P has 28 pins in DIP package, with Port B, C, and D registers for I/O.
โ
Use millis() for non-blocking timing in real IoT projects instead of delay().
โ Arduino-based IoT prototyping is a marketable skill โ freelance projects range from โน3,000โโน20,000.
Earning Checkpoint โ Skills Audit
| Skill Learned | Tool / Platform | Portfolio Output | Earning Ready? |
|---|---|---|---|
| Arduino Board Setup | Arduino IDE + USB | โ | โฌ Foundation skill โ not billable alone |
| Blink Sketch | Arduino IDE | First working sketch | โฌ Foundation โ proves you can program hardware |
| Digital I/O (LED + Button) | Arduino + breadboard | Traffic Light Simulation | โ Yes โ can build LED displays for shops |
| Analog Sensor Reading | LM35 + LDR + Arduino | Smart Night Light | โ Yes โ IoT monitoring prototypes |
| Serial Communication | Serial Monitor + Arduino | Temperature data logger | โ Yes โ sensor data visualization |
| ATMega328 Architecture | Conceptual | โ | โ Yes โ interview ready (embedded roles) |
| Pin Mapping & PWM | Conceptual + hands-on | LED fade / motor control | โ Yes โ custom circuit design projects |
| Troubleshooting | Arduino IDE + drivers | Debug documentation | โ Yes โ technical support gigs |
What's Next โ Unit 3 Preview
๐ Coming Up in Unit 3: Sensors, Actuators & IoT Communication
Now that you can set up an Arduino and write basic sketches, Unit 3 dives deeper into the IoT ecosystem:
- Sensors: Temperature (DHT11/22), Ultrasonic (HC-SR04), PIR Motion, Gas (MQ-2), Soil Moisture
- Actuators: Servo motors, DC motors with L298N driver, Relay modules
- Communication: Wi-Fi with ESP8266/ESP32, Bluetooth with HC-05, MQTT protocol
- Cloud Integration: Sending sensor data to ThingSpeak, Blynk, and AWS IoT Core
- Real Projects: Smart Home automation, Weather Station, Intruder Detection System
Prerequisite: Complete all 3 labs in this chapter and have a working Blink + External LED circuit before moving on.
โ Unit 2 complete. Ready for Unit 3: Sensors, Actuators & IoT Communication!
[QR: Link to EduArtha video tutorial โ Arduino Setup & Hardware Basics]