0% found this document useful (0 votes)
1 views5 pages

Programs IoT

The document contains multiple Arduino code snippets for various projects, including light sensing, BLE control for an LED, ultrasonic distance measurement, vibration detection, and temperature/humidity monitoring. Each code snippet includes setup and loop functions, demonstrating how to read sensor data and control outputs based on conditions. Additionally, it features a signature from G B Janardhana Swamy, an Assistant Professor at The National Institute of Engineering, Mysore.
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)
1 views5 pages

Programs IoT

The document contains multiple Arduino code snippets for various projects, including light sensing, BLE control for an LED, ultrasonic distance measurement, vibration detection, and temperature/humidity monitoring. Each code snippet includes setup and loop functions, demonstrating how to read sensor data and control outputs based on conditions. Additionally, it features a signature from G B Janardhana Swamy, an Assistant Professor at The National Institute of Engineering, Mysore.
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

1.

int light;
void setup() {
pinMode(13, OUTPUT);
[Link](9600);
}

void loop() {
// put your main code here, to run repeatedly:
light= analogRead(A0);
if(light<250){
digitalWrite(13,HIGH);
}else{
digitalWrite(13,LOW);
}

[Link](light);
delay(100);
}

2.#include <ArduinoBLE.h>

#define LED_PIN 7

BLEService ledService("180A"); // Define BLE Service


BLEByteCharacteristic ledCharacteristic("2A57", BLERead | BLEWrite); // Define
LED Characteristic

void setup() {
[Link](115200);
while (!Serial);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);

// Start BLE
if (![Link]()) {
[Link]("Failed to start BLE!");
while (1);
}

[Link]("BLE Initialized. Setting up Service...");

// Set Service & Characteristic


[Link]("Arduino_R4_LED");
[Link](ledService);
[Link](ledCharacteristic);
[Link](ledService);
[Link](0); // Default OFF

// Start Advertising
[Link]();
[Link]("BLE Ready! Connect and send '1' to turn ON, '0' to turn OFF.");
}

void loop() {
BLEDevice central = [Link](); // Wait for a BLE device

if (central) {
[Link]("Connected to: ");
[Link]([Link]());

while ([Link]()) {
if ([Link]()) {
byte ledState;
[Link](ledState); // Correct way to read value

if (ledState == '1') {
digitalWrite(LED_PIN, HIGH);
[Link]("LED ON");
} else if (ledState == '0') {
digitalWrite(LED_PIN, LOW);
[Link]("LED OFF");
}
}
}

[Link]("Disconnected from BLE device.");


}
}
3.
/ Define pins
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 7;

// Variables for distance calculation


long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
[Link](9600);
}

void loop() {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;

// Print to serial
[Link]("duration: ");
[Link](duration);
[Link]("Distance: ");
[Link](distance);
[Link](" cm");

// Blink LED if distance is less than 20 cm


if (distance < 10) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
} else {
digitalWrite(ledPin, LOW);
}

delay(100);
}

[Link] int vibrationPin = 2; // DO pin from sensor module


const int ledPin = 7; // External LED

void setup() {
pinMode(vibrationPin, INPUT); // No need for INPUT_PULLUP with module
pinMode(ledPin, OUTPUT);
[Link](9600);
}

void loop() {
int vibrationState = digitalRead(vibrationPin);

if (vibrationState == LOW) { // LOW = Vibration detected


digitalWrite(ledPin, HIGH);
[Link]("Vibration Detected!");
delay(200);
} else {
digitalWrite(ledPin, LOW);
}

delay(50);
}

5.
#include <DHT.h>

// DHT11 setup
#define DHTPIN 2 // DHT11 data pin connected to D2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Temperature Threshold
const float tempThreshold = 30.0; // Alert if temperature > 30°C

void setup() {
[Link](115200);
unsigned long startMillis = millis();
while (!Serial && millis() - startMillis < 5000); // Wait max 5 sec for Serial Monitor

[Link]("Initializing DHT sensor...");


[Link]();

void loop() {
float temp = [Link](); // Read temperature in Celsius
float humidity = [Link](); // Read humidity

// Check if reading is valid


if (isnan(temp) || isnan(humidity)) {
[Link]("Failed to read from DHT sensor!");
delay(2000); // Retry every 2 seconds
return;
}

// Print temperature and humidity


[Link]("Temperature: ");
[Link](temp);
[Link]("°C | Humidity: ");
[Link](humidity);
[Link]("%");

// Check temperature threshold


if (temp > tempThreshold) {

[Link](" ALERT: Temperature exceeded threshold!");


// You can extend this to send HTTP request, Email, Blynk, MQTT here
}

delay(5000); // Read every 5 seconds


}

Thanks and Regards,


G B Janardhana Swamy
Assistant Professor & Program Coordinator, Dept. of CSE
IIC Convenor & OBE Committee Member
The National Institute of Engineering, Mysore

+91 98443 66119

Mon, Mar 2, 9:37 AM


G B Janardhana Swamy <gbjanardhanaswamy@[Link]>
(10 days ago)
to Manoj, Harshini, Nishanth

Thanks and Regards,


G B Janardhana Swamy
Assistant Professor & Program Coordinator, Dept. of CSE
IIC Convenor & OBE Committee Member
The National Institute of Engineering, Mysore

+91 98443 66119

You might also like