Week 11 – Networking and Communications

Environmental monitoring system with OLED visualization and automatic fan control using an SHT31 sensor.

Project Overview Sensor Interface

For this assignment I created a complete environmental monitoring interface using an OLED display, an SHT31 temperature and humidity sensor, and a fan output.

The goal of the system is to continuously measure temperature and humidity, display the values on an OLED screen, and automatically activate a fan when the temperature exceeds 30°C.

This project combines several elements developed during previous weeks:

  • Input device: SHT31 temperature and humidity sensor
  • Output device: fan control
  • Interface: OLED screen visualization
  • Embedded programming using Arduino IDE
Hardware Used Components
Component Purpose
SHT31 Sensor Reads temperature and humidity values
SH1106 OLED Display Displays environmental information
Fan Activated when temperature exceeds threshold
Microcontroller Processes data and controls the system

Both the SHT31 sensor and the SH1106 OLED display use the I2C communication protocol, allowing them to share the same SDA and SCL lines.

Libraries Used Code Structure

Several libraries were included to simplify communication with the sensor and the OLED display:


#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <SPI.h>
  • Arduino.h provides the basic Arduino functions.
  • Wire.h is used for I2C communication.
  • Adafruit_GFX.h provides text and graphic drawing functions.
  • Adafruit_SH110X.h controls the OLED display.
  • SPI.h is included although not directly used in this project.
Pin and Address Definitions Configuration

#define i2c_Address 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

#define SHT31_ADDR 0x44
#define FAN_PIN 1

The OLED display uses the I2C address 0x3C, while the SHT31 sensor uses the address 0x44.

The display resolution is configured as 128x64 pixels, and the fan output is connected to pin 1.

Display Object OLED Setup

Adafruit_SH1106G display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  OLED_RESET
);

This creates the OLED display object using the specified screen dimensions and the I2C communication bus.

Once created, this object is used later to write text, draw shapes, and update the screen.

Setup Function Initialization

The setup function initializes all peripherals before entering the main loop.


Serial.begin(115200);
Wire.begin();
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);

The serial monitor is initialized at 115200 baud for debugging purposes. The I2C bus is started with Wire.begin().

The fan pin is configured as an output and starts in the OFF state.

The display is also initialized:


if (!display.begin(i2c_Address, true)) {
  Serial.println("Fail to initialize OLED SH110X");
  while (1);
}

If the OLED cannot be detected, the program stops completely.

After initialization, a startup message is shown:


display.println("Iniciando...");
Sensor Communication I2C Protocol

In the loop function, the program first sends a command to the SHT31 sensor:


Wire.beginTransmission(SHT31_ADDR);
Wire.write(0x24);
Wire.write(0x00);
Wire.endTransmission();

The bytes 0x24 0x00 tell the SHT31 sensor to perform a temperature and humidity measurement.

After sending the command, the program waits 20 milliseconds for the measurement to complete.

Reading Sensor Data Data Processing

Wire.requestFrom(SHT31_ADDR, 6);

The sensor returns 6 bytes of data:

  • 2 bytes for temperature
  • 1 CRC byte
  • 2 bytes for humidity
  • 1 CRC byte

These bytes are combined into raw 16-bit values:


uint16_t rawTemp = (data[0] << 8) | data[1];
uint16_t rawHum  = (data[3] << 8) | data[4];

Then the raw values are converted into human-readable units:


temperature = -45.0 + (175.0 * rawTemp / 65535.0);
humidity = 100.0 * rawHum / 65535.0;

The first formula converts the raw temperature value into degrees Celsius, while the second converts humidity into percentage.

Fan Control Logic Output Device

Once the temperature is calculated, the system decides whether the fan should be turned on or off.


if (temperature > 30.0) {
  digitalWrite(FAN_PIN, HIGH);
} else {
  digitalWrite(FAN_PIN, LOW);
}

If the measured temperature is above 30°C, the fan is activated. Otherwise, the fan remains off.

This simple control logic creates an automatic cooling system.

Serial Monitor Output Debugging

The measured values are also printed to the serial monitor:


Serial.print("Temp: ");
Serial.print(temperature);
Serial.print(" C | Hum: ");
Serial.print(humidity);

This allows the user to monitor sensor readings in real time and verify whether the fan is ON or OFF.

OLED Interface Visualization

The OLED display is updated every cycle with the current sensor values.


display.clearDisplay();
display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SH110X_WHITE);

A border is drawn around the screen for visual organization.

The display then shows:

  • Sensor title
  • Temperature value
  • Humidity value
  • Fan status

Example:


display.print(temperature, 1);
display.print(" C");

display.print(humidity, 1);
display.print(" %");

The fan state is also shown:


display.println("FAN ON");
Error Handling Reliability

The code includes basic error handling in case the sensor or display fails.

If the sensor does not respond correctly, the OLED shows:


display.println("Error SHT3X");

If the reading process fails, the OLED shows:


display.println("Error lectura");

This makes the system easier to debug and more reliable during operation.

Final Result System Behavior

The final system successfully measures temperature and humidity, displays the values on the OLED screen, and automatically controls the fan.

  • Real-time temperature monitoring
  • Real-time humidity monitoring
  • Automatic fan activation above 30°C
  • OLED status visualization
  • Serial monitor debugging
Final environmental monitoring system
Final environmental monitoring system with OLED display and fan control.
Reflection

This project combined input devices, output devices, and user interface design into a single embedded system.

It demonstrated how sensor data can be processed, displayed, and used to trigger automatic actions.

This type of system can later be integrated into smart enclosures, climate monitoring systems, greenhouse control, or intelligent storage systems.