3. Infrared Emitter/Detector Tutorial


Your Bit:Bot has 2 TCRT5000 reflective optical sensors (infrared) on its bottom. If you have a look at the bottom of your Bit:Bot you will see them. The lighter blue one is an infrared emitter and the dark one is the infrared receiver. They are analogue devices so they would usually work in a similar way to the light detectors however when 4Tronix developed the Bit:Bot they decided to connect them to digital i/o pins so we will interact with them like a digital device. When they see a lot of IR reflected they will return a logic 0. When they see no IR reflected they will return a logic 1.

Dark colours will reflect less IR than light colours; this means that we can use these sensors to detect a black line on a white background. In this example we will create a program that will light the NeoPixles of the Bit:Bot when a black line is detected and the NeoPixles will alter their brightness depending on the ambient light. These sensors have no filters on them meaning that they’re susceptible to interference from the sun; because of this this type of IR sensor should not be used for object detection; either ultrasonic or IR TOF (infrared lasers) sensors would be a better solution for that.

Open Mu and start a new project; copy and paste the below code into the new project (its code from the previous example).

[pastacode lang=”python” manual=”%23%20IR%20sensor%20example%20for%20the%204tronix%20Bit%3ABot%20and%20BBC%20Micro%3ABit%0A%23%20Author%20David%20Bradshaw%202017%0A%23%20Demonstrates%20how%20to%20use%20the%20IR%20sensors%20TCR5000%0A%0Afrom%20microbit%20import%20*%0Aimport%20neopixel%20%20%23%20Neopixel%20Library%20so%20we%20can%20control%20the%20NeoPixels%20lights%0A%0Anp%20%3D%20neopixel.NeoPixel(pin13%2C%2012)%0A%0A%23%20Both%20light%20sensor%20are%20on%20the%20same%20pin%20so%20we%20also%20use%20a%20select%20pin%0AlightSensor%20%3D%20pin2%0AsensorSelect%20%3D%20pin16%0A%0A%23%20Set%20the%20pin%20number%20of%20the%20IR%20detectors%0AleftLineSensor%20%3D%20pin11%0ArightLineSensor%20%3D%20pin5%0A%0A%0Adef%20leftLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(0%2C%206)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()%0A%0A%0Adef%20rightLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(6%2C%2012)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()%0A%0A%0Adef%20setBrightness(minValue)%3A%0A%20%20%20%20sensorSelect.write_digital(0)%0A%20%20%20%20brightnessLeft%20%3D%20lightSensor.read_analog()%0A%20%20%20%20sensorSelect.write_digital(1)%0A%20%20%20%20brightnessRight%20%3D%20lightSensor.read_analog()%0A%0A%20%20%20%20brightness%20%3D%20int((brightnessLeft%20%2B%20brightnessRight)%20%2F%202)%0A%20%20%20%20brightness%20%3D%20int(brightness%20%2F%2025)%0A%20%20%20%20if(brightness%20%3C%20minValue)%3A%0A%20%20%20%20%20%20%20%20brightness%20%3D%20minValue%0A%20%20%20%20return%20brightness%0A%0Awhile%20True%3A” message=”Code from Previous Example to Light the LEDs” highlight=”” provider=”manual”/]

I have deleted the code from the program loop, I have added 2 variables for the left and right line sensors and I have changed the comments at the top slightly to reflect the new project.

Now we’re going to create a method that will take readings from both left and right IR sensors. Under the lightSense() method ensure that their is 2 blank lines with no white space and type;

[pastacode lang=”python” manual=”def%20lineDetector(side)%3A%20%20%23%20side%20%3D%3D%200%20is%20left%2C%20side%20%3D%3D%201%20is%20right%0A%20%20%20%20if(side%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20isLine%20%3D%20leftLineSensor.read_digital()%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20isLine%20%3D%20rightLineSensor.read_digital()%0A%20%20%20%20if(isLine%20%3D%3D%201)%3A%20%20%23%20Sensor%20can%20see%20the%20line%0A%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20False” message=”Just a Line Detector Function” highlight=”” provider=”manual”/]

I have introduced some new concepts here. Firstly we have used some conditional statements known as if statements (you would have seen these in the previous example for the setBrightness() method) and secondly our method returns a boolean value depending on what the IR detector sees.

The if statements are quite easy to understand; if the condition is true, for example if side is equal to 0 then execute the code indented underneath the if statement. If the condition is false then skip to the else clause. When writing if statements everything indented under the if statement is part of the true condition. You don’t have to have else clauses with statements if you don’t need them.

The boolean data type will hold one of 2 values either True of False. This method will return a True value if a line is detected and a False value if not. When writing conditional statements we use 2 equal signs rather than 1. This is so the Micro:Bit knows that we are comparing two values rather than assigning a value to a variable.

Now we have a method to interpret the data coming back from our IR detectors all we need to do is write some code for the program loop to make the LEDs light when no IR is reflected; when its over a non reflective surface such as a black line, or when the Bit:Bot is picked up.

Put the following code in the program loop;

[pastacode lang=”python” manual=”while%20True%3A%0A%20%20%20%20if(lineDetector(1)%20is%20True)%3A%0A%20%20%20%20%20%20%20%20rightLights(setBrightness(3)%2C%200%2C%200)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20rightLights(0%2C%200%2C%200)%0A%0A%20%20%20if(lineDetector(0)%20is%20True)%3A%0A%20%20%20%20%20%20%20leftLights(setBrightness(3)%2C%200%2C%200)%0A%20%20%20else%3A%0A%20%20%20%20%20%20%20leftLights(0%2C%200%2C%200)” message=”Decide Which LEDs to Light” highlight=”” provider=”manual”/]

Notice that where the light intensity is normally entered for the red colour component I have use the setBrightness() method instead with a minimum brightness of 3. This will make sure that the LEDs are dim in low light and bright in bright light. The above code will check each IR detector in turn to see if a line is detected and either turn lights off or on depending on the results.

Below is all of the code from this example;

[pastacode lang=”python” manual=”%23%20IR%20sensor%20example%20for%20the%204tronix%20Bit%3ABot%20and%20BBC%20Micro%3ABit%0A%23%20Author%20David%20Bradshaw%202017%0A%23%20Demonstrates%20how%20to%20use%20the%20IR%20sensors%20TCR5000%0A%0Afrom%20microbit%20import%20*%0Aimport%20neopixel%20%20%23%20Neopixel%20Library%20so%20we%20can%20control%20the%20NeoPixels%20lights%0A%0Anp%20%3D%20neopixel.NeoPixel(pin13%2C%2012)%0A%0A%23%20Both%20light%20sensor%20are%20on%20the%20same%20pin%20so%20we%20also%20use%20a%20select%20pin%0AlightSensor%20%3D%20pin2%0AsensorSelect%20%3D%20pin16%0A%0A%23%20Set%20the%20pin%20number%20of%20the%20IR%20detectors%0AleftLineSensor%20%3D%20pin11%0ArightLineSensor%20%3D%20pin5%0A%0A%0Adef%20leftLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(0%2C%206)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()%0A%0A%0Adef%20rightLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(6%2C%2012)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()%0A%0A%0Adef%20setBrightness(minValue)%3A%0A%20%20%20%20sensorSelect.write_digital(0)%0A%20%20%20%20brightnessLeft%20%3D%20lightSensor.read_analog()%0A%20%20%20%20sensorSelect.write_digital(1)%0A%20%20%20%20brightnessRight%20%3D%20lightSensor.read_analog()%0A%0A%20%20%20%20brightness%20%3D%20int((brightnessLeft%20%2B%20brightnessRight)%20%2F%202)%0A%20%20%20%20brightness%20%3D%20int(brightness%20%2F%2025)%0A%20%20%20%20if(brightness%20%3C%20minValue)%3A%0A%20%20%20%20%20%20%20%20brightness%20%3D%20minValue%0A%20%20%20%20return%20brightness%0A%0A%0Adef%20lineDetector(side)%3A%0A%20%20%20%20if(side%20%3D%3D%200)%3A%0A%20%20%20%20%20%20%20%20isLine%20%3D%20leftLineSensor.read_digital()%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20isLine%20%3D%20rightLineSensor.read_digital()%0A%0A%20%20%20%20if(isLine%20%3D%3D%201)%3A%20%20%23%20Sensor%20can%20see%20the%20line%0A%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20False%0A%0A%0Awhile%20True%3A%0A%20%20%20%20if(lineDetector(1)%20is%20True)%3A%0A%20%20%20%20%20%20%20%20rightLights(setBrightness(3)%2C%200%2C%200)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20rightLights(0%2C%200%2C%200)%0A%0A%20%20%20%20if(lineDetector(0)%20is%20True)%3A%0A%20%20%20%20%20%20%20%20leftLights(setBrightness(3)%2C%200%2C%200)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20leftLights(0%2C%200%2C%200)” message=”Full Code Example” highlight=”” provider=”manual”/]

The code is starting to look more complicated now. We have the main building blocks of our line following robot all we need now is a way to control the motors and to decide how the robot will behave when it either sees or doesn’t see a line. We will also need to think of what we can do to stop the robot from getting stuck if it gets confusing data back from the sensors.

Check your code and Flash it to the Micro:Bit, if you have errors copy and paste the code from above or download the files from the bottom of this page. Have a play with it and see how the Bit:Bot behaves. Pick your Bit:Bot up off the floor and the LEDs will come on because no IR will be reflected then point the IR detectors towards a bright window and you will see the LEDs turn off. This is because the IR light coming from the sun is interfering with the IR detectors. Finally with the LEDs on vary the amount of light getting to the light sensors and you will notice how the LEDs brightness changes.

Well done for completing this example. Now we can start having some fun in the next example with a line following robot utilising all the code we have developed up to now.

Code Files

The link below is the above code that can be downloaded and loaded into Mu.


For the code to work unzip the file, open in Mu and upload to the Micro:Bit.