---
title: "The IR Distance Sensors"
date: "2021-06-18"
categories: ["All", "Guides"]
tags: []
excerpt: "Learn what the IR distance sensors are and how to code them."
featuredImage: "/images/resources/ir-sensor.gif"
---

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1624056955058-9NJ754KNZ5MCZ87ZHLL3/spinning+annotated+version.gif)

There are two infrared (IR) distance sensors located on the Rover. One on either side.

**What Do They Do?**

The IR sensors gives data on the distance between the robot the object the sensor is facing. There is a sensor on each side of the Rover so we can detect if there are any obstacles (like walls) near the sides of the Rover.

The IR sensors work differently to the front Ultrasonic distance sensor. If you’re curious about how the Ultrasonic sensor works read our [ultrasonic sensor blog post](/resources/ultrasonic-sensor)

**What is IR?**

Before we jump into how the IR sensors work, let’s look at what IR actually is. IR stands for infrared. Infrared is a type of light. Human eyes cannot see **infrared light**. The light we see from the sun & light bulbs is called **visible light**. **Visible light** and **infrared light **are different types of light.

All types of light are made up of something called electromagnetic waves. The science behind electromagnetic waves and how we see them is complicated, for now all we need to know is that they exist and they’re all around us. Electromagnetic waves vary in length, similar to how there are big waves and small waves at the beach.  The diagram below shows the entire range of electromagnetic wave lengths.

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1624242877647-DBR3PP2LNB8TKWQJ8MZQ/my+spectrum+diagram.png)

**Visible light** is electromagnetic waves with a length of 400–700 nanometres (nm). **Infrared light** is electromagnetic waves with a length of 700nm to 1mm.

You can see in the diagram, there are a bunch of different types of electromagnetic wave ranges like radio, ultraviolet and gamma ray. The human eye can only perceive electromagnetic waves in the **Visible light** range but that doesn’t mean the other electromagnetic waves don’t exist. Lots of cool technology uses electromagnetic waves from all over the spectrum.

**Who Can See Infrared Light?**

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1624259558972-9DK7XAQOW0SPZFW8DIFC/snake+joke.png)

Howard the snake loves code

There are some snake species that can detect infrared light but they don’t use their eyes. They have small pits on their faces that are sensitive to infrared light. They use this ability to hunt for prey. The infrared waves produced by the body heat of the snake’s prey is what the snake is able to detect.

Although we cannot see it with our eyes, just like snakes we can use build devices to emit and detect infrared light. This is exactly what the IR sensor on our robot does.

Ok, now that we know a bit about IR, let’s look at how the IR sensors on the rover work.

### How do the IR Sensors Work?

The IR sensor uses a laser to detect how far away objects are. Yes the Rover uses two IR lasers. How do these lasers help the Rover detect distance? It uses the following process:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1624234434184-QRO7PVDGHKDS2LFKIFU2/ir+diagram+animation.gif)

- The IR sensor emits an IR laser outwards.

- The light travels out and bounces off of an object or surface.

- The reflected light is now traveling back to the sensor.

- The sensor detects the returning light.

**Based on how long it takes the light to reflect back, the sensor calculates how far away the object is using on the speed of light.**

**If the Rover’s IR Sensor Emits Infrared Lasers, Does that Make the Rover a Laser Gun?**

Well yes, but actually no.

### IR Problems & Limitations

**The IR Sensors are Limited to a distance of Roughly 1.25m**

The larger the distance the emitted IR laser has to travel, the more likely it is IR detector is unable detect it. Increasing the range of an IR sensor requires a more powerful laser.

The Rover’s built in IR sensor are designed to be efficient in small scale activities, you can always add your own electronics to the Rover and equip more powerful sensors. To read more on how to add your own electronics [see this blog post.](/resources/showcase-rover-and-arduino-line-follow)

### Programming the IR Sensors

Open up the Code Editor, connect to a Micromelon Rover and open up the sensor view. To open sensor view, click the ROVER button next to your rover’s name. If you need a refresher on how to use the rover & code editor, check out [this post.](/resources/getting-started-with-the-micromelon-rover)

In the sensor view dialog, the IR sensor values are located on the sides of the rover diagram. The left IR sensor value is on the left side, the right value is on the right. The CM next to the values indicates these are centimetre values. To make sure your sensor works, point the IR sensors at different objects and measure how far away they are.

**What does 255cm mean?**

255 is the returned value when an IR sensor is unable to determine an accurate value. If you are seeing 255 it means you may be attempting to detect objects further away than 1m.

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1623908456123-SFLGD6EJY3YZD9ICDIIR/opening+sensor+view.gif)

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1624249315328-XCV5AINWZLKUUJ15D6QW/using+sensor+mode++small.gif)

**Using The IR Sensors In Code**

Let’s write a simple program to print our left IR sensor values every second for 10 seconds.

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1624258875807-9MY12LCYZU1EWZGX5PXS/writing+a+piece+of+code+left+only.gif)

- Start by adding a **Repeat 10 Times **block.

- Inside the loop, place a **Print **statement. You can find this in the **Utilities **category.

- Place the **Read IR Distance** block inside our **Print** statement.

You can find the **Read IR Distance** block in the **Sensors **category. The python command is **IR.ReadLeft()**.

- Add a **Delay For 1 Second** block so this loop waits 1 second before running again.

- Press Play!

When we run this; we will see the left IR distance value every second for 10 seconds.

### Wrapping Up

Now that we’re familiar with what the IR distance sensors are, how they work and how to program them it’s time to start doing some activities. Try these activities that are done best using the IR sensors.

          Resources

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1647214830941-XGY83MWLD0GDNDSCOF33/driving+school.png)

  ](/resources/driving-school-basics)

	    Mar 14, 2022

                [Driving School Basics](/resources/driving-school-basics)

	    Mar 14, 2022

Learn the basics of rover motors and sensors while navigating Duck City.

                [Read More &#8594;](/resources/driving-school-basics)

	    Mar 14, 2022

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1707816086925-3340QDPKLWEWZY20T5HC/maze.png)

  ](/resources/maze)

	    Jun 4, 2021

                [Activity: Maze](/resources/maze)

	    Jun 4, 2021

Learn branching, iteration, algorithm design, and how to use the Rover’s motors, ultrasonic, colour, and IR sensors.

                [Read More &#8594;](/resources/maze)

	    Jun 4, 2021

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1707816124090-JAEYY3E5NGRDOMCLXOXE/Lane+guidance.png)

  ](/resources/lane-guidance)

	    Jun 3, 2021

                [Activity: Lane Guidance](/resources/lane-guidance)

	    Jun 3, 2021

Learn iteration, branching, algorithm design, maths, and how to use the motors, ultrasonic, and IR sensors.

                [Read More &#8594;](/resources/lane-guidance)

	    Jun 3, 2021

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1622686877131-P5FU2IDAB8U05PX8M98Z/sumo.png)

  ](/resources/sumo)

	    Jun 3, 2021

                [Activity: Sumo](/resources/sumo)

	    Jun 3, 2021

Learn branching, iteration, algorithm design, and how to use the Rover’s motors, colour, ultrasonic, and IR sensors.

                [Read More &#8594;](/resources/sumo)

	    Jun 3, 2021

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1622686289562-SMFP45KWR3TR66AGB1JW/surveybot.png)

  ](/resources/survey-bot)

	    Jun 3, 2021

                [Activity: Survey Bot](/resources/survey-bot)

	    Jun 3, 2021

Learn iteration while using the IR sensors.

                [Read More &#8594;](/resources/survey-bot)

	    Jun 3, 2021

### Related Posts

          Resources

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1639455538805-9NI7CPKR7IYG6D4RBKAN/high+quality+sumo.png)

  ](/resources/creating-a-sumo-unit)

	[All](/resources?category=All)

                [Creating A Sumo Unit For Your Digital Technologies Class](/resources/creating-a-sumo-unit)

	[All](/resources?category=All)

How to run a sumo unit in your digital technologies classroom.

                [Read More &#8594;](/resources/creating-a-sumo-unit)

	[All](/resources?category=All)

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1637464845856-BBPD7GTIIHHOSJCFORWF/the+ultrasonic+sensor.png)

  ](/resources/ultrasonic-sensor)

	[All](/resources?category=All), [Guides](/resources?category=Guides)

                [The Ultrasonic Sensor](/resources/ultrasonic-sensor)

	[All](/resources?category=All), [Guides](/resources?category=Guides)

Learn all about the ultrasonic sensor!

                [Read More &#8594;](/resources/ultrasonic-sensor)

	[All](/resources?category=All), [Guides](/resources?category=Guides)

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1625616494179-LZHZP5MUA0E0MQKEB7HC/cropped+image+4.png)

  ](/resources/year-7-digital-tech-at-st-peters)

	[All](/resources?category=All), [Customer Stories](/resources?category=Customer+Stories)

                [Case Study: Year 7 Digital Tech at St Peters Lutheran College](/resources/year-7-digital-tech-at-st-peters)

	[All](/resources?category=All), [Customer Stories](/resources?category=Customer+Stories)

See how Meg Foley at St Peters conducted a challenge for their Year 7s using Micromelon.

                [Read More &#8594;](/resources/year-7-digital-tech-at-st-peters)

	[All](/resources?category=All), [Customer Stories](/resources?category=Customer+Stories)

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1637464675848-K9YEOYK65IWRFRX4QJ1R/blog+header+image+.png)

  ](/resources/robot-simulator)

	[All](/resources?category=All), [Getting Started](/resources?category=Getting+Started)

                [Getting Started With The Robot Simulator](/resources/robot-simulator)

	[All](/resources?category=All), [Getting Started](/resources?category=Getting+Started)

How to get started with the Micromelon Robot Simulator.

                [Read More &#8594;](/resources/robot-simulator)

	[All](/resources?category=All), [Getting Started](/resources?category=Getting+Started)

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1626837419969-WBB4D85KQIXSOP750N5M/resized+banner+%281%29.png)

  ](/resources/getting-started-with-the-micromelon-rover)

	[All](/resources?category=All), [Getting Started](/resources?category=Getting+Started)

                [Getting Started With The Micromelon Rover](/resources/getting-started-with-the-micromelon-rover)

	[All](/resources?category=All), [Getting Started](/resources?category=Getting+Started)

Crash course on basic rover function, how and what to program and starter activities to attempt.

                [Read More &#8594;](/resources/getting-started-with-the-micromelon-rover)

	[All](/resources?category=All), [Getting Started](/resources?category=Getting+Started)

      [

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/1622691207784-0X6JV7RYRQXZAFRG712S/pisonescape.png)

  ](/resources/prison-escape)

	[Activities](/resources?category=Activities), [All](/resources?category=All)

                [Activity: Prison Escape](/resources/prison-escape)

	[Activities](/resources?category=Activities), [All](/resources?category=All)

Learn branching and iteration using the colour sensors and motors.

                [Read More &#8594;](/resources/prison-escape)

	[Activities](/resources?category=Activities), [All](/resources?category=All)
