Categories
TUTORIALS

Using Distance to Activate Light

In this tutorial, I’ll explain how to make a ultrasonic sensor use distance to activate an LED. There are lots of creative uses for this. Read on to explore-

For me, this is a part of a larger project to make a bike path that automatically illuminates when being used. Two of these sensors will be placed across from one another to find the bike within the lane. This will be tested with a single LED here in the tutorial, although the ultimate project could use several, or in our case, an entire strip. This is a great base to a larger project, with lots of applications!

Parts List:

  • Arduino Uno
  • A HC-SR04 Ultrasonic Sensor
  • 2 wires
  • 1 LED
  • 1 330ohm resistor

Setup:

The board is easy to set up. Here are some images to help you:

Importantly-
– White is Echo- plug this into pin 8
– Brown is Trig- plug this into pin 7
– Black is GND – plug into ground
– Red is VCC – plug into 5V

The Code

The code is short and sweet. You can copy it from here to make a single LED light up, as shown- but it’s way more fun if you connect it to a couple more!

I know there are a lot of people who can explain this sensor better than I can, including most of the internet. I’ll tell you what I do know- the trig sends ultrasonic “pings” out into the air, which hit an object, and echo reads the bounce back. The sensor measures the distance upon its return- and because of this round trip journey, you’ll see in the code that we do a calculation to split this distance in two!

You’ll use the serialĀ  monitor to receive information about this distance. Here, I have the sensor tell me when the item being sensed is “Too far, can’t reach,” – but you can easily switch the code to tell you when something is too close.

 

This is a good way to get you started on several different projects. Now you have two ways to understand distance: the serial monitor, and a visual signal. In addition, you learned some good basic skills on which to build!

Leave a Reply

Your email address will not be published. Required fields are marked *