Overview
This week was about input devices. Input devices allow a microcontroller to receive information from the physical world. They can detect changes such as distance, light, touch, temperature, motion, or button states, and convert these changes into electrical signals.
Input signals can be digital or analog. A digital input only has two states: HIGH and LOW, usually represented as 1 and 0. An analog input changes continuously within a voltage range, and the microcontroller converts this voltage into a numerical value using ADC.
For this week, I used a button module to test digital input, a line finder sensor to test analog input, and an HC-SR04 ultrasonic distance sensor for my individual assignment.
Assignment
Group Assignment
The group assignment was to probe an input device's analog levels and digital signals.
Individual Assignment
The individual assignment was to measure something by adding a sensor to a microcontroller board that I designed and reading it.
Individual Assignment
HC-SR04 ultrasonic distance sensor
For my individual assignment, I used an HC-SR04 ultrasonic distance sensor as my input device. The goal was to add a sensor to a microcontroller board that I designed and read its data.
The HC-SR04 sensor measures distance by sending an ultrasonic pulse from the Trig pin and receiving the reflected signal through the Echo pin. The microcontroller measures the duration of the Echo pulse and converts it into a distance value in centimeters.
Wiring
I connected the HC-SR04 sensor to my own XIAO ESP32S3 PCB. The sensor was powered by 5V and GND. The Trig pin was connected to D2, and the Echo pin was connected to D3.
| HC-SR04 | XIAO ESP32S3 PCB |
|---|---|
| VCC | 5V |
| GND | GND |
| Trig | D2 |
| Echo | D3 |
Arduino code
I wrote the following Arduino code to trigger the ultrasonic pulse, read the Echo pulse duration, and convert it into distance in centimeters.
const int trigPin = D2;
const int echoPin = D3;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
float distance = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Testing process
After uploading the code, I opened the Arduino Serial Monitor and set the baud rate to 115200. The sensor started printing distance values in centimeters.
I tested the sensor by moving my hand closer to and farther away from the front of the sensor. When there was no object close to the sensor, the measured distance was around 390 cm. When I moved my hand closer, the value decreased to around 8 cm, 6 cm, and 5 cm.
Result
The HC-SR04 sensor worked successfully with my XIAO ESP32S3 PCB. The distance value changed according to the position of my hand. When my hand moved closer to the sensor, the measured distance became smaller. When my hand moved farther away, the value became larger.
This confirmed that my board successfully read input data from the ultrasonic distance sensor.
Group Assignment
For the group assignment, I tested both digital and analog input signals. I used a button module to observe a digital input signal, and I used a line finder sensor to observe an analog input level.
Digital input test: Button module
For the digital input test, I used a button module. The module has three pins: GND, VCC, and OUT. Since the OUT pin changes between two states, HIGH and LOW, it can be used as a digital input device.
I connected the button module to my PCB. VCC was connected to 3.3V, GND was connected to GND, and OUT was connected to a digital input pin on the board.
| Button module | PCB / XIAO ESP32S3 |
|---|---|
| GND | GND |
| VCC | 3.3V |
| OUT | D3 |
Button Arduino code
I used the following code to read the button value and print it in the Serial Monitor.
const int buttonPin = D3;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonValue = digitalRead(buttonPin);
Serial.println(buttonValue);
delay(100);
}
Serial Monitor test
After uploading the code, I opened the Arduino Serial Monitor. When the button was pressed and released, the value changed between 0 and 1. This showed that the microcontroller could successfully read the button state.
Multimeter test
To check the physical voltage level of the signal, I measured the voltage between OUT and GND using a multimeter. The multimeter was set to DC voltage mode. The black probe was connected to GND, and the red probe was connected to OUT.
In one measured state, the OUT pin was about 3.29V, which represents a HIGH signal. In another measured state, the voltage was around 2.2V in my test setup. This value was not close to 0V because the measurement connection included a 2.2kΩ resistor in the circuit. Instead of assuming an ideal 0V reading, I recorded the actual voltage under my test condition.
At the same time, the Arduino Serial Monitor showed that the microcontroller could distinguish the two button states as 0 and 1. This means the button module worked as a digital input device, while the multimeter helped me observe the physical voltage level of the signal.
| Input device | Signal type | Measured behavior | Serial Monitor |
|---|---|---|---|
| Button module | Digital | The OUT voltage changed between two measured states: about 3.29V and around 2.2V in this setup because the test circuit included a 2.2kΩ resistor. | 0 / 1 |
Through this test, I understood that the Serial Monitor shows the value interpreted by the microcontroller, while the multimeter shows the physical voltage level of the signal under the actual circuit condition.
Analog input test: Line finder sensor
To test an analog input, I used a line finder sensor. A line finder sensor detects the difference between light and dark surfaces using reflected infrared light. This module has both DO and AO pins. DO is a digital output, while AO is an analog output.
I connected the AO pin to an analog input pin on the XIAO ESP32S3 board, and I also connected the DO pin to a digital input pin. In the Serial Monitor, the DO value stayed at 1 during my test, while the AO value changed continuously when I moved the sensor over different surfaces.
In my test, the AO value changed between values such as 2652, 3367, 3633, 3978, and 4069. These values are not simply 0 or 1, so they show that the AO pin provides an analog signal related to the reflected infrared light.
| Line finder pin | Connection | Read method | Result |
|---|---|---|---|
| VCC | 3.3V | - | Power |
| GND | GND | - | Ground |
| DO | Digital input pin | digitalRead() | Digital value |
| AO | Analog input pin | analogRead() | Continuous analog value |
Line finder Arduino code
I used the following code to read both the digital output and the analog output of the line finder sensor.
const int lineDigitalPin = D3;
const int lineAnalogPin = A0;
void setup() {
Serial.begin(115200);
pinMode(lineDigitalPin, INPUT);
}
void loop() {
int digitalValue = digitalRead(lineDigitalPin);
int analogValue = analogRead(lineAnalogPin);
Serial.print("DO: ");
Serial.print(digitalValue);
Serial.print(" AO: ");
Serial.println(analogValue);
delay(200);
}
Group assignment result
In the group assignment, I tested both digital and analog input signals. The button module showed a digital signal because the Serial Monitor value changed between 0 and 1. The line finder sensor showed an analog signal because its AO value changed continuously according to the surface and reflection condition.
These tests helped me understand the difference between digital input and analog input. Digital input is useful for simple state detection, while analog input can describe gradual changes in the physical environment.
Problems and Solutions
| Problem | Reason | Solution |
|---|---|---|
| The board upload sometimes failed. | The ESP32S3 board sometimes did not enter upload mode correctly, or the serial port was occupied. | I reselected the board and port in Arduino IDE, closed the Serial Monitor before uploading, and used the BOOT/RESET method when needed. |
| The HC-SR04 reading sometimes showed a very large value. | When there was no nearby object in front of the sensor, the echo signal returned a long distance value. | I tested the sensor by placing my hand closer to the front of the sensor and observing whether the distance value became smaller. |
| The button voltage did not drop to an ideal 0V in the multimeter test. | The measurement setup included a 2.2kΩ resistor, so the measured voltage was affected by the actual circuit condition. | I recorded the real measured value and compared it with the Serial Monitor reading, which still showed a clear 0 / 1 digital change. |
| The line finder DO value stayed at 1. | The digital threshold may not have been crossed during the test, or the sensor threshold was not adjusted. | I focused on the AO value, which changed continuously and clearly showed analog input behavior. |
What I Learned
This week helped me understand how different input devices send information to a microcontroller. A button module provides a simple digital signal, while a line finder sensor can provide an analog value through its AO pin. The HC-SR04 sensor measures distance by using time information from an ultrasonic pulse.
I also learned that the Serial Monitor and the multimeter show different aspects of the same system. The Serial Monitor shows the value interpreted by the microcontroller, while the multimeter shows the physical voltage level at a specific point in the circuit.
The most important lesson was that an input device is not only a component. It is part of a complete sensing process: physical change, electrical signal, microcontroller reading, and interpreted data.
Reflection
Through this week, I learned how input devices allow a microcontroller to sense the physical world. The button module helped me understand digital input, because it only has two states. The line finder sensor helped me understand analog input, because its AO value changed continuously according to reflected light.
The HC-SR04 ultrasonic distance sensor was especially useful for my individual assignment. Compared with a button, it does not only provide HIGH or LOW states. Instead, it provides a measured distance value. This made me understand how sensor data can be converted into meaningful information for interaction design.
For my final project, input devices will be very important. For example, distance sensing could help my robot detect whether a user is approaching or moving away. Other sensors, such as touch sensors, microphones, or cameras, could also help the robot respond more naturally to people.