A temperature sensor is a device used to determine the temperature of the surrounding environment. Most temperature sensors work on the principle that the material's resistance changes depending on its temperature. The most common temperature sensors are:
The main difference between sensors is the measured temperature range, precision and response time. Temperature sensor usually outputs the analogue value, but some existing sensors have a digital interface [1]. The thermistor can have a positive (PTC) or negative (NTC) thermal coefficient. For PTC, resistance rises with rising temperature, while resistance decreases in higher temperatures for NTC. An analogue thermistor must calculate the value read if the result should be presented in known units. Digital temperature sensors usually express the result in Celsius degrees or other units.
Temperature sensors are most commonly used in environmental monitoring devices and thermoelectric switches. In IoT applications, the sensor can be used for greenhouse temperature monitoring, warehouse temperature monitoring to avoid freezing, fire suppression systems and tracking the temperature of the soil, water and plants. The sample temperature sensor is present in figure 1 and connection schematic in 2.
An example code:
//Thermistor sensor output is connected to the analogue A0 pin int thermoPin = 0; //The analogue reading from the thermistor output int thermoReading; void setup(void) { //Begin serial communication Serial.begin(9600); //Initialize the thermistor analogue pin as an input pinMode(thermoPin, INPUT); } void loop(void) { //Read the analogue value of the thermistor sensor thermoReading = analogRead(thermoPin); Serial.print("Thermistor reading = "); //Print out Serial.println(thermoReading); delay(10); }
Digital temperature sensors automatically convert the temperature reading into some known unit, e.g. Celsius, Fahrenheit or Kelvin Degrees. Digital thermometers use one of the popular communication links. An example of a digital thermometer is DS18B20 by Dallas Semiconductors (figures 3 and 4). It uses a 1-Wire communication protocol; a sample schematic is present in the figure 5.
#include <OneWire.h> //library for 1-Wire protocol #include <DallasTemperature.h> //library for DS18B20 digital thermometer const int SENSOR_PIN = 13; //DS18B20 pin OneWire oneWire(SENSOR_PIN); //oneWire class DallasTemperature tempSensor(&oneWire); //connect oneWire to DallasTemperature library float tempCelsius; //temperature in Celsius degrees void setup() { Serial.begin(9600); //initialize serial port tempSensor.begin(); //initialize DS18B20 } void loop() { tempSensor.requestTemperatures(); //command to read temperatures tempCelsius = tempSensor.getTempCByIndex(0); //read temperature (in Celsius) Serial.print("Temp: "); Serial.print(tempCelsius); //print the temperature Serial.println(" C"); delay(1000); }
A humidity sensor (hygrometer) is a sensor that detects the amount of water or water vapour in the environment. The most common principle of air humidity sensors is the change of capacitance or resistance of materials that absorb moisture from the atmosphere. Soil humidity sensors measure the resistance between the two electrodes. Soluble salts and water amounts influence the resistance between electrodes in the soil. The output of a humidity sensor is an analogue signal value or digital value sent with some popular protocols [2]. A DHT11 (temperature+humidity) sensor is present in figure 6 and its connection to the microcontroller in 7.
IoT applications include monitoring humidors, greenhouse humidity, agriculture, art galleries and museum environments.
An example code [3]:
#include <dht.h> dht DHT; #define DHT_PIN 7 void setup(){ Serial.begin(9600); } void loop() { int chk = DHT.read11(DHT_PIN); Serial.print("Humidity = "); Serial.println(DHT.humidity); delay(1000); }
A sound sensor is a sensor that detects vibrations in a gas, liquid or solid environment. At first, the sound wave pressure makes mechanical vibrations, which transfer to changes in capacitance, electromagnetic induction, light modulation or piezoelectric generation to create an electric signal. The electrical signal is then amplified to the required output levels. Sound sensors can record sound and detect noise and its level.
Sound sensors are used in drone detection, gunshot alert, seismic detection and vault safety alarms.
Sample digital sound sensor is present in figure 8 and its application with Arduino in figure 9.
An example code:
//Sound sensor output is connected to the digital 7 pin int soundPin = 7; //Stores sound sensor detection readings int soundReading = HIGH; void setup(void) { //Begin serial communication Serial.begin(9600); //Initialize the sound detector module pin as an input pinMode(soundPin, INPUT); } void loop(void) { //Read the digital value to determine whether the sound has been detected soundReading = digitalRead(soundPin); if (soundPin==LOW) { //When sound detector detected the sound Serial.println("Sound detected!"); //Print out } else { //When the sound is not detected Serial.println("Sound not detected!"); //Print out } delay(10); }
Gas sensors are a group that can detect and measure the concentration of certain gasses in the air. The working principle of electrochemical sensors is to absorb the gas and create current from an electrochemical reaction. For process acceleration, a heating element can be used. For each type of gas, different kind of sensor needs to be used. Multiple types of gas sensors can also be combined in a single device. The single gas sensor output is an analogue signal, but devices with various sensors have a digital interface.
The smoke or air pollution sensors usually use LED or laser that emits light and a detector normally shaded from the light. If there are particles of smoke or polluted air inside the sensor, the light is reflected by them, which can be observed by the detector.
Gas sensors are used for safety devices, air quality control, and manufacturing equipment. IoT applications include air quality control management in smart buildings and smart cities or toxic gas detection in sewers and underground mines.\\MQ-7 Carbon Monoxide detector is present in figure 10 and its connection using analogue signal in figure 11.
An example code:
int gasPin = A0; //Gas sensor output is connected to the analog A0 pin int gasReading; //Stores gas sensor detection reading void setup(void) { Serial.begin(9600); //Begin serial communication pinMode(gasPin, INPUT); //Initialize the gas detector pin as an input } void loop(void) { gasReading = analogRead(gasPin); //Read the analog value of the gas sensor Serial.print("Gas detector value: "); //Print out Serial.println(gasReading); delay(10); //Short delay }
The smoke sensors usually emit LED light, and a detector is typically shaded from the light. If there are particles of smoke present inside the sensor, the light is reflected by them, which can be observed by the detector.
Smoke detectors are used in fire alarm systems.
The air pollution sensors usually use a laser directed onto the detector. Between the laser and detector, the thin stream of air flows and pollution particles create shades on the detector. Thus, the detector can distinguish the sizes of particles and count the number of them.
Air pollution sensors are used in air purifiers and air quality measurement stations to monitor current air conditions, mainly in cities. Because the air pollution sensor generates more data, the serial connection is often used for reading measurement results. An example of an air pollution sensor that can count particles of PM1.0, PM2.5, and PM10 is PMS5003. PMS series sensors are controlled with a serial port and additional signalling GPIOs with 3.3V logic, but they require 5V to power on an internal fan that ensures correct airflow. A PMS5003 sensor is present in figures 12 and 13, and its connection in figure 14.
An example code that uses the PMS5003 sensor:
#include <HardwareSerial.h> #include <Arduino.h> // Define the serial port for the PMS5003 sensor HardwareSerial pmsSerial(1); #define SET_PIN 22; #define RESET_PIN 4; #define RXD_PIN 16; //to TXD of the sensor #define TDX_PIN 17; //to RXD of the sensor bool verifyChecksum(uint8_t *data, int len); void setup() { Serial.begin(9600); pinMode(SET_PIN, OUTPUT); //controls sensor's low power mode //(LOW) -> turns fan down pinMode(RESET_PIN, OUTPUT); //controls sensor's reset (LOW) digitalWrite(SET_PIN, HIGH); //enable both digitalWrite(RESET_PIN, HIGH); pmsSerial.begin(9600, SERIAL_8N1, RXD_PIN, TXD_PIN); } void loop() { if (pmsSerial.available()) { if (pmsSerial.peek() == 0x42) { if (pmsSerial.available() >= 32) { uint8_t buffer[32]; pmsSerial.readBytes(buffer, 32); if (verifyChecksum(buffer, 30)) { uint16_t pm25 = makeWord(buffer[10], buffer[11]); uint16_t pm10 = makeWord(buffer[12], buffer[13]); Serial.print("PM2.5: "); Serial.print(pm25); Serial.print(" ug/m3\t"); Serial.print("PM10: "); Serial.print(pm10); Serial.println(" ug/m3"); } } } } } // Function to verify the checksum bool verifyChecksum(uint8_t *data, int len) { uint16_t checksum = 0; for (int i = 0; i < len - 2; i++) { checksum += data[i]; } return (checksum == makeWord(data[len - 2], data[len - 1])); }
Air pressure sensors can measure the absolute pressure in the surrounding environment. Some popular sensors use a piezo-resistive sensing element, which is then connected to the amplifier and analogue digital converter. Frint-end uses the logic to interface the microcontroller. Usually, barometric sensor readings depend on the temperature, so they include the temperature sensor for temperature compensation of the pressure. Popular examples of barometric sensors are BME280 and BMP280. Both include barometric sensors and temperature sensors built in for compensation and possible measurement, while BME280 also consists of a humidity sensor. Communication with these sensors is done with an I2C or SPI bus.
Barometric sensors are commonly used in home automation appliances for heating, venting, air conditioning (HVAC), airflow measurement and weather stations. Because air pressure varies with altitude, they are often used in altimeters. Sample connection schematic is present in figure 16 and the module itself in figure 15.
An example code of BME280 use is below:
#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #define BME_ADDRESS 0x76 Adafruit_BME280 bme; // I2C void setup() { Serial.begin(9600); bool status; Wire.begin(5,4); //SDA=GPIO5, SCL=GPIO4) status = bme.begin(BME_ADDRESS); if (!status) { Serial.println("Could not contact BME sensor"); while (1); } delay(1000); } void loop() { Serial.print("Temperature="); Serial.print(bme.readTemperature()); Serial.println("*C"); Serial.print("Air pressure="); Serial.print(bme.readPressure() / 100.0F); Serial.println("hPa"); Serial.print("Humidity="); Serial.print(bme.readHumidity()); Serial.println("%rh"); Serial.println(); delay(1000); }