Code Blocks for Ultrasonic range finder project using Arduino in Tinkercad

CODE BLOCKS FOR ULTRASONIC RANGE FINDER PROJECT USING ARDUINO IN TINKERCAD

In this tutorial, you will see Code Blocks for Ultrasonic range finder project using Arduino in Tinkercad, before this let understand the ultrasonic sensor.

An ultrasonic sensor is an electronic component used to find the range of a targeted object by emitting ultrasonic waves (sound waves).

It has a high pitch of the sound that humans cannot hear and it sends a sound that has a frequency of about 40 KHz.This sensor mainly consists of two parts, a transducer that produces ultrasonic sound waves and another that listens for its echo.

REQUIRED HARDWARE OR COMPONENT

S.N. COMPONENTS QUANTITY
1. Arduino Uno 1
2. Ultrasonic Sensor HC SR04 1

CIRCUIT DIAGRAM

CONNECTION TABLE FOR ULTRASONIC SENSOR INTERFACING WITH ARDUINO IN TINKERCAD

S.N. ARDUINO ULTRASONIC SENSOR
1. 5 V VCC
2. 8 TRIG
3. 7 ECHO
4. GND GND

CIRCUIT EXPLANATION

In this Circuit, we are using an Ultrasonic Sensor HC SR04 with Arduino which has Four Pins that are VCC, TRIG, ECHO and GND. The VCC and GND  pins are connected to the pins 5V and GND of Arduino respectively. The Trigger (TRIG) pin of the sensor is connected to the digital output pin 8 and the echo (ECHO) pin is connected to the digital input pin 7 of Arduino respectively.

ARRANGEMENTS OF BLOCKS AND TEXT CODE FOR ULTRASONIC RANGE FINDER PROJECT IN TINKERCAD

ultrasonic Sensor Code Blocks and Text code tinkercad
Click to Enlarge

CODE BLOCKS AND TEXT CODE EXPLANATION

BLOCK EXPLANATION

Step 1: First of all click on the variables blocks then click on the create variable… then enter the new variable name as “centimeter” (or any name), To measure targeted objects in centimeters.

Create new variable Tinkercad

and then click on the OK button. After that, we get three variable command block in the variable blocks like this

Variable code blocks tinkercad

Then we will drag the set command block from it and drop it in the work area.

set command block Tinkercad

Step 2:  After that, go to the input blocks option drag “read ultrasonic distance sensor” command blocks

read ultrasonic distance sensor
Click to Enlarge

from  it and drop it into set command block like this

set command block tinkercad
Click to Enlarge

After that, change the read ultrasonic distance sensor on the the trigger pin from 0 to 8 and from the next echo pin from the trigger pin to 7, where the sensor TRIG and ECHO pin is connected to the Arduino.

set echo pin number tinkercad
Click to Enlarge

Step 3: Again go to the variables blocks and click on the create variable…  button and enter the new variable name as “inches” (or any name) to measure the distance of a targeted object in inches.

Create New variable inches

and then click on the OK button. After that we get inches command block too in the variable block, from the created variable we will drag set command block from it and drop it at the work area below the first command block

Drag inches block ultrasonic sensor tinkercad
Click to Enlarge

Now, we will go to the math block and drag the first block from it 

Math Block

and drop it at the into set inches block in place of 0

Click to Enlarge

After that, go to the variable block once again and drag the centimeter block from it and drop it into the set block at the place of elliptical shape

And then enter 2.54 to the next elliptical shape box 

convert centimeter to Inches Tinkercad
Click to Enlarge

and select the  divide sign from the dropdown menu like this

centimeter to inch divide by 2.54
Click to Enlarge

As we know, conversion of cm to inch, we have to divide cm by 2.54.

1 inch = 2.54 centimeters

1 centimeter = 1/ 2.54 inch

Step 4: After that, we will go to the Output block and drag the “print to serial monitor” command from it and drop it at the work area below the second block

Print to Serial Monitor Tinkercad
Click to Enlarge

Now, go to the variable block and drag inches variable command from it and drop it into print to serial monitor at the place of  “hello world”, and set without new line from the drop-down menu

Print to Serial Monitor without new line
Click to Enlarge

Step 5: Go to the output block and drag the “print to the serial monitor” command block from it and drop it at the work area below the third block and write “in |” (in for inches and | to separate) in place of “hello world” and by clicking to the next pop-up menu, we will again select without a newline.

Serial print Ultrasonic sensor range to inch
Click to Enlarge

Step 6: Again drag and drop “print to the serial monitor” command block from the output block and drop it at the work area below the fourth block and drag the “centimeter” variable this time and drop it into “print to serial monitor” in place of  “hello world”. Because after calculation of object distance in inches we want to calculate it in centimeters too.

Serial print Ultrasonic sensor range to inch
Click to Enlarge

Step 7: Once again, go to the output block and drag the “print to the serial monitor” command block from it and drop it at the work area below the fifth block and write “cm” (centimeter) at the place of “hello world” and by clicking to the next dropdown menu, we will select with a newline. So that after each cycle data will print in the next line

Serial print Ultrasonic sensor range to inch and cm Tinkercad
Click to Enlarge

Step 8: At the end go to the control block and drag the “wait” command block from it and drop it at the work area below the sixth command block.

This wait command block is used to pause the program for 1 second

CODE BLOCKS FOR ULTRASONIC RANGE FINDER PROJECT USING ARDUINO IN TINKERCAD
Click to Enlarge

TEXT CODE EXPLANATION 

/*
  Tutorial: https://pijaeducation.com/tinkercad/code-blocks-ultrasonic-range-finder-project-with-arduino-in-tinkercad/
*/

int centimeter = 0;
int inches = 0;

long readUltrasonicDistance(int triggerPin, int echoPin) {
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  centimeter = 0.01723 * readUltrasonicDistance(8, 7);
  inches = (centimeter / 2.54);
  Serial.print(inches);
  Serial.print(" in | ");
  Serial.print(centimeter);
  Serial.println("cm.");
  delay(1000); // Wait for 1000 millisecond(s)
}

Here,  In loop() function, Distance in centimeter is calculated using the formula,

★ As we know , speed of sound in air = 340m/sec and it corresponds to about 29.412 micro sec/cm

★ Here, we will use this formula  to measure the distance travelled by the ultrasonic sound sensor

                             Distance = (Time * speed of sound) / 2

This “2” is divided here because the sound is travelling back and forth. Back and forth means first the sound is emitted from the ultrasonic sound sensor and then it is retrieved back after targeting the object.

Distance = (100 × 340) / 1000000) / 2

= 0.017

START SIMULATION CODE BLOCKS FOR ULTRASONIC RANGE FINDER

ULTRASONIC RANGE FINDER USING ARDUINO IN OUTPUT SIMULATION TINKERCAD
Click to Enlarge

 

[pad]


NEXT POST
CODE BLOCKS FOR PIR SENSOR AND ARDUINO IN TINKERCAD


PREVIOUS POST
CODE BLOCKS FOR SWITCH AND LED INTERFACING WITH ARDUINO IN TINKERCAD