Project Overview Input + Control

For this week, I worked with input devices by integrating a temperature sensor into my system. The goal was to read environmental temperature and use that data to control an output device automatically.

I used the SHT31 temperature sensor as the input device and programmed the system so that when the temperature reaches 30°C, a fan is activated.

This project builds directly on previous weeks:

  • Week 6: PCB design
  • Week 8: PCB fabrication and soldering
  • Week 9: Functional integration (sensor + control)
Sensor SHT31

The SHT31 is a digital temperature and humidity sensor that communicates using the I2C protocol. It provides accurate environmental measurements and is widely used in embedded systems.

Key characteristics

  • Digital communication (I2C)
  • High accuracy temperature readings
  • Fast response time
  • Low power consumption

In this project, only the temperature data was used to trigger the fan.

Parameter Value
Temperature range -40°C to 125°C
Accuracy ±0.3°C
Communication I2C
Supply voltage 2.4V – 5.5V
Connections Hardware

The sensor was connected to the same PCB fabricated in Week 8. The communication was done through the I2C interface.

Component Connection
SHT31 VCC 3.3V
SHT31 GND GND
SHT31 SDA Microcontroller SDA
SHT31 SCL Microcontroller SCL
Fan Controlled output (transistor or driver)

The fan cannot be powered directly from the microcontroller, so it is controlled through a transistor or driver circuit.

Working Principle Logic

The system continuously reads temperature data from the SHT31 sensor. Based on this reading, a decision is made:

  • If temperature is below 30°C → fan OFF
  • If temperature is 30°C or higher → fan ON

This type of system is an example of a basic control loop using an input device.

Code Embedded Programming

The code reads the temperature via I2C and controls a digital output connected to the fan.

#include "pico/stdlib.h"
#include "hardware/i2c.h"

#define I2C_PORT i2c0
#define SDA_PIN 4
#define SCL_PIN 5

#define FAN_PIN 15

#define SHT31_ADDR 0x44

void read_temperature(float *temperature) {
    uint8_t cmd[2] = {0x24, 0x00};
    i2c_write_blocking(I2C_PORT, SHT31_ADDR, cmd, 2, false);

    sleep_ms(15);

    uint8_t data[6];
    i2c_read_blocking(I2C_PORT, SHT31_ADDR, data, 6, false);

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

    *temperature = -45 + (175 * ((float)rawTemp / 65535.0));
}

int main() {

    stdio_init_all();

    i2c_init(I2C_PORT, 100000);
    gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
    gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
    gpio_pull_up(SDA_PIN);
    gpio_pull_up(SCL_PIN);

    gpio_init(FAN_PIN);
    gpio_set_dir(FAN_PIN, GPIO_OUT);

    float temperature;

    while (true) {
        read_temperature(&temperature);

        if (temperature >= 30.0) {
            gpio_put(FAN_PIN, 1);
        } else {
            gpio_put(FAN_PIN, 0);
        }

        sleep_ms(1000);
    }
}
Code Explanation Analysis

I2C Communication

The sensor is accessed through I2C using SDA and SCL pins. The command 0x2400 triggers a temperature measurement.

Temperature Conversion

The raw sensor data is converted into Celsius using the formula:

T = -45 + 175 * (raw / 65535)

Control Logic

The program compares the measured temperature with the threshold (30°C). If the condition is met, the fan is activated.

Loop Execution

The system runs continuously, updating every second.

Result System Behavior

The final system successfully reads temperature data and controls the fan automatically.

  • Stable readings from the sensor
  • Immediate response at 30°C
  • Reliable switching of the fan
Final system with sensor and fan control.
Reflection

This week demonstrated how input devices can be integrated into embedded systems to create responsive behavior.

The combination of sensor data and control logic is fundamental in automation systems. This project also shows the continuity of the design process, from PCB design to real-world functionality.

Understanding how to read sensors and react to their data is a key step toward building more complex systems, such as smart enclosures, environmental monitoring, or automated control systems.