Skip to content

11. Input Devices

Learning outcomes

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

https://fabacademy.org/2021/labs/vancouver/students/terrence-carew/assignments/week11g.html

This week I worked on designing a microcontroller board to connect and interface with the output and input devices i intend to use for my final project.

The design of the microcontroller board and schematic is shown below:







what is an input device

An input device responds to changes in the environment and produces a suitable electrical signal for processing in an electronic circuit.In all input devices, other forms of energy are transformed into electrical energy.



1) VCC (Power): This pin powers the sensor, typically with +5V from the microcontroller board.

2) Trig (Trigger): This pin is used to initiate the ultrasonic pulse. A short pulse of 10 microseconds sent to this pin starts the ultrasonic burst.

3) Echo (Echo Pulse): This pin outputs a pulse from the sensor. The duration of this pulse is proportional to the time taken for the ultrasonic wave to travel to the object and back to the sensor.

4) GND (Ground): This pin is connected to the ground of the circuit.

Firstly i connected the HC-SR04 ultrasonic sensor to a microcontroller board i made to test the device and make sure it is in good working order.

The connections are as follows :

  • VCC connects to the 5V output
  • Trig connects to pin7
  • Echo connects to pin5
  • GND connects to the ground pin







I obtained a sample program from the following website.

https://how2electronics.com/how-to-use-hc-sr04-ultrasonic-distance-sensor-with-arduino/

I made the following changes to the code listed below:

  • The echo pin was changed to 3 and the trigger pin was changed to 7.

Code HC-SR04

/*
  Read data from an ultrasonic sensor connected to the Arduino board
  and print the distance measurement in centimeters
  through the serial communication.

  Board: Arduino Uno R4 (or R3)
  Component: Ultrasonic distance Sensor(HC-SR04)
*/

// Define the pin numbers for the ultrasonic sensor
const int echoPin = 3;
const int trigPin = 2;

void setup() {
  Serial.begin(9600);                    // Start serial communication with a baud rate of 9600
  pinMode(echoPin, INPUT);               // Set echo pin as input
  pinMode(trigPin, OUTPUT);              // Set trig pin as output
  Serial.println("Ultrasonic sensor:");  // Print a message indicating the ultrasonic sensor is ready
}

void loop() {
  float distance = readDistance();       // Call the function to read the sensor data and get the distance

  if (distance > 400) {                  // Check if the distance is greater than 400 cm
    Serial.println("Out of Range");      // Print "Out of Range"
  } else {
    Serial.print(distance);              // Print the distance value
    Serial.println(" cm");               // Print " cm" to indicate the unit of measurement
  }

  delay(400);                            // Delay for 400 milliseconds before repeating the loop
}

// Function to read the sensor data and calculate the distance
float readDistance() {
  digitalWrite(trigPin, LOW);            // Set trig pin to low to ensure a clean pulse
  delayMicroseconds(2);                  // Delay for 2 microseconds
  digitalWrite(trigPin, HIGH);           // Send a 10 microsecond pulse by setting trig pin to high
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);            // Set trig pin back to low

  // Measure the pulse width of the echo pin and calculate the distance value
  float pulse_duration = pulseIn(echoPin, HIGH); // Read pulse duration
  float distance = pulse_duration / 58.00;       // Calculate the distance

  return distance;                               // Return the calculated distance
}

I uploaded the code to my microcontroller board and the results was observed on the serial monitor as shown below



Video

From Youtube

Original Design files and source code

1614 Board Design

Ultrasonic Sensor Code

References

https://www.bbc.co.uk/bitesize/guides/zk37hyc/revision/7

https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/


Last update: December 7, 2023