articles

GrovePi+ Project 2: Adjustable Digital Distance Sensor For Robot Car!

Monday, September 12, 2016

On project 1 I faded two LEDs simultaneously using the GrovePi+ starter kit, and for my second project  I will be creating an adjustable distance parking sensor using a GrovePi+ board, an ultrasonic ranger, two LEDs, buzzer and and RGB LCD Display.
video

The adjustable distance parking sensor measures the distance the sensor is from an object or obstacle and it will emit an LED from blue to red as the object is withing range and closer to the target area.

I have set up the target area to be less then 30 cm and the range to be less than 100 cm, but you can adjust the distance to any distance you like. When the sensor detects and object within the 100-cm range, the blue LED will light up. When the objects is withing the 30-cm target area, the red LED will light up and the buzzer will buzz. 
video
On this video below I adjusted the buzzer sound by adding print ( 'start') and print ( 'stop') with 1 second sleep time. 
video
In order for the ultrasonic ranger data to display on the RGB LDC, you must import the following two lines:
import sys
 sys. path. append ( "/ home / pi / GrovePi / Software / Python / grove_rgb_lcd")

 from grove_rgb_lcd import *

and write the following lines to your code:
distance = ultrasonicRead (ultrasonic_ranger)
         print distance
         dist = int (distance)
         if dist> 100:
             setText ( "distance =" + str (dist))

         elif dist> 30:
             setText ( "distance =" + str (dist))

         else:
             setText ( "distance =" + str (dist))

The RGB LCD backlight display will flicker it's connected to the board while powered on. To stop the RGB LCD display backlight from flickering, simply power off the unit and power it back on.

The entire code to make the adjustable distance parking sensor to work is below.  The ultrasonic ranger, blue and red LEDs and buzzer connect to digital ports (D2, D5, D6 and D8). The RGB LCD display connects to any I2C port.
solid green background setRGB (0, 255, 0) with  time.sleep (.5) 

The function SetRGB is for changing the background color of the LCD display. You can have the background color with a solid color, shades of a color or random colors. I have SetRGB set to shade of white (255 - c, 255 - c, 255) with sleep time 0.0039 but if you want solid green color simply write (0, 255, 0). If you want random colors write the following: (random.randint (0, 255) , random.randint (0, 255), random.randint (0, 255)). For more color variations, check out Dexter Industries GitHub

Executing the code is easy. You can either use the sudo python command or use the python shell run module command by pressing F5 on your keyboard, which is much faster.

You Might Also Like

0 comments

About Gadget Explained

Connect With Gadget Explained