Tag Archives: HC-SR04

Arduino Tutorial 55: Measuring Distance With HC-SR04 Ultrasonic Sensor

This lesson builds on the work done in Lessons 53 and 55. In this lesson we use the Ultrasonic Sensor to build a distance sensor. The HC-SR04 ultrasonic sensor can be programmed to send out a ping, and then measure the time it takes to hear the echo of the ping come back, after bouncing off a target. Knowing the speed of sound allows us to calculate distance to the sensor.

The circuit uses the following Schematic:

Schematic for Connecting the HC-SR04 to an Arduino

This is the code we develop in the video

The sensor is part of our Elegoo Kit , so if you get this kit, you will be using the same hardware we are using. This project builds on the work we did in Lesson 53.

For this build we will be using an Arduino Nano, which allows the project to be built on a single breadboard. You can use the Arduino Uno if you do not have a Nano, and things will work out the same. The build neatness is also facilitated by using small straight jumper wires, which you can get HERE.

Arduino Tutorial 54: Measuring Speed of Sound With HC-SR04 Sensor

This is our HC-SR04 Ultrasonic Sensor Connected to our Arduino Nano.

In this lesson we explore use of the HC-SR04 sensor to measure the speed of sound. The hookup and programming are pretty simple. The Elegoo Kit includes this sensor, so if you have the kit, you will be using the same hardware we are using. This project builds on the work we did in Lesson 53.

For this build we will be using an Arduino Nano, which allows the project to be built on a single breadboard. This allows cleaner build, and one less likely to have problems from intermittent  connections. The build neatness is also facilitated by using small straight jumper wires, which you can get HERE.

You can connect the sensor up according to this schematic:

Schematic for Connecting the HC-SR04 to an Arduino

The connection pins are the same when connecting to a Nano.

The video below explains how to measure speed of sound from data coming from this sensor.

Code used in Today’s Lesson:

 

Arduino Tutorial 53: Understanding and Connecting the HC-SR04 Sensor

This is our HC-SR04 Ultrasonic Sensor Connected to our Arduino Nano.

In this lesson we explore using the HC-SR04 from our Arduino Kit to measure distance. The sensor sends out a ping, and then waits to hear the echo. It measures the time between when the ping is sent and when the echo is heard. Knowing pingTravelTime, allows you to calculate distance from the sensor. In this lesson we will show you how to connect the sensor, and program it to read pingTravelTime. In future lessons we will show you how to change this into distance.

Notice we used an Arduino Nano, which allows the entire project to be build on a single breadboard. This allows a neater, smaller build, and one less likely to have problems from poor or loose connections. The build neatness is also facilitated by using small straight jumper wires, which you can get HERE.

Connecting up the HC-SR04 sensor is simple, as illustrated in this diagram:

Schematic for Connecting the HC-SR04 to an Arduino

This video will take you through our build and initial work step-by-step.

The simple code below is what we used in the video to begin making measurements with the sensor.

 

 

Python with Arduino LESSON 4: Expanding your Virtual World

In this lesson we will expand the virtual world we created in Python with Arduino LESSON 3. We will be creating a virtual world that will track a simple scene in the real world. In this project, the virtual world will track both the position and the color of a target in the real world. This lesson requires that you have the Python software and libraries installed, which we explained in LESSON 2.

This is our circuit with the HC-SR04 ultrasonic sensor and the TCS230 Color Sensor

This Lesson will be a bit more involved, and I will take you through it step-by-step. I will need to break things into two parts. In today’s lesson we will cover the Arduino side. We will develop the software that will measure distance and color, and then send those numbers over the serial port. Then in tomorrows lesson, we will develop the Python software to create a really cool virtual graphic to display the data in a virtual world.

For this project you will need the HC-SR04 ultrasonic sensor, the TCS230 Color Sensor, the Arduino Microcontroller, and some male/female jumper wires to connect to the color sensor.

The Ultrasonic Sensor can be attached per the schematic below:

Simple Circuit for Measuring Distance

Detailed tutorial on using this sensor was described in Arduino LESSON 18, so we will not go through all the details of using the sensor here. Review that lesson if you need more help. Key point here is to connect it as seen in diagram above.

You will also need to connect up the Color Sensor.

Connecting the Color Sensor to the Arduino

Color Sensor Pin
Arduino Pin
S0 GND
S1 5V
S2 pin 7
S3 pin 8
OUT pin 4
VCC 5V
GND GND

Use of the color sensor was described in detail in Arduino LESSON 15.  You should be able to develop to write the software yourself based on earlier lessons to make measurements from both the Color Sensor, and Ultrasonic Sensor, but if you get stuck, you can glance at my code below. Again, it is important for you to write your own code and not copy and paste mine. Mine is just a reference if you get stuck.

The key point to notice with this code is the print statements, summarized below:

 Notice that we are printing  our color strengths and distance on one line separated by commas. It is important to note the order of the data. When we read this in Python, we will read it in as one line of text, and then we will parse it into its individual values. So, we must make note and remember the order the data is arranged in in this line.

Remember when you have your python program reading this data, you must have your serial monitor closed. For now though, run your program and look at the serial monitor to verify you are getting correct data in the expected format.

In the next Lesson, LESSON 5, we will build the Python program to create a virtual world from this data.