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..
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define...
Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define all timers as unsigned variables unsigned long timer1 = 0; // timer1 increments every 100ms = 0.1s const int USER_LED_OUTPUT = 13; const int USER_BUTTON_INPUT = 2; void setup() { // initialize the digital pin as an output. pinMode(USER_LED_OUTPUT, OUTPUT); pinMode(USER_BUTTON_INPUT, INPUT); Serial.begin(9600); } void loop() { // run loop forever static int state, old_state,counter; timers(); // logic for state change if(state == 0)...
write the code of 4 digit 7-segment display using arduino uno in assembly language by using...
write the code of 4 digit 7-segment display using arduino uno in assembly language by using software AVR STUDIO 5.1?
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT