For this project we will using RPi and Python. Why are using an Ultrasonic Sensor you might ask? …. why not! Fancy teaching this in class check out the education resource


This project using an ultrasonic sensor to measure distance and light up a coloured LED depending on the distance from the sensor.


Ultrasonic Sensor click to find out more
LEDs (3: Red, green, orange)
Resistors 1K and 220ohm
Jumper cables male to Female solderless
GPIO expansion cable optional
Solderless 400 Point Breadboard


The three Python scripts used in the project to test the ultrasonic sensor, test the LEDs and combine the two elements together. This creates a basic simulation of a parking sensor. Click on the links to see the code or for further explanation. – Takes three measurements from the ultrasonic sensor and calculates the average. This is displayed and one second later it takes another average. By Matt Hawkins Raspberry Pi Spy – Flashes the three LEDS, in sequence, 5 times. – The complete script that takes three measurements from the ultrasonic sensor and then lights up a single LED dependant on the distance measured.

STEP 1 Setting up the breadboard

Check the pins on the Ultrasonic sensor to ensure that they are not bent.  If they are very carefully straighten the pin(s) back into the correct place.  You should be able to do this by hand.

STEP 2 Connecting between RPi & Ultrasonic Sensor

The Wiring Diagram for connecting the GPIO pins to the breadboard. Click the link to read about GPIO

Wiring Diagram For Ultrasonic-Pi

STEP 3 Writing the code

GPIO in Python

There are 3 python files that we are using. Setting up the GPIO, Blinky for making the LEDS blink on and off and Finally the Ultrasonic sensor which measures the distance using the sensor data and uses this to light up the LEDS.

The easiest way to control the GPIO pins is using the RPi.GPIO Python library. Installing the library is easy if you follow my RPi.GPIO Installation Guide. Once installed using the pins is as easy as :

import RPi.GPIO as GPIO
# Use GPIO numbers not pin numbers
# set up the GPIO channels - one input and one output
GPIO.setup(7, GPIO.IN)
GPIO.setup(8, GPIO.OUT)
# input from GPIO7
input_value = GPIO.input(7)
# output to GPIO8
GPIO.output(8, True)

In this example we use GPIO7 (pin 26) and GPIO8 (pin 24). Python scripts that use the GPIO library must be ‘run’ in sudo mode. i.e. Make sure that code and your RPI / Ultrasonic sensor are connected using the same pin ‘name/numbers’. 

sudo python   Rename ‘yourscript‘ to an appropriate name.

Blinking the LEDS

Everyone loves the flashing lights. see here for the python code Once you have set up your LEDS correctly then you can run the code.  Modify the code to add more LEDs and different sequences of lights to create various patterns. Make a copy of the code/ program for testing different variables.  This will enable you to mess around as much as you like with code without modifying the original file. A good way to name files for testing is to add test to the end of the file name for example

TEST your code.  Modify the variables values of the each of the LEDS

#Modify this bit of the code.
red = 11
yellow = 9
green = 10
led_pin = red

Next use the UltrasonicPI code to light up the LEDS using the ultrasonic sensor.

The UltrasonicPI code blinks the LEDS based on the distance using the ultrasonic sensor. What could you use your new creation for? Maybe you could connect LED strips and create some wall art?

Have fun creating

Add some code to store the data and create some data visualisation using Javascript D3 library or similar.

This project could be added to a moving robot to add a visual effect to the robot.