Skip to content

9. Input Devices

This week

I experimented with a variety of input devices this week and discovered how to connect different sensors to my own PCB. To get a better understanding of how to collect and analyze real-world data for embedded applications, I experimented with a variety of sensing approaches, including motion tracking, distance measuring, and environmental sensing.

Group work

In the group work

Individual assignment

So I connected my board and added the test code that chat gpt wrote under the prompt. make a code for the picture i included for the DHT11 temperature sensor.

#include "DHT.h"

#define DHTPIN 10      // GPIO4 (D2)
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  while (!Serial);   // IMPORTANT for ESP32-C3
  delay(2000);
  dht.begin();
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Temp: ");
  Serial.print(temperature);
  Serial.print(" °C  Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  delay(2000);
}

This program reads temperature and humidity from a DHT11 sensor and sends the values to my computer. So the program is basically set up to grab temperature and humidity readings from this DHT11 sensor thats hooked up to pin 10 on the ESP32-C3 board. It starts by getting the serial communication going, you know, so all the data shows up nicely in the Serial Monitor. That seems straightforward, but I am not totally sure if the delay is always needed, some setups might skip it. Anyway, the readings come out continuously like that. Theres a quick delay right after that, just to give the sensor a moment to settle down before initializing it properly. If the sensor fails to return valid data, it prints an error message. I had one problem with connecting it but just pluging in and out worked in the end for me.

I connected it to the 10th port and it produced this result. alt text alt text