Internet of Things Experiments

University : SPPU

Aim: Write a program using Arduino to control LED (One or more ON/OFF). Or Blinking

ObjectiveTo understand the basic principles of controlling LEDs using Arduino.
To learn how to write and upload code to an Arduino board.
To gain hands-on experience with circuit design and implementation.
OutcomeUpon completion of this assignment, students will be able to:
Control LEDs using Arduino.
Write and upload Arduino code to perform specific tasks (ON/OFF or blinking).
Explain the basic components of an electronic circuit involving LEDs.
Software RequirementsArduino IDE (Integrated Development Environment)  
Hardware RequirementsArduino board (e.g., Arduino Uno) One or more LEDs (e.g., Red, Green, Blue) Resistors (220Ω or 330Ω for each LED) Breadboard Jumper wires Power supply (if not using USB)
ProcedureConnect the longer leg (anode) of each LED to one end of a resistor. Connect the other end of the resistor to a digital pin on the Arduino (e.g., Pin 9 for LED1, Pin 10 for LED2). Connect the shorter leg (cathode) of each LED to the ground (GND) pin on the Arduino. Ensure that the Arduino is powered either through USB or an external power supply.
Code// Define pin numbers for the LEDs
const int led1 = 9; // LED 1 connected to digital pin 9
const int led2 = 10; // LED 2 connected to digital pin 10
void setup() {
// Initialize the digital pins as outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
// Turn LED 1 ON and LED 2 OFF
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(1000); // Wait for 1 second
// Turn LED 1 OFF and LED 2 ON
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(1000); // Wait for 1 second
}
Simulation Output
ConclusionIn this assignment, we successfully controlled one or more LEDs using an Arduino board. We learned how to set up a simple circuit, write the necessary code, and observe the output. This exercise provided a foundational understanding of how microcontrollers can interact with electronic components, paving the way for more complex projects in the future.

Aim: Create a program that illuminates the green LED if the counter is less than 100, illuminates the yellow LED if the counter is between 101 and 200 and illuminates the red LED if the counter is greater than 200

ObjectiveTo understand the basic principles of controlling LEDs using Arduino.
To learn how to write and upload code to an Arduino board.
To gain hands-on experience with circuit design and implementation.
OutcomeUpon completion of this assignment, students will be able to: Control LEDs using Arduino.
Write and upload Arduino code to perform specific tasks (ON/OFF or blinking).
Explain the basic components of an electronic circuit involving LEDs.
Software RequirementsArduino IDE (Integrated Development Environment)  
Hardware RequirementsArduino board (e.g., Arduino Uno) 3 LEDs (Green, Yellow, Red) 3 Resistors (220Ω or 330Ω for each LED) Breadboard Jumper wires
ProcedureGreen LED:Connect the anode (longer leg) of the Green LED to digital pin 9 through a resistor (220Ω or 330Ω).Connect the cathode (shorter leg) of the Green LED directly to the ground (GND) pin on the Arduino.Yellow LED:Connect the anode of the Yellow LED to digital pin 10 through a resistor.Connect the cathode of the Yellow LED directly to the ground.Red LED:Connect the anode of the Red LED to digital pin 11 through a resistor.Connect the cathode of the Red LED directly to the ground.
Code// Define pin numbers for the LEDs
const int greenLED = 11; // Green LED connected to digital pin 11
const int yellowLED = 9; // Yellow LED connected to digital pin 9
const int redLED = 6; // Red LED connected to digital pin 6
// Counter variable
int counter = 0;

void setup() {
// Initialize the digital pins as outputs
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
// Start the Serial communication
Serial.begin(9600); // Set the baud rate to 9600
}

void loop() {
// Increment the counter
counter++;
// Print the counter value to the Serial Monitor
Serial.print(“Counter: “);
Serial.println(counter);
// Check the value of the counter and illuminate the appropriate LED
if (counter < 100) {
digitalWrite(greenLED, HIGH); // Turn on Green LED
digitalWrite(yellowLED, LOW); // Turn off Yellow LED
digitalWrite(redLED, LOW); // Turn off Red LED
}
else if (counter >= 100 && counter <= 200) {
digitalWrite(greenLED, LOW); // Turn off Green LED
digitalWrite(yellowLED, HIGH); // Turn on Yellow LED
digitalWrite(redLED, LOW); // Turn off Red LED
}
else if (counter >200 && counter <=300) {
digitalWrite(greenLED, LOW); // Turn off Green LED
digitalWrite(yellowLED, LOW); // Turn off Yellow LED
digitalWrite(redLED, HIGH); // Turn on Red LED
}
else {
counter = 0; // Resetting counter variable
}
// Add a delay to slow down the counter increment
delay(10); // Adjust the delay as needed
}
Simulation Output
ConclusionThis program effectively demonstrates how to control multiple LEDs based on the value of a counter.

Aim: Create a program so that when the user enters ‘b’ the green light blinks, ‘g’ the green light is illuminated ‘y’ the yellow light is illuminated and ‘r’ the red light is illuminated

ObjectiveTo understand the basic principles of controlling LEDs using Arduino.
To learn how to write and upload code to an Arduino board.
To gain hands-on experience with circuit design and implementation.
OutcomeUpon completion of this assignment, students will be able to:
Control LEDs using Arduino.
Write and upload Arduino code to perform specific tasks
Explain the basic components of an electronic circuit involving LEDs.
Software RequirementsArduino IDE (Integrated Development Environment)  
Hardware RequirementsArduino board (e.g., Arduino Uno) 3 LEDs (Green, Yellow, Red) 3 Resistors (220Ω or 330Ω for each LED) Breadboard Jumper wires
ProcedureGreen LED:Connect the anode (longer leg) of the Green LED to digital pin 11 through a resistor (220Ω or 330Ω).Connect the cathode (shorter leg) of the Green LED directly to the ground (GND) pin on the Arduino.Yellow LED:Connect the anode of the Yellow LED to digital pin 9 through a resistor.Connect the cathode of the Yellow LED directly to the ground.Red LED:Connect the anode of the Red LED to digital pin 6 through a resistor.Connect the cathode of the Red LED directly to the ground.
Code// Define pin numbers for the LEDs
const int greenLED = 11; // Green LED connected to digital pin 11
const int yellowLED = 9; // Yellow LED connected to digital pin 9
const int redLED = 6; // Red LED connected to digital pin 6
// Counter variable
int counter = 0;

void setup() {
// Initialize the digital pins as outputs
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
// Start the Serial communication
Serial.begin(9600); // Set the baud rate to 9600
Serial.println(“Enter ‘b’ to blink green LED, ‘g’ for green, ‘y’ for yellow, ‘r’ for red.”);
}
void loop() {
// Check if data is available to read
if (Serial.available() > 0) {
char input = Serial.read(); // Read the input character
// Control LEDs based on user input

switch (input) {
case ‘b’: // Blink green LED for (int i = 0; i < 5; i++) { // Blink 5 times
digitalWrite(greenLED, HIGH); // Turn on Green LED
delay(500); // Wait for 500 milliseconds
digitalWrite(greenLED, LOW); // Turn off Green LED
delay(500); // Wait for 500 milliseconds }
break;

case ‘g’: // Illuminate green LED
digitalWrite(greenLED, HIGH); // Turn on Green LED
digitalWrite(yellowLED, LOW); // Turn off Yellow LED
digitalWrite(redLED, LOW); // Turn off Red LED
break;

case ‘y’: // Illuminate yellow LED
digitalWrite(greenLED, LOW); // Turn off Green LED
digitalWrite(yellowLED, HIGH); // Turn on Yellow LED
digitalWrite(redLED, LOW); // Turn off Red LED
break;

case ‘r’: // Illuminate red LED
digitalWrite(greenLED, LOW); // Turn off Green LED
digitalWrite(yellowLED, LOW); // Turn off Yellow LED
digitalWrite(redLED, HIGH); // Turn on Red LED break;

default: // Turn off all LEDs if an invalid character is entered
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
}

}
}
Simulation Output
ConclusionThis program effectively demonstrates how to control multiple LEDs based on the user input.

Aim: Write a program that asks the user for a number and outputs the number squared that is entered.

ObjectiveTo understand how to interface an Arduino with a serial monitor. To learn how to read user input from the serial port. To perform basic arithmetic operations using Arduino. To display the output back to the user through the serial monitor.
OutcomeUpon completion of this assignment, students will be able to: Write and upload a program to an Arduino board. Use the serial monitor for input and output operations. Understand basic programming concepts such as variables, data types, and arithmetic operations.
Software RequirementsArduino IDE (Integrated Development Environment)  
Hardware RequirementsArduino board (e.g., Arduino Uno)
ProcedureSetup the Arduino Environment:Install the Arduino IDE on your computer if it is not already installed.Connect the Arduino board to your computer using the USB cable.Open the Arduino IDE:Launch the Arduino IDE on your computer.In the Arduino IDE, create a new sketch (File > New).Write the code provided in the “Code” section below.Upload the Program:Select the correct board and port from the Tools menu.Click on the upload button (right arrow icon) to upload the program to the Arduino.Open the Serial Monitor:After uploading, open the Serial Monitor (Tools > Serial Monitor) to interact with the program.Set the baud rate to 9600 bps.Test the Program:Enter a number in the input field of the Serial Monitor and press “Enter.”Observe the output displayed in the Serial Monitor, which will show the squared value of the entered number.
Codevoid setup() {
// Initialize serial communication at 9600 bits per second
Serial.begin(9600);
Serial.println(“Enter a number:”);
}
void loop() {
// Check if data is available to read
if (Serial.available() > 0) {
// Read the input from the serial monitor
String input = Serial.readStringUntil(‘\n’);
// Convert the input to an integer
int number = input.toInt();
// Calculate the square of the number
int squared = number * number;
// Output the result to the serial monitor
Serial.print(“The square of “);
Serial.print(number);
Serial.print(” is “);
Serial.println(squared);
// Prompt the user for another number
Serial.println(“Enter another number:”);
}
}
Simulation Output
ConclusionIn this lab, we successfully created a simple Arduino program that interacts with the user through the serial monitor. By entering a number, the user can see the squared value of that number displayed back to them. This exercise not only reinforces basic programming concepts but also demonstrates how to use Arduino for simple input and output operations, laying the groundwork for more complex Internet of Things applications.

Aim: Write a program to control the color of the LED by turning 3 different potentiometers. One will be read for the value of Red, one for the value of Green, and one for the value of Blue

ObjectiveTo understand how to interface potentiometers with an Arduino.
To learn how to control an RGB LED using PWM (Pulse Width Modulation).
To explore the concept of analog input and output in Arduino.
To develop skills in reading analog values and mapping them to PWM signals.
OutcomeUpon completion of this assignment, students will be able to:
Interface multiple potentiometers with an Arduino.
Control the brightness of an RGB LED using PWM signals.
Understand how to read and process analog input values.
Software RequirementsArduino IDE (Integrated Development Environment)  
Hardware RequirementsArduino board (e.g., Arduino Uno, Arduino Nano)
Common cathode RGB LED
3 Potentiometers (10kΩ recommended)
3 Resistors (220Ω for each color channel of the RGB LED)
Breadboard and jumper wires USB cable to connect the Arduino to the computer
ProcedureSetup the Arduino Environment:
Install the Arduino IDE on your computer if it is not already installed.
Connect the Arduino board to your computer using the USB cable.Wiring the Circuit:Connect the RGB LED to the Arduino:The longest leg (cathode) of the RGB LED goes to GND.Connect the Red leg to a PWM-capable pin (e.g., pin 9) through a 220Ω resistor.Connect the Green leg to another PWM-capable pin (e.g., pin 10) through a 220Ω resistor.Connect the Blue leg to another PWM-capable pin (e.g., pin 11) through a 220Ω resistor.Connect the potentiometers:Connect one terminal of each potentiometer to 5V.Connect the other terminal of each potentiometer to GND.Connect the middle terminal (wiper) of the first potentiometer to an analog pin (e.g., A0 for Red), the second to A1 (for Green), and the third to A2 (for Blue).Launch the Arduino IDE on your computer.In the Arduino IDE, create a new sketch (File > New).Write the code provided in the “Code” section below.Select the correct board and port from the Tools menu.Click on the upload button (right arrow icon) to upload the program to the Arduino.Test the Program: After uploading, turn the potentiometers and observe the RGB LED change colors based on the potentiometer values.
Code// Define the pins for the RGB LED
const int redPin = 9;    // PWM pin for Red
const int greenPin = 10; // PWM pin for Green
const int bluePin = 11;  // PWM pin for Blue  

// Define the pins for the potentiometers
const int potRedPin = A0;   // Potentiometer for Red
const int potGreenPin = A1; // Potentiometer for Green
const int potBluePin = A2;  // Potentiometer for Blue  

void setup() {  
// Set the RGB LED pins as output  
pinMode(redPin, OUTPUT);  
pinMode(greenPin, OUTPUT);  
pinMode(bluePin, OUTPUT);
}  

void loop() {  
// Read the values from the potentiometers  
int redValue = analogRead(potRedPin);  
int greenValue = analogRead(potGreenPin);  
int blueValue = analogRead(potBluePin);    
// Map the potentiometer values (0-1023) to PWM values (0-255)  
redValue = map(redValue, 0, 1023, 0, 255);  
greenValue = map(greenValue, 0, 1023, 0, 255);  
blueValue = map(blueValue, 0, 1023, 0, 255);    
// Set the color of the RGB LED  
analogWrite(redPin, redValue);  
analogWrite(greenPin, greenValue);  
analogWrite(bluePin, blueValue);    
// Small delay for stability  
delay(10);
}  
Simulation Output
ConclusionIn this lab, we successfully created a program that allows them to control the color of an RGB LED using three potentiometers. By adjusting the potentiometers, we can manipulate the intensity of each color channel (Red, Green, and Blue), resulting in a wide range of colors being displayed by the RGB LED. This exercise enhances understanding of analog input and output, as well as the practical application of PWM in controlling LED brightness, which is fundamental in various Internet of Things projects.