Many people have created Instructables on how to use the Arduino Uno with an Ultrasonic Sensor and, sometimes, also with an LCD screen. I have always found, however, that these other instructables often skip over steps that are not obvious to beginners. As a result, I have attempted to create a tutorial that includes every possible detail so that other beginners can, hopefully, learn from it.
I first used an Arduino UNO but found it was a little large for the purpose. I then examined the Arduino Nano. This small board offers almost everything that the UNO does, but with a much smaller footprint. With some maneuvering, I got it to fit onto the same breadboard as the LCD, the Ultrasonic Sensor and the various wires, resistors and the potentiometer.
The resultant build is completely functional and is a good stepping stone to making a more permanent setup. I decided to make my first Instructable to document this process and, hopefully, help out others who want to do the same thing. Wherever possible, I have indicated where I have gotten my information from and I have also tried to put as much supporting documentation into the sketch as possible to allow anyone who reads it to understand what is going on.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Parts You Will Need
There are only a handful of parts that you need and, fortunately, they are very inexpensive.
1 - Full size Breadboard (830 pins)
1 - Arduino Nano (with pin headers installed on both sides)
1 - HC-SRO4 Ultrasonic Sensor
1 - 16x2 LCD display (with a single header installed). NOTE: you do not need the more expensive I2C version of this module. We can work directly with the 16 pin "basic" unit
1 - 10 K Potentiometer
1 - Ballast Resistor for using with the LED backlight for the 16x2 (normally 100 Ohm- 220 Ohm, I found a 48 Ohm resistor worked best for me)
1 -1K Ohm Load Limiting Resistor - for use with HC-SR04
Breadboard wires in various lengths and colours.
OPTIONAL - Breadboard power supply - A power module that connects directly to the breadboard allowing you to be more portable instead of staying tethered to a PC, or powering the system via the Arduino Nano.
1 - PC/ Laptop to program your Arduino Nano - Note You may also need CH340 drivers to allow your Windows PC to connect correctly to the Arduino Nano. Download drivers HERE
1 - Arduino Integrated Development Environment (IDE) - Download IDE HERE
Step 2: Install the IDE Then the CH340 Drivers
If you don't already have the IDE or CH340 drivers installed please proceed with this step
1) Download the IDE from HERE.
2) Detailed instructions on how to install the IDE can be found on the Arduino Web site HERE
3) Download the CH340 Serial drivers from HERE .
4) Detailed instructions on how to install the drivers can be found HERE.
Your software environment is now up to date
Step 3: Placement of Components
Even a full sized breadboard has only finite space on it, and this project takes it to the limit.
1) If you are using a breadboard power supply, attach it first at the right most pins on your breadboard
2) Install the Arduino Nano, with is USB port facing to the right
3) Install the LCD display at the "top" of the breadboard (See images)
4) Install the HC-SR04 and the Potentiometer. Leave room for the wires and resistors that they will require.
5) Based on the Fritzing diagram connect all of the wires on the breadboard. Note the placement of the 2 resistors on the board as well. - I have added a Fritzing FZZ file for you to download, if you are interested.
6) If you are NOT using a Breadboard power supply make sure that you have jumpers running from the ground and +V line on the "bottom" of the board running to the matching lines on the "top" to ensure that everything gets grounded and powered.
For this configuration I attempted to keep the pins from the LCD and the pins on the Arduino in sequence to make things a simple as possible (D7-D4 on LCD connnects to D7-D4 on the Nano). This also allowed me to use a very clean diagram to show the wiring.
While many sites call for a 220 ohm resistor to protect the LCD backlight on the 2x20 display, I found this to be too high in my case. I tried several progressively smaller values until I found one that worked well for me. In this case it works out to a 48 ohm resistor (that is what it shows up as on my ohm-meter). You should start with a 220 Ohm and only work down if the LCD is not bright enough.
The potentiometer is used to adjust the contrast on the LCD Display, so you may need to use a small screwdriver to turn the inner socket to the position that works best for you.
Step 4: The Arduino Sketch
I used several sources as inspiration for my sketch, but they all required significant modification. I have also attempted to fully comment the code so that it is clear as to why each step is executed the way that it is. I believe the comments outnumber the actually coding instructions by a fair percentage!!!
The most interesting part of this sketch, for me, revolves around the Ultrasonic Sensor. The HC-SR04 is very inexpensive (less than 1 US or Canadian $ on Ali Express). It is also quite accurate for this type of project.
There are 2 round "eyes" on the sensor but they each have a different purpose. One is the sound emitter the other is the receiver. When the TRIG pin is set to HIGH a pulse is sent out. The ECHO Pin will return a value in Milliseconds that is the total delay between when the pulse was sent and when it was received. There are some simple formula's in the script to help convert Milliseconds to either Centimeters or Inches. Remember that the time returned needs to be cut in half because the pulse goes TO the object and then RETURNS, covering the distance twice.
For more details on how the Ultrasonic Sensor works I highly recommend Dejan Nedelkovski's tutorial at Howtomechatronics. He has an excellent video and diagrams explaining the concept much better than I could!
NOTE: The speed of sound is not a constant. It varies based on temperature and pressure. A very interesting expansion to this project would add in a temperature and pressure sensor to compensate for "drift". I have given several sample for alternate temperatures as a starting point, if you want to take the next step!
An Internet source who has spent a lot of time researching these sensors came up with these values. I recommend Andreas Spiess's You Tube channel for a variety of interesting videos. I pulled these values from one of them.
// 340 M/sec is the speed of sound at 15 deg C. (0.034 CM/Sec)
// 331.5 M/sec is the speed of sound at 0 deg C (0.0331.5 CM/Sec)
// 343 M/Sec is the speed of sound at 20 deg C (0.0343 CM/Sec)
// 346 M/Sec is the speed of sound at 25 deg C (0.0346 CM/Sec)
The LCD display is a bit of a challenge, only because it requires so many pins (6!) to control it. The upside is that this basic version of the LCD is also very inexpensive. I can easily find it on Aliexpress for less than $2 Canadian.
Fortunately, once you have it hooked up, controlling it is very straight forward. You clear it, then set where you want to output your text then issue a series of LCD.PRINT commands to push the text and numbers onto the screen. I found a great tutorial on this from Vasco Ferraz at vascoferraz.com. I altered his pin layout to make it clearer to a beginner (Such as myself!).
Step 5: Conclusion
I do not pretend to be either an electrical engineer or professional Coder.( I originally learned how to do programming back in the 1970's!). Because of this, I find the entire Arduino space to be immensely liberating. I, with only basic knowledge, can start with meaningful experiments. Creating things that actually work and show enough real world utility that even my wife says "Cool!" <grin>.
As we all do, I use the resources available to me from the internet to learn how to do things, then I link them together to, hopefully, make something useful. I have done my best to credit these sources within this ible and in my sketch.
Along the way, I believe that I can help others, who are also starting their learning journey. I hope you find this to be a useful Instructable and I welcome any comments or questions you might have.