0% found this document useful (0 votes)
67 views7 pages

IoT Workshop Handout Programs

The document describes code examples for blinking an LED, fading an LED, using an ultrasonic sensor, using a temperature sensor, using a PIR motion sensor, and using a soil moisture sensor with a relay and motor.

Uploaded by

Mayur Shet
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)
67 views7 pages

IoT Workshop Handout Programs

The document describes code examples for blinking an LED, fading an LED, using an ultrasonic sensor, using a temperature sensor, using a PIR motion sensor, and using a soil moisture sensor with a relay and motor.

Uploaded by

Mayur Shet
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/ 7

Hands-on IoT Workshop

Blinking an LED Bulb


Code:
int ledPin=8; //definition digital 8 pins as pin to control
the LED
void setup()
{
pinMode(ledPin,OUTPUT); //Set the digital 8 port mode,
OUTPUT: Output mode
}
void loop()
{
digitalWrite(ledPin,HIGH); //HIGH is set to about 5V PIN8
delay(1000); //Set the delay time, 1000 = 1S
digitalWrite(ledPin,LOW); //LOW is set to about 5V PIN8
delay(1000); //Set the delay time, 1000 = 1S
}

Pin Diagram:

PESU Center for Internet of Things 1


Department of CSE, PES University
Hands-on IoT Workshop

Fading in and out an LED Bulb


This example shows how to fade an LED 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.

Code:
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:


void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:


brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the


fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

Pin Diagram:

PESU Center for Internet of Things 2


Department of CSE, PES University
Hands-on IoT Workshop

Using the Ultrasonic Sensor


Code:
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-
SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-
SR04

// defines variables
long duration; // variable for the duration of sound wave
travel
int distance; // variable for the distance measurement

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting
with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print
some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// Clears the trigPin condition
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 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}

Pin Diagram:

PESU Center for Internet of Things 3


Department of CSE, PES University
Hands-on IoT Workshop

Using the Temperature Sensor


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(2000);
}

Pin Diagram:

Before you can use the DHT11 on the Arduino, you’ll need to install the DHTLib
library. It has all the functions needed to get the humidity and temperature
readings from the sensor. It’s easy to install, just download the DHTLib.zip file
below and open up the Arduino IDE. Then go to Sketch>Include Library>Add .ZIP
Library and select the DHTLib.zip file.
Library Link: https://www.circuitbasics.com/how-to-set-up-the-dht11-
humidity-sensor-on-an-arduino/

PESU Center for Internet of Things 4


Department of CSE, PES University
Hands-on IoT Workshop

PIR Motion Sensor


Code:
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int piroutpin = 3;
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(piroutpin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(piroutpin, LOW);
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(piroutpin) == HIGH){
digitalWrite(ledPin, HIGH);
if(lockLow){
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(piroutpin) == LOW){
digitalWrite(ledPin, LOW);
if(takeLowTime){
lowIn = millis();
takeLowTime = false;
}
if(!lockLow && millis() - lowIn > pause){

PESU Center for Internet of Things 5


Department of CSE, PES University
Hands-on IoT Workshop

lockLow = true;
Serial.print("motion ended at ");
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}

Pin Diagram:

PESU Center for Internet of Things 6


Department of CSE, PES University
Hands-on IoT Workshop

Soil Moisture with Relay and Motor


Code:
int Relay = 13;
int sensor = 8;
int val;
void setup() {
pinMode(13,OUTPUT); //Set pin 13 as OUTPUT pin, to send
signal to relay
pinMode(8,INPUT); //Set pin 8 as input pin, to receive data
from Soil moisture sensor.
}

void loop() {
val = digitalRead(8);
if(val == LOW)
{
digitalWrite(13,LOW); //if soil moisture sensor provides LOW
value send LOW value to relay
}
else
{
digitalWrite(13,HIGH); //if soil moisture sensor provides
HIGH value send HIGH value to relay
}
delay(400);
}

Pin Diagram:

PESU Center for Internet of Things 7


Department of CSE, PES University

You might also like