Skip to content

9-Input devices

Assignments

Group assignment:

  • 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

Individual assignment:

  • Measure something: add a sensor to a microcontroller board that you have designed and read it.

Learning outcomes

  • Demonstrate workflows used in sensing something with input device(s) and MCU board

Have you answered these questions?

  • Linked to the group assignment page.
  • Documented what you learned from interfacing an input device(s) to your microcontroller and optionally, how the physical property relates to the measured results.
  • Documented your design and fabrication process or linked to the board you made in a previous assignment.
  • Explained the programming process(es) you used.
  • Explained any problems you encountered and how you fixed them.
  • Included original design files and source code.
  • Included a ‘hero shot’ of your board.

The road map i made for this week can be accesed here

tt

Group Assignment

For this assignment, we were tasked with checking an input device(s)’s analog levels and digital signals

You can view our group assignment here

Reflection

For this week’s group assignment, we focused on examining how input devices behave by probing both analog readings and digital signals. I worked with the LDR-MH sensor module, which helped me gain a clearer understanding of how light-based sensors operate. Through this, I was able to better distinguish the difference between analog and digital outputs and observe how each type appears in real time. Using the Serial Monitor also gave me a more intuitive sense of how these signals change and respond to different conditions.

Individual Assignment

  • Measure something: add a sensor to a microcontroller board that you have designed and read it.

The input device I decided to work with for this week is a untrasonic sensor, because I’ll be using it for my final project. For this week i’ll be trying to make the led glow more with the distance measured from the ultrasonic snesor.The led will turn on when you reach a certain distance .

This is how my microcontroller looks like

heroshot

Now, for the coding part of the process,I’ll be using arduino IDE

Arduino

Downloading the library

Go to tools- boards- boards manage

esp library

Afterwards, you should access this.When you do, download the board you need.

library

After the board you need is downloaded, then you can access it by following the firzt steps, tools- boards- the board you’ll be using.

Since i’ll be working with a xaio board, i installed xaio.

xaio

Example code

When you want to access an example code, these are the steps you follow.File-examples-the code you want.

blink

These are the steps to use an example code, but because im documenting the code i used after my final project presentation, i didnt end up having to use an example code. I got most of my codes off of sites and chatgpt.

Microcontroller making

I’m going to be showing which input component i used for my final project as I’m continuing documentation after the presentation.

First Mcb

To clarify the wiring and pin usage on my XIAO board:

Ultrasonic Sensor Board Pin XIAO Pin Name Notes Sensor 1 Trigger 21 D6 Shared trigger for both sensors Sensor 1 Echo 6 D4 Echo 1 measurement Sensor 2 Echo 7 D5 Echo 2 measurement VCC 3.3V 3.3V Power supply GND GND GND Ground

This was the first scematic I made. I didn’t really end up useing this in the end for the assignment.

kicad2

note: When making scematics, the dot on line interconnections show that the component is connected to the pin of that wire.

kicad steps

This is how the pcb for this board turned out in the end!

scematic

Final Mcb

This is the schematic of my final board.

kicad

schematics

In the end i ended up changing the boards schematics but i couldnt find a image of the schemtic in kicad. So hetes my system diagram that shows te connection

sd

One ultrasonic sensor: - TRIG → pin 21 (D6) - ECHO → pin 6 (D4)

Second ultrasonic sensor: - TRIG → same pin 21 (D6) - ECHO → pin 7 (D5)

Both sensors: - VCC → 5V - GND → GND

Ultrasonic tryout 1

Clarification on Using a Single Trigger Pin

The code uses a single trigger pin for both ultrasonic sensors. Each sensor has a separate echo pin, allowing the Arduino to measure each distance individually.

Important Note: Using pulseIn() for two sensors with a shared trigger pin can cause slight inaccuracies. pulseIn() waits for a HIGH pulse and might read the second sensor after the first echo ends, meaning distance2 could be slightly offset. In practice, for short distances and my setup, this effect was small enough that both sensors gave usable readings.

In the final project, the two sensors are mainly used for:

Sensor 1: Detect objects in front for obstacle avoidance.

Sensor 2: Detect hand presence for the petting interaction.

This was the code for the ultrasonic only, without the motor’s movement being included. For my project, i had two ultrasonics needed, one for movement detection and one for a petting function(You can put your hand over the bot and it should react.) Because im using two ultrasonics, and because i was almost at a lost of pins in my mcb, i tryed to make the two ultrasonics share the trigger pin and have seperate echo pins. This was the code i used for the test and it ended up working.

Even so, because of some wiring issues with my project in the end, i ended up taking pins for my ultrasonic seperatly. I was able to link another component(matrix display) for my project instead, so it left me with some free pins i could use on the ultrasonic.

How it works

This uses one trigger pin to send ultrasonic pulses, while it listens with two echo pins (two sensors)and then calculates and prints the distance to obstacles in front of each sensor every 0.5 seconds.

I got this code from chatgpt

#define trigPin 21      // Trigger pin for both sensors (D6)
#define echoPin 6       // Echo pin for Sensor 1 (D4)
#define echoPin2 7      // Echo pin for Sensor 2 (D5)

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(echoPin2, INPUT);

  Serial.begin(115200);
}

void loop() {
  long duration1, duration2;
  float distance1, distance2;

  // Send a pulse to trigger both sensors
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read echo pins individually
  duration1 = pulseIn(echoPin, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);

  // Convert to distance in cm
  distance1 = duration1 * 0.034 / 2;
  distance2 = duration2 * 0.034 / 2;

  // Print distances
  Serial.print("Distance1: "); Serial.print(distance1); Serial.print(" cm, ");
  Serial.print("Distance2: "); Serial.println(distance2); Serial.print(" cm");

  delay(500);
}

The provided code is sufficient to detect the presence of objects using two ultrasonic sensors. However, the distance measurements are not highly accurate. This is due to both sensors sharing a single trigger pin and the lack of advanced timing control, filtering, or calibration. As a result, the system is suitable for object detection rather than precise distance measurement.

Code explained

This program uses an Arduino to measure distance with two ultrasonic sensors that share the same trigger pin but have separate echo pins. The trigger pin sends a short electrical pulse that makes both sensors emit an ultrasonic sound burst. Each sensor then listens for the returning echo and reports how long it took on its own echo pin. The Arduino measures this time, converts it into distance using the speed of sound, and prints both distances to the serial monitor every 0.5 seconds.

This way, you can read distances from two different directions at once.

Ultrasonic tryout 2

This is the code that links the ultrasonic to motors for movement

Basicly, when there is a object around 10cm in front of the ultrasonic, it gets detected and the motor will trun backwards for a while, delay and start to turn (one moter going forward and one backward) in a random direction. This way, my bot is able to avoide obsticals.

I got this code from chatgpt

// Motor control pins
#define motor1Pin1 2   // IN1 connected to pin 9
#define motor1Pin2 3  // IN2 connected to pin 20
#define motor2Pin1 4  // IN3 connected to pin 21
#define motor2Pin2 5   // IN4 connected to pin 6

// Ultrasonic sensor pins
#define trigPin 21     // Trigger pin connected to D3 (GPIO 0)
#define echoPin 6      // Echo pin connected to D2 (GPIO 2)

void setup() {
  // Initialize motor control pins as outputs
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);

  // Initialize ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Start serial communication for debugging
  Serial.begin(115200);
}
void loop() {
  // Measure distance using ultrasonic sensor
  long duration, distance;

  // Send a pulse to trigger the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo pin and calculate the duration
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance (in cm)
  distance = (duration / 2) / 29.1;

  // Print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Control the motor behavior based on the distance
  if (distance < 10) {
    moveBackward();  // Move backward if an object is very close
  }
  else if (distance >= 10 && distance <= 20) {
    stopMotors();    // Stop if the object is in range
  }
  else {
    moveForward();   // Move forward if the object is far enough
  }

  delay(100);  // Short delay before the next loop iteration
}
// Function to move motor 1 forward and motor 2 backward
void moveForward() {
  digitalWrite(motor1Pin1, HIGH);  
  digitalWrite(motor1Pin2, LOW);  
  digitalWrite(motor2Pin1, HIGH);  
  digitalWrite(motor2Pin2, LOW);  
}
// Function to move motor 1 backward and motor 2 forward
void moveBackward() {
  digitalWrite(motor1Pin1, LOW);  
  digitalWrite(motor1Pin2, HIGH);  
  digitalWrite(motor2Pin1, LOW);  
  digitalWrite(motor2Pin2, HIGH);  
}
// Function to stop both motors
void stopMotors() {
  digitalWrite(motor1Pin1, LOW);  
  digitalWrite(motor1Pin2, LOW);  
  digitalWrite(motor2Pin1, LOW);  
  digitalWrite(motor2Pin2, LOW);  
}

Shows both sensors measuring distances simultaneously.

Serial monitor displays distance1 and distance2 values in real time.

Code explaination

This code makes a simple obstacle-avoiding robot by combining motor control with an ultrasonic distance sensor. The ultrasonic sensor works by sending a short pulse through the trigger pin and timing how long it takes for the echo pin to receive the reflection, then converting that duration into a distance in centimeters. Based on this distance, the robot decides how to move: if an object is very close (under 10 cm), it moves backward; if the object is at a medium range (10–20 cm, it stops) and if the path is clear (more than 20 cm), it moves forward. The motors are controlled through digital pins that send HIGH/LOW signals to an H-bridge driver, which sets the rotation direction of each motor, allowing the robot to move forward, backward, or stop depending on the sensor reading.

Heres a demo of how it looks on the final project.(whth the additional functions put in later)

Sensor 1 detects obstacles in front. If an object is closer than 10 cm, the bot moves backward, then randomly turns.

Sensor 2 detects hand presence for interaction with the bot.

The robot avoids obstacles autonomously while responding to the user’s hand gestures.

Design files

mcb

kicad

Thank you


Last update: January 3, 2026