Design Tools Workshop Sensors and Codes For Arduino: P. Syam Sundar Asst. Prof. Dept. of ECE Klef

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Design tools workshop

Sensors and codes for


arduino
P. Syam Sundar
Asst. Prof. Dept. of ECE
KLEF
Maintaining observation book
Date: Expt. No. Experiment Name
Circuit diagram / Pin configurations / pin
diagrams / figures:
Aim:
Code:
Apparatus / Equipment:

Procedure: Detailed step wise procedure


Observations / Tabular Forms:
of what you are going to do in the lab /
what was done in the lab
Equations / Calculations:
Result Analysis:
Outputs / Results / Serial Monitor:
Inferences:
Conclusion:

Faculty Signature and Marks:


Relay module
Relay is an electromagnetic switch, which is controlled by small current, and used to
switch ON and OFF relatively much larger current. Means by applying small current we
can switch ON the relay which allows much larger current to flow. A relay is a good
example of controlling the AC (alternate current) devices, using a much smaller DC
current.  Commonly used Relay is Single Pole Double Throw (SPDT) Relay, it has five
terminals as below:
Relay module
Code:
void setup() { // put your setup code here, to run once:
pinMode(11,OUTPUT);
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(11,HIGH);
delay(500);
digitalWrite(11,LOW);
delay(500);
}
LDR
Ldr code
const int ledPin = 13;
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 200) {
digitalWrite(ledPin, HIGH);
Serial.print("Its DARK, Turn on the LED : ");
Serial.println(ldrStatus);
} else {
digitalWrite(ledPin, LOW);
Serial.print("Its BRIGHT, Turn off the LED : ");
Serial.println(ldrStatus);
}
}
LDR alternate code
const int led=12;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(led,OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int d= analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a

voltage (0 - 5V):
float voltage = d * (5.0 / 1023.0);
// print out the value you read:
//Serial.println(d);
Serial.println("voltage= ");
Serial.println(voltage);
delay(100); //As this delay increases, sensor response
time increases. Sensor response slowly.
if(voltage>=3)
{
digitalWrite(led, HIGH);
delay(100);
}
else
{
digitalWrite(led, LOW);
delay(100);
}
}
DHT11 Sensor

The pins on the sensor are S, for signal, the one in the middle is voltage, and
the minus sign is ground. The signal pin goes to header A0 on the Arduino.
The middle pin goes to 5V, and the minus sign goes to GND. Our yellow cable
is our Analog cable, our red cable is power, and our black one is ground.
1. Download DHT11 library and add the zip file.

2. Add the library to the Arduino IDE.

3. Upload the code.

4. When the code is uploaded, open the Serial Monitor

and set the baud rate to 9600.

5. You will see the humidity and temperature.


Adding the DHT library
Scroll all the way down, and click the button to download the DHT_Library.zip

Now download the code attached to the post.


In the Arduino IDE, go to Sketch >> Include
Library >> Add ZIP file.
When you click the 'Add .ZIP library', If you'll notice in the code,
you should get a file window that pops another line of code pops up that
up. Add the DHT_Library.zip. says:
#include <dht.h>

NOTE: The world's lowest recorded relative humidity value occurred at Coober
Pedy in the South Australia desert when the temperature was 93 degrees and the
dew point was minus 21 degrees producing a relative humidity of 1 percent.
(source: 'www.chicagotribune.com')
Code:
#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
void (){
Serial.begin(9600);
delay(500);//Delay to let system boot Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
}//end "setup()"
void (){ //Start of Program
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(5000);//Wait 5 seconds before accessing sensor again. //Fastest should be once every two
seconds.
}// end loop(
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
MQ2 smoke sensor
The MQ-2 smoke sensor is sensitive to smoke and to the
following flammable gases:
 LPG
 Butane
 Propane
 Methane
 Alcohol
 Hydrogen
The resistance of the sensor is different depending on the type
of the gas.
The smoke sensor has a built-in potentiometer that allows you
to adjust the sensor sensitivity according to how accurate you
want to detect gas.
Working:
The voltage that the sensor outputs changes accordingly to the
smoke/gas level that exists in the atmosphere.
The sensor outputs a voltage that is proportional to the
concentration of smoke/gas.
The relationship between voltage and gas concentration is the
following:
The greater the gas concentration, the greater the output
voltage
The lower the gas concentration, the lower the output voltage
The MQ-2 sensor has 4 pins.
Pin-------------------------------------Wiring to Arduino Uno
A0-------------------------------------Analog pins
D0-------------------------------------Digital pins
GND-----------------------------------GND
VCC------------------------------------5V
So, before jumping into the coding part, let's check
whether we've assembled all the necessary hardware
components.
Code:
Serial.print("Pin A0: ");
int redLed = 12; Serial.println(analogSensor); // Checks if
int greenLed = 11; it has reached the threshold value
int buzzer = 10; if (analogSensor > sensorThreshold)
int smokeA0 = A5;// Threshold {
value digitalWrite(redLed, HIGH);
int sensorThreshold = 400; digitalWrite(greenLed, LOW);
void setup() { tone(buzzer, 1000, 200);
pinMode(redLed, OUTPUT); }
pinMode(greenLed, OUTPUT); else
pinMode(buzzer, OUTPUT); {
pinMode(smokeA0, INPUT); digitalWrite(redLed, LOW);
Serial.begin(9600); digitalWrite(greenLed, HIGH);
} noTone(buzzer);
void loop() { }
int analogSensor = delay(100);
analogRead(smokeA0); }
Ir remote
 IR Remote control uses light for Data Transmission
 It uses Infra Red light
 IR light is the light just below the Visible light in the spectrum
 IR LEDs are inexpensive
 IR is subject to interference
 Common sources of interference include sunlight and light
bulbs
 Remote sends pulses of IR light to the receiver
 To prevent interference, these pulses are modulated (usually at
38kHz for TSOP1738 sensor)
 Each button on the remote sends a unique code to the
receiving source
 These codes differ between manufacturers
Code:
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {  
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value  
}
}
#include <IRremote.h> case 0xFF629D:
Serial.println("CH");
const int RECV_PIN = 7; break;
IRrecv irrecv(RECV_PIN); case 0xFFE21D:
decode_results results; Serial.println("CH+");
unsigned long key_value = 0; break;
case 0xFF22DD:
void setup(){ Serial.println("|<<");
Serial.begin(9600); break;
irrecv.enableIRIn(); case 0xFF02FD:
irrecv.blink13(true); Serial.println(">>|");
} break ;
case 0xFFC23D:
void loop(){ Serial.println(">|");
if (irrecv.decode(&results)){ break ;
case 0xFFE01F:
if (results.value == 0XFFFFFFFF) Serial.println("-");
results.value = key_value; break ;
case 0xFFA857:
switch(results.value){ Serial.println("+");
case 0xFFA25D: break ;
Serial.println("CH-");
break;
case 0xFF10EF:
case 0xFF906F:
Serial.println("4");
Serial.println("EQ");
break ;
break ;
case 0xFF38C7:
case 0xFF6897:
Serial.println("5");
Serial.println("0");
break ;
break ;
case 0xFF5AA5:
case 0xFF9867:
Serial.println("6");
Serial.println("100+");
break ;
break ;
case 0xFF42BD:
case 0xFFB04F:
Serial.println("7");
Serial.println("200+");
break ;
break ;
case 0xFF4AB5:
case 0xFF30CF:
Serial.println("8");
Serial.println("1");
break ;
break ;
case 0xFF52AD:
case 0xFF18E7:
Serial.println("9");
Serial.println("2");
break ;
break ;
}
case 0xFF7A85:
key_value = results.value;
Serial.println("3");
irrecv.resume();
break ;
}
}
HOW THE CODE WORKS:
For any IR communication using the IRremote library, first we
need to create an object called irrecv and specify the pin number
where the IR receiver is connected (line 3). This object will take
care of the protocol and processing of the information from the
receiver.
The next step is to create an object called results , from
the decode_results class, which will be used by the irrecv object to
share the decoded information with our application (line 5).
In the void setup() block, first we configure the serial monitor
baud rate. Next we start the IR receiver by calling
the IRrecv member function enableIRIn() (line 10).
The irrecv.blink13(true) function on line 11 will blink the
Arduino’s on board LED every time the receiver gets a signal from
the remote control, which is useful for debugging.
In the void loop() block, the function irrecv.decode will return true
if a code is received and the program will execute the code in the if
statement. The received code is stored in results.value. Then I used
a switch to handle each IR code and print the corresponding key
value.
Before the switch block starts there is a conditional block:
if (results.value == 0XFFFFFFFF)
results.value = key_value;
If we receive 0XFFFFFFFF from the remote, it means a repetition
of the previous key. So in order to handle the repeat key pattern, I
am storing the hex code in a global variable key_value every time a
code is received:
key_value = results.value;
When you receive a repeat pattern, then the previously stored value
is used as the current key press.
At the end of the void loop() section, we call irrecv.resume() to
reset the receiver and prepare it to receive the next code.
#include <IRremote.h>
void loop() {
int RECV_PIN = 2;
if (irrecv.decode(&results))
// the pin where you connect the output
{
pin of TSOP4838
if(itsONled[1] == 1) {
int led1 = 12;
// if first led is on then
int itsONled[] = {0,0};
digitalWrite(led1, LOW);
/* the initial state of LEDs is OFF
// turn it off when button is pressed
(zero) the first zero must remain zero
itsONled[1] = 0;
but you can change the others to 1's
// and set its state as off
if you want a certain led to light when
} else {
the board is powered */
// else if first led is off
#define code1 -10201
digitalWrite(led1, HIGH);
// code received from button A
// turn it on when the button is pressed
IRrecv irrecv(RECV_PIN);
itsONled[1] = 1;
decode_results results;
// and set its state as on
void setup(){
}
Serial.begin(9600);
irrecv.resume();
irrecv.enableIRIn();
// Receive the next value
// Start the receiver
}
pinMode(led1, OUTPUT);
}
}
https://forum.arduino.cc/index.php?topic=261445.0

You might also like