Input Devices

Goal of the assignment

An input device is any hardware device that sends data to the microcontroller, allowing you to interact with and control the board. The input devices such as keyboard, mouse, sensors which send data to the microcontroller.

For this assignment I am going to use HC-SR04 which is ultrasonic sensor as input device. I am going to use my final board that I have made which is explained in previous assignment in Output devices.
Below are the images of the final board

  • PNG image of the board file which will be given to the moldela for the PCB fabrication.

FinalPCB

  • Image of the board after milled.

FinalPCB

Ultrasonic sensor HC - SR04.

Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit. The basic principle of work: (1) Using IO trigger for at least 10us high level signal, (2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back. (3) IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning. Test distance = (high level time × velocity of sound (340M/S) / 2,

FinalPCB

Timing Diagram:

FinalPCB

Connection Diagram:

First you have to define the Trig and Echo pins. In this case they are the pins number 9 and 10 on the Board and they are named trigPin and echoPin. Then you need a Long variable, named “duration” for the travel time that you will get from the sensor and an integer variable for the distance.

In the setup you have to define the trigPin as an output and the echoPin as an Input and also start the serial communication for showing the results on the serial monitor.

In the loop first you have to make sure that the trigPin is clear so you have to set that pin on a LOW State for just 2 µs. Now for generating the Ultra sound wave we have to set the trigPin on HIGH State for 10 µs. Using the pulseIn() function you have to read the travel time and put that value into the variable “duration”. This function has 2 parameters, the first one is the name of the echo pin and for the second one you can write either HIGH or LOW. In this case, HIGH means that the pulsIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the timing. At the end the function will return the length of the pulse in microseconds. For getting the distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation previously. At the end we will print the value of the distance on the Serial Monitor.



FinalPCB

FinalPCB

Arduino code and output:
FinalPCB

Below is the working video of ultrasonic HC- SR04 Sensor:






Switch as a input device

I am using the hello world circiuts from the assigment6 which is in ELectronics Design.

The simple form of the input device is button, which can be simply read by the MCUs. The following video is showing the use of simple push button as a input device, the code is written in a way to turn ON the led and turn OFF the led when the button is pressed.


What does the code do?
We set the pinButton variable as integer 8 and we connect the button at pin 8 on the Board. Then the LED is connected to pin 2 using the resistor in series with it.

Related Products: Switch Slide | Switch Snap Action | Switch Tactile

In the setup() function we set the pin 8 as INPUT and pin 2 as OUTPUT. In the loop() function we read the value of the pin 8 and store it in the variable stateButton. Using a if() function the Arduino makes some decisions: if the button is pressed (stateButton == 1) then give voltage to pin 2 (HIGH), else, if stateButton is not 1 (not pressed) do not output voltage on pin 2.

DOWNLOAD DESIGN FILES