Skip to content

8. Input & Output Device

This week, our focus was on exploring the functionality of input and output sensors with the microcontroller board. I have experimented with various sensors and output devices, and I talk about on this page. These endeavors have not only enriched my understanding but have also contributed significantly to my final project.

DHT11 Sensor

DHT11 sensor is commonly used for measuring temperature and humidity. The blue module is the sensor itself, while the black part with wires connected to it is the breakout board that helps interface the sensor with a microcontroller or similar device. The DHT11 provides digital output and is often used in various DIY electronics projects, including weather stations and environmental monitoring systems.

Here’s an example code to use the DHT11 sensor with an Arduino using the Arduino IDE. This code uses the DHT sensor library. It can be installed via the Arduino Library Manager by searching for the DHT sensor library.

Steps to set up:

1.Install the DHT library:

• Go to Sketch > Include Library > Manage Libraries

• In the Library Manager, search for “DHT sensor library” and install it.

2.Wire the DHT11 to your Arduino:

• VCC: Connect to 5V or 3.3V on the Arduino.

• GND: Connect to Ground.

• DATA: Connect to any digital pin (e.g., Pin 2).

Code to Use This Sensor in Arduino IDE Program

// Include the DHT library
#include "DHT.h"

// Define the pin where the data wire is connected
#define DHTPIN 2  // Pin where the sensor is connected

// Define the DHT type (DHT11 in this case)
#define DHTTYPE DHT11   

// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Start serial communication
  Serial.begin(9600);

  // Start the DHT sensor
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements
  delay(2000);

  // Read humidity as a percentage
  float humidity = dht.readHumidity();

  // Read temperature in Celsius
  float temperature = dht.readTemperature();

  // Check if any readings failed and exit early (to try again)
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print the readings to the Serial Monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
}

The code reads the humidity and temperature from the DHT11 sensor every 2 seconds and if the readings fail (due to any sensor error), it will print an error message, then print the temperature (in Celsius) and humidity to the Serial Monitor.

MQ-2 Gas Sensorx

These sensors are typically used for detecting various gases such as methane (CH4), carbon dioxide (CO2), carbon monoxide (CO), alcohol, or LPG. These sensors also work by measuring changes in resistance when exposed to different gases. The internal material, typically a tin dioxide (SnO2) layer, reacts with gases in the environment, which alters its electrical resistance.

Wiring for the Sensor

• VCC: Connect to 5V on the Arduino.

• GND: Connect to Ground (GND) on the Arduino.

• A0 (Analog Output): Connect to one of the analog input pins (e.g., A0) on the Arduino.

Code to Use This Sensor in Arduino IDE Program

// Define the pins
const int analogPin = A0;  // Analog output pin from the sensor
const int digitalPin = 2;  // Digital output pin from the sensor

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set the digital pin as an input
  pinMode(digitalPin, INPUT);

  // Optional: Add a delay to allow sensor stabilization
  delay(20000);  // Allow the sensor to warm up (20 seconds)
}

void loop() {
  // Read the analog value from the sensor
  int analogValue = analogRead(analogPin);

  // Read the digital value from the sensor
  int digitalValue = digitalRead(digitalPin);

  // Print the values to the Serial Monitor
  Serial.print("Analog Value: ");
  Serial.print(analogValue);  // Analog value represents gas concentration
  Serial.print(" | Digital Value: ");
  Serial.println(digitalValue);  // Digital value shows if gas exceeds threshold

  // Delay before reading the values again
  delay(1000);  // Read every 1 second
}

The potentiometer on the sensor’s breakout board can be adjusted to change the gas concentration threshold for the digital output. Turning the potentiometer will set a different sensitivity level for the gas you’re detecting.

Buzzer

This type of buzzer is commonly used to generate sound or alarms in electronic projects.

The buzzer is an active buzzer, as it only has two wires. Active buzzers are very simple to use since they don’t require complex control signals.

Buzzer Pinout

The two pins on the buzzer are:

• VCC: This is the positive voltage input (connect to 5V or a digital output pin).

• GND: Ground, connect this pin to the GND pin on the Arduino

we can check by this code if the buzzer is work or not

// Define the pin where the buzzer is connected
const int buzzerPin = 9; // You can use any digital pin

void setup() {
  // Set the buzzer pin as an output
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Turn the buzzer on
  digitalWrite(buzzerPin, HIGH);
  delay(1000);  // Wait for 1 second (buzzer on)

  // Turn the buzzer off
  digitalWrite(buzzerPin, LOW);
  delay(1000);  // Wait for 1 second (buzzer off)
}

The pin mode is initially set as an output for control. Using digital write commands, a HIGH signal (5V) activates the buzzer while a LOW signal (0V) deactivates it. This operation is cyclic, with the buzzer sounding for 1 second and then staying silent for another second, all within a loop structure facilitated by delay functions.

We could integrate the buzzer to trigger an alarm when a gas concentration exceeds a certain threshold. For example, when the digital output of the gas sensor detects dangerous gas levels, the buzzer could be activated to sound an alarm.


Last update: October 20, 2024