跳转至

Week 9 - Input Devices

This week we start learning about Input Devices. Input devices are the gateway through which embedded systems perceive the external world. They can convert real-world information such as button presses, temperature, humidity, light, distance, sound, etc. into electrical signals, which are then read and processed by the microcontroller.

The signal forms output by different input devices are not the same. Some devices output digital signals, which have only two states: high level and low level; some devices output analog signals, where the voltage changes continuously with the external environment; and some sensors output data through communication protocols, such as OneWire, I2C, SPI, or UART. Therefore, the focus of this week is to understand the types of input signals and learn how to read sensor data on a microcontroller.

Group assignment:

The group assignment mainly involves using test equipment such as oscilloscopes to observe the signal characteristics of input devices. By measuring the digital signals of the key module, analyze the changes in high and low levels when it is pressed and released; at the same time, understand the differences between analog and digital signals, as well as the basic working principles of common communication interfaces.

Individual assignment:

Individual assignments require integrating at least one sensor on a self-designed microcontroller development board and implementing data acquisition and processing through programming. This time, I choose to use the DHT22 temperature and humidity sensor, read temperature and humidity data via MicroPython, and output the results in the REPL. Finally, based on the sensor parameters and actual readings, a simple analysis of the measurement accuracy is conducted.

Overall, the focus of this week is not to implement complex sensor fusion, but to complete the entire process from hardware connection, protocol understanding, code reading to data verification. Through these basic exercises, we can lay the foundation for environmental perception and interactive control in subsequent projects.

Group Assignment

Group Assignment Page: Week 9 — Group Assignment: Input Devices | FAB26 - ChaiHuo Makerspace 2026

1. Input Signal Type

Before the formal measurement, we first distinguished several common types of input signals.

Signal Type Features Typical Equipment
Digital Signal There are only two states, high level and low level Push button, touch switch, limit switch
Analog Signal The voltage changes continuously and requires ADC reading Potentiometer, Photoresistor, Analog Temperature Sensor
Bus Communication Signal Transmit data via Communication Protocol I2C Sensor, SPI Sensor, DHT22

Digital signals are typically used to determine \"whether a certain event has occurred\", such as whether a button has been pressed. Analog signals, on the other hand, can represent continuously varying physical quantities, such as position or light intensity. Bus communication signals encode data into a specific protocol, and the microcontroller needs to read it according to the protocol timing.

2. Measure digital signals

This group test uses the button module as a digital input device. The output terminal of the button module is connected to the oscilloscope probe, and GND is connected to the oscilloscope ground. By pressing and releasing the button, the level changes are observed.

To verify the digital signal output of the key module, we connected the oscilloscope probe to the DIN pin of the module, and the ground clip to GND. This way, the oscilloscope can directly observe the waveform of the key output signal changing over time.

Image

After completing the wiring, press the button on the module, and the oscilloscope successfully captures the high-low level transition waveform. The rising and falling edges of the waveform are synchronized with the button action, indicating that the button module can stably output digital signals.

Image

In an oscilloscope, the vertical axis represents voltage, and the horizontal axis represents time.

The parameters observed this time are as follows:

Project Observation Results
Vertical Scale 2 V / div
Waveform Height Approximately 1.75 div
Estimated Voltage 2 V × 1.75 ≈ 3.3 V
Horizontal Scale 100 ms / div
One cycle Approximately 2 div
Estimation Cycle 100 ms × 2 = 200 ms

3. Comparison between Analog Signals and Digital Signals

Although the actual measurement this time was of the key digital signal, the signal characteristics of analog input devices were also discussed in the group assignment. The output of an analog sensor is not simply 0 and 1, but a continuously varying voltage.

For example, the voltage at the output terminal of a potentiometer changes continuously with the position of the . If the power supply is 3.3 V, the potentiometer output may vary between 0 V and 3.3 V. A microcontroller cannot directly use this continuous voltage as digital data, so it needs to pass through ADC \(Analog\-to\-Digital Converter\) to convert the voltage into a digital value.

The differences between digital signals and analog signals can be summarized as follows:

This observation has allowed me to understand the differences between input devices more clearly. For inputs such as buttons, the program mainly determines high and low levels; for analog sensors, the program needs to read the ADC value and further convert it into an actual physical quantity.

4. Principles of Common Input Interfaces

In addition to directly outputting digital or analog signals, input devices may also output data through a communication protocol.

4.1 ADC Analog-to-Digital Conversion

ADC is used to read analog voltage. The microcontroller will convert the input voltage into a digital value. For example, the range of a 12-bit ADC is typically 0-4095, with a larger value indicating a higher input voltage.

4.2 I2C Bus

I2C uses SDA and SCL two signal lines for communication, suitable for connecting multiple low-speed sensors, such as temperature and humidity sensors, barometric pressure sensors, OLED displays, etc. Each device is distinguished by its address.

4.3 SPI Bus

SPI typically uses signal lines such as MOSI, MISO, SCK, and CS, with a relatively high speed, making it suitable for connecting displays, memories, or high-speed sensors.

4.4 OneWire / OneWire Class Protocol

The DHT22 uses a communication method similar to the single-wire bus, requiring only one data line to transmit temperature and humidity data. It has relatively strict timing requirements, so it usually requires the use of an existing library for reading.

Individual Assignment

1. Sensor Selection

For this individual assignment, I chose to use *the DHT22 Temperature and Humidity Sensor *as the input device. The DHT22 can measure both ambient temperature and relative humidity simultaneously, with its output being a digital signal, making it suitable for learning sensor data acquisition.

The advantage of DHT22 is that its wiring is simple, requiring only VCC, GND, and one data pin; meanwhile, the dht library has already been provided in MicroPython, which allows for relatively convenient data reading.

The input device materials used this time are as follows:

Material Quantity Function
DHT22 Temperature and Humidity Sensor Module 1 Collect temperature and humidity
Independently Developed Development Board 1 As the main control platform
Connection Line Several Connect the sensor to the development board

2. Hardware Connection

First, according to the module pin definition, connect the sensor\'s VCC and GND to the power and ground interfaces of the development board respectively, then connect the data pin DIO to the GPIO pin of the development board.

The original record uses GPIO20, while the code actually uses GPIO6. During the final test, it is necessary to ensure that the hardware wiring and the pin numbers in the code are consistent; otherwise, the program will fail to read.

Image

Image

After power is applied, the module indicator light turns on, indicating that the sensor is powered normally. After the hardware connection is completed, you can proceed to the program reading stage.

3. DHT22 Communication Principle

The DHT22 is a digital temperature and humidity sensor that has already completed the measurement of humidity and temperature internally, and sends the results to the microcontroller via a single-wire digital signal. The microcontroller does not need to perform analog voltage conversion on its own, but only needs to read the data according to the protocol.

Its communication process is roughly as follows:

  1. The microcontroller sends a start signal to the DHT22.

  2. The DHT22 responds to the host\'s request.

  3. The sensor sends temperature and humidity data according to a fixed timing sequence.

  4. The single-chip microcomputer parses the data and calculates whether the checksum is correct.

Since the communication timing of the DHT22 is relatively strict and writing low-level drivers manually is rather cumbersome, this time we directly use the dht library provided by MicroPython for reading.

Image

Image

Image

Image

4. Program Writing and Burning

This program runs in the MicroPython environment. The program first imports machine\.Pin, time, and dht modules, then creates a DHT22 object, reads temperature and humidity in a loop, and prints them via REPL.

Python
# Python env   : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time    : 2025/9/1 上午11:22
# @Author  : ben0i0d
# @File    : main.py
# @Description : dht22驱动测试文件 测试输出温湿度

# ======================================== 导入相关模块 =========================================

from machine import Pin
import time
import dht

# ======================================== 全局变量 ============================================

# ======================================== 功能函数 ============================================

# ======================================== 自定义类 ============================================

# ======================================== 初始化配置 ===========================================

# 延时等待设备初始化
time.sleep(3)
# 打印调试信息
print("FreakStudio : Using OneWire to read DHT22 sensor")

# 延时1s,等待DHT22传感器上电完成
time.sleep(1)
# 假设数据脚接 GPIO6
d = dht.DHT22(Pin(6))

# ========================================  主程序  ============================================

while True:
    try:
        d.measure()
        temp = d.temperature()
        hum = d.humidity()
        print("Temperature: %.1f C  Humidity: %.1f %%" % (temp, hum))
    except Exception as e:
        print("Read error:", e)
    time.sleep(2)

In the code, d\.measure\(\) is used to trigger sensor measurement, d\.temperature\(\) reads the temperature, d\.humidity\(\) reads the humidity. A 2-second delay is set between each reading because the sampling speed of the DHT22 cannot be too fast, and frequent readings are prone to errors.

Image

The development board successfully reads DHT22 data and prints the temperature and humidity in the REPL, indicating that both the hardware connection and the program reading are working properly.

5. Sensor Performance Parameters

The main performance parameters of the DHT22 are as follows:

Humidity Temperature
Resolution 0.1 %RH 0.1 ℃
Measuring Range 0 ~ 99.9 %RH -40 ~ 80 ℃
Accuracy ±2 %RH \(at 25℃\) ±0.5 ℃
Recommended Storage Conditions ≤ 60% RH 10℃ ~ 40℃
Operating Voltage \multicolumn{2}{c}{3.3V ~ 5V}

According to the common specifications of the DHT22, its temperature measurement range is approximately -40°C to 80°C, with a temperature accuracy typically ±0.5°C; the humidity measurement range is approximately 0-100%RH, with a humidity accuracy typically ±2%RH to ±5%RH. Compared to the DHT11, the DHT22 has a larger measurement range and higher accuracy, making it more suitable for environmental monitoring projects.

6. Measurement Accuracy Verification

To verify whether the sensor readings are reasonable, I continuously read multiple sets of temperature and humidity data and observed whether the readings were stable. Since no calibrated professional temperature and humidity meter was used for comparison this time, the main focus here is on stability verification rather than strict laboratory calibration.

Test Method:

  1. Place the DHT22 in a stable indoor environment.

  2. Read data once every 2 seconds.

  3. Observe whether there are obvious jumps in the temperature and humidity readings.

  4. Roughly compare with indoor perceived temperature and environmental humidity.

The recording method is as follows:

Number of times **Temperature \(°C\) ** Humidity \(%RH\) **Observe **
1 Room temperature reading Environmental humidity reading Reading is normal
2 Smaller change Smaller change Stable
3 Smaller change Smaller change Stable

As can be seen from the REPL output results, the sensor can continuously return temperature and humidity data without frequent occurrences of Read error. In an indoor environment, the temperature readings change little, and the humidity does not fluctuate significantly, indicating that the sensor reading process is basically stable.

If more rigorous accuracy verification is required in the future, a calibrated thermo-hygrometer can be used as a reference value, and then the error between the DHT22 reading and the reference instrument can be calculated. For example:

Error = DHT22 Reading - Reference Instrument Reading

This way, a more accurate deviation range can be obtained, and it can be determined whether the sensor meets the project requirements.

7. Design Documents and Bill of Material

The source files of the development board used this week come from the self-made development board files of the previous Electronic Design / Electronic Product Production Week.

The Bill of Material for input device-related items is as follows:

Name Quantity Description
DHT22 Temperature and Humidity Sensor Module 1 Input device, used for collecting temperature and humidity
Independently Produce ESP32-C3 / XIAO-C3 Development Board 1 Main Control and Interface Expansion
Connection Line Several Connect VCC, GND, DIO
MicroPython Firmware 1 Run the read program

8. Summary

Through this week\'s assignment, I completed the basic process from hardware connection of input devices to program reading. In the group assignment, we observed the digital signals of the key module using an oscilloscope and understood concepts such as high and low levels, rising edges, falling edges, and voltage cycles; in the individual assignment, I used the DHT22 temperature and humidity sensor to implement data acquisition and printed the results using MicroPython.

This exercise has made me realize that the key to input devices lies not only in \"connecting the wires\" but also in understanding the type of signals they output. If it\'s a button, attention should be paid to high and low levels and jitter; if it\'s an analog sensor, an ADC should be used; if it\'s a digital sensor like DHT22, attention should be paid to communication timing and reading frequency. Only by understanding the signals themselves can sensor data be used more stably in subsequent practical projects.

[!NOTE]

AI Assistance:

During the preparation of this documentation, ChatGPT (GPT-4) was used as a language assistance tool.

It helped with sentence polishing and translation from Chinese to English to improve readability and clarity.