Skip to content

Week 11. Input Device


Image description

Image Courtesy: Photo by Jorge Ramirez on Unsplash

Input devices are essential components of any computer system as they allow users to communicate and interact with computers and other electronic devices. This week, we'll look at various input devices, their uses, and their functions. In this assignment, we will have a look at the different kinds of input devices available. We'll talk about how these devices function and how they might be used in different situations.

By the end of this assignment, we will have a better understanding of the role of input devices in computing and how to choose the best device for your needs. This time i chose ultrasonic sensor with ATTiny 3216 chip.

Assignment Tasks:

  1. Group Assignment: Probe an input device’s analog levels and digital signals.

  2. Individual Assignment: Measure something: add a sensor to a microcontroller board (which was designed earlier/new one) and read it.

Learning Process

Ultrasonic Sensor

An ultrasonic sensor is a type of sensor that uses sound waves with frequencies above the range of human hearing to detect the distance of objects.

The sensor emits a high-frequency sound wave and measures the time it takes for the wave to bounce off an object and return to the sensor. Based on the time it takes for the sound wave to travel to the object and back, the sensor can calculate the distance between the sensor and the object.

Ultrasonic sensors are commonly used in a wide range of applications, such as robotics, automation, and automotive systems. They are often used to detect the presence or absence of objects, measure distances, or detect movement.

One advantage of ultrasonic sensors is their ability to detect objects at a distance without the need for direct contact or line-of-sight. They can also operate in a wide range of environments and can be relatively inexpensive compared to other types of sensors.

However, ultrasonic sensors can be affected by factors such as temperature, humidity, and wind, which can affect the accuracy of distance measurements. They may also be susceptible to interference from other ultrasonic sources or noise in the environment.

Principle of HC-SR04

The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object.

An ultrasonic sensor generates high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver as shown in below figure.

01

By measuring the time required for the echo to reach to the receiver, we can calculate the distance. This is the basic working principle of Ultrasonic module to measure distance.

HC-SR-04 has an ultrasonic transmitter, receiver and control circuit.

02

n the ultrasonic module HCSR04, we have to give trigger pulse, so that it will generate ultrasound of frequency 40 kHz. After generating ultrasound i.e. 8 pulses of 40 kHz, it makes echo pin high. Echo pin remains high until it does not get the echo sound back. So the width of echo pin will be the time for sound to travel to the object and return back. Once we get the time we can calculate distance, as we know the speed of sound.

The calculation as follows,

Distance = Speed x Time

With the speed of sound waves, 343 m/s, we can calculate the distance.

Total Distance = (343 x Time)/2

Total distance is divided by 2 because signal travels from sensor to object and returns to the sensor.

Pinout of HC-SR-04

03

VCC: +5 V supply

TRIG: Trigger input of sensor. Microcontroller applies 10 us trigger pulse to the HC-SR04 ultrasonic module.

ECHO: Echo output of sensor. Microcontroller reads/monitors this pin to detect the obstacle or to find the distance.

GND: Ground

Board Design

I chose the ATTiny 3216 chip for my project. The plan was intended to operate this device as a radar by coupling a servo motor with an HC-SR04 sensor. This chip was chosen in order to test BH1750, another sensor for light intensity sensing as well.

The schematic was as shown below.

101

Download Schematic here

The PCB design:

102

Download Schematic here

Bill Of Materials:

a. PCB Board                        -   50mm x 50mm (Minimum Size as per design)

b. ATTiny 3216 microcontroller      -   1 no.

c. Capacitor - 100nf                -   1 no.

d. UPDI - 2x3 pin                   -   1 no.

e. Resistor R499                    -   1 no.

f. Resistor R0                      -   1 no.

g. LED                              -   1 no.

h. Pinhead 1x6                      -   1 no.

i. Pinhead 1x4                      -   3 nos

Board Production

I milled my board in accordance with my design using mods and a Roland PCB Milling Machine, much like in the electronics production week. I also soldered each component after that. And below you can see my board.

103

Programming

First step was to burn the bootloader to the chip. I used UPDI programmer to burn the bootloader.

After that it time to program and upload the same to our board. I used Adrian’s program for my assignment and added software serial since i made a mistake while connecting the pins on the design stage. I used FTDI for serial communication, which was made by one of our previous year fellow student Mr. Saheen Paliyil.

Arduino Code
#include <SoftwareSerial.h>

SoftwareSerial mySerial(PIN_PC1, PIN_PC2); // RX, TX

const int EchoPin = 15;
const int TriggerPin = 14;

void setup() {
  mySerial.begin(115200);
  pinMode(TriggerPin, OUTPUT);
  pinMode(EchoPin, INPUT);
}

void loop() {
  int cm = ping(TriggerPin, EchoPin);
  mySerial.print("");
  mySerial.println(cm);
  delay(500);
}

int ping(int TriggerPin, int EchoPin) {
  long duration, distanceCm;

  digitalWrite(TriggerPin, LOW);  //to generate a clean pulse we set LOW 4us
  delayMicroseconds(4);
  digitalWrite(TriggerPin, HIGH);  //we generate Trigger (trigger) of 10us
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);

  duration = pulseIn(EchoPin, HIGH);  //we measure the time between pulses, in microseconds

  distanceCm = duration * 10 / 292/ 2;   //we convert distance, in cm
  return distanceCm;
}

And then, I used Processing for getting a result. the code used was as shown below:

Processing Code
import processing.serial.*;

float sensorValue; //variable for the serial data
Serial myPort;

void setup() {  //as dynamic/setup function called initially, only once
  size(1024, 200);// is the window (1024=sensor max. value)
  //replace the port String with the port where your Arduino is connected
  //myPort = new Serial(this, "/dev/tty.wchusbserial1450", 115200);
  myPort = new Serial(this, "/dev/cu.usbserial-A603X58B", 115200); // serial port
  background(255);  //set background white

}

void draw() {  //draw function loops 

  noStroke(); // outline
  fill(255,0,0,20); // color inside
  rect(0, 0, sensorValue, height); //position and size

  fill(255,70);
  rect(sensorValue, 0, width-sensorValue, height);

  println(sensorValue);
  fill(0,0,0);// these are the colors inside
  text(sensorValue + " " + "cm" , sensorValue, height/2);
  textSize(32);

}

void serialEvent(Serial myPort) { // sketch read the serial data
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    float[] values = float(split(inString, ","));
    if (values.length >=1) {
      sensorValue = values[0]; //first value in the list
    }
  }
}

And the final Result:



Group Assignment

Detailed Study Report on our Group Assignment Page.



Help Taken & Other References

Chat GPT used for doubt clearing and content helps.

Ultrasonic Module HC-SR04

Back to top