9. Input Devices
Group assigment
- Probe an input device(s)'s analog levels and digital signals.
- Document your work on the group work page and reflect on your individual page what you learned
Link to a Group page of Input Devices
Individual assigment
- Measure something: add a sensor to a microcontroller board that you have designed and read it.
Summary
HC-SR04 Ultrasonic sensor
This is an electronic device that measures distance by using ultrasonic sound waves. It works very similarly to how bats and dolphins use echolocation; mainly to locate prey when hunting.
It has two main parts: Transmitter that emits a high-frequency sound wave (usually above 20 kHz, which is inaudible to humans) and Receiver that dects the sound wave when it bounces back from an object.
It calculates the distance to the object using the time it took for the echo to return:
Distance= Speed of sound x Time delay / 2
Note: The division by 2 accounts for the sound traveling to the object and back.

Circuit connection
On Xiao RP2040,
- I connected TRIG to D2
- ECHO to D1
- LED is connected to D0
Arduino code
// Defining pin connections
#define TRIG D2
#define ECHO D1
#define BLUE_LED D0
// Setting pins mode
void setup() {
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(BLUE_LED, OUTPUT);
}
// Main function
void loop() {
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int duration, distance;
duration = pulseIn(ECHO, HIGH);
distance = duration * 0.0343 / 2;
Serial.print("distance is ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 10){
digitalWrite(BLUE_LED, HIGH);
delay(300);
digitalWrite(BLUE_LED, LOW);
delay(300);
Serial.println("Too close");
}
else{
digitalWrite(BLUE_LED, LOW);
}
delay(800);
}


Result
As shown in the result, as move I move an object far and closer to the sensor, the distance is displayed onto the serial output.

HC-SR501 PIR module Sensor
This is a motion sensor divice that detects motion by measuring changes in infrared (IR) radiation from its environment. It's widely used in homemade(DIY) electronics, automation systems, and security applications.

Arduino code HC-SR501
// Define the pin connected to the PIR sensor OUT pin
const int pirPin = D2;
void setup() {
// Start the serial communication
Serial.begin(115200);
// Setting the PIR pin as an input
pinMode(pirPin, INPUT);
}
void loop() {
// Reading the value from the PIR sensor (HIGH or LOW)
int motionDetected = digitalRead(pirPin);
if (motionDetected == HIGH) {
// Motion detected
Serial.println("Motion Detected!");
} else {
// if no motion
Serial.println("No motion detected.");
}
// Add a small delay to avoid flooding the Serial Monitor
delay(1000);
}


Prevously, the module was detecting every motion even the slighithest motion of curtain waving due to wind. This made quite difficult to actual motion of an abject.

In the end, I found that I have to adjust sensitivity and timer accordingly by turning the sensitivity potentiometer to reduce the sensor's sensitivity. Meaning it will only trigger when larger movements are detected. This is the easiest way to avoid false positives.

Result
Download files
Here are source code files for Ultrasonic and PIR Sensors
Ultrasonic Sensor HC-SR501 PIR Motion Sensor