Introduction

This week we have to read and use data from the input device. because of Covid19, we don’t have access to the lab and work with Arduino kits that our lab has to us.

Experience


In computing, an input device is a piece of computer hardware equipment used to provide data
and control signals to an information processing system such as a computer or information
appliance. Examples of input devices include keyboards, mouse, scanners, digital cameras,
joysticks, and microphones. In my case, I have the ultrasonic sensor which measures the distance.
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig, and Echo. The Ground and the VCC pins
of the module need to be connected to the Ground and the 5 volts pins on the Arduino Board respectively
and the trig and echo pins to any Digital I/O pin on the Arduino Board. the other sensor which is an input
device is the light sensor.


How it works. It emits ultrasound at 40 000 Hz which travels through the air and if there is an object or
obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the
sound you can calculate the distance.

screenshot


let’s start with ultrasonic. The ultrasonic can be connected right to the Arduino board but I use the
breadboard and jumpers for that to make it look more accurate. In the setup, I 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. After connecting GND, +5v to the Arduino’s Gnd and Vcc Is Connected echo
and Trig to any digital pin In my case that is 9 and 10. then start writing the code, first of all, defining
the variables and pins which are used. then writing the rest part of the code. The code was written with
the use of tutorials.
the next step which I want to do was to use the ultrasonic with the LED.
to make LED light when the distance is greater than 10sm for example for
that I need to add one small section to code which contains If else.
The if…​else allows greater control over the flow of code than the basic if statement, by allowing multiple
tests to be grouped. An else clause (if at all exists) will be executed if the condition in the if statement
results in false. The else can proceed another if test, so that multiple, mutually exclusive tests can be run
at the same time. Each test will proceed to the next one until a true test is encountered. When a true test
is found, its associated block of code is run, and the program then skips to the line following the entire
if/else construction. If no test proves to be true, the default else block is executed, if one is present,
and sets the default behavior.
Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited
number of such else if branches are allowed.

screenshot

// defines pins numbers const int trigPin = 11; const int echoPin = 10; // defines variables long duration; int distance; void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor if (distance <= 20) { digitalWrite(LED_BUILTIN, HIGH); } else { digitalWrite(LED_BUILTIN, LOW); } Serial.print("Distance: "); Serial.println(distance); }


after succeeding that task next step was to make the dc motor rotate with ultrasonic to make it
rotate depending on the ultrasonics data, next make the motor change the rotation direction.
For this task the motor shield is needed because we are going to control dc motor but that does
not make it more complicated it have all same pins. I use pins from 9 to 12 But jumper were used
only for ultrasonic because shield is directly conected to the our Arduino board.

// defines pins numbers const int trigPin = 11; const int echoPin = 10; // defines variables long duration; int distance; void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication pinMode(12, OUTPUT); //Initiates Motor Channel A pin pinMode(9, OUTPUT); //Initiates Brake Channel A pin } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor if (distance < 15) { delay(1500); digitalWrite(12, HIGH); //Establishes forward direction of Channel A digitalWrite(9, LOW); //Disengage the Brake for Channel A analogWrite(3, 255); //Spins the motor on Channel A at full speed } else if (distance > 16){ delay(1000); //Delay 4 seconds Motor rotates in clock wise direction digitalWrite(12,LOW); //Pin#7 as Low digitalWrite(9,LOW); //Pin#8 as Low delay(3000); //Delay 3 seconds Motor halts digitalWrite(9,HIGH); //Pin#8 as High digitalWrite(12,LOW); //Pin#7 as Low analogWrite(3, 255); //Spins the motor on Channel A at full speed //delay(4000); //Delay for 4 seconds Motor rotates in anti clock wise direction } Serial.print("Distance: "); Serial.println(distance);

Conclusion


This week was interesting for me. It was all new and full of experiments.
I got used to Arduino and all related to it.