Reader small image

You're reading from  Raspberry Pi Sensors

Product typeBook
Published inApr 2015
Publisher
ISBN-139781784393618
Edition1st Edition
Right arrow
Author (1)
Rushi Gajjar
Rushi Gajjar
author image
Rushi Gajjar

Rushi Gajjar is an entrepreneur, embedded systems hardware developer and a lifetime electronics enthusiast. He works in the field of research and development of high-speed single-board embedded computers and wireless sensor nodes for the Internet of Things. Prior to that, his extensive work as a freelancer in the domain of electronics hardware design introduced him to rapid prototyping development boards and single board embedded computers such as the Raspberry Pi. In the spare time, he loves to develop the projects on Raspberry Pi including sensors, imaging, data logging, web-servers, and machine learning automation systems. He authored a DIY and hardware based book titled Raspberry Pi Sensors which takes deep dive in developing sensor interfacing based projects with Raspberry Pi. His vision encompasses connecting every entity in world to the Internet for enhancing the human living experience. https://www.linkedin.com/in/rushigajjarhttp://www.amazon.com/Raspberry-Pi-Sensors-Rushi-Gajjar/dp/1784393614/ref=sr_1_9?ie=UTF8&qid=1453884207&sr=8-9&keywords=raspberry+Pi+Sensorshttp://rushigajjar.blogspot.in/
Read more about Rushi Gajjar

Right arrow

Chapter 3. Measuring Distance Using Ultrasonic Sensors

We, humans have five senses. They are touch, smell, sight, hearing, and taste. However, computers and robots can have as many senses as we want. We can sense different things around us; for example, changes in the temperature can be felt by our skin, but we cannot precisely say what the actual temperature value that we are feeling is. Computers such as RasPi can be used to sense and monitor the surrounding entities. It does the job well, precisely, and untiringly. The computing and interfacing capability of RasPi allows us to interface sensors with it.

Measuring distance using meter tapes and odometers is impractical or inconvenient for some of the applications. If you want to measure the depth of the ocean, how can you use meter tapes? The best option is to use technology such as Sonar or satellites. However, in our homes, labs, and even in our daily life, we often use different ultrasonic sensors for various applications. These applications...

The mysterious ultrasonic sensor


When you go on an expedition to mountains, you must have experienced the echo phenomenon while shouting loudly towards high mountains. You can even experience this phenomenon in a hall that doesn't have interiors such as curtains and furniture (in a new house). The ultrasonic sensor works on a similar principal. Ultrasonic sensors generate ultrasound waves that are targeted towards an obstacle after which they wait for the echo to be heard. However, why don't you hear any sound when you use an ultrasonic sensor? The answer is pretty simple: this sensor works at an ultrasonic frequency, which is higher than the audible frequency range of humans. The human's average theoretical audible frequency range is 20 Hz to 20 KHz. The ultrasonic sensor transmits the sound waves (also called as a sonic burst) higher than 20 KHz frequency. Ultrasonic waves are mainly used because they are not audible to the human ear and also because they provide precise distance measurement...

Building the project!


It's now time to connect the ultrasonic sensor with the RasPi board. The ultrasonic sensor works on a 5V power supply. Fortunately, we have the 5V supply pin on the RasPi board. We can provide the 5V supply from RasPi to the ultrasonic sensor. However, in reply, the ultrasonic sensor generates a 5V echo signal as an output to RasPi.

Tip

It is always recommended that you connect the ground terminals of the devices first and then the voltage supply terminals. This should be followed with almost all the electronic devices we connect with development boards such as RasPi.

As you have read in Chapter 2, Meeting the World of Electronics, we know that our RasPi needs the 3.3V level on the GPIO pins to operate safely. So, how do we connect them? It is a serious matter. In this regard, Kirchhoff will help us. With the help of Kirchhoff's current and voltage laws, we can divide the voltage into two parts. If we divide the 5V supply into 3.3V and 1.7V, we can use the echo pulse coming...

Fixing common problems


While building the project, many of us will not get the output the first time. Let's solve the common problems collaboratively. There are some frequently occurring problems while building the projects with the RasPi and the ultrasonic sensors.

Is it showing the distance incorrectly?

You are so enthusiastic to write the code by yourselves, and you might have been mistaken. Check the timings of turning the trigger pin for the perfect amount of time, and check whether you have read the echo pin correctly.

Some of the device response may be slow. I recommend that you add a delay of 60 ms after making the trigger pin low, as shown in the following code:

GPIO.output(23, False)
time.sleep(0.06)

If you are still not getting the result, try playing around with these values of delay. Also, check for the correct indentation of the code, which might be the major reason that you are facing the problem.

There is a high chance that you may be having so many things on your table that the...

A wearable device for the visually impaired


The ultrasonic sensor can provide an added sense to a visually impaired or blind person. You can contribute to the society by making this kind of a wearable device, which can either play a warning sound in the person's ear or a vibration alert on the stick. There are such sticks available in the market, and you can build your own, too.

Your aim is to build a wearable device based on the RasPi and ultrasonic sensor. Also, you will play the warning sound on the headphones that are connected with the RasPi when the visually impaired person is approaching an obstacle that is about 100 cms away. Sounds cool, doesn't it? This project will work for both RasPi 2 Model B and RasPi 1 Model B and B+ users. We will learn this step-by-step process to assemble this project on the hardware and also understand how to code it. Let's dig deeper and understand how this can be done.

Building the hardware

When you run the RasPi, you must have noticed that your project...

Summary


This chapter gave us lot of knowledge about the RasPi and ultrasonic sensor interfacing, and we enjoyed a lot while building the project. You got to understand how ultrasonic sensors work. You understood that the voltage levels must be the same between the RasPi and the sensors. We used a voltage divider to convert 5V to 3.3V for the RasPi. We set up the hardware and software to start executing the project. We got the distance measurement of any target devices in our lab. At least now, we know how far our ceiling is!

We thought to make a project for a good cause, and we created wearable devices using the RasPi and a USB battery pack. Playing a sound on RasPi device to indicate the alerts was a real fun. It was then interesting to know about crontab to start the file execution at the boot-up of the RasPi module without any need of configuration.

In the next chapter, we will play with the temperature-humidity sensor along with the light sensor. The RasPi does not have any analog-to-digital...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Sensors
Published in: Apr 2015Publisher: ISBN-13: 9781784393618
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rushi Gajjar

Rushi Gajjar is an entrepreneur, embedded systems hardware developer and a lifetime electronics enthusiast. He works in the field of research and development of high-speed single-board embedded computers and wireless sensor nodes for the Internet of Things. Prior to that, his extensive work as a freelancer in the domain of electronics hardware design introduced him to rapid prototyping development boards and single board embedded computers such as the Raspberry Pi. In the spare time, he loves to develop the projects on Raspberry Pi including sensors, imaging, data logging, web-servers, and machine learning automation systems. He authored a DIY and hardware based book titled Raspberry Pi Sensors which takes deep dive in developing sensor interfacing based projects with Raspberry Pi. His vision encompasses connecting every entity in world to the Internet for enhancing the human living experience. https://www.linkedin.com/in/rushigajjarhttp://www.amazon.com/Raspberry-Pi-Sensors-Rushi-Gajjar/dp/1784393614/ref=sr_1_9?ie=UTF8&qid=1453884207&sr=8-9&keywords=raspberry+Pi+Sensorshttp://rushigajjar.blogspot.in/
Read more about Rushi Gajjar