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
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 assignmenthere
Reflection
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
Now, for the coding part of the process,I’ll be using arduino IDE
Arduino¶
Downloading the library¶
Go to tools- boards- boards manage
Afterwards, you should access this.When you do, download the board you need.
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.
Example code¶
When you want to access an example code, these are the steps you follow.File-examples-the code you want.
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
This was the first scematic I made. I didn’t really end up useing this in the end for the assignment.
note: When making scematics, the dot on line interconnections show that the component is connected to the pin of that wire.
This is how the pcb for this board turned out in the end!
Final Mcb
This is the schematic of my final board.
Ultrasonic tryout 1¶
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 // Connect to Trig pin of the sensor
#define echoPin 6 // Connect to Echo pin of the sensor
#define echoPin2 7 // Connect to Echo pin of the sensor
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(echoPin2, INPUT);
Serial.begin(115200); // Start serial communication
}
void loop() {
long duration;
float distance;
long duration2;
float distance2;
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read echo response duration
duration = pulseIn(echoPin, HIGH);
duration2 = pulseIn(echoPin2, HIGH);
// Convert duration to distance in cm
distance = duration * 0.034 / 2;
distance2 = duration2 * 0.034 / 2;
// Print distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm");
Serial.print(" ");
Serial.print(distance2);
Serial.println(" cm");
delay(500); // Wait half a second before next reading
}
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);
}
<
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)