0% found this document useful (0 votes)
7 views85 pages

Practical

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views85 pages

Practical

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Het Patel 20031101611039

RASHTRIYA RAKSHA UNIVERSITY


Pioneering National Security and Police University of India
An institute of national importance.
National Security is Supreme!

Lavad – Dahegam, Gandhinagar – 382305

School of Information Technology,


Artificial Intelligence & Cyber Security (SITAICS)

B.Tech in Computer Science & Engineering with


specialization in cyber security
6 Semester
th

Internet Of Things Lab File

Name: Patel Hetkumar Arvindkumar


Enrollment Number: 200031101611039
Submitted To: Dhaval Deshkar Sir
Het Patel 20031101611039

Experiment 1

 Blink LED light.


Het Patel 20031101611039

CODE:

void setup()
{
pinMode(13, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

 Setup: The Setup( ) function is called when program starts.Use it to


initialize variables , pin , mod , start using libraries , etc.The Setup( )
function runs only once ,after each power up or reset of Ardiuno
board.

 Loop: The loop ( ) function does precisely what its name suggests ,
and loop consecutively , allowing program to change and respond.It’s
used to actively control the Ardiuno board.

 pinMode: Configures the specified pin to behave either as an input


or an output. See the Digital Pins page for details on the functionality
of the pins.

 As of Arduino 1.0.1, it is possible to enable the internal


pullup resistors with the model INPUT_PULLUP. Additionally, the
INPUT mode explicitly disables the internal pullups.

 digitalWrite: Write a HIGH or a LOW value to a digital pin.


Het Patel 20031101611039

 delay: Pauses the program for the amount of time (in


milliseconds)specified as parameter.

 To limit the current through externally connected LED to 20mA,


calculate the minimum value of series resistor that you may want to
use.

➔ To calculate the minimum value of the series resistor


required to limit the current through an externally connected
LED to 20mA, you'll need to know the forward voltage drop
(Vf) of the LED and the supply voltage (Vs) of your Arduino.

➔ Let's assume the forward voltage drop of the LED is 2 volts


and the supply voltage of your Arduino is 5 volts. The
voltage across the series resistor (Vr) can be calculated as:
Vr = Vs - Vf Vr = 5V - 2V Vr = 3V

➔ Ohm's Law states that the current (I) flowing through a


resistor is equal to the voltage across the resistor divided by
its resistance (R):
I = Vr / R

➔ Rearranging the equation to solve for the resistance, we


have:
R = Vr / I R = 3V / 0.02A R = 150 ohms

➔ Therefore, to limit the current through the LED to 20mA, you


would need a minimum value of a 150-ohm resistor in series
with the LED.
Het Patel 20031101611039

Experiment 2

 Modify the program in Experiment No. 1 to generate


20%duty cycle for LED to remain turn ON.
That means, the LED will be OFF for 80% of time
and ON for 20% of time.
Het Patel 20031101611039

CODE:

void setup() {
pinMode(13,OUTPUT);
}
void loop () {
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(4000);
}
Het Patel 20031101611039

Experiment 3

 Modify and update hardware, without changing the


program of experiment No. 2, such that two LEDs behave
in following way.

LED 1: will be OFF for 80% of time and ON for 20% of time.
LED 2: will be OFF for 20% of time and ON for 80% of time.
Het Patel 20031101611039

CODE:

void setup() {
pinMode(13,OUTPUT);
pinMode(7 ,OUTPUT);
}
void loop () {
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(4000);
Het Patel 20031101611039

digitalWrite(7,HIGH);
delay(4000);
digitalWrite(7,LOW);
delay(1000);
}

Experiment 4

 Fading of LED light.


Het Patel 20031101611039

CODE:

int led = 9;
int brightness = 0;
Het Patel 20031101611039

int fadeAmount = 5;

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}

delay(30);
}

 analogWrite : Writes an analog value (PWM wave) to a pin. Can be


used to light a LED at varying brightnesses or drive a motor at
various speeds. After a call to analogWrite(), the pin will generate a
steady rectangular wave of the specified duty cycle until the next call
to analogWrite() (or a call to digitalRead() or digitalWrite()) on the
same pin.

 Explain following code:


if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}

➔ In the given code snippet, the conditional statement checks whether


the brightness value is less than or equal to 0 or greater than or equal
to 255. If the condition is true, it means that the brightness value has
reached either the minimum or maximum limit.
Het Patel 20031101611039

➔ When this condition is true, the variable fadeAmount is assigned the


negative value of itself (-fadeAmount). This indicates a change in the
direction of the fade effect.
➔ Typically, this code is used in a fade effect where the brightness of an
LED gradually increases or decreases. When the brightness reaches
the minimum or maximum limit, the fade effect changes direction to
create a continuous fading pattern.

 On which output pins of Arduino UNO can we run program for fading
LED? Explain.
➔ On an Arduino Uno, you can run a program for fading an LED on any
of the digital pins marked with a "~" symbol (e.g., pins 3, 5, 6, 9, 10,
and 11). These pins support Pulse Width Modulation (PWM), which
allows you to control the intensity or brightness of an LED.

Experiment 5

 Write a program to blink LED on Pin 13 and Fade the LED


on Pin 9 simultaneously.
Het Patel 20031101611039

CODE:

const int blinkLED = 13;


const int fadeLED = 9;
Het Patel 20031101611039

int led = 9;
int brightness = 0;
int fadeAmount = 5;

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
digitalWrite(blinkLED, HIGH);
delay(500);
digitalWrite(blinkLED, LOW);
delay(500);
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}

Experiment 6

 Write a program to blink LED on Pins 13 , 11, 9 and 7


simultaneously every second.
Het Patel 20031101611039

CODE:

const int ledPins[] = {13, 11, 9, 7};


const int numLEDs = sizeof(ledPins) / sizeof(ledPins[0]);
Het Patel 20031101611039

void setup() {
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], HIGH);
}
delay(500);
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], LOW);
}
delay(500);
}

Experiment 7

(1) to turn on LED on Pins 13 , 11, 9 and 7 one after


another with delay of 2 second. (This means, first LED
Het Patel 20031101611039

on pin 13 turns on. After 2 second delay, second LED


on pin 11 turns on. This continues till all four LEDs turn
ON)
(2) After all four LEDs are on, turn all of them OFF
together.
(3) Repeat above steps continuously.
Het Patel 20031101611039

CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(11, HIGH);
delay(2000);
digitalWrite(9, HIGH);
delay(2000);
digitalWrite(7, HIGH);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9, LOW);
digitalWrite(7, LOW);
delay(2000);
}
Het Patel 20031101611039

Experiment 8

Assume four LEDs are connected to Pins 13, 11, 9 and 7 of Arduino
UNO. Write a
program to turn on one LED at a time.

Second 1: LED on Pin 13 turns on, all other LEDs are off
Second 2: LED on Pin 13 turns off, LED on pin 11 turns on, other
LEDs are off.
Second 3: LED on Pin 11 turns off, LED on pin 9 turns on, other LEDs
are off
Second 4: LED on Pin 9 turns off, LED on pin 7 turns on, other LEDs
are off.
Second 5: LED on Pin 7 turns off, LED on pin 13 turns on, other LEDs
are off.

Use delay of 2 Seconds between each transactions.


Het Patel 20031101611039
Het Patel 20031101611039

CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9 , OUTPUT);
pinMode(7 , OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, HIGH);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9 , HIGH);
digitalWrite(7 , LOW);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , HIGH);
delay(2000);
digitalWrite(13, HIGH);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
delay(2000);
}
Het Patel 20031101611039

Experiment 9

1. Use IDE program: File > Examples > Digital > Button
2. Connect external LED on Pin 13, use current limiting resister.
3. Connect push button switch on Pin 2 of Arduino UNO.
Connect a register in series with switch.
4. Run program mentioned in step 1 and observe that when switch is
pressed, the LED turns off.
Het Patel 20031101611039

➔ When switch is press LED turns OFF.


Het Patel 20031101611039

CODE:
const int buttonPin = 2;
const int ledPin = 13;

int buttonState = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}

 How will you generate two states (Logic low and logic high) using
push button switch?

➔ To generate two states (logic low and logic high) using a push-button
switch with an Arduino, you can use the internal pull-up resistor and
implement a simple button state detection code. Here's a basic
example:
Het Patel 20031101611039

1) Connect one terminal of the push-button switch to a digital pin on the


Arduino (e.g., Pin 2).

2) Connect the other terminal of the push-button switch to ground


(GND).

3) In your Arduino sketch, set the pinMode for the digital pin connected
to the push-button switch as INPUT_PULLUP. This activates the
internal pull-up resistor, which will pull the pin to a HIGH logic level
when the button is not pressed.

4) Use a digitalRead() function to read the state of the digital pin


connected to the push-button switch.

5) Based on the state of the pin, you can perform certain actions or
change the state of other components.

 digitalRead : Reads the value from a specified digital pin, either


HIGH or LOW.
Syntax : digitalRead(pin)

➔ Schematic view of circuit :


Het Patel 20031101611039

Experiment 10

 Modify the program of such that the LED turns on only


when the switch is pressed.
Het Patel 20031101611039

➔ Press switch and all LED turn’s ON.


Het Patel 20031101611039

CODE:

void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9 , OUTPUT);
pinMode(7 , OUTPUT);
pinMode(4 , INPUT_PULLUP);
}
void loop()
{
byte buttonState = digitalRead(4);

if (buttonState == LOW) {
digitalWrite(13, HIGH);
digitalWrite(11, HIGH);
digitalWrite(9 , HIGH);
digitalWrite(7 , HIGH);
}
else {
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
}
}
Het Patel 20031101611039

Experiment 11

(a) Measure values of different resistors.


Het Patel 20031101611039

(b) Take a variable resistor (Potentiometer). Measure the values of


resistance with help of a multimeter for three different positions of the
potentiometer settings.
Het Patel 20031101611039

(c) Measure the value of LDR for various light intensities. Observe how
resistance changes with light intensity.
Het Patel 20031101611039

(d) How do you measure Short-Circuit with the help of a Multimeter?

1. Disconnect the Arduino from any power source and remove any
connections to external components.
2. Set your multimeter to the continuity mode (the symbol for continuity
is usually depicted as a sound wave or a diode).
3. Turn on the multimeter and touch the probe tips together. You should
hear a beep or see the continuity indicator activate. This ensures that
the continuity function is working correctly.
4. Place one probe of the multimeter on the ground (GND) pin of the
Arduino.
5. Use the other probe to touch the suspected pins or traces that may
be involved in the short circuit.
6. If there is a short circuit, the multimeter will indicate continuity by
beeping or showing a "short" indication on the display.
7. Move the probe along the suspected area of the short circuit to
pinpoint the exact location where the short is occurring.
Het Patel 20031101611039

Experiment 12
(A) Connect five resistors in series.
(B) Connect one end to +5 V from Arduino and
other end to Ground Pin of Arduino.
(C) Measure voltage values on different
combinations of Resistors.
Het Patel 20031101611039
Het Patel 20031101611039
Het Patel 20031101611039

Experiment 13

1. Connect LDR in series with one Resistor.


2. Connect one end to +5 V from Arduino and
other end to Ground Pin of Arduino.
3. Measure voltage across LDR for different Light
intensities on LDR.
4. Switch the positions of LDR and Resistor.
Repeat point 3 on this configuration.
Het Patel 20031101611039
Het Patel 20031101611039

Experiment 14

1. Use IDE program: File > Examples > Analog >


AnalogInput
2. Connect circuit as mentioned in the comment of above
program. Read the comments in IDE window.
3. The aim of the program is to observe how blinking rate of
the LED changes with change in position of
potentiometer.
4. Execute the program and observe the output on
hardware. Observe how the blinking rate of LED changes with the
change in the position of potentiometer.
Het Patel 20031101611039

CODE:
int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0
void setup()
{
pinMode(13, OUTPUT);
}

void loop()
{
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin,HIGH);
delay(sensorValue);
digitalWrite(13,LOW);
delay(sensorValue);
}
Het Patel 20031101611039

 analogRead: Reads the value from the specified analog pin. Arduino
boards contain a multichannel, 10-bit analog to digital converter. This
means that it will map input voltages between 0 and the operating
voltage(5V or 3.3V) into integer values between 0 and 1023.

 Schematic view:
Het Patel 20031101611039

Experiment 15

1) Keep the hardware similar to Experiment 14.


2) Change the program such that in place of blinking rate
of LED, the intensity of LED changes with the position
of the potentiometer.
Het Patel 20031101611039

CODE:

const int sensorPin = A0;


const int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
int sensorValue = analogRead(sensorPin);
int brightness = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
}
Het Patel 20031101611039

Experiment 16

1. Modify the input side of the circuit of experiment 14. In


place of potentiometer, use LDR.
2. Learn how to connect LDR to input pin of Arduino. Discuss
your learning and the input circuit with the instructor.
3. Use the same program of Experiment 14.
4. Observe how the blinking rate of LED changes with
the change in light intensity on LDR.
Het Patel 20031101611039

CODE:
int sensorPin = A0;
int ledPin = 12;
int sensorValue = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin,HIGH);
delay(sensorValue);
digitalWrite(ledPin,LOW);
delay(sensorValue);
}
Het Patel 20031101611039

 Explain principle of operation of LDR. With help of graph, explain how


resistance varies with light intensity for a typical LDR.
➔ The LDR, or Light Dependent Resistor, operates based on the
principle of photoconductivity. It is a type of resistor whose
resistance changes in response to the amount of light falling on its
surface. Here's a brief explanation of its principle of operation:
➔ Inside the LDR, there is a semiconductor material, typically cadmium
sulfide (CdS) or cadmium selenide (CdSe), with light-sensitive
properties. The semiconductor has a high resistance in the dark, and
as light intensity increases, its resistance decreases.
➔ When light photons strike the semiconductor material, they
excite the electrons within the material. This excitation causes a
greater number of free charge carriers, either electrons or holes,
depending on the type of semiconductor used. As a result, the
conductivity of the material increases, leading to a decrease in
resistance.

➔ In the graph, the y-axis represents resistance (R), and the x-axis
represents light intensity. As light intensity increases, the resistance
of the LDR decreases. Conversely, as light intensity decreases, the
resistance of the LDR increases.
➔ When using an LDR with an Arduino, you can connect it in a voltage
divider circuit, as described in the previous answer. By measuring the
voltage at the junction point of the LDR and the fixed resistor, you can
Het Patel 20031101611039

indirectly determine the light intensity based on the corresponding


resistance of the LDR.
➔ By mapping the analogRead() values obtained from the Arduino to
the resistance range of the LDR, you can create a relationship
between the light intensity and the analog values read by the Arduino.
This allows you to design projects that respond to changes in light
conditions, such as automatic lighting systems or light-controlled
sensors.
Het Patel 20031101611039

Experiment 17

1. Keep the hardware similar to Experiment 16.


2. Change the program such that in place of blinking rate of
LED, the intensity of LED changes with the light intensity on
the LDR.
Het Patel 20031101611039

CODE:

int sensorPin = A0;


int ledPin = 12;
int sensorValue = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
sensorValue = analogRead(sensorPin);
int bright = sensorValue/4;
analogWrite(ledPin,bright);
}
Het Patel 20031101611039

Experiment 18 , 19 ,20
Can’t able to perform in Ardiuno IDE.
Het Patel 20031101611039

Experiment 21

 Using one 7-Segment LED Display, design a circuit


and write program to display numbers from 9 to 0. The
display changes the number every two seconds
continuously.
Het Patel 20031101611039

CODE:

int a = 6;
int b = 7;
int c = 8;
int d = 3;
int e = 2;
int f = 5;
int g = 4;

void setup() {
pinMode (a, OUTPUT);
pinMode (b, OUTPUT);
pinMode (c, OUTPUT);
pinMode (d, OUTPUT);
pinMode (e, OUTPUT);
pinMode (f, OUTPUT);
pinMode (g, OUTPUT);
}

void loop() {
nine();
delay(2000);
eight();
delay(2000);
seven();
delay(2000);
six();
delay(2000);
five();
delay(2000);
four();
delay(2000);
three();
Het Patel 20031101611039

delay(2000);
two();
delay(2000);
one();
delay(2000);
zero();
delay(2000);
}
void zero() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, LOW);
}
void one() {
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}
void two() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, HIGH);
}
Het Patel 20031101611039

void three() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, HIGH);
}
void four() {
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
void five() {
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
void six() {
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
Het Patel 20031101611039

}
void seven() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}
void eight() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
void nine() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}

Experiment 22
Het Patel 20031101611039

1. Use IDE program: File > Examples > Basic >


Analog Read Serial

2. Connect the middle pin of potentiometer to A0 Analog


Input of Arduino. Connect one pin of potentiometer to +5 V
and other to Ground.

3. Open Serial Monitor from “Tools” Menu and see changes


in values as you rotate the potentiometer switch.

4. Close “Serial Monitor” and open “Serial Plotter” and


observe the change in resistance values being plotted.
Het Patel 20031101611039

CODE:
void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
Het Patel 20031101611039

Serial.begin(9600) : This starts serial communication, so that the Arduino


can send out commands through the USB connection. The value 9600 is
called the 'baud rate' of the connection. This is how fast the data is to be
sent.

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().
Het Patel 20031101611039

Experiment 23

1. Use IDE program: File > Examples > Basic >


DigitalReadSerial

2. Connect a push-button switch in series with


resistor.Connect +5 V to other end of resistor and connect
the other terminal of switch to Ground. Connect the junction
of Switch and Resistor to Pin 2 of arduino.

3. Observe the serial monitor. Observe what happens when


you press switch. Does value in Serial monitor change?

4. Observe similar change in serial plotter.


Het Patel 20031101611039

➔ Before press the switch.

➔ After press the switch.

CODE:
Het Patel 20031101611039

int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(1);
}

Experiment 24
Het Patel 20031101611039

1. Use IDE program: File > Examples > Analog >


AnalogInOutSerial

2. Connect LED to Pin 9 of Arduino, in series with a


resistor.

3. Connect a potentiometer, middle pin to A0 pin, side pins to


+5 V and Ground.

4. Observe the output on Serial Monitor and Plotter.


Observe the change in intensity of LED.
Het Patel 20031101611039
Het Patel 20031101611039

CODE:

const int analogInPin = A0;


const int analogOutPin = 9;
int sensorValue = 0;
int outputValue = 0;

void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
}
Het Patel 20031101611039

Experiment 25

1. Use IDE program: File > Examples > Analog >


AnalogInOutSerial

2. Connect LED to Pin 9 of Arduino, in series with a


resistor.

3. Connect a potentiometer, middle pin to A0 pin, side


pins to +5 V and Ground.

4. Change the Experiment 18 by sending output


without using “map” function.
Het Patel 20031101611039
Het Patel 20031101611039

CODE:

const int analogInPin = A0;


const int analogOutPin = 9;

int sensorValue = 0;
int outputValue = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
}

 Change the Experiment 18 by sending output


without using “map” function.
➔ Can’t able to perform Experiment 18 in Ardiuno IDE.
Het Patel 20031101611039

Experiment 26

1. Understand the program.

CODE:
int incomingByte = 0; // for incoming serial data
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop()
{
// send data only when you receive data:
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte - 48, DEC);
}
}

Serial.available( ) : Get the number of bytes (characters) available


for reading from the serial port. This is data that’s already arrived and
stored in the serial receive buffer (which holds 64 bytes).
➔ Serial.available() inherits from the Stream utility class.

Serial.read( ) : Reads incoming serial data.


➔ Serial.read() inherits from the Stream utility class.
Het Patel 20031101611039

Experiment 27

1. Modify the program and hardware of Experiment 26 in the


following way.
2. Read an integer number between 3 and 5 from serial
monitor input.
3. If the number is out of range, then print on serial monitor
“Number is out of Range.”

4. If number is “3”, turn on LED on Pin 3 for 2 seconds.


→ If number is “4”, turn on LED on Pin 4 for 2
seconds.
→ If number is “5”, turn on LED on Pin 5 for 2
seconds.
Het Patel 20031101611039
Het Patel 20031101611039

CODE:

int incomingByte = 0;
int ledPin1=3;
int ledPin2=4;
int ledPin3=5;

void setup(){
Serial.begin(9600);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
}

void loop(){
if(Serial.available()>0);
{
incomingByte = Serial.read();
Serial.print("I recived: ");
Serial.println(incomingByte-48,DEC);
if(incomingByte-48 == 3);
{
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
delay(2000);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
if(incomingByte-48 == 4);
{
digitalWrite(ledPin1,LOW);
Het Patel 20031101611039

digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);
delay(2000);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
if(incomingByte-48 == 5);
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
delay(2000);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
if(incomingByte-48>5 || incomingByte-48<3);
{
Serial.println("Number is out of Range: ");
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
}
}
Het Patel 20031101611039

Experiment 28

1. Connect the Ultrasonic Sensor to Arduino.

2. Upload the Program from Google Folder


“Exp_28”

3. Understand the program and perform the


Experiment.
Het Patel 20031101611039
Het Patel 20031101611039

CODE:

int distanceThreshold = 0;
int cm = 0;
int inches = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}

void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

void loop()
{
distanceThreshold = 350;
cm = 0.01723 * readUltrasonicDistance(7, 6);
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");
Het Patel 20031101611039

if (cm > distanceThreshold) {


digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
if (cm <= distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(100);
}
Het Patel 20031101611039

Experiment 29

1. Modify the program of experiment 28 in the


following way.
2. Continuously send the data to the website
“ThingSpeak.com”. Observe the data and
Graph.

3. Write the summary of steps on how you have


sent data to “ThingSpeak.com”.
Het Patel 20031101611039
Het Patel 20031101611039

CODE:

int distanceThreshold = 0;
int cm = 0;
int inches = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}

void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

void loop()
{
distanceThreshold = 350;
cm = 0.01723 * readUltrasonicDistance(7, 6);
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");
Het Patel 20031101611039

if (cm > distanceThreshold) {


digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
if (cm <= distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(100);
}
 Continuously send the data to the website
“ThingSpeak.com”. Observe the data and Graph.
➔ Can’t able to Perform.

 Write the summary of steps on how you have


sent data to “ThingSpeak.com”.
➔ Can’t able to Perform.
Het Patel 20031101611039

Experiment 30

1. Use IDE program: File > Examples > Liquid


Crystal > Hello-World

2. Perform above experiment and observe the


result on LCD.

3. Change the Text you are sending to display with


today’s date and observe the output.
Het Patel 20031101611039
Het Patel 20031101611039

CODE:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
lcd.begin(16, 2);
lcd.print("19/6/2023");
}

void loop(){
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}

Experiment 32
Het Patel 20031101611039

1. Connect a Push Button Switch as input to


Arduino Pin No. 2
2. Connect a DC motor (in series with a resistor) to
Pin 10 on Arduino.
3. Write the program such that when the Push
Button is pressed, the motor turns on.
4. Now, modify the program in such a way that
when the Push Button is pressed, the motor
turns OFF.

➔ Push Button is pressed and DC motor turn ON.


Het Patel 20031101611039

CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
}
void loop()
{
if (digitalRead(2)== HIGH){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
}

➔ Push Button is pressed and DC motor turn OFF.


Het Patel 20031101611039

CODE:

void setup()
{
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
}

void loop()
{
if (digitalRead(2)== LOW){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
}

You might also like