Book Image

Arduino Development Cookbook

By : Cornel M Amariei
Book Image

Arduino Development Cookbook

By: Cornel M Amariei

Overview of this book

Table of Contents (16 chapters)
Arduino Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Measuring distance – infrared and ultrasonic


A distance sensor is the most important sensor for any robot. It's usually referred to as the "eyes" of a robot. Distance sensors are very useful as we can make systems that react based on how close we are to them or based on the presence of various obstacles.

There are two common technologies used in amateur distance sensing: infrared sensors, such as the classic Sharp IR, and ultrasonic sensors, usually called sonars. Now, let's build a distance-controlled LED!

Getting ready

To build a distance-controlled LED, we will need the following ingredients:

  • An Arduino board connected to a computer via USB

  • One LED

  • A Sharp infrared proximity sensor such as the GP2Y0A21YK or the GP2Y0A02YK0F

How to do it…

Connecting a Sharp IR is easy. Maybe this is why it's so popular. Follow these simple steps to connect one:

  1. Each Sharp IR has three pins. One is the power input, which we connect to 5V. Another is the ground that we will connect to one GND pin. Lastly, there is...