The sensor converts a physical parameter into a signal, generally in terms of voltage or current. Sensors are the sensory organs of the Embedded system.
Some commonly used sensors are as follows:
- Soil Humidity Sensor
- IR Sensor
- Smoke sensor
- Ultrasonic Sensor
- Rain Sensor
- Temperature Sensor
- Pressure Sensor
- Touch Sensor
- Photoresist Sensor
- Accelerometer Sensor
1.Soil Humidity Sensor:
The Moisture Sensor is used to measure the water content(moisture) of soil. When the soil is having water shortage, the module output is at high voltage (5 Volt), else the output is at a low level (0 Volt).
//Digital read from the analog sensor
int led_pin =13;
int sensor_pin =8;
void setup()
{
pinMode(led_pin, OUTPUT);
pinMode(sensor_pin, INPUT);
}
void loop()
{
if(digitalRead(sensor_pin) == HIGH)
{
digitalWrite(led_pin, HIGH);//Turn on LED if soil is Dry
}
else
{
digitalWrite(led_pin, LOW);//Turn off LED if soil is Dry
delay(1000);
}
}
//Analog read
int sensor_pin = A0;
int output_value ;
void setup() {
Serial.begin(9600);
Serial.println("Reading From the Sensor ...");
delay(2000);
}
void loop() {
output_value= analogRead(sensor_pin);
output_value = map(output_value,550,0,0,100);// we will map the output values to 0-100,
// because the moisture is measured in
// percentage
//For wet soil it was 550 for dry soil it is 100.
Serial.print("Mositure : ");
Serial.print(output_value);
Serial.println("%");
delay(1000);
}
2.IR Sensor:
IR sensors work on the basic principle of reflection and absorption of light. One of the sensors emits infrared light. if the surface is blackish it absorbs light and the IR sensor reads no reading. but if it is whitish then light gets reflected and we get a reading on the IR sensor.
int IRSensor = 2; // connect ir sensor to arduino pin 2
int LED = 13; // conect Led to arduino pin 13
void setup()
{
pinMode (IRSensor, INPUT); // sensor pin INPUT
pinMode (LED, OUTPUT); // Led pin OUTPUT
}
void loop()
{
int statusSensor = digitalRead (IRSensor);
if (statusSensor == 1)
digitalWrite(LED, LOW); // LED LOW
}
else
{
digitalWrite(LED, HIGH); // LED High
}
}
3.Smoke sensor:
A smoke sensor is a device that senses smoke, typically as an indicator of fire. Commercial security devices issue a signal to a fire alarm control panel as part of a fire alarm system. While household smoke detectors, also known as smoke alarms. Generally issue a local audible or visual alarm from the detector itself or several detectors if there are multiple smoke detectors interlinked.
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
int sensorThres = 400;
//MQ-2 Gas sensor
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
Serial.begin(9600);
}
void loop()
{
int analogSensor = analogRead(smokeA0);
Serial.print("Pin A0: ");
Serial.println(analogSensor);
if (analogSensor > sensorThres)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzer);
}
delay(100);
}
4.Ultrasonic Sensor:
As the name indicates, ultrasonic/level sensors measure distance by using ultrasonic waves. The sensor head emits an ultrasonic wave and receives the wave reflected back from the target. ultrasonic/level sensors measure the distance to the target by measuring the time between the emission and reception.
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// 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 in centi meter speed=d/t speed of sound is 340 m/s
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
5.Rain sensor:
A rain sensor is one kind of switching device which is used to detect the rainfall. It works like a switch and the working principle of this sensor is, whenever there is rain, the switch will be normally closed.
const int capteur_D = 4;
const int capteur_A = A0;//to take analog value
int val_analogique;//To take the digital Value from sensor module
void setup()
{
pinMode(capteur_D, INPUT);
pinMode(capteur_A, INPUT);
Serial.begin(9600);
}
void loop()
{
if(digitalRead(capteur_D) == LOW)
{
Serial.println("Digital value : wet");
delay(10);
}
else
{
Serial.println("Digital value : dry");
delay(10);
}
val_analogique=analogRead(capteur_A);
Serial.print("Analog value : ");
Serial.println(val_analogique);
Serial.println("");
delay(1000);
}
6. Temperature Sensor:
The temperature sensor is a device, usually an RTD (resistance temperature detector) or a thermocouple, that collects the data about temperature from a particular source and converts the data into an understandable form for a device or an observer.
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(void)
{
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
void loop(void)
{
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0));
// Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
delay(1000);
}
7.Pressure sensor:
Pressure pads are designed to detect when pressure is applied to an area, and they come in a variety of quality and accuracy. The simplest pressure pads are often the equivalent of big switches. Inside a pressure, the pad is two layers of foil separated by a layer of foam with holes in it. When the foam is squashed, the metal contacts touch through the foam and complete the circuit.
const int FSR_PIN = A0; // Pin connected to FSR/resistor divider
const int LED_PIN1 = 7;
const int LED_PIN2 = 6;
const int LED_PIN3 = 4;
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 3230.0; // Measured resistance of 3.3k resistor
void setup()
{
Serial.begin(9600);
pinMode(FSR_PIN, INPUT);
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN3, OUTPUT);
}
void loop()
{
int fsrADC = analogRead(FSR_PIN);
// If the FSR has no pressure, the resistance will be
// near infinite. So the voltage should be near 0.
if (fsrADC != 0) // If the analog reading is non-zero
{
// Use ADC reading to calculate voltage:
float fsrV = fsrADC * VCC / 1023.0;
// Use voltage and static resistor value to
// calculate FSR resistance:
float fsrR = R_DIV * (VCC / fsrV - 1.0);
Serial.println("Resistance: " + String(fsrR) + " ohms");
// Guesstimate force based on slopes in figure 3 of
// FSR datasheet:
float force;
float fsrG = 1.0 / fsrR; // Calculate conductance
// Break parabolic curve down into two linear slopes:
if (fsrR <= 600)
force = (fsrG - 0.00075) / 0.00000032639;
else
force = fsrG / 0.000000642857;
Serial.println("Force: " + String(force) + " g");
Serial.println();
if(force<10)
{
digitalWrite(LED_PIN1,LOW);
digitalWrite(LED_PIN2,LOW);
digitalWrite(LED_PIN3,LOW);}
if(force>20)
{
digitalWrite(LED_PIN1,HIGH);
digitalWrite(LED_PIN2,LOW);
digitalWrite(LED_PIN3,LOW);
}
if(force>60)
{
digitalWrite(LED_PIN2,HIGH);
digitalWrite(LED_PIN1,LOW);
digitalWrite(LED_PIN3,LOW);
}
if(force>100)
{
digitalWrite(LED_PIN3,HIGH);
digitalWrite(LED_PIN2,LOW);
digitalWrite(LED_PIN1,LOW);
}
delay(500);
}
else
{
// No pressure detected
}
}
8.Touchsensor:
It is a simple sensor which detects the touch of the human beings.
#define sensorPin 1 // capactitive touch sensor - Arduino Digital pin D1
int ledPin = 13; // Output display LED (on board LED) - Arduino Digital pin D13
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
}
void loop()
{
int senseValue = digitalRead(sensorPin);
if (senseValue == HIGH){
digitalWrite(ledPin, HIGH);
Serial.println("TOUCHED");
}
else{
digitalWrite(ledPin,LOW);
Serial.println("not touched");
}
delay(500);
}
9.Photo resist sensor:
When the value read from the photoresistor sensor module goes below the threshold value. It becomes dark, the Arduino on-board LED is switched on. The LED is switched off when the analog value from the sensor goes above the threshold value.
int sensorPin = A0; // select the analog input pin for the photoresistor
int threshold = 300; // analog input trigger level from photoresistor
int sensorValue = 0; // photoresistor value read from analog input pin
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // built-in LED, usually on pin 13
}
void loop() {
if (analogRead(sensorPin) < threshold) {
// low light, so switch the light (LED) on
digitalWrite(LED_BUILTIN, HIGH);
}
else {
// light level high enough, so switch the light (LED) off
digitalWrite(LED_BUILTIN, LOW);
}
// wait for ADC to settle before next reading
delay(20);
}