Arduino Tutorial 58: How to Build a Portable Distance Sensor with the HC-SR04 Ultrasonic Sensor

preview_player
Показать описание
You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this lesson we begin to explore building a portable distance sensor based on the HC-SR04 Ultrasonic Sensor. In order to do this, we must determine how to power the system, and we need some form of portable display.

You can get the kit I am using for this series at the following link:

I strongly suggest picking up an arduino nano, since it can plug directly into the breadboard, making a portable system more practical. You can pick one up here:

As projects get more complicated in these lessons, you guys really need to get a set of breadboard jumper wires which allow you to make neater connections on the board in your projects. You can pick a pack of these wires up here:

In addition as projects get more complicated, you are going to need a bigger breadboard. This is a reasonable one here:

This lesson is available at our WEB site:

#Arduino
Рекомендации по теме
Комментарии
Автор

Please make video on RC arduino module or bluetooth module for wireless communication to build a RC car.

manavt
Автор

Will be there any PCB assembly tutorial in future?

emmetray
Автор

Sir please make tutorial on wifi module with arduino.

deepakgohil
Автор

her you go, the homework assignment as requested...
I integrated a joystick to control to move characters in the LCD (MM, Inches, feet)
This thing got big over 200 lines of code
Thank you Paul, I've learned all of this from you....

#include <LiquidCrystal.h>
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
const int trigerPin=2, echoPin=3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int dt=20 ;
int longdt=100;
int dt3=100;
int dt4=1000;
float pingTravelTime;
float pingtravedistanance;
float distancetotarget;
float feet;
float mm;
String msgintro ("Arduino Distance");
String msgintro2 ("Device");
String msgintro3 ("Uses Ultra-Sound");
String msgintro4 ("to Measure");
String msgintro5 ("Distance");
String msgmeasure ("SELECT");
String msgmeasuremm ("MM");
String msgmeasureinch ("INCHES");
String msgmeasurefeet ("FEET");
String msgclear(" ");
String msgclear2(" ");
String created ("created by");
String Frank("Frank Victorine ");
int analogread;
int joysticky=A1;
int readjoyy;
int joystickbutton=A3;
int readjoyb=1;

void setup() {
Serial.begin(9600);
pinMode(trigerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(joysticky, INPUT);
digitalWrite(joystickbutton, HIGH);
pinMode(joystickbutton, INPUT);
lcd.begin(16, 2);
msgfirst ();
}

void loop() {
measure();
diagonstic();
}

void LCD (){

}
void HCSR04 (){ //loop for HSR04
digitalWrite(trigerPin, LOW);
delayMicroseconds(dt);
digitalWrite(trigerPin, HIGH);
delayMicroseconds(dt);
digitalWrite(trigerPin, LOW);
digitalWrite(echoPin, HIGH);
pingTravelTime=pulseIn(echoPin, HIGH);


feet=distancetotarget/12;
mm=distancetotarget*25.4;
//delay(longdt);

//delay (dt3);
//Serial.print(feet);

//Serial.println(mm);
}

void msgfirst (){
delay (dt4);
lcd.print(msgintro);
delay (dt4);
lcd.setCursor(5, 2);
lcd.print(msgintro2);
delay (dt4);
delay (dt4);
lcd.clear();
lcd.print(msgintro3);
lcd.setCursor(3, 2);
lcd.print(msgintro4);
delay (2000);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(msgintro5);

delay (2000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print(created);
lcd.setCursor(0, 2);
lcd.print(Frank);
delay (3000);

lcd.clear();
lcd.setCursor(5, 0);
lcd.print(msgmeasure);
delay (3000);

}

void measure(){
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(msgmeasuremm);
lcd.setCursor(4, 0);
lcd.print(msgmeasureinch);
lcd.setCursor(12, 0);
lcd.print(msgmeasurefeet);

if (readjoyy==517 || readjoyy==518 ){
delay(100);
lcd.setCursor(4, 0);
lcd.print(msgclear);
delay(500);
lcd.setCursor(4, 0);
lcd.print(msgmeasureinch);
}

if (readjoyy==0 || readjoyy==1 ){
delay(100);
lcd.setCursor(0, 0);
lcd.print(msgclear2);
delay(500);
lcd.setCursor(0, 0);
lcd.print(msgmeasuremm);
}

if (readjoyy==1022 || readjoyy==1023 ){
delay(100);
lcd.setCursor(12, 0);
lcd.print(msgclear);
delay(500);
lcd.setCursor(12, 0);
lcd.print(msgmeasurefeet);
}

if (readjoyy==1023 && readjoyb==0 ){
HCSR04();
lcd.clear();
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Distance in FT");
lcd.setCursor(0, 2);
lcd.print(feet);
delay(2000);
lcd.setCursor(0, 2);
lcd.print(feet);
delay(2000);
lcd.setCursor(0, 2);
lcd.print(feet);
delay(8000);
}

if (readjoyy==518 && readjoyb==0 ){
HCSR04();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance INCHES");
lcd.setCursor(0, 2);
lcd.print(distancetotarget);
delay(2000);
lcd.setCursor(0, 2);
lcd.print(distancetotarget);
lcd.setCursor(0, 2);
delay(2000);
lcd.print(distancetotarget);
delay(8000);
}

if (readjoyy==0 && readjoyb==0 ){
HCSR04();
lcd.clear();
delay(100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Distance in MM");
lcd.setCursor(0, 2);
lcd.print(mm);
delay(2000);
lcd.setCursor(0, 2);
lcd.print(mm);
delay(2000);
lcd.setCursor(0, 2);
lcd.print(mm);
delay(8000);
}
}
void diagonstic(){
Serial.println("");

Serial.print("joy stick Y axis: ");
Serial.print( readjoyy);
Serial.println("");

Serial.print("joy stick button: ");
Serial.print(readjoyb);
Serial.println("");
Serial.print("Distance in inches: ");

Serial.print("Distance in MM: ");
Serial.println(mm);
Serial.print("Distance in ft: ");
Serial.print(feet);
Serial.println("");
delay(500);
}

Victorine
Автор

another great video. and I'd like to say thank you for the skills that I've developed with this series of videos. Please please keep them coming.

daveharkin
Автор

Thank you, Paul! I have learned so much from you and I'm excited to keep going. I am a teacher and I have been working from home for about two months now. In that time I have learned so much from your videos and I plan on creating an Arduino club once life returns to normal for my students.

KisorcererAMP
Автор

I did it Paul. You are so right. If you want to be a creator of tech, you have to go where no one else has. This means there's no cut and paste to guide you. I've built some complex things that required writing code from scratch and having to do a lot of math to ensure my components would work together. I was fortunate to have gone back to school to study IoT design and prototyping. I finished the program at the age of 60. As I said before, I've gotten more out of your classes than I got at the University. This really is the best way to learn this subject, as long as you don't simply copy things.

hughpatterson
Автор

I went ahead and did the portable build before we took the diversion into the serial plotter. I was excited to receive my new breadboard and jumper wires and that seemed like the best way I could think of to put them to use. The process of putting it together was an amazing experience. You have to put a lot of faith into process. What I would do differently (without having watched your build yet) would be to test each subsystem as I put it in. The complexity of the whole build once you get something even as simple as an LED, one sensor, a Nano, and the power supply module all on the same board gets pretty dizzying. I was lucky that it mostly worked out of the box but I remember thinking, "Man, if something was wrong, this would be a nightmare to debug." Hard to believe that I'm closing in on the finish to this series. . .

RobVollmar
Автор

how bout connecting another same size breadboard on the existing one it can connect

prashantgunjal
Автор

Hi Paul, I had already done this homework at lesson 54, I swapped over to I2C LCD driver and a Nano also used the kit supplied PSU board which has an input for a PP3 battery connector. Contrast is also on the I2C board as is the backlight, this reduces the number of connections and removes the pot from the board. you underestimate how much info you impart in your videos. Cheers

leebasham
Автор

Hello Sir, I did tho project, had a lot of fun doing it.
It is awesome, I am really surprised for the progress I made.
I started the tutorial series two weeks ago and here I am up to date.
Thank you so much for your precious lessons!


I am also learning Fusion 360, from your other series.

ioni.
Автор

Sir I think that you should wear your shirt that you wear in your AI on Jetson nano series. You look really smart in that shirt.

tanishbhongade
Автор

Yipee. I am finally caught up with the series. Its been exactly two weeks since I got my elegoo kit. I actually did this homework before it was even set lol back in tutorial 55. Can't wait for the new videos and to learn a whole lot more. Great job Paul, stay safe in these troubling times.

cubc
Автор

pls make a video about nrf24L01 module tutorial.

VinhPham-tdwm
Автор

Homework done, working. Power by 9V battery via Vin. Still to get my fancy power supply like you have, but this works for now.

steveholt
Автор

Oh yeah, now we are on it. This will be enjoyable. See you in lesson 59. Have a great week.

opalprestonshirley
Автор

I got it working. I used the breadboard power supply and the battery/9v power supply. (because I am using the most complete starter kit mega2560 project)

tobygibbes
Автор

Got it working, but had to go back and look at earlier lesson to get the include header for the lcd and to create the lcd object (I forgot). Also forgot to do the lcd.begin(16, 2).

joerio
Автор

HELP. Not quite here yet. Paul can you give some of us some advice. Not able to get libraries that we have downloaded following your techniques to run on the Arduino. Arduino gives an Error whether we use "" or <> for the file name.

lawrencex
Автор

I did the homework!! And it feels great, thank you, Paul.

fatoumatabintadiallo