0% found this document useful (0 votes)
14 views51 pages

21ECO106J - LAB - Manual-Experiments 2

The document outlines a series of experiments using Arduino to blink an LED, control brightness, display numbers on a 7-segment display, and monitor temperature and distance using sensors. Each experiment includes hardware requirements, code structure, procedures, and questions for pre and post-lab assessments. The aim is to familiarize users with Arduino programming and interfacing various components through practical applications.

Uploaded by

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

21ECO106J - LAB - Manual-Experiments 2

The document outlines a series of experiments using Arduino to blink an LED, control brightness, display numbers on a 7-segment display, and monitor temperature and distance using sensors. Each experiment includes hardware requirements, code structure, procedures, and questions for pre and post-lab assessments. The aim is to familiarize users with Arduino programming and interfacing various components through practical applications.

Uploaded by

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

Exp.

1: Getting Started With ARDUINO


Aim:Toblink the on-board LED with 75% Duty cycle and total time period of 1000ms.

Components Requirement:

Hardware components : i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable
Software Tools : Arduino IDE,
Tinkercad online Virtual Lab (For simulatuion)

Theory:

Arduino is an open-source prototyping platform in electronics based on easy-to-use hardware and


software. Subtly speaking, Arduino is a microcontroller based prototyping board which can be used in
developing digital devices that can read inputs like finger on a button, touch on a screen, light on a
sensor etc. and turning it in to output like switching on an LED, rotating a motor, playing songs

through a speaker etc.

Since this experimentuses onboard LED, no hardware interface is required.

Code Structure

void setup() {
// put your setup code here, to run once:
1
pinMode(13, OUTPUT);

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(13, HIGH);

delay(750); // Wait for 750 millisecond(s)

digitalWrite(13, LOW);

delay(250); // Wait for 250 millisecond(s)

Bare minimum code

 setup: It is called only when the Arduino is powered on or reset. It is used to initialize vari-
ables and pin modes
 loop: The loop functions runs continuously till the device is powered off. The main logic of
the code goes here. Similar to while (1) for micro-controller programming.

PinMode

 A pin on Arduino can be set as input or output by using pinMode function.


 pinMode(13, OUTPUT); // sets pin 13 as output pin
 pinMode(13, INPUT); // sets pin 13 as input pin

Pre lab Questions:


1. How many GPIO pins are there in Arduino Uno Development Board?
2. Write about Expansion connectors inArduino Uno Development Board.

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B USB cable.
2. Write the code and compile it in Arduino IDE.
3. Select USB port in Arduino IDE.
4. Select Arduino Uno Development Board in Arduino IDE.
5. Download the code into Arduino Uno Development Board
6. Run the code in Arduino Uno Development Board.
7. Observe and verify the output.

Post lab Questions:


1. What register is used to configure the IO port direction?
2. What statement is used to send a digital data in IO pin Using Arduino IDE?

2
Output:

Paste your Screenshots here.

Result:

3
Exp. 2: GPIO LED

Aim:To control the brightness of the LED which is connected in any of the PWM pin.

Components Requirement:

Hardware components : i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable
Software Tools : Arduino IDE,
Tinkercad online Virtual Lab (For simulation)

Theory:

Led blinking is the most beginner & easy step to start your experiment with Arduino. Let’s get started.
Firstly, identify anode(+ve) & cathode (-ve) leg of LED. Following diagram gives a clear idea of LED.

A LED is connected to the GPIO pin 9 through a ballast resister to limit the current through the
LED.

Code Structure

4
void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Bare minimum code

 setup: It is called only when the Arduino is powered on or reset. It is used to initialize vari-
ables and pin modes
 loop: The loop functions runs continuously till the device is powered off. The main logic of
the code goes here. Similar to while (1) for micro-controller programming.

PinMode

 A pin on Arduino can be set as input or output by using pinMode function.


 pinMode (9, OUTPUT); // sets pin 9 as output pin
 pinMode (9, INPUT); // sets pin as input pin

Pre lab Questions:


1. How many analog pins are there in Arduino Uno Development Board?
2. Which of general purpose working register are used as 16-bit indirect address register pointers?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B USB
cable.
2. Write the code and compile it in Arduino IDE.
3. Select USB port in Arduino IDE.
4. Select Arduino Uno Development Board in Arduino IDE.
5. Download the code into Arduino Uno Development Board
6. Run the code in Arduino Uno Development Board.
7. Observe and verify the output.

Post lab Questions:

1. What are the register associated with each port?


2. What statement is used to send an analog data in IO pin Using Arduino IDE?

5
Output:

Paste your Screenshots here.

Code
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.

The analogWrite() function uses PWM, so if you


want to change the pin you're using, be sure to
use another PWM capable pin. On most Arduino,
the PWM pins are identified with a "~" sign,
like ~3, ~5, ~6, ~9, ~10 and ~11.
*/

int brightness = 0;

void setup()
{
6
pinMode(9, OUTPUT);
}

void loop()
{
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(9, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(9, brightness);
delay(30); // Wait for 30 millisecond(s)
}}

Result:

7
Exp. 3:DISPLAY INTERFACE-7 SEGMENT
Aim:Todisplay 7-segment LED

Components Requirement:

Hardware components : i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable
Software Tools : Arduino IDE,
Tinkercad online Virtual Lab (For simulatuion)

Theory:

A seven segment display got its name from the very fact that it got seven illuminating
segments. Each of these segments has a LED (Light Emitting Diode), hence the lighting. The
LEDs are so fabricated that lighting of each LED is contained to its own segment. The
important thing to notice here that the LEDs in any seven segment display are arranged in
common anode mode (common positive) or common cathode mode (common negative).

8
Code Structure

void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Pre lab Questions:


1. How are LEDs connected in 7 segment?
2. What is common cathode 7 segment?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B USB ca-
ble.
9
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.

Post lab Questions:

1. Give the syntax of the switch statement.


2. Which are digital PWM pins?

Output:

Paste your Screenshots here.

Code.
#define segA 6//connecting segment A to PIN6
#define segB 7// connecting segment B to PIN7
#define segC 8// connecting segment C to PIN8

10
#define segD 9// connecting segment D to PIN9
#define segE 10// connecting segment E to PIN10
#define segF 11// connecting segment F to PIN11
#define segG 12// connecting segment F to PIN12

int COUNT=0;//count integer for 0-9 increment


void setup()

{
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

void loop()

switch (COUNT)
{
case 0://when count value is zero show”0” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 1:// when count value is 1 show”1” on disp

digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 2:// when count value is 2 show”2” on disp


11
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;

case 3:// when count value is 3 show”3” on disp

digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;

case 4:// when count value is 4 show”4” on disp

digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 5:// when count value is 5 show”5” on disp

digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);

break;

case 6:// when count value is 6 show”6” on disp

digitalWrite(segA, HIGH);
12
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 7:// when count value is 7 show”7” on disp

digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 8:// when count value is 8 show”8” on disp

digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 9:// when count value is 9 show”9” on disp

digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

if (COUNT<10)
{
COUNT++;
delay(1000);///increment count integer for every second
13
}

if (COUNT==10)
{
COUNT=0;// if count integer value is equal to 10, reset it to zero.
delay(1000);
}

Result:

14
Exp. 4: SENSOR INTERFACING FOR TEMPERATURE MONITORING

Aim: To indicate the temperature in “Red”, “Yellow”, “Green” LEDs and also send the
value to terminal in PC via serial port.

Components Requirement:

Hardware components: (i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable

Software Tools: Arduino IDE, Tinkercad online Virtual Lab (For simulation)

Theory:

The Temperature Sensor LM35 series are precision integrated-circuit temperature devices with an
output voltage linearly proportional to the Centigrade temperature.
The LM35 device has an advantage over linear temperature sensors calibrated in Kelvin, as the user is
not required to subtract a large constant voltage from the output to obtain convenient Centigrade
scaling. The LM35 device does not require any external calibration or trimming to provide typical
accuracies of ±¼°C at room temperature and ±¾°C over a full −55°C to 150°C temperature range. It
is a 3-terminal device that provides analog voltage proportional to the temperature. Higher the
temperature, higher is the output voltage. The output analog voltage can be converted to digital form
using ADC so that a microcontroller can process it.

15
Code Structure:

void setup()
{
// put your setup code here, to run once:
}

void loop()
{
// put your main code here, to run repeatedly:
}

Pre-Lab Questions:

1. What is temperature sensor?


2. What are the 2 types of serial communication?
Procedure:

1. Connect the Arduino Uno Development Board to the Desktop/Laptop through


Type B USB cable.
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
16
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.

Note:
 Red LED should glow when temperature is greater than 70 degree Celsius
 Yellow LED should glow when temperature is greater than 21 degree Celsius
and less than 70 degreeCelsius.
 Green LED should glow when temperature is Less than 20 degree Celsius

Post Lab:

1. Why temper sensor is connected to A0?


2. List few Arduino serial communications functions.

Output:

Paste your Screenshots here.

17
Code:

int sensorValue = 0;

int SerialValue = 0;

void setup()

pinMode(A0, INPUT);

pinMode(13, OUTPUT); //red

pinMode(12, OUTPUT); //green

pinMode(11, OUTPUT); //yellow

Serial.begin(9600);

void loop()

sensorValue = analogRead(A0);

SerialValue = (sensorValue-102)/2;

Serial.println(SerialValue);

if (SerialValue>70)

// ONLY RED GLOWS

digitalWrite(13,HIGH);

digitalWrite(12,LOW);

18
digitalWrite(11,LOW);

if (SerialValue>21 &&SerialValue<70)

// ONLY YELLOW GLOWS

digitalWrite(13,LOW);

digitalWrite(12,LOW);

digitalWrite(11,HIGH);

if(SerialValue<20)

// ONLY GREEN GLOWS

digitalWrite(13,LOW);

digitalWrite(12,HIGH);

digitalWrite(11,LOW);

delay(2); // Wait for 2 millisecond(s)

Result:

19
Exp5: Sensor Interfacing for Displacement Measurement

Aim: To indicate the distance range using three LEDs and use ultrasonic sensor to measure distance.

Components Requirement:

Hardware components : i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable
Software Tools : Arduino IDE,
Tinker cad online Virtual Lab (For simulation)

Theory:

Ultrasonic sensor HC-SR04 is used here to measure distance in range of 2cm-400cm with accuracy of
3mm. The sensor module consists of ultrasonic transmitter, receiver and the control circuit. The work-
ing principle of ultrasonic sensor is as follows:

 High level signal is sent for 10us using Trigger.


 The module sends eight 40 KHz signals automatically, and then detects whether pulse is re-
ceived or not.
 If the signal is received, then it is through high level. The time of high duration is the time gap
between sending and receiving the signal.
 Distance= (Time x Speed of Sound in Air (340 m/s))/2

20
Code Structure

void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Bare minimum code

 setup : It is called only when the Arduino is powered on or reset. It is used to initialize vari-
ables and pin modes
 loop : The loop functions runs continuously till the device is powered off. The main logic
of the code goes here. Similar to while (1) for micro-controller programming.

PinMode

 A pin on arduino can be set as input or output by using pinMode function.


 pinMode(9, OUTPUT); // sets pin 9 as output pin
 pinMode(9, INPUT); // sets pin as input pin

Prelab Questions:
1. What are ultrasonic sensors?
2. What environmental conditions affect an ultrasonic sensor?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B USB ca-
ble.
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.

PostLab Questions:

1. Write a note on the Arduino function used to detect the length of a pulse.
2. What is a dead zone in ultrasonic ranging?

21
Output:

Paste your Screenshots here.

Code.
long cm = 0;

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

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

void loop()
{
cm =readUltrasonicDistance(7, 6);

Serial.print(cm);
Serial.println("cm");

if (cm > 250) {


22
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= 250 && cm > 175) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= 175 && cm > 100) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if (cm <= 100) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}

delay(100); // Wait for 100 millisecond(s)


}

long readUltrasonicDistance(int triggerPin, int echoPin)


{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds *0.01723
return (pulseIn(echoPin, HIGH)*0.01723);

RESULT:

23
Exp. 6: PWM BASED SERVO MOTOR INTERFACING

Aim: To interface a Servo Motor using PWM Signals.

Components Requirement:
Hardware components: (i) Desktop/ Laptop (Host)
(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable

Software Tools: Arduino IDE, Tinkercad online Virtual Lab (For simulation)

Theory:
A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear
position, velocity and acceleration. It consists of a suitable motor coupled to a sensor for position
feedback. It also requires a relatively sophisticated controller, often a dedicated module designed spe-
cifically for use with servomotors.
Servomotors are not a specific class of motor, although the term servomotor is often used to refer to a
motor suitable for use in a closed-loop control system.
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital
control is used to create a square wave, a signal switched between on and off. This on-off pattern can
simulate voltages in between the full Vcc of the board (e.g., 5 V on Uno, 3.3 V on a MKR board) and
off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal
spends off.

24
Code Structure
void setup() { // put your setup code here, to run once:
}
void loop() { // put your main code here, to run repeatedly:
}

Pre lab Questions:


1. Write about the construction of the Servo Motor.
2. List the applications of Servo Motor.

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B USB cable.
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.

Post lab Questions:


1. List the advantages and disadvantages of Servo Motor.

2. How PWM is used to control the Servo Motor?

Output:

Paste your Screenshots here.

25
CODE:

#include <Servo.h>
int pos = 0;
Servo servo_9;
void setup()
{
servo_9.attach(9);
}
void loop()
{
for (pos = 0; pos<= 180; pos += 1) {
servo_9.write(pos);
delay(15);
}
for (pos = 180; pos>= 0; pos -= 1) {
servo_9.write(pos);
delay(15);
}
}

26
CODE:
int sensorValue = 0;
int outputValue = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(A0);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(9, outputValue);
Serial.print("sensor =");
Serial.print(sensorValue);
Serial.print("\toutput =");
Serial.println(outputValue);
delay(2);
}

RESULT:

27
Exp. 7: SERIAL COMMUNICATION

Aim:
1. To establish serial communication between Arduino Uno and PC
2. To receive a number through serial communication and display it in 7 segment display.

Components Requirement:

Hardware components : i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable
Software Tools : Arduino IDE,
Tinkercad online Virtual Lab (For simulatuion)

Theory:

Arduino is an open-source prototyping platform in electronics based on easy-to-use hardware and


software.The communication between arduino and computer is established at a specific baud rate. The
baud rate specifies how fast the data is sent over the serial line or in simple terms, the speed of serial
communication. Some common rates for UART are 9600 baud, 11520 baud etc. To start serial
communication the baud rate set for arduino and computer must be the same, if baud rate for both is
set at 9600 baud, then to transmit 1 bit of data it will take 1/9600 sec = 0.014 msec.The main purpose
of this serial communication is to transfer the sketch from computer to Arduino, send information to
computer etc.The most common type of serial communication protocol is UART i.e. Universal
Asynchronous Receiver Transmitter. The UART connection requires a 9 pin DE – 9 Connector. But,
most modern computers and laptops do not include these COM ports. Hence, if we want to connect
our microcontrollers like 8051, AVR or ARM, we need to use an external serial to USB converter.But
Arduino UNO has an on board serial to USB converter and hence we can directly connect the Arduino
to the computer. Using this USB connection and Arduino IDE, we can send data to Arduino or receive
data from Arduino. This data can be monitored with the help of Serial Monitor in the Arduino IDE.

Code Structure

28
void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Bare minimum code

 setup : It is called only when the Arduino is powered on or reset. It is used to initialize variables and
pin modes
 loop : The loop functions runs continuously till the device is powered off. The main logic of the code
goes here. Similar to while (1) for micro-controller programming.

PinMode

 A pin on arduino can be set as input or output by using pinMode function.


 pinMode(13, OUTPUT); // sets pin 13 as output pin
 pinMode(13, INPUT); // sets pin 13 as input pin

Pre Lab:
1. What is serial communication?
2. What is baud rate?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B USB ca-
ble.
2. Write the code and compile it in Arduino IDE.
3. Select USB port in Arduino IDE.
4. Select Arduino Uno Development Board in Arduino IDE.
5. Download the code into Arduino Uno Development Board
6. Run the code in Arduino Uno Development Board.
7. Observe and verify the output.

Post Lab:
1. How do you establish serial communication between Arduin and PC?
2. In the code, explain the statement
COUNT = Serial.read()-48;

29
Output:

Paste your Screenshots here.

CODE:

#define segA 6//connecting segment A to PIN6


#define segB 7// connecting segment B to PIN7
#define segC 8// connecting segment C to PIN8
#define segD 9// connecting segment D to PIN9
#define segE 10// connecting segment E to PIN101.
#define segF 11// connecting segment F to PIN11
#define segG 12// connecting segment G to PIN12
int COUNT=7;//count integer for 0-9 increment
void setup()

{
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

//Initialize serial and wait for port to open:


Serial.begin(9600);
while (!Serial) {
30
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Welcome all. I am Arduino");
}
void loop()
{
switch (COUNT)
{
case 0://when count value is zero show”0” on disp
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 1:// when count value is 1 show”1” on disp


digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 2:// when count value is 2 show”2” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, LOW);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, LOW);

digitalWrite(segG, HIGH);

break;

case 3:// when count value is 3 show”3” on disp


31
digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, HIGH);
break;

case 4:// when count value is 4 show”4” on disp

digitalWrite(segA, LOW);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);
break;

case 5:// when count value is 5 show”5” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, LOW);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);

32
digitalWrite(segG, HIGH);

break;

case 6:// when count value is 6 show”6” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, LOW);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);

break;

case 7:// when count value is 7 show”7” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, LOW);

digitalWrite(segE, LOW);

digitalWrite(segF, LOW);

digitalWrite(segG, LOW);
break;

case 8:// when count value is 8 show”8” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

33
digitalWrite(segD, HIGH);

digitalWrite(segE, HIGH);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);
break;

case 9:// when count value is 9 show”9” on disp

digitalWrite(segA, HIGH);

digitalWrite(segB, HIGH);

digitalWrite(segC, HIGH);

digitalWrite(segD, HIGH);

digitalWrite(segE, LOW);

digitalWrite(segF, HIGH);

digitalWrite(segG, HIGH);
break;
break;

}
while(!Serial.available());
COUNT = Serial.read()-48;
Serial.println(COUNT);
/*
if (COUNT<10)
{
COUNT++;
delay(1000);///increment count integer for every second
}
if (COUNT==10)
{
COUNT=0;// if count integer value is equal to 10, reset it to zero.
delay(1000);
}*/
}

Result:

34
Exp.8: DC MOTOR INTERFACE

Aim: To run a DC motor with the help of a motor driver(L239D) using Arduino Uno.

Components Requirement:

Hardware components: (i) Desktop/ Laptop (Host)


(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable

Software Tools: Arduino IDE, Tinkercad online Virtual Lab (For simulation)

Theory:

A DC motor is any of a class of rotary electrical motors that converts direct current
electrical energy into mechanical energy. The most common types rely on the forces
produced by magnetic fields. Nearly all types of DC motors have some internal
mechanism, either electromechanical or electronic, to periodically change the direction
of current in part of the motor.

The L293D is a popular 16-Pin Motor Driver IC. As the name suggests it is mainly
used to drive motors. A single L293D IC is capable of running two DC motors at the
same time; also the direction of these two motors can be controlled independently.

Pin Pin Name Description


Number

1 Enable 1,2 This pin enables the input pin Input 1(2) and Input 2(7)

2 Input 1 Directly controls the Output 1 pin. Controlled by digital circuits

35
3 Output 1 Connected to one end of Motor 1

4 Ground Ground pins are connected to ground of circuit (0V)

5 Ground Ground pins are connected to ground of circuit (0V)

6 Output 2 Connected to another end of Motor 1

7 Input 2 Directly controls the Output 2 pin. Controlled by digital circuits

8 Vcc2 (Vs) Connected to Voltage pin for running motors (4.5V to 36V)

9 Enable 3,4 This pin enables the input pin Input 3(10) and Input 4(15)

10 Input 3 Directly controls the Output 3 pin. Controlled by digital circuits

11 Output 3 Connected to one end of Motor 2

12 Ground Ground pins are connected to ground of circuit (0V)

13 Ground Ground pins are connected to ground of circuit (0V)

14 Output 4 Connected to another end of Motor 2

15 Input 4 Directly controls the Output 4 pin. Controlled by digital circuits

16 Vcc2 (Vss) Connected to +5V to enable IC function

36
Features

 Can be used to run Two DC motors with the same IC.


 Speed and Direction control is possible
 Motor voltage Vcc2 (Vs): 4.5V to 36V
 Maximum Peak motor current: 1.2A
 Maximum Continuous Motor Current: 600mA
 Supply Voltage to Vcc1(vss): 4.5V to 7V
 Transition time: 300ns (at 5Vand 24V)
 Automatic Thermal shutdown is available
 Available in 16-pin DIP, TSSOP, SOIC packages

Code Structure:

void setup()
{
// put your setup code here, to run once:
}

void loop()
{
// put your main code here, to run repeatedly:
}

Pre-Lab Questions:

1. What is DC motor?
2. On what principle DC motor works?

37
Procedure:

1. Connect the Arduino Uno Development Board to the Desktop/Laptop through


Type B USB cable.
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.
Note:
We use a slide switch to alter the direction of rotation of the motor. When the
slide switch is in the left side, the DC motor rotates in an anticlockwise fashion. When
switch slides to the right, the DC motor rotates in clockwise direction.
Post Lab:

1. What is the purpose of H-Bridge in interfacing a DC motor with Arduino?


2. Write a C program to control two DC motors based on the two-slide switch.

Output

Paste your Screenshots here.

38
Code:

void setup()
{
pinMode(13, OUTPUT); // enable l293d
pinMode(11,OUTPUT); // input 1
pinMode(9,OUTPUT); // input 2

digitalWrite(13,HIGH); // enable motor


pinMode(7,INPUT);
}
void loop()
{
if(digitalRead(7)== 1)
{
analogWrite(11,10);
digitalWrite(9,LOW);
}
if(digitalRead(7)== 0)
{
analogWrite(9,10);
digitalWrite(11,LOW);
}
}
Result:

39
Exp 9: Interrupt Programming
Aim: To run a buzzer and LED using Interrupt in Arduino.
Components Requirement:
Hardware components: (i) Desktop/ Laptop (Host)
(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable

Software Tools: Arduino IDE, Tinkercad online Virtual Lab (For simulation)

Theory:

An Interrupt's job is to make sure that the processor responds quickly to important events.
When a certain signal is detected, an Interrupt (as the name suggests) interrupts whatever the
processor is doing, and executes some code designed to react to whatever external stimulus is
being fed to the Arduino .

Code Structure

void setup() {
// put your setup code here, to run once:
40
}

void loop() {
// put your main code here, to run repeatedly:

Pre lab Questions:


1) What are the external interrupts in Arduino?

2) What are the USART related interrupts in Arduino?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B
USB cable.
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.

Post lab Questions:


1) List the modes of interrupt.
2) Which pins are allocated for INT.0 and INT.1 in Arduino Uno board?

41
Output

Paste your Screenshots here.

CODE:

const byte LED_Pin = 13;


const byte BUZZER_Pin = 12;

const byte interruptPin2 = 2;


const byte interruptPin3 = 3;

volatile byte LED_state = HIGH;


volatile byte BUZZER_state = HIGH;

void setup() {
pinMode(LED_Pin, OUTPUT);
pinMode(BUZZER_Pin, OUTPUT);

digitalWrite(LED_Pin, LED_state);
digitalWrite(BUZZER_Pin, BUZZER_state);

pinMode(interruptPin2, INPUT_PULLUP);
pinMode(interruptPin3, INPUT_PULLUP);

42
attachInterrupt(digitalPinToInterrupt(interruptPin2), blink2, CHANGE);
attachInterrupt(digitalPinToInterrupt(interruptPin3), blink3, CHANGE); Serial.begin(9600);
Serial.println("Welcome");
}

void loop() {
digitalWrite(LED_Pin, LED_state);
digitalWrite(BUZZER_Pin, BUZZER_state);
Serial.println("0");
}

void blink2() {
LED_state =
!LED_state;
Serial.println("2");
}

void blink3() {
BUZZER_state= !BUZZER_state;
Serial.println("4");
}

RESULT:

43
Exp 10: Watch Dog Timer

Aim: To implement a Watchdog Timer on an Arduino:


Components Requirement:
Hardware components: (i) Desktop/ Laptop (Host)
(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable

Software Tools: Arduino IDE, Tinkercad online Virtual Lab (For simulation)

Theory:

A Watchdog Timer (WDT) is a hardware timer used to reset the system if the software fails to
operate correctly. It's a safety feature to ensure the system recovers from software failures.
Implementing a WDT on an Arduino involves configuring the internal watchdog timer of the
microcontroller.

How does the Watchdog timer work?

The watchdog timer needs to be configured according to the need of the application.

The watchdog timer uses an internal 128kHz clock source.

When enabled, it starts counting from 0 to a value selected by the user. If the watchdog timer is not
reset by the time it reaches the user selected value, the watchdog resets the microcontroller.

44
The ATmega328P watchdog timer can be configured for 10 different time settings (the time after
which the watchdog timer overflows, thus causing a reset).

The various times are: 16ms, 32ms, 64ms, 0.125s, 0.25s, 0.5s, 1s, 2s, 4s and 8s.

Example

Let’s see how to configure the watchdog timer for an Arduino UNO board.

Here, we will use a simple example of LED blinking.

The LEDs are blinked for a certain time before entering a while(1) loop. The while(1) loop is used as a
substitute for a system in the hanged state.

Since the watchdog timer is not reset when in the while(1) loop, the watchdog causes a system reset
and the LEDs start blinking again before the system hangs and restarts again. This continues in a loop.

Here, we will be using the on-board LED connected to the pin 13 of the Arduino UNO board. For this
example sketch, the only thing required is the Arduino UNO board.

Word of caution:

The Watchdog timer is disabled at the start of the code. A delay of 3 seconds is used before enabling
the Watchdog.

This delay is important in order to let the bootloader in Arduino to check if a new code is being up-
loaded and to give it time to burn the code into the flash.

This is important as a precaution. A situation may occur, wherein due to faulty coding, or improper
considerations; the code written resets the microcontroller at very short durations infinitely.

This will damage the Arduino board and lead to sketches not being uploaded to the board.

This may not be the case for the new Optiboot loader that comes with the newer version of the Ar-
duino, but it will definitely happen to the older ones.

In case if you break the Arduino in this manner, you will have to burn the bootloader using a different
Arduino as an ISP into the bricked Arduino.

45
Code Structure

void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Pre lab Questions:


1) What is the use of watchdog timer in Arduino?

2) What are the basics of watchdog timer?

Lab Procedure:
a. Connect the Arduino Uno Development Board to the Desktop/Laptop through
Type B USB cable.
b. Make the circuit connection accordingly.
c. Write the code and compile it in Arduino IDE.
d. Select USB port in Arduino IDE.
e. Select Arduino Uno Development Board in Arduino IDE.
f. Download the code into Arduino Uno Development Board
g. Run the code in Arduino Uno Development Board.
h. Observe and verify the output.

Post lab Questions:


3) What is the period of watchdog timer?
4) What is the default timer for watchdog?

Output

Paste your Screenshots here.

46
CODE:

#include<avr/wdt.h>
void setup() {
Serial.begin(9600);
wdt_disable(); //Disable WDT
delay(3000);
wdt_enable(WDTO_2S); //Enable WDT with a timeout of 2 seconds
Serial.println("WDT Enabled");
}
void loop() {
for(int i = 0; i<5; i++)
{
Serial.println("Looping");
delay(1000);
wdt_reset(); //Reset the watchdog
}
while(1); //Watchdog timer should get triggered here
}

RESULT:

47
Lab 11: I2C Communication

Aim:To interface two Arduino using I2C Communication

Components Requirement:
Hardware components: (i) Desktop/ Laptop (Host)
(ii) Arduino Uno Development Board (Target)
(iii) Type B USB Cable

Software Tools: Arduino IDE, Tinkercad online Virtual Lab (For simulation)

Theory:

The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) that the
Arduino or Genuino Master board pulses at a regular interval, and a serial data pin (SDA) over which
data is sent between the two devices. As the clock line changes from low to high (known as the rising
edge of the clock pulse), a single bit of information - that will form in sequence the address of a
specific device and a a command or data - is transferred from the board to the I2C device over the
SDA line. When this information is sent - bit after bit -, the called upon device executes the request
and transmits it's data back - if required - to the board over the same line using the clock signal still
generated by the Master on SCL as timing.

Code Structure

void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:
48
}

Pre lab Questions:


1) Name the two lines of I2C protocol.
2) In Arduino board, which pins are used for I 2 C?

Lab Procedure:
1. Connect the Arduino Uno Development Board to the Desktop/Laptop through Type B
USB cable.
2. Make the circuit connection accordingly.
3. Write the code and compile it in Arduino IDE.
4. Select USB port in Arduino IDE.
5. Select Arduino Uno Development Board in Arduino IDE.
6. Download the code into Arduino Uno Development Board
7. Run the code in Arduino Uno Development Board.
8. Observe and verify the output.

Post lab Questions:


1) List the functions done by Wire.beginTransmission(address).
2) What are the functions used to receive data over I2C?

Output:

49
CODE:
Arduino 1:

#include
<Wire.h> int
analogIN = 0; int
msg = 0;
void setup() {
pinMode(A0, INPUT);
// Start the I2C Bus as Master
Wire.begin();
Serial.begin(9600);
Serial.print("Welcome to I2C Demo. I am Arduino
1"); }

void loop(){
analogIN=analogRead(A0);
msg = map(analogIN, 0, 1023, 0, 255);
Wire.beginTransmission(9); // transmit to device #9
Wire.write(msg); // sends x
Wire.endTransmission(); // stop transmitting
Serial.println(msg);
delay(500);
}

Arduino 2:

#include
<Wire.h> int
LED = 13; int x
= 0; void
setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);
Serial.begin(9600);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
}

void receiveEvent(int bytes) {


x = Wire.read(); // read one character from the I2C
Serial.println(x);
}
void loop() {
//If value received is 0 blink LED for 200
50
ms if (x ==0) {
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);

}
//If value received is 3 blink LED for 400
ms if (x ==4) {
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
}
}

RESULT:

51

You might also like