# IR Breakbeam Sensors

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/021/673/medium800/sensors_2167_iso_demo_SIZE.jpg?1418244593)

Infrared (IR) break-beam sensors are a simple way to detect motion. They work by having an emitter side that sends out a beam of human-invisible IR light, then a receiver across the way which is sensitive to that same light. When something passes between the two, and its not transparent to IR, then the 'beam is broken' and the receiver will let you know.

Compared to PIR sensors, breakbeams are faster and allow better control of where you want to detect the motion. Compared to Sonar modules, they're less expensive. However, you do need **both** emitter and receiver

![](https://cdn-learn.adafruit.com/assets/assets/000/021/674/medium800/sensors_2167_quarter_ORIG.jpg?1418244774)

The receiver is on the left, it has three wires. The transmitter is on the right, it has two wires

# IR Breakbeam Sensors

## Arduino

Wiring these sensors for Arduino use is really easy.

First up you'll need to power the transmitter. Connect the black wire to ground and the red wire directly to 3.3V or 5V power. It will draw 9mA from 3.3V (lower power) and 20mA from 5V (better range)

Next up you'll want to connect up the receiver. Connect the black wire to ground, the red wire to 3.3V or 5V (whichever logic level you like) and then the white or yellow wire to your digital input.

Note that you do not _have_ to share power supply ground or power between the two, the 'signal' is sent optically.

The receiver is **open collector** which means that you do need a pull up resistor. Most microcontrollers have the ability to turn on a built in pull up resistor. If you do not, connect a 10K resistor between the white wire of the receiver and the red wire.

On an Arduino, we'll connect the signal (yellow/white) pin to Digital #4

![](https://cdn-learn.adafruit.com/assets/assets/000/021/675/medium800/sensors_Irbreak.jpg?1418245185)

Run this demo code on your Arduino

```auto
/* 
  IR Breakbeam sensor demo!
*/

#define LEDPIN 13
  // Pin 13: Arduino has an LED connected on pin 13
  // Pin 11: Teensy 2.0 has the LED on pin 11
  // Pin  6: Teensy++ 2.0 has the LED on pin 6
  // Pin 13: Teensy 3.0 has the LED on pin 13

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);      
  // initialize the sensor pin as an input:
  pinMode(SENSORPIN, INPUT);     
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
  
  Serial.begin(9600);
}

void loop(){
  // read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }
  
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}
```

With the above wiring, when you put you hand between the sensor pair, the onboard LED will turn on and the serial console will print out messages:

![](https://cdn-learn.adafruit.com/assets/assets/000/021/676/medium800/sensors_breakserial.png?1418245524)

Warning: Note that sunlight has infrared in it. The setup will work best in a darkened room where most or all the infrared light is from the emitter. If you need to use a sensor in a ambient light environment, you'll likely want to use a different type of sensor such as a distance measuring device or possibly a passive infrared (PIR) sensor.

# IR Breakbeam Sensors

## CircuitPython

It's easy to read a break beam sensor from CircuitPython code using [built-in digital input/output capabilities](../../../../circuitpython-digital-inputs-and-outputs).

First wire up a break beam transmitter and receiver just like you would for an Arduino.&nbsp; Here's an example of wiring to a Feather M0:

![](https://cdn-learn.adafruit.com/assets/assets/000/048/996/medium800/proximity_m0_break_beam_bb.png?1512770592)

- **Board 3V** (or 5V if your board has it) to **both** receiver and transmitter **red wire**.
- **Board GND&nbsp;** to&nbsp; **both&nbsp;** receiver and transmitter&nbsp; **black wire.**
- **Board D5&nbsp;** (or any other digital input) to receiver&nbsp; **yellow wire.**

Next&nbsp;[connect to the board's serial REPL&nbsp;](../../../../micropython-basics-how-to-load-micropython-on-a-board/serial-terminal)so you are at the CircuitPython&nbsp; **\>\>\>** &nbsp;prompt.

Now import the **board** and **digitalio** modules that allow you to create a digital input.&nbsp; Be sure you've read the [CircuitPython digital I/O guide](../../../../circuitpython-digital-inputs-and-outputs) for more background too!

```auto
import board
import digitalio
```

Create a digital input for the pin connected to the receiver, D5 in this case:

```auto
break_beam = digitalio.DigitalInOut(board.D5)
break_beam.direction = digitalio.Direction.INPUT
break_beam.pull = digitalio.Pull.UP
```

Notice you set the **direction** property to input, and the **pull** property to a pull-up (just like the [digital I/O guide mentions](../../../../circuitpython-digital-inputs-and-outputs)).&nbsp; This is necessary to configure the digital input with an internal pull-up resistor so it always reads a good value from the break beam sensor.

Checking if the sensor detects a break is as easy as reading the **value** property of the digital input.&nbsp; When value is true it means the input is at a high logic level which occurs when the&nbsp;receiver can see the transmitter and the beam is not broken.&nbsp; However if you get a value of false the input is at a low logic level which means the&nbsp;receiver cannot see the transmitter and the beam is broken!

Try reading the&nbsp;value with nothing blocking the transmitter and receiver:

```auto
break_beam.value
```

![](https://cdn-learn.adafruit.com/assets/assets/000/048/997/medium800/proximity_Screen_Shot_2017-12-08_at_2.14.32_PM.png?1512771317)

Now put something large and opaque, like your hand, in front of the transmitter to block the light.&nbsp; Read the value again:

```auto
break_beam.value
```

![](https://cdn-learn.adafruit.com/assets/assets/000/048/998/medium800/proximity_Screen_Shot_2017-12-08_at_2.14.44_PM.png?1512771327)

Awesome!&nbsp; Notice the digital input value was true, or at a high logic level, when nothing was blocking the beam.&nbsp; As soon as your hand covered the beam the input value turned false, or low logic level, to indicate an obstruction.

You can put all of this together into a complete program that prints a message whenever the beam is blocked.&nbsp; Save this as **main.py** on your board and examine the serial monitor for output, a message is printed when the beam is blocked:

```auto
import time

import board
import digitalio


# Create digital input with pull-up resistor on pin D5
# for break beam sensor.
break_beam = digitalio.DigitalInOut(board.D5)
break_beam.direction = digitalio.Direction.INPUT
break_beam.pull = digitalio.Pull.UP

# Main loop runs forever and prints a message once a second
# while the sensor is blocked/broken.
while True:
    if not break_beam.value:
        # Break beam input is at a low logic level, i.e. broken!
        print('Beam is broken!')
    time.sleep(1.0)  # Delay for 1 second and repeat again.
```

That's all there is to reading a beam break sensor with CircuitPython!


## Featured Products

### IR Break Beam Sensors with Premium Wire Header Ends - 3mm LEDs

[IR Break Beam Sensors with Premium Wire Header Ends - 3mm LEDs](https://www.adafruit.com/product/2167)
Infrared (IR) break-beam sensors are a simple way to detect motion. They work by having an emitter side that sends out a beam of human-invisible IR light, then a receiver across the way which is sensitive to that same light. When something passes between the two, and its not transparent to IR,...

In Stock
[Buy Now](https://www.adafruit.com/product/2167)
[Related Guides to the Product](https://learn.adafruit.com/products/2167/guides)
### IR Break Beam Sensor with Premium Wire Header Ends - 5mm LEDs

[IR Break Beam Sensor with Premium Wire Header Ends - 5mm LEDs](https://www.adafruit.com/product/2168)
Infrared (IR) break-beam sensors are a simple way to detect motion. They work by having an emitter side that sends out a beam of human-invisible IR light, then a receiver across the way which is sensitive to that same light. When something passes between the two, and its not transparent to IR,...

In Stock
[Buy Now](https://www.adafruit.com/product/2168)
[Related Guides to the Product](https://learn.adafruit.com/products/2168/guides)

## Related Guides

- [Proximity Based Lighting](https://learn.adafruit.com/proximity-based-lighting.md)
- [No-Code WipperSnapper Summoning Horn](https://learn.adafruit.com/adafruit-io-wippersnapper-summoning-horn.md)
- [Adafruit VL53L4CX Time of Flight Distance Sensor](https://learn.adafruit.com/adafruit-vl53l4cx-time-of-flight-distance-sensor.md)
- [PIR Motion Sensor](https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor.md)
- [Ultrasonic Ruler](https://learn.adafruit.com/ultrasonic-ruler.md)
- [Adafruit VCNL4020 Proximity and Light Sensor](https://learn.adafruit.com/adafruit-vcnl4020-proximity-and-light-sensor.md)
- [PropMaker Jack O'Lantern](https://learn.adafruit.com/propmaker-jack-o-lantern.md)
- [Matrix Portal Money-Sensing Tip Jar](https://learn.adafruit.com/matrix-portal-money-sensing-tip-jar.md)
- [Fog Machine with Motion Sensor and Adafruit IO](https://learn.adafruit.com/fog-machine-remote-trigger.md)
- [Adafruit APDS9960 breakout](https://learn.adafruit.com/adafruit-apds9960-breakout.md)
- [Tombstone Prop-Maker RP2040](https://learn.adafruit.com/tombstone-prop-maker-rp2040.md)
- [Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout](https://learn.adafruit.com/adafruit-vl53l0x-micro-lidar-distance-sensor-breakout.md)
- [Using VCNL4010 Proximity Sensor](https://learn.adafruit.com/using-vcnl4010-proximity-sensor.md)
- [Screaming Cauldron](https://learn.adafruit.com/screaming-cauldron.md)
- [No-Code Room Occupancy Status ](https://learn.adafruit.com/no-code-room-occupancy-status.md)
