Skip to content

5. Input & Output device

This week’s tasks:

🐰 Measure something: Add a sensor to a microcontroller board and read it.

🐰 Add an output device to a microcontroller board and program it to do something.

We were introduced to various inputs like water level, temperature (DHT22), humidity, air quality sensor (MQ135), LDR, and soil moisture sensor HD-38. When given a power source such as a battery or a USB, these inputs work the way you encode it, etc. Input device pins are connected to the microcontroller.

Heading into the week of our final project, we finalized what elements are we going to need for it. We will be using water level, temperature (DHT22), humidity, air quality sensor (MQ135), LDR, and soil moisture sensor HD-38 sensores.

Research

“What is an input and output device? Input is any data that is sent to the computer for processing. Output is the result of processed data that we can see through some other devices. Input devices are controlled by the users. Output devices are controlled by computers. Mouse, Keyboards etc., are input device examples.”

What is the sensor? A sensor is a device that detects and responds to some type of input from the physical environment. The input can be light, heat, motion, moisture, pressure or any number of other environmental phenomena.

My work

First, we chose Arduino Uno then went to the library and wrote the name of the sensor, and then we wrote the code connected the wires, and put them in the right place. Second, we pressed verify and then upload to see the results.

Water Level Sensor

🐰 The water level sensor is a device that keeps track of the water level in tanks, containers, or reservoirs. It uses various technologies to measure the level accurately, making it valuable for a wide range of applications. ​

The water level sensor acts as a variable resistor. Its resistance changes based on the water level. When submerged in more water, it has better conductivity and lower resistance. Conversely, in low water, it exhibits higher resistance due to weak conductivity. The sensor typically has three main pins.

Signal: Provides an analog value representing the water level.

VCC: Requires a voltage of 3.3V to 5V.

GND: Connects to ground.

Code Example

// #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  // lcd.init();
  // lcd.backlight();
}
void loop() {
  int value = analogRead(A0);
  // lcd.setCursor(0, 0);
  // lcd.print("Value :");
  // lcd.print(value);
  // lcd.print("   ");
//  Serial.println(value);

  // lcd.setCursor(0, 1);
  // lcd.print("W Level :");

if (value == 0){ 
//   //   lcd.print("Empty ");
Serial.println("empty");
 } else if (value > 1 && value < 200) {
//   //   lcd.print("LOW   ");
Serial.println("Low");
} else if (value > 201 && value < 220) {
//   //   lcd.print("Medium");
Serial.println("Medium");
} else if (value > 220){
//   //   lcd.print("HIGH  ");
Serial.println("High");
 }
}

via GIPHY

Soil moisture sensor HD-38

🐰 Soil moisture sensors are devices that measure the water content in the soil. They provide valuable information regarding the moisture levels in the soil, helping farmers, researchers, and other users make informed decisions about irrigation, crop management, and environmental monitoring. There are several types of soil moisture sensors available on the market, each utilizing different principles to measure soil moisture.

Optimizes Plant Growth: By providing the correct water amount, sensors ensure optimal conditions for plant health and growth. They can also assist in identifying areas prone to drying out or retaining excessive moisture.

Code Example

// Sensor pins
#define sensorPower 7
#define sensorPin A1

void setup() {
    pinMode(sensorPower, OUTPUT);

    // Initially keep the sensor OFF
    digitalWrite(sensorPower, LOW);

    Serial.begin(9600);
}

void loop() {
    //get the reading from the function below and print it
    Serial.print("Analog output: ");
    Serial.println(readSensor());

    delay(1000);
}

//  This function returns the analog soil moisture measurement
int readSensor() {
    digitalWrite(sensorPower, HIGH);    // Turn the sensor ON
    delay(10);                          // Allow power to settle
    int val = analogRead(sensorPin);    // Read the analog value form sensor
    digitalWrite(sensorPower, LOW);     // Turn the sensor OFF
    return val;                         // Return analog moisture value
}

via GIPHY

via GIPHY

Temperature-Humidity Sensor (DHT11)

🐰 Temperature and humidity sensors are among the most commonly used environmental sensors. Humidity sensors are also sometimes referred to as hygrometers. These devices are used to provide the actual humidity condition within the air at any given point or in any given place.

via GIPHY

Air quality MQ135

🐰 The MQ-135 Gas sensor can detect gases like Ammonia (NH3), sulfur (S), Benzene (C6H6), CO2, and other harmful gases and smoke. Similar to other MQ series gas sensor, this sensor also has a digital and analog output pin.

via GIPHY

LDR

LDR (Light Dependent Resistor) as the name states is a special type of resistor that works on the photoconductivity principle means that resistance changes according to the intensity of light. Its resistance decreases with an increase in the intensity of light.

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}
void loop() {
  // reads the input on analog pin A0 (value between 0 and 1023)
  int analogValue = analogRead(A0);
  Serial.print("Analog reading: ");
  Serial.print(analogValue);   // the raw analog reading
  // We'll have a few threshholds, qualitatively determined
  if (analogValue < 10) {
    Serial.println(" - Dark");
  } else if (analogValue < 200) {
    Serial.println(" - Dim");
  } else if (analogValue < 500) {
    Serial.println(" - Light");
  } else if (analogValue < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
  delay(500);
}

via GIPHY

Video

files

click to download my car model

From Youtube


Last update: August 1, 2024