# Measuring distance/proximity

The Electronics Center has a wide variety of object/distance detection sensors available for your project needs.

Everything listed below is available for checkout from the Electronics Center (ECCE 167).

**If you just need to sense if an object has moved (e.g., counting fan blade rotations or detecting a waving hand):**

- Passive Infrared (PIR) sensor
- IR LED &amp; transceiver (nice for "photogate" or "beam break" applications)
- Hall sensor (good for detecting magnets)

**If you need to** ***quanitfy*** **or** ***measure*** **a distance to an object, we recommend:**

- Ultrasonic Sensor (HC-SR04)
- SHARP 2Y0A02 IR Sensor
- LiDAR

# Passive Infrared (PIR) Sensors

#### Overview

PIR or Passive Infrared Sensors are commonly used in projects where students want a device capable of motion detection. These sensors detect movement by measuring changes in infrared radiation emitted by warm objects, such as people or animals, within their field of view. These sensors are known as "passive" sensors, as they merely listen to changes in energy, and do not emit any signals themselves. This passive characteristic makes them ideal for low power applications.

Staff at the ITLL tested the detection range of these sensors along with doing some research and found it could sense motion up to approximately 23 feet or 7 meters (depending on model and conditions). You may have seen these sensors before as they are commonly used for automatic lights, interactive exhibits, or security alarms. At the ITLL, you can find them controlling the lights within our lab spaces to save energy.

Students are welcome to come by and check out sensors from the Electronics Center. These are the sensors you can check out from the ITLL:  [link to inventory](https://o365coloradoedu.sharepoint.com/:x:/r/sites/ITLP-Electronic/Equipment%20%20Tools/ITLL%20Sensors%20and%20Equipment%20Checkout.xlsx?d=w013360c09b7841b8b5cb500c1f4349f2&csf=1&web=1&e=iPg7ao&nav=MTVfezAwMDAwMDAwLTAwMDEtMDAwMC0wOTAwLTAwMDAwMDAwMDAwMH0) . If you do not see a sensor listed feel free to come in and chat with an Engineer about you project.

#### What is a PIR Sensor?

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/XmCSoqEY40DrI3tr-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/XmCSoqEY40DrI3tr-image.png)

A PIR sensor comes with a small white dome shaped lens on top of the PCB. This lens, the Fresnel lens, helps focus light from any direction to the small pyroelectric sensor underneath. When motion occurs, half of this sensor detects a change in IR before the other, and the difference causes a signal change (from LOW to HIGH). These sensors also include a sensitivity and time delay adjustment potentiometer. Which allow you to change the range or distance the sensor can detect, and how long the sensor remains active (before switching back from HIGH to LOW). Below you can find an image labeling the potentiometers.

#### Wiring the Sensor

<span style="text-decoration: underline;">**Important:** </span> Look up your specific sensors pinout and input voltage as they can differ. The pinout for the sensor we used in this demo was...

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/LO2hnNoNVONr0ZIy-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/LO2hnNoNVONr0ZIy-image.png)

Example circuit setup:

- PIR sensor output connected to Arduino digital pin 2, VCC connected to 5V, and GND connected to GND.
- LED connected to Arduino digital pin 6 and 330 ohm resistor (resistor band color is different on picture).

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-05/scaled-1680-/f6ZwqTylB5Zat53J-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-05/f6ZwqTylB5Zat53J-image.png)

#### Troubleshooting

1.  If sensor reading always HIGH or ON  
    \- Sensor may be warming up (10-60 seconds after being turned on)  
    \- Reduce sensitivity with potentiometer  
    \- Ensure stable power supply
2.  If no output changes  
    \- Verify correct wiring   
    \- Use Serial Monitor to debug readings
3. If false triggering  
    \- Avoid pointing sensor at heat source (like a vent)  
    \- Ensure no vibration of sensor mount

#### Simple Code

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-05/scaled-1680-/91pmwFvc0DJ0uE6j-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-05/91pmwFvc0DJ0uE6j-image.png)

<table border="0" cellpadding="0" cellspacing="0" id="bkmrk-int-led-%3D-6%3B%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0-"><tbody><tr><td class="code">

1. `int` `LED = 6;        ` `// LED connected to digital pin 6`
2. `int` `PIR = 2;        ` `// PIR motion sensor connected to digital pin 2`
3. 
4. `int` `state = LOW;    ` `// Tracks previous motion state:`
5. `                    ` `// LOW  = no motion previously detected`
6. `                    ` `// HIGH = motion previously detected`
7. 
8. `int` `value = 0;      ` `// Stores current reading from PIR sensor (HIGH or LOW)`
9. 
10. `void` `setup() {`
11. `  ` `pinMode(LED, OUTPUT);   ` `// Set LED pin as output (Arduino controls LED)`
12. `  ` `pinMode(PIR, INPUT);    ` `// Set PIR pin as input (Arduino reads sensor signal)`
13. `  `
14. `  ` `Serial.begin(9600);     ` `// Start serial communication for debugging/output`
15. `}`
16. 
17. `void` `loop() {`
18. `  ` `// Read the current value from the PIR sensor`
19. `  ` `value = digitalRead(PIR);  ` `// Will be HIGH (motion) or LOW (no motion)`
20. 
21. `  ` `// If motion is detected`
22. `  ` `if` `(value == HIGH) {`
23. `    ` `digitalWrite(LED, HIGH);  ` `// Turn LED ON`
24. `    ` `delay(100);               ` `// Keep LED on briefly (100 ms)`
25. 
26. `    ` `// Only print when motion FIRST starts (prevents spamming Serial Monitor)`
27. `    ` `if` `(state == LOW) { `
28. `      ` `Serial.println(` `"Motion Detected"` `); `
29. `      ` `state = HIGH;           ` `// Update state to indicate motion is active`
30. `    ` `}`
31. `  ` `}`
32. `  ` `else` `{  ` `// If NO motion is detected`
33. `    ` `digitalWrite(LED, LOW);   ` `// Turn LED OFF`
34. `    ` `delay(100);               ` `// Small delay`
35. 
36. `    ` `// Only print when motion JUST stopped`
37. `    ` `if` `(state == HIGH) {`
38. `      ` `Serial.println(` `"Motion has stopped"` `);`
39. `      ` `state = LOW;            ` `// Update state to indicate no motion`
40. `    ` `}  `
41. `  ` `}`
42. `}`

</td></tr></tbody></table>

# Ultrasonic Sensor

#### Overview

<span style="background-color: rgb(224, 62, 45);">Ultrasonic Sensors are a common form of proximity and distance sensors used in a wide variety of different projects. These sensors feature both an emitter (trigger) which releases an ultrasonic sound wave, and a receiver (echo) which reads the ultrasonic sound wave as it returns. </span>

<span style="color: rgb(132, 63, 161);">Claire suggested edits: </span>

<span style="color: rgb(132, 63, 161);">In the **Overview,** provide the range and accuracy of these sensors. I'd get rid of the first sentence. Second sentence is great because it explains the working principle.</span>

<span style="color: rgb(132, 63, 161);">I actually had something written up about these sensors elsewhere, and I will paste my sentences here. Feel free to take 'em or leave 'em:</span>

<span style="color: rgb(132, 63, 161);">These sensors can detect objects within a range of 2 cm - 4 m with a ranging accuracy of up to 3mm. One thing to note is that they have a measure angle of 15 degrees, so they do well at measuring objects directly in front of the sensor. For most applications, this is favorable. </span>

<span style="color: rgb(132, 63, 161);">[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-04/scaled-1680-/AHOe22zi3F5TGZV5-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-04/AHOe22zi3F5TGZV5-image.png)</span>

<span style="color: rgb(132, 63, 161);">Helpful resource from Sparkfun with example code, and wiring diagram: [https://www.sparkfun.com/ultrasonic-distance-sensor-hc-sr04.html](https://www.sparkfun.com/ultrasonic-distance-sensor-hc-sr04.html)</span>

 Students are welcome to come by and check out sensors from the Electronics Center. These are the sensors you can check out from the ITLL:  [link to inventory](https://o365coloradoedu.sharepoint.com/:x:/r/sites/ITLP-Electronic/Equipment%20%20Tools/ITLL%20Sensors%20and%20Equipment%20Checkout.xlsx?d=w013360c09b7841b8b5cb500c1f4349f2&csf=1&web=1&e=iPg7ao&nav=MTVfezAwMDAwMDAwLTAwMDEtMDAwMC0wOTAwLTAwMDAwMDAwMDAwMH0) . If you do not see a sensor listed feel free to come in and chat with an Engineer about you project.

#### What is an Ultrasonic Sensor?  


[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/F0vYgMLJZIoZeoMA-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/F0vYgMLJZIoZeoMA-image.png)

<span style="color: rgb(0, 0, 0); background-color: rgb(224, 62, 45);">A PIR sensor comes with a small white dome shaped lens on top of the PCB. This lens, the Fresnel lens, helps focus light from any direction to the small pyroelectric sensor underneath. When motion occurs, half of this sensor detects a change in IR before the other, and the difference causes a signal change (from LOW to HIGH). These sensors also include a sensitivity and time delay adjustment potentiometer. Which allow you to change the range or distance the sensor can detect, and how long the sensor remains active (before switching back from HIGH to LOW). Below you can find an image labeling the potentiometers.</span>

#### Wiring the Sensor

<span style="text-decoration: underline;">**Important:** </span> Look up your specific sensors pinout and input voltage as they can differ. The pinout for the sensor we used in this demo was...

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/eyrPzlFX31SiXERn-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/eyrPzlFX31SiXERn-image.png)

Example circuit setup:

- Ultrasonic sensor output (trig) connected to pin 3, input (echo) connected to pin 2, VCC connected to 5V, and GND (ground) connected to GND.
- The code uploaded to the Arduino (below) sets the trigger pin HIGH, emitting a wave, that then returns and is received by the echo pin. Using an equation that utilizes the speed of light we are able to determine the time it takes for the wave to return after being transmitted, giving us our distance.

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/69buws5JQe8qagxX-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/69buws5JQe8qagxX-image.png)

#### Troubleshooting

<span style="color: rgb(132, 63, 161);">Claire edits: </span>

1.  If sensor reading 0 only  
    \- Run test code below  
    \- Ensure pin definitions are correct and wires are plugged in to correct spots
2. If nothing shows up in serial monitor  
    \- Make sure you include Serial.begin(9600);  
    \- Ensure serial monitor has 9600 baud rate selected

#### Simple Code

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/tJBUdKsKJY2gxueK-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/tJBUdKsKJY2gxueK-image.png)

```
// Define pin numbers
const int trigPin = 3;                   // pin sending the ultrasonic wave
const int echoPin = 2;                   // pin reading the ultrasonic wave
long duration;                           // time it takes for wave to travel
int distance;                            // distance wave has traveled

void setup() {
 
  pinMode(trigPin, OUTPUT);             // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);              // Sets the echoPin as an Input
 
  Serial.begin(9600);                   //initializing serial communication at 9600 baud rate  
}

void loop() {

  // Trigger the sensor by sending a HIGH pulse for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delay(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);    // pulseIn() returns the sound wave travel time in microseconds

  distance = duration * 0.034 / 2;      // Calc. distance 9speed of sound is 0.034 cm per microsec. and wave goes there and back0

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(200);                          // Delay before the next reading
}
```

# Measuring distance/proximity

#### If you need to quantify distance:

<details id="bkmrk-sharp-2y0a02%2C-ir-sen"><summary>SHARP 2Y0A02, IR sensor (λ = 850 ± 70nm)</summary>

##### At-a-glance useful details:

- Sensing range: 20 - 150 cm
- Power requirements: 4.5 - 5.5 V (typ current consumption = 33mA)
- ##### [link to datasheet](https://mm.digikey.com/Volume0/opasdata/d220001/medias/docus/1/GP2Y0A02YK0F.pdf)
- Output voltage ***is not linear with distance; see plot below (from datasheet, linked above)***
- [![{8D5D58AC-AA88-469C-8E14-8B48FF36A4EF}.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2025-12/scaled-1680-/KhBLI110GKIugMh2-8d5d58ac-aa88-469c-8e14-8b48ff36a4ef.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2025-12/KhBLI110GKIugMh2-8d5d58ac-aa88-469c-8e14-8b48ff36a4ef.png)

[![{54C81708-A400-4B9E-AF00-ADA567605335}.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2025-12/scaled-1680-/RHFMUCY2xDDhYmj6-54c81708-a400-4b9e-af00-ada567605335.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2025-12/RHFMUCY2xDDhYmj6-54c81708-a400-4b9e-af00-ada567605335.png)

[![{71064E72-9B44-41D5-8E68-45F4EB5E22A0}.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2025-12/scaled-1680-/YqyR5GH4g6DBPeoB-71064e72-9b44-41d5-8e68-45f4eb5e22a0.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2025-12/YqyR5GH4g6DBPeoB-71064e72-9b44-41d5-8e68-45f4eb5e22a0.png)

</details><details id="bkmrk-ultrasonic-sensor-%28h"><summary>Ultrasonic Sensor (HC-SR04)</summary>

[![image.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-04/scaled-1680-/AHOe22zi3F5TGZV5-image.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-04/AHOe22zi3F5TGZV5-image.png)

These sensors can detect objects within a range of 2 cm - 4 m with a ranging accuracy of up to 3mm. One thing to note is that they have a measure angle of 15 degrees, so they do well at measuring objects directly in front of the sensor. For most applications, this is favorable.

Helpful resource from Sparkfun with example code, and wiring diagram: [https://www.sparkfun.com/ultrasonic-distance-sensor-hc-sr04.html](https://www.sparkfun.com/ultrasonic-distance-sensor-hc-sr04.html)

</details>####  

#### If you just need to know if an object is present or not:

#####  (e.g., detecting fan speed, motion detector, etc.)

<details id="bkmrk-hall-sensor-%28ah1815%2C"><summary>Hall Sensor (AH1815, from Treston)</summary>

</details>

# IR LED & transceiver

#### Two options:

1. Single PCB with LED and transceiver
2. LED and transceiver from Treston (BYOC or build your own circuit)

#### Good for:

Detecting a moving object less than few inches away from the sensor

# Hall Sensor (AH1815, from Treston)

Author: Claire Porter

#### Overview

Hall sensors are available from the Treston. The presence of a magnetic field switches their output pin (pin 3) from HIGH to LOW.

Detection distance: ~1cm

<details id="bkmrk-pictures-illustratin"><summary>Pictures illustrating short range of sensor</summary>

To test the range of the Hall sensor. I used my nametag magnet and held it far away from the sensor. I slowly brought the magnet close until I read a low signal on the digital pin to which I wired the output of this sensor (yellow jumper wire going to Arduino digital pin 8). The images below demonstrate the threshold distance close enough to trigger the sensor, as well as the fact that the sensor detects a field behind or in front of it.

[![Media (1).jpg](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/6SVjeAYuhdQPl33Y-media-1.jpg)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/6SVjeAYuhdQPl33Y-media-1.jpg)

[![Media.jpg](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/i5sz91JDUmDoS9t6-media.jpg)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/i5sz91JDUmDoS9t6-media.jpg)

</details>Magnetic field operating point: ~350 Gauss (a cheap kitchen magnet is around 100 Gauss, a strong magnet is 200-2,000 Gauss)

*Pinout for the AH1815, note the orientation/non-flat face is up*

[![{F75335B5-CB78-4AE7-ADCD-45B9B438A954}.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/kKcHVb7b0HCAH1Fo-f75335b5-cb78-4ae7-adcd-45b9b438a954.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/kKcHVb7b0HCAH1Fo-f75335b5-cb78-4ae7-adcd-45b9b438a954.png)

#### How to use

You will need to add a pull-up resistor to the output pin. I'd recommend a 10k resistor.

[![{0465A7D3-0478-4D10-95AE-48BE238D1C61}.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/PoRo0bmP2NWkAQnt-0465a7d3-0478-4d10-95ae-48be238d1c61.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/PoRo0bmP2NWkAQnt-0465a7d3-0478-4d10-95ae-48be238d1c61.png)

Image above is from this component's [ data sheet](https://cdn.sparkfun.com/assets/4/4/8/2/a/AH1815.pdf)

See if you can wire this circuit just based off of the image above!

<details id="bkmrk-need-help%3F-i-will-sh"><summary>Need help?</summary>

I will show you an image below of how I wired it onto a breadboard. Importantly, you could wire this differently, or solder on longer wires onto each pin of the Hall sensor, or solder this onto a protoboard.

Regardless of whether you are using a breadboard or soldering, you need to make sure that:

1. Leftmost pin goes to power (2.5-5.5V input range, so you can use this with a 3.3V microcontroller)
2. Middle pin goes to ground
3. Rightmost pin goes to a digital pin on your microcontroller
4. Add a 10kOhm resistor between power and rightmost (output) pin
5. Add a 100 nF capacitor across power and ground to decrease noise

[![shared image.jpg](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/zxPNx7nF7c7Bu6mI-shared-image.jpg)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/zxPNx7nF7c7Bu6mI-shared-image.jpg)

</details>#### Sample Arduino code

<div id="bkmrk-int-hallpin-%3D-8%3B-voi"><div>int hallpin = 8;</div>  
<div>void setup() {</div><div>  pinMode(8, INPUT);</div><div>  Serial.begin(9600);</div><div>}</div>  
<div>void loop() {</div><div>  if (digitalRead(hallpin) == true) {   //this is the same as asking if digitalRead(hallpin) == HIGH</div><div>    Serial.println("Waiting...");</div><div>    delay(100);</div><div>  }</div><div>  else{</div><div>    Serial.println("OOh a magnet!");</div><div>    delay(100);</div><div>  }</div><div>}</div>  
</div>#### Troubleshooting

1\) There's no output when I run the code above

- Make sure you've opened the Serial monitor and the baud rate in the code matches that in the dropdown menu 
    - Click this:  
        
        [![{79CB7409-0E4E-44EC-AFE9-F2D3445B234F}.png](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/scaled-1680-/zViRpuDiLl8JCqzD-79cb7409-0e4e-44ec-afe9-f2d3445b234f.png)](https://itll-wiki.colorado.edu/bookstack/uploads/images/gallery/2026-07/zViRpuDiLl8JCqzD-79cb7409-0e4e-44ec-afe9-f2d3445b234f.png)

2\) The output bounces all over the place

- Verify that you have the pull-up resistor installed between power and output
- Verify that the value of the pull-up resistor is between 10k and 100kOhms

3\) The output thinks there is a magnet there when there isn't

- Verify that a pull-up resistor is present

4\) It's too slow

- Decrease the delay(**100**) to delay(10), etc.