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 will require the electrical plug socket where we connect the adaptor to power up the RasPi. Our project needs to be wearable. So, as you have rightly guessed, we need a battery. The battery specifications can be quite variable, but you need to stick to 4000mAH–10000mAH and 1A–1.5A USB battery pack. You will easily get the USB battery pack for the RasPi in the online stores. The power banks for mobile phones are also OK, but stick with the current ratings. We also need headphones that have a standard 3.5 mm connector to play the warning tones. Here is a list of the hardware we need:

  • A HC-SR04 ultrasonic range finder
  • 1KΩ, 2KΩ, and 270Ω resistors
  • A 2 mm or 3 mm LED
  • Female-to-male jumper wires and female-to-female jumper wires
  • Breadboard
  • The RasPi
  • USB Power Bank 4000mAH–10000mAH with 1A–1.5A current rating
  • Headphones with a 3.5 mm connector
  • Small wire strips to keep the wires folded
  • An Ethernet cable
  • A personal computer

Once you have made your testing project in the previous section, additionally, you would need to buy a battery bank and an LED. Any mobile phone's headphones would work fine with the RasPi. Building this device is easy. Connect the ultrasonic sensor with the same breadboard configuration, as we did in the previous section. Additionally, we also connect an LED to the RasPi pin number 11 (BCM mode GPIO 17), with the same method that we followed in Chapter 2, Meeting the World of Electronics. The power bank will be connected through the micro-USB jack of the RasPi, where we usually connect our power adapter. Headphones need to be connected to the 3.5 mm jack of the RasPi. The USB cable and headphone wires will be lengthy, so I would like to suggest that you use small wire strips. Small wire strips will help you tie wires and allow you to make your project look neat with tangle-free wires.

This wearable device can be worn at the level of the chest or at the level of the head. You can make use of a cap to attach this device. Pack it beautifully once you finish the programming and testing. Let's have a look at the programming side of it.

Software setup

The device should play a buzz or a beep sound on the headphones. You can get amazing sounds from the www.freesound.org/browse/tags/beep/ web page and download them to the RasPi. I suggest that you download the file that has the shortest duration, for example, 1 second or 2 seconds. Otherwise, it would be annoying to hear the beep sound on headphones for 5 seconds. Either you can download it using the Xming server and inbuilt browser (from PuTTY, using the lxsession command) or transfer it from your PC to the RasPi. But wait, how will you transfer a file on the RasPi?

Tip

There is a wonderful program available to copy the files from Windows to the RasPi. This program is known as WinSCP (you can download it from www.winscp.net). Give the IP address, login ID, and password of the RasPi in the FTP mode. You can copy the files from Windows to the RasPi by dragging and dropping them. If you find difficulty using WinSCP, the operating tutorials could be found at https://www.siteground.com/tutorials/ssh/ssh_winscp.htm.

We will make a directory called project using the mkdir project command. We will perform every operation inside this folder. I am assuming that we are currently at /home/pi/project address. You can check it once using the pwd command.

As we have already downloaded the essential libraries in Chapter 1, Meeting Your Buddy – the Raspberry Pi, for the support of wiringPi in C, we will now download a library and drivers to play music files on our RasPi. Enter the following commands to configure the drivers in your RasPi. These commands hold good for both RasPi 2 B and B+ users. I assume that you have shared an Internet connection to the RasPi as per the guidelines of Chapter 1, Meeting Your Buddy – the Raspberry Pi. We are running a PuTTY session on RasPi through an Ethernet cable:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install alsa-utils
sudo apt-get install mpg321

It will take some time to download, unpack, and install. Once it is installed, just reboot the RasPi and start the session of PuTTY again on your PC. We can now load the drivers for sound, which we just installed by entering these commands:

sudo modprobe snd_bcm2835
sudo amixer cset numid=3 1

Now, it's time to write the code that measures the distance as well as notifies the person with an LED indication and a short-duration beep sound. Write sudo nano project1.py and start typing the following code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(FALSE)
print "Measuring Distance"
print "Press ctrl+c to stop me"
GPIO.setup(23,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(24,GPIO.IN)
time.sleep(0.02)
GPIO.output(23, False)
time.sleep(1)
while True:
     GPIO.output(17, False)
     GPIO.output(23, True)
     time.sleep(0.00001)
     GPIO.output(23, False)
     while GPIO.input(24) == 0:
          start_time = time.time()
     while GPIO.input(24) == 1:
          end_time = time.time()
     time = end_time – start_time
     distance = 17150 * time
     print "Measured Distance is:", distance, "cms."
     if  distance < 100 and x > 30:
          print "Obstacle Detected"
          os.system('mpg321 buzz.mp3 &')
          GPIO.output(17, True)
GPIO.cleanup()

Press Ctrl + X, followed by Y, and then press Enter to save and exit the nano editor. We need to take execution permission from Linux to run the code. So, we will enter the following command and then execute it:

chmod +x project1.py
sudo python project1.py

Try with different distances and check whether the warnings are set properly or not. You can put some text lines to easily debug the code. So far, we had connected Windows PC and the RasPi with an Ethernet cable. However, it is hard when you install it on your head as a wearable and connect the Ethernet cable or keyboard and display it to run the program when the RasPi powers up. Is there any function that whenever the RasPi is booted, the code we just created executes automatically? Fortunately, yes! There is a facility in the Linux kernel called as crontab. Crontab allows us to run the desired program whenever the RasPi is booted. Shell scripting will help us here. We will write a small script in the same directory with the /home/pi/project address. Now, we can write the following shell script to execute the project1.py file we created. Open a nano editor by typing the sudo nano project1_startup.sh command:

#!/bin/sh
cd /
cd home/pi/project
sudo python project1.py
cd /
#end of script

This file will be executed by the crontab, so we should give execution access to the file using the chmod +x project1_startup.sh command. Now, we just need to check whether the code executes perfectly or not. Type the ./project1_startup.sh command to execute the shell script file and test whether the sound or LED blinks for less than 100 cm distance. Once this is done, we will edit the crontab file with the following code to enter our shell file to be executed:

mkdir chronlogs
sudo crontab -e

A file with lot of text will be opened. At the end of the file, just enter the following script and press Ctrl + X and Y and then press Enter to save the file:

@reboot sh /home/pi/project/project1_startup.sh >/home/pi/project/cronlogs/cronlog 2>&1

Tip

Note that it is a single line, and hence, it should be inserted without pressing Enter.

These commands will also log the errors and execution status to the chronlogs folder in the project directory. We are now ready to test the project execution during boot-up. Test it using the sudo reboot command.

With this, we do not need the keyboard, mouse, Ethernet cable, or PC to configure and run the desired code when the RasPi is started up. Just wear the RasPi and connect the battery supply. Your RasPi is ready to measure the distance and give the indications and warning sound whenever the obstacle is less than 100 cms away. If you want to remove the project from the startup, just re-edit the crontab file and remove the last line we added.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.111.24