IOT Lab 4
IOT Lab 4
IOT Lab 4
Connection Diagram:
Program:
void setup() {
pinMode(2, INPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
Serial.begin(9600);
}
Page No 1
IOT Lab 4 Enrollment No: IU2141050017
void loop()
{
int x = digitalRead(2);
if(x==HIGH){
Serial.println("Motion detected");
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
}
else
{
Serial.println("Motion not detected");
digitalWrite(5,LOW);
digitalWrite(4,LOW);
}
}
Function Description:
• Serial.begin(): Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with Serial Monitor, make sure to use one of the baud rates listed in the menu at the
bottom right corner of its screen. You can, however, specify other rates - for example, to communicate
over pins 0 and 1 with a component that requires a particular baud rate.
• Serial.println(): Prints data to the serial port as human-readable ASCII text followed by a carriage return
character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same
forms as Serial.print().
• digitalRead: digitalRead() is basically an in-built function used by Arduino to read values from sensors,
buttons, etc.
• pinMode: The pinMode() function is used to configure a specific pin to behave either as an input or an
output.
• digitalWrite: The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the
pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding
value.
• setup(): The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start
using libraries, etc. The setup() function will only run once, after each powerup or reset of the Arduino
board.
• loop(): After creating a setup() function, which initializes and sets the initial values, the loop() function
does precisely what its name suggests, and loops consecutively, allowing your program to change and
respond. Use it to actively control the Arduino board.
Faculty Signature
Page No 2
IOT Lab 4 Enrollment No: IU2141050017
Page No 3