Arduino UNO - Lab Manual-5-33

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Experiment 1:

AIM: To Interface Liquid Crystal Display (LCD) with Arduino uno.

PIN DIAGRAM:

• Click on Start > Arduino Application > type code

Connections from Arduino uno to LCD Module CN18 TO CN29

5
Code :
#include <LiquidCrystal.h> // include the library code:
// with the arduino pin number it is connected to
const int rs = 6, en = 7, d4 =2, d5 = 3, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print(" WELCOME TO "); // Print a message to the LCD.
lcd.setCursor(0,1); // setting cursor
lcd.print("PHYSITECH IoTKit");
delay(2000);
lcd.clear();
lcd.print(" WELCOME TO ");
}
void loop() {
lcd.display();// Turn on the display:
delay(5000);
}
Upload code from sketch option and then connect CN22 TO C29 & CN23 TO C31

6
Experiment 2:

AIM: To Interface Light Emitting Diode (LED) with Arduino uno.

PIN DIAGRAM:

• Click on Start > Arduino Application > type code


Code
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2,HIGH); // turn on led
digitalWrite(3,HIGH); // turn on led
digitalWrite(4,HIGH); // turn on led
digitalWrite(5,HIGH); // turn on led
delay(800); // delay for repeatation of on and off
digitalWrite(2,LOW); // turn off led
digitalWrite(3,LOW); // turn off led
digitalWrite(4,LOW); // turn off led
digitalWrite(5,LOW); // turn off led
}
Upload code from sketch option and then connect CN23 TO CN11

7
Experiment 3:

AIM: To Interface Switch and LED’s Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to Led’s and switches

8
Code:
const int buttonPin1 = 6; // number of pushbutton 1 pin
int buttonState1 = LOW; // set the default variable value for pushbutton1 status
const int ledPin1 = 5; // number of the LED 1 pin
const int buttonPin2 = 7; // number of pushbutton 2 pin
int buttonState2 = LOW; // set the default variable value for pushbutton2 status
const int ledPin2 = 4; // number of the LED 2 pin
const int buttonPin3 = 8; // number of pushbutton 3 pin
int buttonState3 = LOW; // set the default variable value for pushbutton3 status
const int ledPin3 = 3; // number of the LED 3 pin
const int buttonPin4 = 9; // number of pushbutton 4 pin
int buttonState4 = LOW; // set the default variable value for pushbutton4 status
const int ledPin4 = 2; // number of the LED 4 pin
void setup() { // Set Pins to Outputs Or Inputs
pinMode(buttonPin1, INPUT); // initialize the pushbutton pins as an inputs:
pinMode(ledPin1, OUTPUT); // initialize the LED pins as an outputs:
pinMode(buttonPin2, INPUT); // initialize the pushbutton pins as an inputs:
pinMode(ledPin2, OUTPUT); // initialize the LED pins as an outputs:
pinMode(buttonPin3, INPUT); // initialize the pushbutton pins as an inputs:
pinMode(ledPin3, OUTPUT); // initialize the LED pins as an outputs:
pinMode(buttonPin4, INPUT); // initialize the pushbutton pins as an inputs:
pinMode(ledPin4, OUTPUT); // initialize the LED pins as an outputs:
Serial.begin(9600); // initialize serial communication at 9600 baud
}

void loop() {
buttonState1 = digitalRead(buttonPin1); // read current states of the pushbutton value:
buttonState2 = digitalRead(buttonPin2); // read current states of the pushbutton value:
buttonState3 = digitalRead(buttonPin3); // read current states of the pushbutton value:
buttonState4 = digitalRead(buttonPin4); // read current states of the pushbutton value:

9
// check if the pushbutton is pressed buttonState# == HIGH/LOW
// if pressed change buttonState == HIGH to turn on ledPin#
// else if buttonState == LOW then digitalWrite(ledPin#, LOW) Keeps Led off.
if (buttonState1 == HIGH) { //check buttonState
digitalWrite(ledPin1, LOW); //if HIGH turn LED on:
} else {
digitalWrite(ledPin1, HIGH); // turn LED off:
}
Serial.println(buttonState1); //Print buttonState to serial
if (buttonState2 == HIGH) { //check buttonState
digitalWrite(ledPin2, LOW); //if HIGH turn LED on:
} else {
digitalWrite(ledPin2, HIGH); // turn LED off:
delay(10);
}
Serial.println(buttonState2); //Print buttonState to serial
if (buttonState3 == HIGH) { //check buttonState
digitalWrite(ledPin3, LOW); //if HIGH turn LED on:
} else {
digitalWrite(ledPin3, HIGH); // turn LED off:
delay(10);
Serial.println(buttonState3); //Print buttonState to serial
}
if (buttonState4 == HIGH) { //check buttonState
digitalWrite(ledPin4, LOW); //if HIGH turn LED on:
} else {
digitalWrite(ledPin4, HIGH); // turn LED off:
delay(10);
Serial.println(buttonState4); //Print buttonState to serial
}
}

Upload code from sketch option and then connect CN28 TO CN13 & CN23 TO CN11

10
Experiment 4:

AIM: To Interface Soil Moisture sensor with Arduino uno.

PIN DIAGRAM:

• Click on Start > Arduino Application > type code

11
Code

int soil_pin = A1;


void setup() {
pinMode(soil_pin, INPUT);
Serial.begin(9600);
}

void loop() {
int soil_value = analogRead(soil_pin);
Serial.print("Soil Value : ");
Serial.println(soil_value);
delay(1000);
}
Upload code from sketch option and then connect CN8 TO CN16

12
Experiment 5:

AIM: To Interface Gas sensor with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to Gas sensor

13
Code:
int gas_pin = A0;
void setup() {
pinMode(gas_pin, INPUT);
Serial.begin(9600);
}
void loop() {
int gas_value = analogRead(gas_pin);
Serial.print("Gas Value : ");
Serial.println(gas_value);
delay(1000);
}

Upload code from sketch option and then connect CN7 TO CN17

14
Experiment 6:

AIM: To Interface Bluetooth with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to Buletooth sensor

15
Code:
#include<SoftwareSerial.h>
#define TxD 11
#define RxD 10
SoftwareSerial bluetoothSerial(TxD, RxD);
char c;
void setup() {
bluetoothSerial.begin(9600);
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop() {
if(bluetoothSerial.available())
{c=bluetoothSerial.read();
Serial.println(c);
if(c=='1'){
digitalWrite(12, HIGH);
}
if(c=='2'){
digitalWrite(13, HIGH);
}
if(c=='3'){
digitalWrite(12, LOW);
}
if(c=='4'){
digitalWrite(13, LOW);
}
}

16
}

Upload code from sketch option and then connect CN5 TO CN19

Experiment 7:

AIM: To Interface DHT11 Module with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to DHT 11 Sensor

17
Code:
#include <dht.h>
#include <dht.h>
#include <DHT.h>
dht DHT;
#define DHT11_PIN 4
void setup(){
Serial.begin(9600);
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humi dity = ");
Serial.println(DHT.humidity);
delay(2000);
}
Upload code from sketch option and then connect CN4 TO CN26

18
Experiment 8:

AIM: To Interface Ultrasonic SR04 Module with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to Ultrasonic sensor

19
Code:
const int trigPin = 6;
const int echoPin = 7;
// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);
}
Upload code from sketch option and then connect CN6 TO CN25

20
Experiment 9:

AIM: To Interface Relay Module with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno from Relay Module

21
Code:
int RELAY = 13;
void setup() {
pinMode(RELAY,OUTPUT);
digitalWrite(RELAY,LOW);
Serial.begin(9600);
}
void loop()
{
digitalWrite(RELAY,LOW);
Serial.println("RELAY OFF");
delay(1000);
digitalWrite(RELAY,HIGH);
Serial.println("RELAY ON");
delay(2000);
}
Upload code from sketch option and then connect CN9 TO CN20

22
Experiment 10:

AIM: To Interface Servo Module with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to Servo motor

23
Code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 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
}
}
Upload code from sketch option and then connect CN3 TO C21

24
Experiment 11:

AIM: To Interface ESP8266 Module with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno board to ESP8266 Module

25
Code:

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include<SerialESP8266wifi.h>
#include<ThingSpeak.h>
#include<DHT.h>
#include<ArduinoJson.h>
#include <Wire.h>
String rawData;
#define RX 2
#define TX 3
const int rs = 16, en = 17, d4 =13, d5 = 12, d6 = 11, d7 = 10;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define DHTTYPE DHT11
#define dht_pin 4
#define light_pin 5
#define trig_pin 7
#define echo_pin 6
#define gas_pin A0
#define soil_pin A1
float temp = 0.0;
float humidity = 0.0;
int light = 1;
float distance = 0.0;
long duration;
int gas_value = 0;
int moisture_value = 0;

String AP = "PAL_DEMO_SYSTEM"; // CHANGE ME


String PASS = "kirankumar@1"; // CHANGE ME
String API = "GK9KOUPZEFVY23HC"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
SoftwareSerial esp8266(RX,TX);
StaticJsonBuffer<200> jsonBuffer;
DHT dht(dht_pin,DHTTYPE);
void setup()
{
Serial.begin(9600);
esp8266.begin(115200);

26
sendCommand("AT",5,"OK");
sendCommand("AT+IPR = 9600",5,"OK");
sendCommand("AT+UART_DEF=9600,8,1,0,0",5,"OK");
Serial.begin(9600);
esp8266.begin(9600);
lcd.begin(16, 2);
lcd.print("welcome");
//Serial.begin(9600);
//esp8266.begin(9600);
dht.begin();
pinMode(light_pin,INPUT);
pinMode(trig_pin,OUTPUT);
pinMode(echo_pin,INPUT);
pinMode(gas_pin,INPUT);
pinMode(soil_pin,INPUT);
//sendCommand("AT",5,"OK");
//sendCommand("AT+IPR = 9600",5,"OK");
//sendCommand("AT+UART_DEF=9600,8,1,0,0",5,"OK");
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
temp = dht.readTemperature();
Serial.print("Temperature : ");
Serial.println(temp);
humidity = dht.readHumidity();
Serial.print("Humidity : ");
Serial.println(humidity);
lcd.setCursor(0,0);
lcd.print("T:");
lcd.print(temp);
lcd.print(" H:");
lcd.print(humidity);
light = digitalRead(light_pin);
Serial.print("Light : ");
Serial.println(light);
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo_pin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
Serial.print("Distance : ");

27
Serial.print(distance);
Serial.println(" cm");
gas_value = analogRead(gas_pin);
Serial.print("Gas Value : ");
Serial.println(gas_value);
lcd.setCursor(0,1);
lcd.print("D:");
lcd.print(distance);
lcd.print(" G:");
lcd.print(gas_value);
moisture_value = analogRead(soil_pin);
Serial.print("Moisture Level: ");
Serial.println(moisture_value);
String getData = "GET /update?api_key="+ API
+"&field1="+temp+"&field2="+humidity+"&field3="+light+"&field4="+distance+"&field5
="+gas_value+"&field6="+moisture_value;
//String getData = "GET
/channels/695467/fields/1/last.json?api_key=Z4Y31YEM9I2VUNBH";
sendCommand("AT+CIPMUX=1",5,"OK");
delay(1000);
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
delay(1000);
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
delay(1000);
esp8266.println(getData);
delay(1500);
// if(esp8266.available()>0)
// {
// Serial.println("OKKKKK");
// rawData = esp8266.readString();
// delay(1000);
// Serial.println(rawData);
// delay(1000);
// }
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
Serial.println();
Serial.println("Data sent to cloud Success");
lcd.clear();
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok

28
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("TRUE");
countTrueCommand++;
countTimeCommand = 0;
}

if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}

found = false;
}

Connect CN10 TO 15
Connect required sensors to be monitor like LDR, Ultrasonic, DHT11 and Gas sensor with
ESP 8266 and Arduino uno.
Open thinspeak web site and lodin to your account channel and monitor the data

29
Experiment 12:

AIM: To Interface Light Dependent Resistor with Arduino uno.

PIN DIAGRAM:

Connections from Arduino to Light dependent resistor (LDR )Sensor

30
Code:
int light_pin =13;
void setup() {
pinMode(light_pin, INPUT)
pinMode(11, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light_data = digitalRead(light_pin);
if(light_data==0){
Serial.println("Light Detected!");
digitalWrite(11, LOW);
}
else{
Serial.println("Light not Detected!");
digitalWrite(11, HIGH);
}
delay(500);
}

31
Experiment 13:

AIM: To Interface Buzzer with Arduino uno.

PIN DIAGRAM:

Connections from Arduino uno to Buzzer module

32
Code:
/*
* Example testing sketch to test Relay
*
*/
//Define pin
// RM26 - RM27 connected
// CN10 - CN7
int BUZZER = 13;
void setup() {
pinMode(BUZZER,OUTPUT);
digitalWrite(BUZZER,LOW);
Serial.begin(9600);
}

void loop()
{
digitalWrite(BUZZER,LOW);
Serial.println("BUZZER OFF");
delay(1500);

digitalWrite(BUZZER,HIGH);
Serial.println("BUZZER ON");
delay(1500);
}

33

You might also like