Question

In: Electrical Engineering

I need an Arduino uno code to measure high low levels with hcsr04 of a tank...

I need an Arduino uno code to measure high low levels with hcsr04 of a tank and display it on i2c 20x4 lcd.

Solutions

Expert Solution

THE Hcsr04 is a ultrasonic sensor its emits an ultra sound of 40000Hz which travels through air and if their is an object or obstacle it will bounce back to the module, by considering the time travel you can calculate the distance.

The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs 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.

In order to generate the ultrasound you need to set the Trigger pin on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

first define the echo pin and trigger pins here they are number 9 and 10 on the arduino board and they are named trig pin andecho pin,then we need a long variable termed duration which you will get from the sensor and integer variable for the distance.

In the setup first define trigpin as output and echo pin as input, start serial communications to show the result on 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. In order to  distance in cm we need to multiply the received travel time value from the echo pin by the speed of sound that is 340m/s and divide it by 2. At the end we will print the value of the distance on the lcd i2c 20x4

/*
* Ultrasonic Sensor HC-SR04 and Arduino board
*
*/

#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 9;
const int echoPin = 10;

long duration;
int distanceCm, distanceInch;

void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(10);
}

Related Solutions

We need to code an LCD with a temperature and humidity sensor for the arduino UNO...
We need to code an LCD with a temperature and humidity sensor for the arduino UNO board. The LCD should simply display the temperature and humidity reading from the DHT11 sensor. We must do this without the use of arduino or lcd libraries, and only use raw c code. For example we need to use PORTD |= (1<<5); to set the pin to 5. The LCD is connected to pins A0 and A1, and it is using I2C. The temperature...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i need someone explain the steps for me #include <stdlib.h> #include <DHT.h> #define DHTPIN 5         // DHT data pin connected to Arduino pin 5 #define DHTTYPE DHT11     // DHT11 (DHT Sensor Type ) DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor #define SSID "WiFi Name"     // "WiFi Name" #define PASS "WiFi Password"       // "Password" #define IP "184.106.153.149"// thingspeak.com ip String msg = "GET /update?key=Your API...
I need simulation on protues of speed control of unipolar stepper motor using uno arduino. Must...
I need simulation on protues of speed control of unipolar stepper motor using uno arduino. Must using protues..
need a code for mpu 6050 gyro module for arduino it needs to read the angle...
need a code for mpu 6050 gyro module for arduino it needs to read the angle of the pendulum and feed information back into stepper motor to try to attain balance
EXPALIN WITH EXAMPLES IF LOW LEVELS OF LIVING CAN EXIST SIMULTANEOUSLY WITH HIGH LEVELS OF PER...
EXPALIN WITH EXAMPLES IF LOW LEVELS OF LIVING CAN EXIST SIMULTANEOUSLY WITH HIGH LEVELS OF PER CAPITA INCOME
Linux: I have a working script but need a high/low comparison added to each guess by...
Linux: I have a working script but need a high/low comparison added to each guess by the user. choice=$(( $RANDOM % 100 + 1 )) read -p 'Please choose a number from 1-100:' variable while [ $variable -ne $choice ]; do echo "Your choice was wrong. Please try again." read -p 'Please choose a number from 1-100:' variable done echo "You chose correctly!!!" The comparison is the variable chosen by the game is supposed to be guessed by the user....
I have a sim 808 (fona 808) breakout connected to an adruino uno. write a code...
I have a sim 808 (fona 808) breakout connected to an adruino uno. write a code that makes it pick up automatically when called from certain numbers.
I need answer for b) High blood cholesterol levels increase the risk of heart disease. Young...
I need answer for b) High blood cholesterol levels increase the risk of heart disease. Young women are generally less afflicted with high cholesterol than other groups. The cholesterol levels for women aged 20 to 34 years follow an approximately normal distribution with mean 185 milligrams per deciliter (mg/dl) and standard deviation 39 mg/dl. a) Cholesterol levels above 240 mg/dl demand medical attention. What percent of young women have levels above 240 mg/dl? b) Among a random sample of 150...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT