Skip to content

12. Intput devices

WEEK 12

This week’s assignments are:

  • To measure something using an input sensor to a microcontroller board that I designed and read it
  • To probe an input device’s analog levels and digital signals(Group Assignment added HERE)

Ultrasonic Sensor

Using the board I made the previous week I wanted to connect an ultrasonic sensor to get the reading in centimeters in the serial monitor. I also thought of writing a code for the light to blink when the ultrasonic sensor detects an object and also to blink faster when the object moves closer.

So I started by using an Ultrasonic Sensor HC-SR04 and connected the pins to an Arduino Mega 2560 board to run a sample code…

// ---------------------------------------------------------------- //
// Arduino Ultrasoninc Sensor HC-SR04
// Re-writed by Arbi Abdul Jabbaar
// Using Arduino IDE 1.8.7
// Using HC-SR04 Module
// Tested on 17 September 2019
// ---------------------------------------------------------------- //

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  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; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

Also got the readings in the serial monitor in Arduino IDE…

Since the Arduino Mega board was a success I tried with the programmer I made in the previous weeks using the same program but by changing the pins…

Then I added a code to make the LED blink depending on how close the object is placed near the ultrasonic sensor

if(distance<5){
  digitalWrite(8,HIGH);
  delay(50);
  digitalWrite(8,LOW);
  delay(50);
}
else if(distance<10){
  digitalWrite(8,HIGH);
  delay(100);
  digitalWrite(8,LOW);
  delay(100);
}
else if(distance<15){
  digitalWrite(8,HIGH);
  delay(200);
  digitalWrite(8,LOW);
  delay(200);
}
else if(distance<20){
  digitalWrite(8,HIGH);
  delay(400);
  digitalWrite(8,LOW);
  delay(400);
}
else{
  digitalWrite(8,LOW);
}

Then later added a speaker to have a buzzer output…

if(distance<5){
  digitalWrite(8,HIGH);
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(50);
  digitalWrite(8,LOW);
  noTone(buzzer);     // Stop sound...
  delay(50);
}
else if(distance<10){
  digitalWrite(8,HIGH);
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(100);
  digitalWrite(8,LOW);
  noTone(buzzer);     // Stop sound...
  delay(100);
}
else if(distance<15){
  digitalWrite(8,HIGH);
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(200);
  digitalWrite(8,LOW);
  noTone(buzzer);     // Stop sound...
  delay(200);
}
else if(distance<20){
  digitalWrite(8,HIGH);
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(400);
  digitalWrite(8,LOW);
  noTone(buzzer);     // Stop sound...
  delay(400);
}
else{
  digitalWrite(8,LOW);
}

Last update: June 28, 2022