Iot Lab Manual 18Csl81: Department of Computer Science and Engineering
Iot Lab Manual 18Csl81: Department of Computer Science and Engineering
BELGAUM
Prepared By
1
CONTENTS
Page
Sl.No Program
No.
IOT
2
3
4
5
6
7
8
9
10
11
Internet of Things(IOT)
To download Arduino: Browse Arduino -> Go to Arduino Home -> Software -> Downloads -
> Click on Windows (at the bottom) to download the Arduino Software- for Windows.
After downloading -> Select on that file and right click -> Select Extract here.
Go to Arduino folder after extracting and select the desired Arduino Software.
Important Points:
1. Male connecting wire – has a pin on both sides
2. Female connecting wire – has no pins on either side
3. In all our five experiments above we will be using only male wires
4. LED – long edge is positive and short edge is negative
5. On board – Positive: 13 and Negative: GND
6. View the bread board horizontally – then vertical lines are same and horizontal
lines are not same
The UNO is the best board to get started with electronics and coding. If this is your first
experience tinkering with the platform, the UNO is the most robust board you can start
playing with. The UNO is the most used and documented board of the whole Arduino family.
12
with a USB cable or power it with a AC-to-DC adapter or battery to get started.. You can
tinker with your UNO without worring too much about doing something wrong, worst case
scenario you can replace the chip for a few dollars and start over again.
"Uno" means one in Italian and was chosen to mark the release of Arduino Software (IDE)
1.0. The Uno board and version 1.0 of Arduino Software (IDE) were the reference versions of
Arduino, now evolved to newer releases. The Uno board is the first in a series of USB
Arduino boards, and the reference model for the Arduino platform; for an extensive list of
current, past or outdated boards see the Arduino index of boards.
UART COMMUNICATION
UART stands for Universal Asynchronous Receiver/Transmitter. It’s not a
communication protocol like SPI and I2C, but a physical circuit in a microcontroller, or a
stand-alone IC. A UART’s main purpose is to transmit and receive serial data.
The UART that is going to transmit data receives the data from a data bus. The data bus is
used to send data to the UART by another device like a CPU, memory, or microcontroller.
Data is transferred from the data bus to the transmitting UART in parallel form. After the
transmitting UART gets the parallel data from the data bus, it adds a start bit, a parity bit, and
a stop bit, creating the data packet. Next, the data packet is output serially, bit by bit at the Tx
pin. The receiving UART reads the data packet bit by bit at its Rx pin. The receiving
UART then converts the data back into parallel form and removes the start bit, parity bit, and
stop bits. Finally, the receiving UART transfers the data packet in parallel to the data bus on
the receiving end.
Circuit Connection
13
Steps:
1. Type the program in the Arduino IDE.
2. Connect the Arduino board with CPU using the USB cable
3. Save the program
4. Tools -> Board -> Select Arduino UNO
5. Tools -> Port -> Select port for Arduino UNO
6. Verify the program
7. Upload the program
8. Click on serial monitor (right hand side corner) for output.
Code:
void setup() {
// put your setup code here, to run once
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("HELLOWORLD");
delay(500);
}
Output:
14
PROGRAM 2 - POINT-TO-POINT COMMUNICATION OF TWO MOTES OVER THE RADIO
FREQUENCY.
Components required: 2 Arduino boards (UNO or MEGA), 2 data cables, 2 Bread boards, 2
HC-12 modules, connecting wires
HC-12
HC-12 wireless serial port communication module is a new-generation multichannel
embedded wireless data transmission module. Its wireless working frequency band is 433.4-
473.0MHz, multiple channels can be set, with the stepping of 400 KHz, and there are totally
100 channels. The maximum transmitting power of module is 100mW (20dBm), the
receiving sensitivity is -117dBm at baud rate of 5,000bps in the air, and the communication
distance is 1,000m in open space.
The module is encapsulated with stamp hole, can adopt patch welding, and its dimension is
27.8mm × 14.4mm × 4mm (including antenna cap, excluding spring antenna), so it is very
convenient for customers to go into application system. There is a PCB antenna pedestal
ANT1 on the module, and user can use external antenna of 433M frequency band through
coaxial cable; there is also an antenna solder eye ANT2 in the module, and it is convenient
for user to weld spring antenna. User could select one of these antennas according to use
requirements.
Product Features
1. Long-distance wireless transmission (1,000m in open space/baud rate 5,000bps in
the air)
2. Working frequency range (433.4-473.0MHz, up to 100 communication channels)
3. Maximum 100mW (20dBm) transmitting power (8 gears of power can be set)
4. Three working modes, adapting to different application situations
5. Built-in MCU, performing communication with external device through serial port
6. The number of bytes transmitted unlimited to one time
15
7. Update software version through serial port
Product Application
1. Wireless sensor
2. Community building security
3. Robot wireless control
4. Industrial remote control and telemetering
5. Automatic data acquisition
6. Container information management
7. POS system
8. Wireless acquisition of gas meter data
9. Vehicle keyless entry system
10. PC wireless networking
Circuit Connection
16
Steps:
1. Type the programs in the Arduino IDE for sender and receiver separately in two
different tabs.
2. Sender side: ARDUINO MEGA or ARDUINO UNO
2 -> TX
3 -> RX
GND -> GND
5V -> VCC
Arduino HC-12
2 -> TX
3 -> RX
GND -> GND
5V -> VCC
4. Connect the Arduino boards with CPU using the USB cable (separately for
sender board and receiver board)
5. Save the programs
6. Sender side: Tools -> Board -> Select Arduino MEGA
7. Sender side: Tools -> Port -> Select port for Arduino MEGA
8. Receiver side: Tools -> Board -> Select Arduino UNO
9. Receiver side: Tools -> Port -> Select port for Arduino UNO
10. Sender side: Compile the
program Upload the program
Click on serial monitor (right hand side corner) for output
11. Receiver side: Compile the
program Upload the program
Click on serial monitor (right hand side corner) for output
12. On the sender’s side, type any text and click on send
13. The sent text will appear on the receiver’s side
Code:
Sender
//HC-12 messenger send/receive
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX
void setup() {
17
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
if(Serial.available() > 0){//Read from serial monitor and send over HC-
12 String input = Serial.readString();
mySerial.println(input);
}
if(mySerial.available() > 1){//Read from HC-12 and send to serial
monitor String input = mySerial.readString();
Serial.println(input);
}
delay(20);
}
Receiver
//HC-12 messenger send/receive
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
if(Serial.available() > 0){//Read from serial monitor and send over HC-
12 String input = Serial.readString();
mySerial.println(input);
}
if(mySerial.available() > 1){//Read from HC-12 and send to serial
monitor String input = mySerial.readString();
Serial.println(input);
}
delay(20);
}
Output:
18
PROGRAM 4 - I2C PROTOCOL STUDY
Components required: Arduino board (UNO), data cable, Sensors GY-521, (GY-9250
GY-6500), I2C pressure sensor (0x3C), connecting wires
ADXL345
ADXL345 is a 3-axis accelerometer with high-resolution (13-bit) measurement at up to
±16 g. Digital output data is formatted as 16-bit twos complement and is accessible
through either an SPI (3- or 4-wire) or I2C digital interface.
The ADXL345 is well suited for mobile device applications. It measures the static
acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration
resulting from motion or shock. Its high resolution (4 mg/LSB) enables measurement of
inclination changes less than 1.0°.
Several special sensing functions are provided. Activity and inactivity sensing detect the
presence or lack of motion and if the acceleration on any axis exceeds a user-set level.
Tap sensing detects single and double taps. Free-fall sensing detects if the device is
falling. These functions can be mapped to one of two interrupt output pins. An
integrated, patent pending, 32-level first in, first out (FIFO) buffer can be used to store
data to minimize host processor intervention.
Low power modes enable intelligent motion-based power management with threshold
sensing and active acceleration measurement at extremely low power dissipation. The
ADXL345 is supplied in a small, thin, 3 mm × 5 mm × 1 mm, 14-lead, plastic package.
19
Applications
Handsets
Medical instrumentation
Gaming and pointing devices
Industrial instrumentation
Personal navigation devices
Hard disk drive (HDD) protection
GY 521- MPU-6050
The accelerometer measures the acceleration along one direction, while the gyroscope
measures the angular acceleration on one axis. The analogic pins are not set on INPUT
because it's their default setting. The values read by the analogic pins will be sent to
the serial port. Open the Serial Monitor, move the sensor and try to see how the values
change.
Circuit Connection
20
Steps:
1. Type the program in the Arduino IDE – scan and data files separately in two
different files
2. Sensors must have clock (SCL), data (SDA), VCC and GND
3. Connect the two sensor points vertically to single port in horizontal bread board
and from that point make a single connection to the Arduino board
4. Bread board Arduino board
VCC of two sensors to vertical ports in bread board -> 5V
2 GNDs to bread board -> GND
-> from bread board take wire and connect to
2 SCL of sensors to vertical point bread board right top first port
2 SDA to bread board -> Right top second port (above pin 13 and GND)
5. Connect the Arduino board with CPU using the USB cable
6. Save the programs
7. Tools -> Board -> Select Arduino UNO (for both scan and data files)
8. Tools -> Port -> Select port for Arduino UNO (for both scan and data files)
9. Compile the program (for both scan and data files)
10. Upload the program (for both scan and data files)
11. Click on serial monitor of scan file (right hand side corner) for output (output: no.
of devices connected as well as addresses of those sensors), then close the serial
monitor
12. In the code -> data file (take the address from Step 11 and change it in data
file) #include<wire.h>
Int Adxladdress = 0x68; -> sensor address
Int Adxladdress = 0x3C; -> sensor address
Save the changes
13. Click on serial monitor of data file (right hand side corner) for output
Code:
Scan File:
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
21
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the
address. Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address
0x"); if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Data File:
22
#include <Wire.h>
int ADXLAddress = 0x3C; // Device address in which is also included the 8th bit for
selecting the mode, read in this case.
//#define X_Axis_Register_DATAX0 0x0D // Hexadecima address for the DATAX0 internal
register.
//#define X_Axis_Register_DATAX1 0x0E // Hexadecima address for the DATAX1 internal
register.
#define X_Axis_Register_DATAX0 0x0D // Hexadecima address for the DATAX0 internal
register.
#define Y_Axis_Register_DATAX1 0x0E
#define Z_Axis_Register_DATAX2 0x0F // Hexadecimal address for the DATAX0 internal
register.
//#define X_Axis_Register_DATAX1 0x0E // Hexadecimal address for the DATAX1
internal register.// Hexadecimal address for the DATAX1 internal register.
#define Power_Register 0x2D // Power Control Register
int X0,X1,X2,X_out;
void setup() {
Wire.begin(); // Initiate the Wire library
Serial.begin(9600);
delay(5000);
// Enable measurement
Wire.beginTransmission(ADXLAddress);
Wire.write(Power_Register);
// Bit D3 High for measuring enable (0000
1000) Wire.write(8);
Wire.endTransmission();
}
void loop() {
Wire.beginTransmission(ADXLAddress); // Begin transmission to the Sensor
//Ask the particular registers for data
Wire.write(X_Axis_Register_DATAX0);
Wire.write(Y_Axis_Register_DATAX1);
Wire.write(Z_Axis_Register_DATAX2);
Wire.endTransmission(); // Ends the transmission and transmits the data from the two
//registers
Wire.requestFrom(ADXLAddress,3); // Request the transmitted two bytes from the two
//registers
if(Wire.available()<=3) {
X0 = Wire.read(); // Reads the data from the register
X1 = Wire.read();
23
X2 = Wire.read();
}
Serial.print("X0= ");
Serial.print(X0);
Serial.print(" X1= ");
Serial.println(X1);
Serial.print(" X2= ");
Serial.println(X2);
}
Output:
24
PROGRAM 5 - READING TEMPERATURE AND RELATIVE HUMIDITY VALUE FROM THE SENSOR
Components required: Arduino board (UNO), data cable, DHT11 sensor
DHT11 sensor
This DHT11 Temperature and Humidity Sensor features a calibrated digital signal
output with the temperature and humidity sensor capability. It is integrated with a high-
performance 8-bit microcontroller. Its technology ensures the high reliability and excellent
long-term stability. This sensor includes a resistive element and a sensor for wet NTC
temperature measuring devices. It has excellent quality, fast response, anti-interference
ability and high performance.
Specification
1. Supply Voltage: +5 V
2. Temperature range :0-50 °C error of ± 2 °C
3. Humidity :20-90% RH ± 5% RH error
4. Interface: Digital
Circuit Connection
25
Steps:
1. Type the program in the Arduino IDE
2. Make the following connections:
Sensor Board
DATA -> 7
GND -> GND
VCC -> 5V
3. Connect the Arduino board with CPU using the USB cable
4. Save the program
5. Tools -> Board -> Select Arduino UNO
6. Tools -> Port -> Select port for Arduino UNO
7. Compile the program
8. Upload the program
9. Click on serial monitor (right hand side corner) for output.
10. To include Header files/Libraries:
Download DHT library -> In IDE, Sketch -> Include library -> Add zip library -> Search
the folders (Downloads) -> DHT library -> open
Code:
#include <dht.h>
dht DHT;
#define DHT11_PIN 7
void setup(){
Serial.begin(9600);
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(1000);
}
26
Output:
27
REFERENCES:
https://www.arduino.cc/
https://components101.com/dht11-temperature-sensor
https://create.arduino.cc/projecthub/Nicholas_N/how-to-use-the-accelerometer-gyroscope-gy-521-6dfc19
https://www.digikey.com/en/product-highlight/a/analog-devices/adxl345-3-axis-digital-accelerometer
https://www.rhydolabz.com/wiki/?p=1417
https://www.instructables.com/id/Long-Range-18km-Arduino-to-Arduino-Wireless-Commun/
https://www.elecrow.com/download/HC-12.pdf
28