Lesson 9

Finish line sensors

We have completed all the parts of the code we need for the racetrack’s finish line. Now it’s time to move onto the finish line.

Step 1: Arranging the code

Before we begin, let’s make our code easier to manage.

Our code is getting longer and longer as we continue. Luckily mBlock lets us divide our code into multiple parts, making your code more organized. It may sound a bit complicated, but it isn’t!

Open the My Blocks menu, create a new block, and name it “start” as shown in the video. Then, add it to your work area.

Our new block, “start,” is now saved in our My Blocks menu and added to our main work area as “define start!”

Next, we will move all our code for the starting line into the define start block.

Don’t worry, you will soon understand what we are doing!

Make sure that the define start block is placed below the main code!
The “when Arduino starts up” block should always be the highest block.

What have we accomplished?

We have created a new block named start. Then we “defined” it by moving all the code for the starting line into it.

In other words, the code for our starting line is the “definition” of our start block.


Now, if we use the start block in our main code, Arduino will follow the code inside our start block.

Use the start block as shown in the video:

When you upload the code, the race should still work the same as it did before. Only this time, our code is more organized.

As we make more of these blocks, you will see how much easier it is to organize your code this way.

Imagine our race program is a book. Our start block is a chapter, and our main code is the table of contents. We use the table of contents (main code) to place the chapters (blocks) in a particular order.

Now, let’s upload the code and make sure the race still works. Then, continue to step 2.


Step 2: Connecting the finish line

Connect the orange network cable from the race control unit to the finish line.
To keep the cable from getting in the way of the race, place the cable in the same way as the pictures below:


The finish line consists of the following components:

– 2 X 7-Segment digit display screens
– 2 X Infrared sensors
– Network cable connector


We will start by activating the 7-segment displays.

We call these displays “7-segment” because each digit consists of 7 small lines that can form a number from 0 to 9.

Step 3: Activating the 7-segment display

First, we need to configure the displays by telling Arduino which pins connect to them.
Once configured, we will be able to display digits on our 7-segment displays.

Make the changes to the code as shown in the video:

We label the screens “screen #1” and “screen #2,” one for each 7-segment display.

Step 4: Display digits

Let’s display the number 1 on screen #1. Then, the number 2 on screen #2.

Make the changes to the code according to the video:


Here is the result you should get. The left screen should show 2, and the right screen should show 1.

Now that we know how to use our 7-segment displays, lets move to the sensors.

We have two infrared sensors—one for each track. The sensors tell Arduino when a car crosses the finish line.

What is infrared light?

Infrared light is light that human eyes can’t see. But, infrared sensors can.
Countless electronic devices emit and detect infrared light, and you probably use them every day!

For example, a TV remote control has an infrared LED light that flashes when a button is pressed. Then, the TV has a sensor that detects the light from the remote.

The remote’s LED flashes differently for each button, and the TV is programmed to recognize the flashes.

How does our infrared sensor work?

Our sensor has two components, located one after the other:

1) Infrared led
2) Infrared sensor 

Remember the example about TV remotes? The remote control has an LED, and the TV has a sensor. But to detect our cars, we have the LED and sensor in the same place!

Why are the LED and sensor located at the same place?

For Arduino to see a car, the sensor needs to have different values when there is/isn’t a car at the finish line.

When the control module is on, the infrared LED will always shine infrared light. If there is no car at the finish line, the light will diffuse into the room. The infrared sensor will only be able to detect a little bit of this infrared light.

But if there is a car at the finish line, the infrared light will reflect off the car and back into the sensor, making the sensor detect a lot of infrared light!

We will tell Arduino to constantly check how much light each of our sensors is getting (we have one sensor for each track). Depending on the amount of light the sensor is detecting, we will determine if there is a car at the finish line!


Of course, our sensor will not know if a car caused a high amount of infrared light or if some other object did. But because we are not expecting any other objects to appear on the track, this is good enough!

Step 5: Print sensor value

Now that we understand how our sensors work let’s try “printing” their values on the 7-segment display. (‘print’ is a fancy was to say that our program will write or display data)

Make the changes to the code that you see in the video below. Then we will explore what it does!

What is happening here?

First, Arduino will read the value of the sensor on the right-hand side of the track. Then, Arduino will print the value on the right-hand side display.

After uploading the code and testing, the value you see in the display is the amount of infrared light detected by the sensor.

In our example, the display’s code is 61. The number you see may be different depending on your room’s lighting. (The sun and regular house lights also emit infrared light.)

Analog reading vs digital reading

When we were programming our button in lesson 8, we used the “read digital pin” block to read the button’s state. Now, we will use the “read analog pin” block. Let’s explore the difference.

When reading a digital pin, Arduino is getting one of two values: “0” or “1.” Which is perfect if we only want to know if the button was pressed (1) or not (0).

This time we are working with a more advanced sensor. We want to know how much light the sensor sees.

An Analog reading from our Arduino will return a value from 0 to 1023. This value is the amount of infrared light that the sensor is detecting. (0 = no light, 1023 = max light)

Such a large scale allows us to program our code to react to small changes in infrared light. But we can also react to big changes in infrared light (like a car on our race track!)

Stage 6: Continues printing

Right now, the sensor value only prints one time when Arduino starts up.

Let’s tell Arduino to print the value continuously by moving our sensor code inside the loop:

Watch this video for an example of how the printed value changes based on the amount of infrared light returned to the sensor:

Next lesson!

Now we know how to use 7-segment screens and infrared sensors! In the next lesson, we will program Arduino to use these components to declare the winner of the race!