This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:hardware2:sensors_environment [2023/10/03 11:01] – pczekalski | en:iot-open:hardware2:sensors_environment [2024/05/27 15:07] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Environment Sensors ====== | ||
| + | {{: | ||
| + | == Temperature Sensor == | ||
| + | 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' | ||
| + | * **thermocouple** – consists of two junctions of dissimilar metals, | ||
| + | * **thermistor** – includes the temperature-dependent resistor, | ||
| + | * **resistive temperature detector** – is made of a pure metal coil. | ||
| + | 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 ((http:// | ||
| + | |||
| + | Temperature sensors are most commonly used in environmental monitoring devices and thermoelectric switches. In IoT applications, | ||
| + | |||
| + | <figure tempsensor_1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure tempsensor_2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code: | ||
| + | <code c> | ||
| + | |||
| + | // | ||
| + | int thermoPin = 0; | ||
| + | //The analogue reading from the thermistor output | ||
| + | int thermoReading; | ||
| + | |||
| + | void setup(void) { | ||
| + | //Begin serial communication | ||
| + | Serial.begin(9600); | ||
| + | // | ||
| + | pinMode(thermoPin, | ||
| + | } | ||
| + | |||
| + | void loop(void) { | ||
| + | //Read the analogue value of the thermistor sensor | ||
| + | thermoReading = analogRead(thermoPin); | ||
| + | Serial.print(" | ||
| + | Serial.println(thermoReading); | ||
| + | delay(10); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Digital Temperature Sensor == | ||
| + | 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 {{ref> | ||
| + | |||
| + | <figure ds18b20_1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure ds18b20_2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure ds18b20_3> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | const int SENSOR_PIN = 13; // | ||
| + | |||
| + | OneWire oneWire(SENSOR_PIN); | ||
| + | DallasTemperature tempSensor(& | ||
| + | // | ||
| + | |||
| + | float tempCelsius; | ||
| + | |||
| + | void setup() | ||
| + | |||
| + | { | ||
| + | Serial.begin(9600); | ||
| + | tempSensor.begin(); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | tempSensor.requestTemperatures(); | ||
| + | // | ||
| + | tempCelsius = tempSensor.getTempCByIndex(0); | ||
| + | // | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(tempCelsius); | ||
| + | Serial.println(" | ||
| + | |||
| + | delay(1000); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | < | ||
| + | |||
| + | == Humidity Sensor == | ||
| + | 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 ((https:// | ||
| + | IoT applications include monitoring humidors, greenhouse humidity, agriculture, | ||
| + | |||
| + | <figure humiditysensor1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure humiditysensor2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code ((http:// | ||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | dht DHT; | ||
| + | |||
| + | #define DHT_PIN 7 | ||
| + | |||
| + | void setup(){ | ||
| + | Serial.begin(9600); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | int chk = DHT.read11(DHT_PIN); | ||
| + | Serial.print(" | ||
| + | Serial.println(DHT.humidity); | ||
| + | delay(1000); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | == Sound Sensor == | ||
| + | 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, | ||
| + | Sound sensors are used in drone detection, gunshot alert, seismic detection and vault safety alarms.\\ | ||
| + | Sample digital sound sensor is present in figure {{ref> | ||
| + | |||
| + | <figure soundsensor1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure soundsensor2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code: | ||
| + | <code c> | ||
| + | //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); | ||
| + | // | ||
| + | pinMode(soundPin, | ||
| + | } | ||
| + | |||
| + | 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(" | ||
| + | } else { //When the sound is not detected | ||
| + | Serial.println(" | ||
| + | } | ||
| + | delay(10); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Chemical and Gas Sensor == | ||
| + | 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, | ||
| + | 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 {{ref> | ||
| + | |||
| + | <figure gassensor1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure gassensor2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code: | ||
| + | <code c> | ||
| + | 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); | ||
| + | pinMode(gasPin, | ||
| + | } | ||
| + | |||
| + | void loop(void) { | ||
| + | gasReading = analogRead(gasPin); | ||
| + | Serial.print(" | ||
| + | Serial.println(gasReading); | ||
| + | delay(10); //Short delay | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Smoke and Air Pollution Sensors == | ||
| + | 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 {{ref> | ||
| + | |||
| + | <figure airpolution1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure airpolution1_2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure airpolution2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code that uses the PMS5003 sensor: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // 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, | ||
| + | //(LOW) -> turns fan down | ||
| + | pinMode(RESET_PIN, | ||
| + | digitalWrite(SET_PIN, | ||
| + | digitalWrite(RESET_PIN, | ||
| + | | ||
| + | pmsSerial.begin(9600, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | if (pmsSerial.available()) { | ||
| + | if (pmsSerial.peek() == 0x42) { | ||
| + | if (pmsSerial.available() >= 32) { | ||
| + | uint8_t buffer[32]; | ||
| + | pmsSerial.readBytes(buffer, | ||
| + | | ||
| + | | ||
| + | if (verifyChecksum(buffer, | ||
| + | uint16_t pm25 = makeWord(buffer[10], | ||
| + | uint16_t pm10 = makeWord(buffer[12], | ||
| + | | ||
| + | Serial.print(" | ||
| + | Serial.print(pm25); | ||
| + | Serial.print(" | ||
| + | Serial.print(" | ||
| + | Serial.print(pm10); | ||
| + | Serial.println(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // 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 Sensor == | ||
| + | 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, | ||
| + | Popular examples of barometric sensors are BME280 and BMP280. Both include barometric sensors and temperature sensors built in for compensation and possible measurement, | ||
| + | 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 {{ref> | ||
| + | |||
| + | <figure sensor_bme280> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure sensor_bme280_2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <note warning> | ||
| + | |||
| + | An example code of BME280 use is below: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define BME_ADDRESS 0x76 | ||
| + | |||
| + | Adafruit_BME280 bme; // I2C | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | bool status; | ||
| + | Wire.begin(5, | ||
| + | status = bme.begin(BME_ADDRESS); | ||
| + | if (!status) { | ||
| + | Serial.println(" | ||
| + | while (1); | ||
| + | } | ||
| + | delay(1000); | ||
| + | } | ||
| + | |||
| + | |||
| + | void loop() { | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(bme.readTemperature()); | ||
| + | Serial.println(" | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(bme.readPressure() / 100.0F); | ||
| + | Serial.println(" | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(bme.readHumidity()); | ||
| + | Serial.println(" | ||
| + | Serial.println(); | ||
| + | | ||
| + | delay(1000); | ||
| + | } | ||
| + | </ | ||
| + | |||