Experiment Number 1:
Program 1.1.1: To interface LED with Arduino and Write a
program to turn ON LED for 1 sec after every 2 seconds
int led = 12;
void setup() {
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
digitalWrite(led, LOW);
delay(2000);
digitalWrite(led, HIGH);
delay(1000);
}
Program 1.1.2: To interface Buzzer with Arduino and Write a
program to turn ON LED for 1 sec after every 2 seconds
#define LED_PIN 13
int buzzerPin = A5;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(2000);
}
Program 1.2.1: To interface Push buttion with Arduino and Write
a program to turn ON LED when push buttion is pressed.
#define LED_PIN 12
#define BUTTON_PIN A5
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
Program 1.2.2: To interface Push Digital sensor (IR) with
Arduino and Write a program to turn ON LED when at sensor
detection
#define LED_PIN 12
#define IR_SENSOR_PIN 6
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(IR_SENSOR_PIN, INPUT);
}
void loop() {
if (digitalRead(IR_SENSOR_PIN) == LOW) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
Program 1.2.3: To interface Push Digital sensor (LDR) with
Arduino and Write a program to turn ON LED when at sensor
detection
const int LEDPin = 12; // Initialize Pin 12 for LED
const int LDRPin = A1; // Initialize LDR Pin A1
void setup() {
Serial.begin(9600);
pinMode(LEDPin, OUTPUT); // Define pin as output
pinMode(LDRPin, INPUT); // Define LDR pin as input
}
void loop() {
int ldrStatus = analogRead(LDRPin); // Read LDR light intensity as analog
value
// Control based on LDR light intensity
if (ldrStatus <= 500) {
digitalWrite(LEDPin, LOW); // Turn LED on
Serial.print(" LIGHT OFF : ");
Serial.println(ldrStatus);
} else {
digitalWrite(LEDPin, HIGH); // Turn LED off
Serial.print("LIGHT ON: ");
Serial.println(ldrStatus); // Print LDR analog value on the serial port
}
}
Experiment Number 2:
Program 2.1.1: To interface DHT11 sensor with Arduino and write a
program to print temperature and humidity readings.
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
Program 2.1.2: To interface OLED with Arduino and write a
program to print temperature and humidity readings on it.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -
1);
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for
128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Temp: " + String(t) + " C");
display.println("Humidity: " + String(h) + " %");
display.display();
}
Experiment Number 3:
Program 3.1.1: To interface motor using relay with Arduino and
write a program to turn on motor when push buttion is pressed.
const int BUTTON_PIN = A5;
const int RELAY_PIN = 9;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
}
Experiment Number 4:
Program 4.1.1: To interface Bluetooth with Arduino and write a
program to turn on LED ON/OFF when ‘I’/’O’ IS Received from
Smartphone Using Bluetooth.
#include<SoftwareSerial.h>
SoftwareSerial btt(3, 2); // RX, TX - of Arduino.
int led = 12;
void setup() {
pinMode(led, OUTPUT);
btt.begin(9600);
while (!btt);
btt.println("Input 6 to Turn LED on and 5 to Turn LED off"); // corrected the
message
}
void loop() {
if (btt.available()) {
int state = btt.parseInt();
if (state == 6) {
digitalWrite(led, HIGH);
btt.println("Command completed: LED turned ON"); // corrected the
message
}
if (state == 5) {
digitalWrite(led, LOW);
btt.println("Command completed: LED turned OFF"); // corrected the
message
}
}
}
Experiment Number 5:
Program 5.1.1: To interface Bluetooth with Arduino and write
program to send sensor data to smartphone using Bluetooth.
#include <SoftwareSerial.h>
#include <DHT.h>
// Define DHT11 sensor pin
const int dhtPin = 7;
// Define Bluetooth module pins
const int bluetoothRx = 2; // RX pin of Arduino
const int bluetoothTx = 3; // TX pin of Arduino
// Define Bluetooth object
SoftwareSerial bluetooth(bluetoothRx, bluetoothTx);
// Define DHT sensor type
#define DHTTYPE DHT11 // DHT 11
// Initialize DHT sensor
DHT dht(dhtPin, DHTTYPE);
void setup() {
// Initialize serial communication with PC
Serial.begin(9600);
// Initialize serial communication with Bluetooth module
bluetooth.begin(9600);
// Initialize DHT sensor
dht.begin();
}
void loop () {
// Read sensor values
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Send sensor data over Bluetooth
bluetooth.print("Humidity: ");
bluetooth.print(humidity);
bluetooth.print("% Temperature: ");
bluetooth.print(temperature);
bluetooth.println("°C");
// Print sensor data to serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
delay(2000); // Delay between sensor readings (adjust as needed)
}