Audible Sensor Readings

Audible Sensor Readings

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)

Leave a comment

All comments are moderated before being published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.