Skip to content

Week 5: Input & Output device

Week 5: Input & Output Devices

This Week’s Tasks

Objective: Complete the assignments focusing on Input & Output Devices, which include these tasks:

  1. Test different types of input and output electronics with ESP32 Kids IOT STEM Microcontroller with Thonny.
  2. Test multiple sensors relevant to the final project with Arduino UNO in ARDUINO IDE.
  3. Create a Prototype of the final project design.

First Task

Understanding Input & Output Devices

Input Devices:

Input devices are components that allow a system to receive data from the external environment. These devices convert physical parameters (like temperature, light, or pressure) into electrical signals that can be processed by the system. Examples include sensors, buttons, and switches. In this week’s tasks, the following input devices were tested:

  • Potentiometer: Measures variable resistance and provides analog input.

  • Humidity & Temperature Sensor (DHT11): Measures and provides data on environmental temperature and humidity.

  • Photoresistor (LDR): Measures light intensity and provides corresponding resistance values.

Output Devices:

Output devices take the processed data from the system and convert it into a perceivable form, such as sound, light, or motion. These devices are used to convey information or perform actions. Examples include LEDs, buzzers, and displays. In this week’s tasks, the following output devices were tested:

  • Buzzer: Produces sound based on electrical signals.

  • 6812 RGB LED: Displays various colors and patterns based on input signals.


ESP32 Kids IOT STEM Microcontroller

The ESP32 Kids IOT STEM Microcontroller is an educational tool designed for learning and experimenting with various input and output devices. It features multiple digital pins, each color-coded for specific functions:

  • Blue: GPIO pins for general digital input/output

  • Red: Power pins (VCC and GND)

  • Yellow: Analog input pins

  • Green: PWM output pins

  • Orange: Communication pins (e.g., I2C, UART)

The following tests were conducted using Port-4 of the ESP32 microcontroller.


A-Buzzer

A buzzer is an audio signaling device that produces a sound when powered. It is commonly used in alarms, timers, and confirmation of user inputs.

Code:

from machine import Pin
import time

Buzzer = Pin(2, Pin.OUT)

while True:
    Buzzer.value(1)
    time.sleep(1)
    Buzzer.value(0)
    time.sleep(0.5)

Outcome: The buzzer emits a sound in a pattern similar to an alarm: it beeps for 1 second, then pauses for 0.5 seconds, and repeats.


Potentiometer

A potentiometer is a three-terminal resistor with a sliding contact that forms an adjustable voltage divider. It is used to measure and control voltage.

Code:

from machine import Pin, ADC
from time import sleep

pot = ADC(Pin(39))
pot.atten(ADC.ATTN_11DB)  # Full range: 3.3v

while True:
    pot_value = pot.read()
    print(pot_value)
    sleep(0.1)

Outcome: Spinning the potentiometer changes the value, indicating varying resistance.


Humidity & Temperature Sensor

A DHT11 sensor measures both temperature and humidity. It is commonly used in weather stations and environmental monitoring systems.

Code:

from machine import Pin
from time import sleep
import dht 

sensor = dht.DHT11(Pin(27))

while True:
    try:
        sleep(0.5)
        sensor.measure()
        temp = sensor.temperature()
        hum = sensor.humidity()
        temp_f = temp * (9/5) + 32.0
        print('Temperature: %3.1f C' % temp)
        print('Temperature: %3.1f F' % temp_f)
        print('Humidity: %3.1f %%' % hum)
    except OSError as e:
        print('Failed to read sensor.')

Outcome: The sensor accurately measures and displays temperature and humidity. Placing it above a hot tea showed an increase in both readings.


6812 RGB LED

An RGB LED can display different colors by adjusting the intensities of red, green, and blue components. This test uses the NeoPixel library to control the LED.

Code:

import machine, neopixel
np = neopixel.NeoPixel(machine.Pin(27), 8)

np[0] = (255, 0, 0)  # set to red, full brightness
np[1] = (0, 128, 0)  # set to green, half brightness
np[2] = (0, 0, 64)   # set to blue, quarter brightness

import time

def demo(np):
    n = np.n
    for i in range(4 * n):
        for j in range(n):
            np[j] = (0, 0, 0)
        np[i % n] = (255, 255, 255)
        np.write()
        time.sleep_ms(25)

    for i in range(4 * n):
        for j in range(n):
            np[j] = (0, 0, 128)
        if (i // n) % 2 == 0:
            np[i % n] = (0, 0, 0)
        else:
            np[n - 1 - (i % n)] = (0, 0, 0)
        np.write()
        time.sleep_ms(60)

    for i in range(0, 4 * 256, 8):
        for j in range(n):
            if (i // 256) % 2 == 0:
                val = i & 0xff
            else:
                val = 255 - (i & 0xff)
            np[j] = (val, 0, 0)
        np.write()

    for i in range(n):
        np[i] = (0, 0, 0)
    np.write()

demo(np)

Outcome: The RGB LED displayed various colors and patterns, demonstrating its ability to show different colors and effects.


Photoresistor

A photoresistor (LDR) changes its resistance based on the amount of light it receives. It is commonly used in light-sensing circuits.

Code:

from machine import ADC, Pin
import time

class LDR:
    def __init__(self, pin, min_value=0, max_value=100):
        if min_value >= max_value:
            raise Exception('Min value is greater or equal to max value')
        self.adc = ADC(Pin(39))
        self.adc.atten(ADC.ATTN_11DB)
        self.min_value = min_value
        self.max_value = max_value

    def read(self):
        return self.adc.read()

    def value(self):
        return (self.max_value - self.min_value) * self.read() / 4095

ldr = LDR(34)

while True:
    value = ldr.value()
    print('value = {}'.format(value))
    time.sleep(0.5)

Outcome: The photoresistor’s readings changed when a flashlight was shone on it, demonstrating its sensitivity to light.


Second Task

For our group project, we focused on integrating multiple sensors with the Arduino UNO to monitor environmental and soil conditions. The sensors used included a water level sensor, temperature & humidity sensor, soil moisture sensor, light intensity sensor, and air quality sensor.

Water-Level Sensor

A water-level sensor measures the level of water in a container. It is used in applications like water tanks and irrigation systems.

  • Connections:

    • VCC: Connected to the VIN pin on the Arduino.
    • GND: Connected to the GND pin on the Arduino.
    • Signal: Connected to analog pin A0 on the Arduino.
  • Code:

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

void setup() {
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(A0);
  if (value == 0) {
    Serial.println("empty");
  } else if (value > 1 && value < 200) {
    Serial.println("Low");
  } else if (value > 201 && value < 220) {
    Serial.println("Medium");
  } else if (value > 220) {
    Serial.println("High");
  }
}

Outcome: The sensor provided accurate readings of water levels, displaying “empty,” “Low,” “Medium,” and “High” based on the measured values.


Temperature & Humidity Sensor

A temperature and humidity sensor measures the surrounding environment, acting as a green-thumb whisperer for plants. It ensures cozy conditions, optimal photosynthesis, and disease prevention. All while keeping the leafs hydrated.

  • Connections:

    • VCC: Connected to the VIN pin on the Arduino.
    • GND: Connected to the GND pin on the Arduino.
    • Data: Connected to digital pin 2 on the Arduino.
  • Code:

#include <DHT11.h>

DHT11 dht11(2);

void setup() {
  Serial.begin(9600);
}

void loop() {
  int temperature = 0;
  int humidity = 0;
  int result = dht11.readTemperatureHumidity(temperature, humidity);
  if (result == 0) {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" Β°C\tHumidity: ");
    Serial.print(humidity);
    Serial.println(" %");
  } else {
    Serial.println(DHT11::getErrorString(result));
  }
}

Outcome: The sensor measured and displayed temperature and humidity accurately, making it suitable for monitoring plant conditions.


Soil Moisture Sensor

A soil moisture sensor measures the moisture content in the soil, helping in irrigation management.

  • Connections:

    • VCC: Connected to the VIN pin on the Arduino.
    • GND: Connected to the GND pin on the Arduino.
    • Signal: Connected to analog pin A1 on the Arduino.
  • Code:

#define sensorPower 7
#define sensorPin A1

void setup() {
  pinMode(sensorPower, OUTPUT);
  digitalWrite(sensorPower, LOW);
  Serial.begin(9600);
}

void loop() {
  Serial.print("Analog output: ");
  Serial.println(readSensor());
  delay(1000);
}

int readSensor() {
  digitalWrite(sensorPower, HIGH);
  delay(10);
  int val = analogRead(sensorPin);
  digitalWrite(sensorPower, LOW);
  return val;
}

Outcome: The sensor provided analog readings of soil moisture levels, which will be useful for automated watering systems.


Light Intensity Sensor

A light intensity sensor measures the amount of light in an environment. It is used in applications like smart lighting and plant growth monitoring.

  • Connections:

    • VCC: Connected to the VIN pin on the Arduino.
    • GND: Connected to the GND pin on the Arduino.
    • Signal: Connected to analog pin A4 on the Arduino.
  • Code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(A4);
  Serial.print("Analog reading: ");


 Serial.print(analogValue);
  delay(1000);

  if (analogValue < 10) {
    Serial.println(" - Dark");
  } else if (analogValue < 800) {
    Serial.println(" - Dim");
  } else if (analogValue < 500) {
    Serial.println(" - Light");
  } else if (analogValue < 300) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
}

Outcome: The sensor accurately measured light intensity, displaying different brightness levels based on the readings.


Air Quality Sensor

An air quality sensor measures the concentration of pollutants in the air, useful for environmental monitoring.

  • Connections:

    • VCC: Connected to the VIN pin on the Arduino.
    • GND: Connected to the GND pin on the Arduino.
    • Signal: Connected to analog pin A2 on the Arduino.
  • Code:

int sensorPin = A2;
int sensorData;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  sensorData = analogRead(sensorPin);
  Serial.print("Air Quality: ");
  Serial.print(sensorData, DEC);
  Serial.println(" PPM");
  delay(100);
}

Outcome: The sensor provided real-time air quality readings, which are critical for monitoring plant health and environmental conditions.


Sensor Integration and Testing
  1. Sensor Wiring:

    • Each sensor was connected to the Arduino UNO with jumper wires, following the pin connections detailed above.

    • Power (VCC) and ground (GND) lines were shared among sensors where applicable, using a breadboard for better organization.

    • Analog and digital signals were connected to their respective pins on the Arduino.

  1. Testing:

    • After connecting all sensors, the Arduino was powered on, and each sensor’s code was uploaded using the Arduino IDE.
    • Serial Monitor in the Arduino IDE was used to observe and verify sensor readings.

Third Task

We redesigned the 3D model of our final project into 2D designs for laser cutting. This allowed us to create a small-sized prototype. The faces of the design were cut using a laser-cutting machine and assembled with tape. We will present this prototype next week, demonstrating the project’s functionality with sensor wiring.


Last update: September 13, 2024