21ECO106J - LAB - Manual-Experiments 2
21ECO106J - LAB - Manual-Experiments 2
Components Requirement:
Theory:
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);
digitalWrite(13, LOW);
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
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.
2
Output:
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:
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:
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
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.
5
Output:
Code
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
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:
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:
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.
Output:
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
{
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;
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, HIGH);
12
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
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:
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:
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:
Output:
17
Code:
int sensorValue = 0;
int SerialValue = 0;
void setup()
pinMode(A0, INPUT);
Serial.begin(9600);
void loop()
sensorValue = analogRead(A0);
SerialValue = (sensorValue-102)/2;
Serial.println(SerialValue);
if (SerialValue>70)
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
18
digitalWrite(11,LOW);
if (SerialValue>21 &&SerialValue<70)
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,HIGH);
if(SerialValue<20)
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
digitalWrite(11,LOW);
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:
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:
20
Code Structure
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
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
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:
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");
RESULT:
23
Exp. 6: PWM BASED SERVO MOTOR INTERFACING
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:
}
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.
Output:
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:
Theory:
Code Structure
28
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
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
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:
CODE:
{
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
32
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
33
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;
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:
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.
1 Enable 1,2 This pin enables the input pin Input 1(2) and Input 2(7)
35
3 Output 1 Connected to one end of Motor 1
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)
36
Features
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:
Output
38
Code:
void setup()
{
pinMode(13, OUTPUT); // enable l293d
pinMode(11,OUTPUT); // input 1
pinMode(9,OUTPUT); // input 2
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:
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.
41
Output
CODE:
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
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.
The watchdog timer needs to be configured according to the need of the application.
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.
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:
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.
Output
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
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
}
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.
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);
}
}
//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