IOT Lab Manuals

EXPERIMENT 1: INTRODUCTION TO ARDUINO IDE Aim: To study Arduino IDE, setup(), loop(), serial communication, analog input, and user- defined functions. Components: Arduino Uno, USB cable, potentiometer, jumper wires. Theory: Arduino IDE is used to write, compile, and upload programs. setup() runs once, while loop() runs repeatedly. Serial communication exchanges data with the computer. Program: void setup() { Serial.begin(9600); greet(); } void loop() { int value = analogRead(A0); Serial.print("Analog Value: "); Serial.println(value); if (Serial.available() > 0) { char ch = Serial.read(); Serial.print("Received: "); Serial.write(ch); Serial.println(); } delay(500); } void greet() { Serial.println("Welcome to Arduino Lab"); } Procedure: 1. Connect Arduino Uno to the computer. 2. Select the board and port in Arduino IDE. 3. Upload the program and open Serial Monitor at 9600 baud. 4. Rotate the potentiometer and send characters. Result: Arduino IDE, serial functions, analog input, and user-defined functions were demonstrated. Viva Questions and Answers 1. What is Arduino IDE? Software for programming Arduino boards. 2. What is setup ()? The function executed once at startup or reset. 3. What is loop ()? The function that repeatedly executes the main program. 4. What is Serial. Begin (9600)? It initializes serial communication at 9600 baud. 5. What is Serial. Print ()? It sends readable text or numeric data. 6. What is Serial. Available ()? It returns the number of received bytes waiting in the buffer. 7. What is Serial. Read ()? It reads one received byte. 8. What is Serial. Write ()? It transmits raw byte data. 9. What is analog Read(A0)? It reads the analog voltage on pin A0. EXPERIMENT 2: DATA TYPES, VARIABLES, CONSTANTS AND OPERATORS Aim: To demonstrate data types, variables, constants, and operators. Components: Arduino Uno and USB cable. Program: const int a = 10; int b = 5; float x = 2.5; char grade = 'A'; bool status = true; void setup() { Serial.begin(9600); Serial.println(a + b); Serial.println(a > b); Serial.println(a % b); Serial.println(a > 0 && status); Serial.println(x); Serial.println(grade); } void loop() { } Procedure: Upload the program and observe the output in Serial Monitor. Result: Data types, variables, constants, and arithmetic, relational, and logical operators were demonstrated. Viva Questions and Answers 1. What is a variable? A named memory location used to store data. 2. What is a constant? A value that cannot be reassigned when declared using const. 3. Name common data types. int, float, char, bool, and long. 4. What are arithmetic operators? +, -, *, /, and %. 5. What are relational operators? >, <, ==, and !=. 6. What are logical operators? &&, ||, and !. EXPERIMENT 3: IF STATEMENTS, SWITCH CASE, LOOPS AND ARRAYS Aim: To demonstrate conditional statements, loops, and arrays. Components: Arduino Uno and USB cable. Program: int numbers[] = {10, 20, 30, 40, 50}; void setup() { Serial.begin(9600); for (int i = 0; i < 5; i++) { if (numbers[i] >= 30) Serial.println(numbers[i]); } int choice = 2; switch (choice) { case 1: Serial.println("One"); break; case 2: Serial.println("Two"); break; default: Serial.println("Other"); } int j = 0; while (j < 3) { Serial.println(j); j++; } } void loop() { } Procedure: Upload the program and observe the values displayed in Serial Monitor. Result: if, switch, for, while, and arrays were demonstrated. Viva Questions and Answers 1. What is an if statement? It executes code when a condition is true. 2. What is switch-case? A selection statement that chooses a branch based on an expression. 3. What is a loop? A structure that repeats instructions. 4. What is an array? A collection of elements of the same data type. 5. What is the first array index? Zero. 6. What is the purpose of break? It exits a loop or switch statement. EXPERIMENT 4: STRINGS, TIME FUNCTIONS AND RANDOM NUMBERS Aim: To demonstrate strings, String objects, time-based functions, and random number generation. Components: Arduino Uno and USB cable. Program: String message = "Arduino Lab"; void setup() { Serial.begin(9600); randomSeed(analogRead(A0)); } void loop() { Serial.println(message); Serial.println(message.length()); Serial.print("Random Number: "); Serial.println(random(1, 101)); Serial.print("Elapsed Time (ms): "); Serial.println(millis()); delay(1000); } Procedure: Upload the program and observe the string, its length, random numbers, and elapsed time. Result: String operations, time functions, and random number generation were demonstrated. Viva Questions and Answers 1. What is a string? A sequence of characters representing text. 2. What is a String object? An object providing text operations such as length(). 3. What does millis() return? Milliseconds elapsed since the program started. 4. What does delay(1000) do? Pauses execution for approximately one second. 5. What does random(1, 101) generate? A pseudo-random integer from 1 to 100. 6. What is randomSeed() used for? It initializes the pseudo-random number generator. EXPERIMENT 5: DIGITAL AND ANALOG I/O FUNCTIONS Aim: To demonstrate digital input/output, analog input, and PWM output. Components: Arduino Uno, LED, 220-ohm resistor, push button, potentiometer. Connections: Button between pin 2 and GND; LED to pin 9 through a resistor; potentiometer wiper to A0. Program: const int led = 9; const int button = 2; void setup() { pinMode(led, OUTPUT); pinMode(button, INPUT_PULLUP); Serial.begin(9600); } void loop() { int value = analogRead(A0); int pressed = digitalRead(button); analogWrite(led, value / 4); Serial.print("Analog Value: "); Serial.println(value); Serial.println(pressed == LOW ? "Pressed" : "Released"); delay(200); } Procedure: Rotate the potentiometer to change LED brightness and press the button to observe its state. Result: Digital input, analog input, and PWM output were demonstrated. Viva Questions and Answers 1. What is digital input? Reading a HIGH or LOW logic level. 2. What is analog input? Measuring a varying voltage using an ADC. 3. What is pinMode()? It configures a pin as input or output. 4. What is digitalWrite()? It sets a digital output HIGH or LOW. 5. What is PWM? Pulse-width modulation, used to control average output power. 6. What is the Arduino Uno ADC range? Normally 0–1023 with its default 10-bit ADC. EXPERIMENT 6: LED, SEVEN-SEGMENT DISPLAY, BUTTON AND SWITCH Aim: To interface an LED, seven-segment display, push button, and switch. Components: Arduino Uno, LED, common-cathode seven-segment display, push button, resistors. Connections: LED to pin 13 through a resistor; button between pin 2 and GND; segments a–g to pins 3–9 through individual resistors. Program: const int button = 2; const int led = 13; const int seg[] = {3, 4, 5, 6, 7, 8, 9}; const byte digit0[] = {1,1,1,1,1,1,0}; const byte digit1[] = {0,1,1,0,0,0,0}; void setup() { pinMode(button, INPUT_PULLUP); pinMode(led, OUTPUT); for (int i = 0; i < 7; i++) pinMode(seg[i], OUTPUT); } void loop() { bool pressed = digitalRead(button) == LOW; digitalWrite(led, pressed); for (int i = 0; i < 7; i++) digitalWrite(seg[i], pressed ? digit1[i] : digit0[i]); } Procedure: Press and release the button. Observe the LED and the display changing between 0 and 1. Result: LED, button, and seven-segment display interfacing were demonstrated. Viva Questions and Answers 1. What is an LED? A light-emitting diode. 2. Why is a resistor used with an LED? To limit current. 3. What is a seven-segment display? A display made of seven controllable LED segments. 4. What is INPUT_PULLUP? It enables the internal pull-up resistor. 5. What is switch debouncing? Removing false transitions caused by mechanical contact bounce. EXPERIMENT 7: INTERRUPTS AND UART COMMUNICATION Aim: To demonstrate external interrupts and UART communication. Components: Arduino Uno, push button, USB cable. Connections: Push button between pin 2 and GND. Program: volatile bool eventFlag = false; void buttonISR() { eventFlag = true; } void setup() { pinMode(2, INPUT_PULLUP); pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); attachInterrupt( digitalPinToInterrupt(2), buttonISR, FALLING ); } void loop() { if (eventFlag) { noInterrupts(); eventFlag = false; interrupts(); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); Serial.println("Interrupt received"); } } Procedure: Upload the program and press the button. Observe the LED and Serial Monitor. Result: External interrupt handling and UART communication were demonstrated. Viva Questions and Answers 1. What is an interrupt? An event that temporarily interrupts normal program execution. 2. What is an ISR? An Interrupt Service Routine. 3. Why is volatile used? It indicates that a variable can change unexpectedly. 4. What is UART? Universal Asynchronous Receiver/Transmitter. 5. What is baud rate? The signaling rate used in serial communication. 6. What does FALLING mean? The signal changes from HIGH to LOW. EXPERIMENT 8: I²C COMMUNICATION PROTOCOL Aim: To demonstrate I²C communication between two Arduino Uno boards. Components: Two Arduino Uno boards and jumper wires. Connections: A4 (SDA) to A4, A5 (SCL) to A5, and GND to GND. Master Program: #include void setup() { Wire.begin(); } void loop() { Wire.beginTransmission(8); Wire.write('A'); Wire.endTransmission(); delay(1000); } Slave Program: #include volatile char received = 0; void receiveEvent(int count) { while (Wire.available()) received = Wire.read(); } void setup() { Serial.begin(9600); Wire.begin(8); Wire.onReceive(receiveEvent); } void loop() { char c; noInterrupts(); c = received; received = 0; interrupts(); if (c != 0) { Serial.print("Received: "); Serial.println(c); } } Procedure: Upload the master and slave programs to separate boards. Connect SDA, SCL, and GND. Open the slave Serial Monitor at 9600 baud. Result: I²C communication between a master and a slave was demonstrated. Viva Questions and Answers 1. What is I²C? Inter-Integrated Circuit, a synchronous serial protocol. 2. What are SDA and SCL? Serial Data and Serial Clock. 3. How many main signal lines does I²C use? Two. 4. What is the function of Wire.h? It provides Arduino I²C communication functions. 5. Can multiple devices share an I²C bus? Yes, using compatible wiring and device addresses. EXPERIMENT 9: SPI COMMUNICATION PROTOCOL Aim: To demonstrate SPI communication between a master and a slave device. Components: Arduino Uno, SPI-compatible slave device, jumper wires. Connections: MOSI to pin 11, MISO to pin 12, SCK to pin 13, SS/CS to pin 10, and common GND. Master Program: #include const int ssPin = 10; void setup() { pinMode(ssPin, OUTPUT); digitalWrite(ssPin, HIGH); SPI.begin(); } void loop() { digitalWrite(ssPin, LOW); SPI.transfer('A'); digitalWrite(ssPin, HIGH); delay(1000); } Procedure: Upload the master program, connect the SPI lines to a compatible slave, and configure the slave to receive data. Result: SPI master transmission was demonstrated. Viva Questions and Answers 1. What is SPI? Serial Peripheral Interface. 2. What are the SPI signal lines? MOSI, MISO, SCK, and SS/CS. 3. What is MOSI? Master Out, Slave In. 4. What is MISO? Master In, Slave Out. 5. What is the function of SCK? It provides the serial clock. 6. Does the master program configure a second Arduino as a slave? No. Separate slave firmware is required. EXPERIMENT 10: POTENTIOMETER, TEMPERATURE SENSOR AND PIR SENSOR Aim: To interface a potentiometer, TMP36 temperature sensor, and PIR sensor. Components: Arduino Uno, potentiometer, TMP36, PIR module. Connections: Potentiometer wiper to A0, TMP36 output to A1, PIR output to pin 2. Program: void setup() { Serial.begin(9600); pinMode(2, INPUT); } void loop() { int pot = analogRead(A0); int raw = analogRead(A1); float voltage = raw * (5.0 / 1023.0); float tempC = (voltage - 0.5) * 100.0; int motion = digitalRead(2); Serial.print("Potentiometer: "); Serial.println(pot); Serial.print("Temperature C: "); Serial.println(tempC); Serial.print("PIR: "); Serial.println(motion); delay(1000); } Procedure: Rotate the potentiometer, observe temperature readings, and move in front of the PIR sensor after its stabilization period. Result: Analog sensing, temperature measurement, and motion detection were demonstrated. Viva Questions and Answers 1. What is a potentiometer? A variable resistor that provides adjustable voltage. 2. What is a temperature sensor? A device that measures temperature. 3. What is a PIR sensor? A passive infrared sensor that detects changes in infrared radiation. 4. What is the ADC resolution of Arduino Uno? 10 bits. 5. Which temperature sensor does the program use? TMP36. 6. Does the TMP36 conversion apply to every temperature sensor? No. Different sensors require different conversion formulas. . EXPERIMENT 11: IR SENSOR, ULTRASONIC SENSOR, ACCELEROMETER AND PWM Aim: To interface IR and ultrasonic sensors, demonstrate PWM, and study accelerometer interfacing. Components: Arduino Uno, IR obstacle sensor, HC-SR04, MPU6050, LED, resistor. Connections:  IR output: pin 2  HC-SR04 TRIG: pin 7  HC-SR04 ECHO: pin 8  LED through resistor: pin 9  MPU6050 SDA: A4  MPU6050 SCL: A5 Program for IR, ultrasonic and PWM: void setup() { pinMode(2, INPUT); pinMode(7, OUTPUT); pinMode(8, INPUT); pinMode(9, OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(7, LOW); delayMicroseconds(2); digitalWrite(7, HIGH); delayMicroseconds(10); digitalWrite(7, LOW); long duration = pulseIn(8, HIGH, 30000); if (duration > 0) { float distance = duration * 0.0343 / 2.0; Serial.print("Distance (cm): "); Serial.println(distance); analogWrite(9, distance < 50 ? 255 : 50); } else { Serial.println("No echo"); analogWrite(9, 0); } Serial.print("IR output: "); Serial.println(digitalRead(2)); delay(200); } Procedure: Place objects at different distances, observe the readings, and check the LED brightness. For accelerometer measurements, connect the MPU6050 over I²C and use a compatible library or register-reading program. Result: IR sensing, ultrasonic distance measurement, and PWM output were demonstrated. Viva Questions and Answers 1. What is an IR sensor? A sensor that uses infrared radiation to detect objects or changes. 2. How does an ultrasonic sensor measure distance? By measuring echo travel time. 3. What is an accelerometer? A sensor that measures acceleration along one or more axes. 4. What is PWM? Pulse-width modulation. 5. What is the MPU6050? A module containing a three-axis accelerometer and gyroscope. 6. Which Arduino pins are used for I²C on Uno? A4 for SDA and A5 for SCL. EXPERIMENT 12: SERVO, STEPPER AND DC MOTOR Aim: To interface servo, stepper, and DC motors with Arduino. Components: Arduino Uno, servo motor, stepper motor and driver, DC motor and driver, external supply. Connections: Servo signal to pin 9; stepper driver inputs to pins 4–7; DC motor driver inputs to pins 2 and 3. Connect grounds together. Program: #include #include Servo servo; Stepper stepper(200, 4, 5, 6, 7); void setup() { servo.attach(9); stepper.setSpeed(30); pinMode(2, OUTPUT); pinMode(3, OUTPUT); } void loop() { servo.write(0); delay(500); servo.write(90); delay(500); servo.write(180); delay(500); stepper.step(200); digitalWrite(2, HIGH); digitalWrite(3, LOW); delay(2000); digitalWrite(2, LOW); digitalWrite(3, LOW); delay(1000); } Procedure: Connect each motor through its appropriate driver, upload the program, and observe the motion. Test motors individually if required. Result: Servo positioning, stepper rotation, and DC motor control were demonstrated. Viva Questions and Answers 1. What is a servo motor? A motor used for controlled angular positioning. 2. What is a stepper motor? A motor that rotates in discrete steps. 3. What is a DC motor? A motor that converts electrical energy into rotational motion. 4. Why is a motor driver needed? To provide the current required by the motor. 5. How is DC motor direction reversed? By reversing the voltage polarity across its terminals, usually with an H-bridge. 6. Why should motors not be connected directly to Arduino pins? The pins cannot safely supply most motors' operating current.