Skip to content

11. Input devices

This week we worked on how to receive input data from sensors. During our group work, we learned how to visualize input data being measured and read by analog and digital sensors on the oscilloscope. We also looked at IR phototransistor, but we were not able to get clear readings since they were quite noisy. We discussed amongst ourselves various possibilites for this event. A number of reasons such as no signficant change in voltage level, or incorrect connections came into our discussion.

Reading Input Data (Temperature and Humidity)

I used the DHT11 sensor to measure temperature and humidity data. In Week 04. Embedded Programming, more information about this sensor has been mentioned.

Hardware setup

DHT11 sensor has 3 pinouts: Signal pin, Vcc (aka. Power) pin and GND pin.

  1. Signal pin of the DHT11 sensor is connected to the D4 (SDA) pin of Xiao RP2040 MCU on the PCB.
  2. Power pin of the DHT11 sensor is connected to the 5V pin of Xiao RP2040 MCU on the PCB since the DHT11 sensor works with a power supply in the range of from 3.5V to 5.5V DC.
  3. GND pin of the DHT11 sensor is connected to the GND pin of Xiao RP2040 MCU on the PCB

Layout/schematics of PCB (with Seeed Xiao RP2040 MCU) can be seen here, and corresponding design files in Kicad can be found from here. I designed the PCB board in Week 06. Electronics Design, and fabricated it in Week 08. Electronics Production.

DHT11 Sensor connected to the pin-header on the PCB, and two glasses beside it: one filled with normal tap water, and the other filled with mildly hot coffee

Code to read Temperature and Humidity Data via DHT11 Sensor

#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <Wire.h>

#define DHTPIN D4     // Digital pin on Seeed Xiao RP2040 MCU connected to the DHT sensor's Data/signal Pin

#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

int count=1;

void setup()
{
  Serial.begin(115200);
  Serial.println(F("DHTxx test!"));
  dht.begin();
  delay(2000);
}

void loop()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t))
  {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  Serial.print(F("Count: "));
  Serial.println(count);
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.println("%");
  Serial.print(F("Temperature: "));
  Serial.print(t);
  Serial.println(F("°C "));
  Serial.println("");
  count=count+1;
  delay(2000);
}

Demo

The following video clip shows temperature and humidity data being measured and read in real-time in Arduino’s Serial Monitor. Each temperature and humidity is accounted for with the “count” variable (which increments by 1 for every next reading). For the demo, I show the DHT11 sensor measuring temperature and humidity data under three conditions:

I. Normal Indoor Room Conditions with no additional conditions [00.00.00 - 00.00.58 in the timeline (in hh-mm-ss)]

II. A Glass filled with Normal Tap Water right beside the DHT11 sensor [00.00.60 - 00.01.43 in the timeline (in hh-mm-ss)]

III. A Glass filled with Mildely Hot Coffe right beside the DHT11 sensor [00.01.45 - 00.07.41 in the timeline (in hh-mm-ss)]

The timeline for condition III is higher because I waited for temperature and humidity readings to be converged, and under this condition, it took longer to converge to values beyond which they were not changing anymore.

Video 1: DHT11 sensor data being measured and read in real-time (at an interval of 2 seconds) under Condition I (from Count 1220 to Count 1249), II (from Count 1250 to Count 1446), and III (from Count 1272 to Count ).

The following observations can be made based on the readings:

I. Readings for the condition I are stable around the temperature of 24.20 Celsius and the humidity of 34%.

II. Readings for condition II are stable around the temperature of 24.10 Celsius and the humidity of 34%.

III. Readings for condition III are stable around the temperature of 29.90 Celsius and the humidity of 25%.

There is no major difference in readings (temperature and humidity) from conditions I and II, but readings from condition III are substantially different (for both variable temperature and humidity).

All measurements have been done in indoor conditions. These readings under different conditions also indicate that the DHT11 sensor should only be used in those situations where temperature and humidity are expected to change gradually, like indoor home conditions. It is not ideal for measuring a sudden change in temperature and humidity, thus not ideal for safety-critical applications, because it takes some time to adapt to the condition. For example, it took roughly 6 minutes to adapt to condition III in my experiment.