Janhavi IoT
Janhavi IoT
Bachelor of Technology
SEMESTER – VII
YEAR 2022
URN: 2019-B-28092001
School of Engineering
Ajeenkya D Y Patil University, Pune
CERTIFICATE
This is to certify that Miss Janhavi Ghogle bearing URN number 2019-B-
2
INDEX
Sr. No. Problem Statements / Exercise Page NO. C-D-I-O
(Model)
1 Write a program using Arduino IDE for blinking 1 I
LED.
2 Write a program for RGB LED using Arduino. 2 I
3 Write a program to Interface five different 4 I
sensors with Arduino Uno.
4 Write a program to Interface Arduino Uno with 7 I
motors (stepper and servo).
5 Write a program to Interface Arduino Uno with 9 I
relays and buzzer.
6 Write a program to Interface Arduino Uno with 10 I
LCD display.
7 Write a program to Interface Arduino Uno with 12 I
ESP8266WiFi module.
8 Write a program to Interface Arduino Uno with 14 I
HC05/06 Bluetooth module.
9 Study and implement MQTT protocol using 16 I
Arduino Uno.
10 Write a program to build your custom app to 22 I
control Arduino Uno.
11 IoT based mini project 24 I
3
Sl No.1:
Write a program using Arduino IDE for blinking LED.
Code:
void setup() { // initialize digital pin 13 as an output.
pinMode(2, OUTPUT);
}
loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Output:
1
Sl No.2:
Write a program for RGB LED using Arduino.
Code:
int red_light_pin= 11;
int green_light_pin = 5;
int blue_light_pin = 2;
void setup() {
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
void loop() {
RGB_color(255, 0, 0); // Red
delay(1000);
RGB_color(0, 255, 0); // Green
delay(1000);
RGB_color(0, 0, 255); // Blue
delay(1000);
RGB_color(255, 255, 125); // Raspberry
delay(1000);
RGB_color(0, 255, 255); // Cyan
delay(1000);
RGB_color(255, 0, 255); // Magenta
delay(1000);
RGB_color(255, 255, 0); // Yellow
delay(1000);
RGB_color(255, 255, 255); // White
delay(1000);
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}
2
Output:
3
Sl No.3:
Write a program to Interface five different sensors with Arduino Uno.
Code:
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define LDR_PIN 2
#define trigPin 10
#define echoPin 9
void loop() {
float temperature = dht.readTemperature();
4
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
int analogValue = analogRead(A0); // Convert the analog value into lux value:
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.println(F("% "));
Serial.print(F("Temperature: "));
Serial.print(temperature);
Serial.println(F("°C "));
Serial.print("Lux: ");
Serial.println(lux);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
5
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
delay(2000); // Wait a few seconds between measurements
}
Output:
6
Sl No.4:
Write a program to Interface Arduino Uno with motors (stepper and servo).
Code:
/*
No code here - use the pushbutton to rotate a single step, and the switch to
control the direction
*/
#include <Servo.h>
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15ms for the servo to reach the
position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15ms for the servo to reach the
position
}
}
7
Output:
8
Sl No.5:
Write a program to Interface Arduino Uno with relays and buzzer.
Code:
const int relayPin = 13;
void setup()
{
pinMode( relayPin, OUTPUT);
}
void loop()
{
digitalWrite( relayPin, HIGH);
delay( 600);
digitalWrite( relayPin, LOW);
delay( 600);
}
Output:
9
Sl No.6:
Write a program to Interface Arduino Uno with LCD display.
Code:
// include the library code:
#include <LiquidCrystal.h>
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" Janhavi");
void loop() {
// Turn off the display:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
delay(500);
}
10
Output:
11
Sl No.7:
Write a program to Interface Arduino Uno with ESP8266WiFi module.
Code:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLED led1(V1);
BlynkTimer timer;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, blinkLedWidget);
}
void loop()
{
Blynk.run();
timer.run();
}
12
Output:
13
Sl No.8:
Write a program to Interface Arduino Uno with HC05/06 Bluetooth module.
Code:
char Incoming_value = 0; //Variable for storing
Incoming_value
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second
(baud) for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0)
{
Incoming_value = Serial.read(); //Read the incoming data and
store it into variable Incoming_value
Serial.print(Incoming_value); //Print Value of
Incoming_value in Serial monitor
Serial.print("\n"); //New line
if(Incoming_value == '1') //Checks whether value of
Incoming_value is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(Incoming_value == '0') //Checks whether value of
Incoming_value is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
14
Output:
15
Sl No.9:
Study and implement MQTT protocol using Arduino Uno.
Code:
SUBSCRIBER:
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass)! = WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
// subscribe to a topic
mqttClient.subscribe(topic);
mqttClient.subscribe(topic2);
mqttClient.subscribe(topic3);
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Topic: ");
Serial.println(topic2);
Serial.print("Topic: ");
Serial.println(topic3);
Serial.println();
}
void loop() {
17
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alive which avoids being disconnected by the broker
mqttClient.poll();
}
PUBLISHER:
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
18
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
int count = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
void loop() {
// call poll() regularly to allow the library to send MQTT keep alive which
19
// avoids being disconnected by the broker
mqttClient.poll();
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print(Rvalue);
mqttClient.endMessage();
mqttClient.beginMessage(topic2);
mqttClient.print(Rvalue2);
mqttClient.endMessage();
mqttClient.beginMessage(topic3);
mqttClient.print(Rvalue3);
mqttClient.endMessage();
Serial.println();
}
20
}
Output:
21
Sl No.10:
Write a program to build your custom app to control Arduino Uno.
Code:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLED led1(V1);
BlynkTimer timer;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, blinkLedWidget);
}
void loop()
{
Blynk.run();
timer.run();
}
22
Output:
23
Sl No.11:
IoT based mini project
Introduction:
Using basic electronics and sensor array, we produce affordable and reliable
reading of biological data.
Hardware:
1. TP4056 Controller
2. AD8232 ECG Monitor Sensor
3. Arduino Pro Mini
4. Node MCU 1.0
5. TM4506 Power Supply Module
CODE:
/*
Optical SP02 Detection (SPK Algorithm) using the MAX30105 Breakout
By: Nathan Seidle @ SparkFun Electronics
Date: October 19th, 2016
https://github.com/sparkfun/MAX30105_Breakout
It is best to attach the sensor to your finger using a rubber band or other tightening device.
Humans are bad at applying constant pressure to a thing. When you press your finger against
the sensor it varies enough to cause the blood in your finger to flow differegenerallyntly which
causes the sensor readings to go wonky.
24
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board
with 5V but it will also run at 3.3V.
*/
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>
#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
#define String httpGETRequest(const char* serverName)
//MAX30105 particleSensor;
#else
uint32_t irBuffer[100]; //infrared LED sensor data
25
uint32_t redBuffer[100]; //red LED sensor data
#endif
int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
pinMode(pulseLED, OUTPUT);
pinMode(readLED, OUTPUT);
pinMode(A0, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(5, INPUT);
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println(F("MAX30105 was not found. Please check wiring/power."));
while (1);
}
/*
Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
while (Serial.available() == 0) ; //wait until user presses a key
Serial.read();
*/
26
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
void loop()
{
27
HTTPClient http;
sensorReadings = httpGETRequest(serverName);
Serial.println(sensorReadings);
JSONVar myObject = JSON.parse(sensorReadings);
doc["red"] = values[0];
doc["IR"] = values[1];
doc["Spo2"] = values[2];
doc["ECG"] = values[3];
delay(1000);
}
}
int Processing()
{
bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
//read the first 100 samples, and determine the signal range
for (byte i = 0 ; i < bufferLength ; i++)
{
28
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
/*
Serial.print(F("red="));
Serial.print(redBuffer[i], DEC);
Serial.print(F(", ir="));
Serial.println(irBuffer[i], DEC);
*/
}
//calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2,
&validSPO2, &heartRate, &validHeartRate);
//Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1
second
while (1)
{
if(digitalRead(5)==1)
{
for(int x=0; x<10; x++)
{
//dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to
the top
for (byte i = 25; i < 100; i++)
{
redBuffer[i - 25] = redBuffer[i];
irBuffer[i - 25] = irBuffer[i];
}
29
digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
//Serial.print(x);
//Serial.print(',');
//Serial.print(F("red="));
Serial.print(redBuffer[i], DEC);
Serial.print(',');
//Serial.print(F(", ir="));
Serial.print(irBuffer[i], DEC);
Serial.print(',');
//Serial.print(F(", HR="));
Serial.print(heartRate, DEC);
Serial.print(',');
//Serial.print(F(", HRvalid="));
//Serial.print(validHeartRate, DEC);
//Serial.print(F(", SPO2="));
Serial.print(spo2, DEC);
//Serial.print(',');
//ecg
Serial.println(analogRead(A0));
//Serial.print(F(", SPO2Valid="));
//Serial.println(validSPO2, DEC);
}
values[0] = redBuffer;
values[1] = irBuffer;
values[2] = spo2;
values[3] = analogRead(A0);
return values;
}
}
}
}
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
31
// Free resources
http.end();
return payload;
}
32
Circuit:
33
34