My Account

Wish List (0)

Saved Carts

Compare

Audible Sensor Readings

December 09, 2014

This tutorial will show you how to make the reading of a sensor audible. In this example, we will be using the HC-SR04 Ultrasonic sensor, but it can be applied to most, if not all, other sensors.

To do this, you will need to have the ultrasonic sensor working, which you can read here: https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi

You will also need text-to-speech software. There are a few different ones, but we are using espeak. To install it open a terminal and type in:

sudo apt-get install espeak

Once this is done, your raspberry pi is ready to talk to you.

The basic idea is to write the output of the reading in a text file and then read it by espeak.

First open your python script (range_sensor.py), scroll down and add these lines below the command print "Distance:",distance,"cm" :

f = open("reading.txt", "w")
f.write("the distance is: (%d) centimeters" % distance)
f.close()

After running the script, you should have a text file called “reading.txt”.

Now you can play it with espeak using the following line:

espeak -f reading.txt

You can also combine these two commands into a bash file so that you can execute both of them at once. So first create a bash file:

touch tell_distance.bash

Then open the file by:

nano tell_distance.bash

And write there the following two lines:

sudo python range_sensor.py
espeak -f reading.txt

and then “ctrl + x” to exit and “y” to save it. Then you need to make the file executable with:

chmod +x tell_distance.bash

and finally you can run it with ./tell_distance.bash.

You might need to set the sound output on your raspberry pi to analogue, you can do that by typing this to the terminal:

amixer cset numid=3 1

(amixer cset numid=3 2 will set it to HDMI)

Last update: September 10, 2015

Comments

Leave a Comment

Leave a Reply

* Name:
* Comment:
* E-mail: (Not Published)
Website: (Site url with http://)
Verification code: