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):

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

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 . 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

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

Important:  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

Example circuit setup: 

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

  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. }

Ultrasonic Sensor

Overview

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. 

 

Claire suggested edits: 

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.

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:

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. 

image.png

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

 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 . 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

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

Important:  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

Example circuit setup: 

image.png

Troubleshooting

Claire edits: 

  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

// 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:

SHARP 2Y0A02, IR sensor (λ = 850 ± 70nm)
At-a-glance useful details:

{54C81708-A400-4B9E-AF00-ADA567605335}.png

{71064E72-9B44-41D5-8E68-45F4EB5E22A0}.png

Ultrasonic Sensor (HC-SR04)

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

 

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

 (e.g., detecting fan speed, motion detector, etc.)
Hall Sensor (AH1815, from Treston)

 


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 

Pictures illustrating short range of sensor

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

 

 

Media.jpg

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

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

Image above is from this component's  data sheet

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

Need help?

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

Sample Arduino code

int hallpin = 8;

void setup() {
  pinMode(8, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(hallpin) == true) {   //this is the same as asking if digitalRead(hallpin) == HIGH
    Serial.println("Waiting...");
    delay(100);
  }
  else{
    Serial.println("OOh a magnet!");
    delay(100);
  }
}

Troubleshooting

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

2) The output bounces all over the place

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

4) It's too slow