0% found this document useful (0 votes)
31 views14 pages

SS Lab Experiment Codes - AHKR

The document outlines various Arduino experiments including LED blinking, interfacing with sensors like PIR and DHT11, displaying temperature and humidity on an LCD, and controlling devices like servomotors and 7-segment displays. Each experiment includes code snippets demonstrating setup and loop functions for different functionalities. Additionally, it covers home automation through Bluetooth control and adjusting LED brightness with a potentiometer.
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)
31 views14 pages

SS Lab Experiment Codes - AHKR

The document outlines various Arduino experiments including LED blinking, interfacing with sensors like PIR and DHT11, displaying temperature and humidity on an LCD, and controlling devices like servomotors and 7-segment displays. Each experiment includes code snippets demonstrating setup and loop functions for different functionalities. Additionally, it covers home automation through Bluetooth control and adjusting LED brightness with a potentiometer.
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/ 14

SMART SYSTEMS LAB EXPERIMENTS

1.LED BLINKING (1 sec ON and 2 sec OFF)

void setup()
{
pinMode(13,OUTPUT);
}

void loop()
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(2000);
}
2.LED ON/OFF WITH PUSH BUTTON SWITCH

int a=0;

void setup()
{
pinMode(7,INPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
}

void loop()
{
a=digitalRead(7);
Serial.print("a=");
Serial.println(a);

if(a==1)
{
digitalWrite(10,HIGH);
}
else
{
digitalWrite(10,LOW);
}
}
3.PIR Sensor Interfacing to Arduino

int a=0;

void setup()
{
pinMode(2,INPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop()
{
a=digitalRead(2);
Serial.print("a=");
Serial.println(a);
if(a==1)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
}
LCD DISPLAY
#include <rgb_lcd.h>
rgb_lcd lcd;
void setup()
{
lcd.begin(16,2);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("2/4 EEE-A");
lcd.setCursor(0,1);
lcd.print("23B91A0201");
}

DISPLAY TEMP in LCD


#include <rgb_lcd.h>
#include <DHT.h>
DHT dht11(2,DHT11);
rgb_lcd lcd;
void setup()
{
lcd.begin(16,2);
dht11.begin();
}
void loop()
{
char a[14];
int temp=dht11.readTemperature()*10;
sprintf(a, "TEMP = %02u.%1u%cC", temp/10, temp%10, 223);
lcd.setCursor(0,0);
lcd.print(a);
delay(1000);
}

In Arduino, "sprintf" stands for "string print formatted" and is a C function used to create a formatted
string by inserting variables into a character array, essentially allowing you to combine text and
variable data into a single string for output.

lcd.print((char)223); = command to get it to print C°


Testing DHT11 (TEMP & HUMIDITY) in Serial Monitor
#include <DHT.h>
DHT dht11(2,DHT11);

void setup()
{

dht11.begin();
Serial.begin(9600);
}
void loop()
{

float humi=dht11.readHumidity();
Serial.print("Humidity=");
Serial.print(humi);
Serial.println("%");

float temp=dht11.readTemperature();
Serial.print("Temperature=");
Serial.print(temp);
Serial.println(" degree C");

delay(1000);
}

4.DISPLAY TEMP & HUMIDITY in LCD


#include <rgb_lcd.h>
#include <DHT.h>
DHT dht11(2,DHT11);
rgb_lcd lcd;
void setup()
{
lcd.begin(16,2);
dht11.begin();
}
void loop()
{
char a[14];
char b[14];
int humi=dht11.readHumidity()*10;
int temp=dht11.readTemperature()*10;
if(temp<0)
{
sprintf(a, "TEMP=-%02u.%1u%cC", abs(temp)/10, abs(temp)%10, 223);
}
else
{
sprintf(a, "TEMP=%02u.%1u%cC", temp/10, temp%10, 223);
}
lcd.setCursor(0,0);
lcd.print(a);
sprintf(b, "HUMIDITY=%02u.%1u%%", humi/10, humi%10);
lcd.setCursor(0,1);
lcd.print(b);
delay(1000);
}
DISPLAY DISTANCE in Serial Monitor

long duration;
int distance;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(10, INPUT);
}
void loop()
{
digitalWrite(9, HIGH);
delayMicroseconds(10);
digitalWrite(9, LOW);
duration = pulseIn(10, HIGH);
distance = duration * 0.0344 / 2; //convert microseconds to seconds
Serial.print("Distance= ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}

5.DISPLAY DISTANCE in LCD

#include <rgb_lcd.h>
rgb_lcd lcd;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0344 / 2; //convert microseconds to seconds
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Distance= ");
lcd.print(distance);
lcd.print("cm");
delay(500);
}
6.HOME AUTOMATION

//Install Android App - Arduino Bluetooth Control

char data=0;
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop()
{
if(Serial.available()>0)
{
data = Serial.read();
Serial.print(data);
if(data=='1')
{
digitalWrite(13,1);
}
else if(data=='0')
{
digitalWrite(13,0);
}
}
}
7.Adjust LED Brightness using Potentiometer (Analog Input & Digital Output)

void setup()
{
pinMode(11,OUTPUT);
pinMode(A1,INPUT);
Serial.begin(9600);
}

void loop()
{
int a = analogRead(A1);
int b = map(a,0,1023,0,255);
analogWrite(11,b);
Serial.print("potentiometer value=");
Serial.println(a);
Serial.print("brightness=");
Serial.println(b);
delay(100);
}
8. Interfacing 4 Digit 7 Segment Display Board to Arduino
(Scrolling the hexadecimal sequence in a 4 Digit 7 Segment Display Board
for every 0.5 sec.)

#include <TM1637Display.h>

#define CLK 2
#define DIO 3

TM1637Display display(CLK, DIO);

int8_t digits[] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
0x77, // A
0x7C, // B
0x39, // C
0x5E, // D
0x79, // E
0x71 // F
};

void setup()
{
display.setBrightness(7); // Set brightness (0-7)
}

void loop()
{
for (int i = 0; i < 16; i++)
{
Sequence(i);
}
}

void Sequence(int startIndex)


{
int8_t data[4];
for (int i = 0; i < 4; i++)
{
data[i] = digits[(startIndex + i) % 16];
}
display.setSegments(data);
delay(500);
}
9. Interfacing Servomotor to Arduino to control the position

#include<Servo.h>
Servo myservo;
void setup()
{
myservo.attach(11);
Serial.begin(9600);
}
void loop()
{
myservo.write(0);
}

Wiper Control

#include<Servo.h>
Servo myservo;
void setup()
{
myservo.attach(11);
Serial.begin(9600);
}
void loop()
{
for(int i=0;i<=180;i++)
{
int Position = i;
myservo.write(Position);
Serial.println(Position);
delay(10);
}
for(int i=180;i>=0;i--)
{
int Position = i;
myservo.write(Position);
Serial.println(Position);
delay(10);
}
}
Interfacing LCD with I2C Module to Arduino

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.init();
lcd.backlight();
}

void loop()
{
lcd.setCursor(0,0);
lcd.print("Hello");
}

You might also like