0% found this document useful (0 votes)
17 views

Lecture 9

The document provides code and instructions for using various sensors and displays with Arduino including an automatic night light, flame sensor, smoke sensor, LCD display using I2C and non-I2C, keypad, and wiring diagrams. It describes how to calibrate the flame and smoke sensors, use the LCD display with and without I2C communication, define the keypad matrix, and read button presses from the keypad. Circuit diagrams and code snippets are provided as examples of interfacing these components with Arduino.

Uploaded by

Nader Shabrawy
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)
17 views

Lecture 9

The document provides code and instructions for using various sensors and displays with Arduino including an automatic night light, flame sensor, smoke sensor, LCD display using I2C and non-I2C, keypad, and wiring diagrams. It describes how to calibrate the flame and smoke sensors, use the LCD display with and without I2C communication, define the keypad matrix, and read button presses from the keypad. Circuit diagrams and code snippets are provided as examples of interfacing these components with Arduino.

Uploaded by

Nader Shabrawy
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/ 31

LECTURE 9

Automatic Night Light ( code)


#define ldr A0
#define relay 3
#define on 0
#define off 1
int threshold=40; //adjust it as you like
int level;
void setup(){
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop()
{
level= analogRead(ldr);
Serial.println(level);
if(level< threshold)
{digitalWrite(relay,on); }
else
{ digitalWrite(relay,off);}
}
Flame sensor
➢ Detects a flame or a light source of a wavelength in
the range of 760nm-1100 nm.

➢ Detection range: up to 80 cm.

➢ Operating temp: -25° ~ 85°

➢ Adjustable detection range.

➢ Detection angle about 60 degrees, it is sensitive to


the flame spectrum.

➢ Operating voltage 3.3V-5V.

➢ Digital and Analog Output.


Basic principle

principle that the infrared ray is sensitive to flame.


It uses the photo diode to detect flame and converts
the brightness of flame to the variable level signal.
Calibrate Flame Sensor Module

Expose the module to flame or strong light and turn the knob of the
potentiometer gently till the D0 indicator light is on
Flame with Arduino (Wiring)
Code of flame sensor

void loop()
#define flame 3
{
#define buzzer 5
Val = digitalRead(flame);
int Val = 0;
Serial.print(“value of flame:");
void setup() Serial.println(Val);
{
if (Val == LOW) {
Serial.begin(9600);
digitalWrite(buzzer, HIGH);
pinMode(flame , INPUT);
}
pinMode(buzzer,OUTPUT);
else {
} digitalWrite(buzzer, LOW);
}
}
Smoke sensor

➢ MQ2 Gas sensor works on 5V DC and draws around 800mW.

➢ It can detect LPG, Smoke, Alcohol, Propane, Hydrogen,


Butane, Methane and Carbon Monoxide concentrations
anywhere from 200 to 10000ppm
Parts-per-million (abbreviated ppm) is the ratio of one gas to
another. For example, 1,000ppm of CO means that if you could
count a million gas molecules, 1,000 of them would be of carbon
monoxide and 999,000 molecules would be some other gases.

➢ preheat time : NOT less than 48 hours


Basic principle
Basic principle

When tin dioxide (semiconductor


particles) is heated in air at high
temperature, oxygen is adsorbed on the
surface. In clean air, donor electrons in tin
dioxide are attracted toward oxygen which
is adsorbed on the surface of the sensing
material. This prevents electric current
flow.
In the presence of reducing gases, the
surface density of adsorbed oxygen
decreases as it reacts with the reducing
gases. Electrons are then released into the
tin dioxide, allowing current to flow freely
through the sensor.
Basic principle
The analog output voltage provided by the sensor changes in proportional to the concentration of
smoke/gas. The greater the gas concentration, the higher is the output voltage; while lesser gas
concentration results in low output voltage. The following animation illustrates the relationship
between gas concentration and output voltage.
Calibrate MQ2 Gas Sensor Module

To calibrate the gas sensor you can hold the gas sensor near smoke/gas you want to detect and
keep turning the potentiometer until the Red LED on the module starts glowing. Turn the screw
clockwise to increase sensitivity or anticlockwise to decrease sensitivity.
Gas with Arduino (Wiring)
Code of smoke detector
#define MQ2pin A5 void loop()
#define buzzer 10 {
int sensorValue; sensorValue = analogRead(MQ2pin);
void setup() Serial.print("Sensor Value: ");
{ Serial.println(sensorValue);
Serial.begin(9600); if(sensorValue > 300)
pinMode(buzzer,OUTPUT); {
Serial.println("Gas sensor warming up!"); Serial.print(" | Smoke detected!");
delay(20000); // allow the MQ-2 to warm up digitalWrite(buzzer,HIGH);
} }
else
{
digitalWrite(buzzer,LOW);
}
delay(2000); // wait 2s for next reading
}
LCD Display
Terminal Functions
Pin Number Name Function
1 VSS Ground Voltage
2 VCC Power 5V
3 VO Contrast Voltage
Register Select
4 RS 1=transferring display data
0=transferring instruction data
Read/Write control bus
5 R/W 1=Read Mode
0=Write Mode
Enable
6 EN 0=Enable
1=Disable
7~10 D0~D3 Bi-directional data bus
11~14 D4~D7 Bi-directional data bus
15 BLA Backlight positive supply
16 BLK Backlight negative supply
LCD with Arduino (Wiring)
Code of LCD display without I2C
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
lcd.print(“ST SMART");
lcd.setCursor(7,1);
lcd.print(“2020");
}
void loop() {
for(int i=0;i<15;i++){
lcd.scrollDisplayRight();
delay(200);
}
for(int i=0;i<15;i++){
lcd.scrollDisplayLeft();
delay(200);
}
}
I2C protocol
• The two wires, or lines are called Serial Clock (or
SCL) and Serial Data (or SDA). The SCL line is
the clock signal which synchronize the data
transfer between the devices on the I2C bus and
it’s generated by the master device. The other line
is the SDA line which carries the data.

• The two lines are “open-drain”which means that


pull up resistors needs to be attached to them so
that the lines are high because the devices on the
I2C bus are active low. Commonly used values for
the resistors are from 2K for higher speeds at
about 400 kbps, to 10K for lower speed at about
100 kbps.

• The speed (standard mode: 100 kbit/s,


full speed:400 kbit/s,
fast mode: 1 mbit/s,
high speed: 3,2 Mbit/s)

• Wire Library on Arduino programming language


I2C protocol
I2C module

➢ Supply voltage: 5V

➢ I2C Address: 0X20~0X27 (the original address is


0X27,you can change it yourself)

➢ the backlight and contrast is adjusted by


potentiometer

➢ SCL is A5 pin , SDA is A4 pin


WHY WE USE I2C MODULE?
Address of I2C Module

A0 A1 A2 Address
Open Open Open 0X27
Jumper Open Open 0X26
Open Jumper Open 0X25
Jumper Jumper Open 0X24
Open Open Jumper 0X23
Jumper Open Jumper 0X22
Open Jumper Jumper 0X21
Jumper Jumper Jumper 0X20
Soldering I2C module with lcd
LCD-I2C with Arduino (Wiring)
Code of LCD display with I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin(); // or some librarys are lcd.init();
lcd.backlight();
lcd.print(“ST SMART");
lcd.setCursor(7,1);
lcd.print(“2020");
}
void loop() {
for(int i=0;i<15;i++){
lcd.scrollDisplayRight();
delay(200);
}
for(int i=0;i<15;i++){
lcd.scrollDisplayLeft();
delay(200);
}
}
Keypad
Keypad
keypad with Arduino (Wiring)
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
}

void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
}
}
Thanks for coming ☺

You might also like