Chapter 6. understanding sensors

The LEGO MINDSTORMS EV3 set includes three types of sensors: Touch, Color, and Infrared. You can use these sensors to make your robot respond to its environment. For example, you can program your robot to make a sound when it sees you, avoid obstacles while driving, or follow colored lines. This part of the book will teach you how to create working robots that use these sensors.

To learn how to work with sensors, we’ll expand the EXPLOR3R robot by adding a bumper that detects obstacles using the Touch Sensor, as shown in Figure 6-1. Once you have a handle on how to make programs that use the Touch Sensor, you’ll learn how to use the other sensors in subsequent chapters.

The EXPLOR3R uses a bumper with a Touch Sensor to detect objects it runs into.

Figure 6-1. The EXPLOR3R uses a bumper with a Touch Sensor to detect objects it runs into.

LEGO MINDSTORMS robots can’t actually see or feel the way humans do, but you can add sensors to them so they can collect and report information about their environment. By designing programs that can interpret this sensor information, you can make your robots seem intelligent by having them respond to their environment. For instance, you could create a program that makes the robot say “blue” when one of its sensors sees a piece of blue paper.

Your EV3 set contains three sensors that you can attach to your robot (see Figure 6-2) as well as some built-in sensors. The Touch Sensor detects whether the red button on the sensor is pressed or released. The Color Sensor detects the color of a surface and the intensity of a light source, as you’ll see in Chapter 7. The Infrared Sensor (covered in Chapter 8) measures the approximate distance to a nearby object, and it receives signals from the infrared remote.

In addition, each motor in the EV3 set has a built-in Rotation Sensor to measure the motor’s position and speed, and the EV3 brick can detect which of its Brick Buttons are pressed (see Chapter 9).

You can see the measurements reported by each sensor by opening the Port View application on the Brick Apps tab of your EV3 brick, as shown in Figure 6-4. For the Touch Sensor, a measurement of 1 means that it’s pressed, while 0 indicates that it’s released.

You can use the buttons on the EV3 to navigate to measurements of the other sensors. At the bottom right of your screen (port 4), you should see the distance measurement of the Infrared Sensor (48% in this example). The two values at the top (–41 and 52) indicate the positions of the motors on ports B and C on your robot.

Some sensors can take more than one type of measurement. To see other measurements of the Infrared Sensor, for example, navigate to port 4, press the Center button, and choose a sensor mode. You’ll learn more about the meaning of each value as you read on.

If your robot is connected to the computer, you can also view sensor measurements on the Hardware Page in the EV3 software, as shown in Figure 6-5. Just use the method you find most convenient.

Now let’s look at how you can use these measurements in your programs. Let’s try using the Touch Sensor in a program that has the robot play a sound when the Touch Sensor is pressed.

Several programming blocks let you use sensors in your program, including the Wait, Loop, and Switch blocks. In this chapter, you’ll learn how each of these blocks work with the Touch Sensor, and the same principles apply to the other sensors in the EV3 set as well.

Earlier, you used a Wait block to pause the program for a set amount of time (say, five seconds). But you can also use a Wait block to pause a program until a sensor is triggered. For example, you can configure a Wait block to pause until the Touch Sensor is pressed by selecting the Touch Sensor mode, as shown in Figure 6-6.

After selecting this mode, you must also choose between Compare and Change mode. In Compare mode, you specify with the State setting whether the program should wait until the sensor is released (0), pressed (1), or bumped (2). If you choose bumped, the program waits for a press followed by a release.

In Change mode, the program waits until the state of the sensor changes: If the sensor is pressed when the block begins running, the program waits until it’s released. If it’s released at first, the program pauses until the sensor is pressed.

The Port setting lets you specify which input port your sensor is connected to (in this case, port 1). Finally, the Measured Value plug allows you to use the last sensor measurement later on in your program (we’ll get back to this in Part V of the book).

Create a new project called EXPLOR3R-Touch with a program called WaitForTouch, as shown in Figure 6-6. The mode of the Wait block is set to Touch Sensor – Compare – State. When you run the program, nothing will happen at first, but when you press the Touch Sensor (by pushing the bumper), the robot should say “Hello.”

Now keep the Touch Sensor pressed as you start the program again. The sound plays immediately because the Wait block has nothing to wait for—the sensor is already pressed.

Now that you’re familiar with the Touch Sensor and the Wait block, you’re ready to make some more exciting programs. The next program, TouchAvoid, will make the EXPLOR3R robot drive around a room and turn around when it feels something, such as a wall or chair, with its bumper. You can see an overview of the program in Figure 6-7.

You can accomplish each action in this diagram with one programming block. You’ll use a Move Steering block in On mode to turn on the motors and then use a Wait block to wait for the sensor to be pressed. (Note that while the program waits, the robot keeps moving forward.)

Once the sensor is triggered, you use a Move Steering block to reverse and then another to turn around, both in On For Rotations mode. After the robot turns around, the program returns to the beginning, which is why you must place the four blocks you use inside a Loop block configured in Unlimited mode. Create the program now, as shown in Figure 6-8.

So far we’ve used the Wait block in Compare mode to make the program pause until the Touch Sensor reaches a state of your choice (pressed or released). Now we’ll create a program with a Wait block in Change mode that will pause the program until the sensor state changes (either from released to pressed or from pressed to released). Create and run the WaitForChange program, as shown in Figure 6-9.

If the bumper is released when the program starts, the robot should drive until it hits an object and then stop. If the bumper is already pressed when the program starts, the robot should continue to try to go forward until the sensor is released and then stop.

For most programs, it’s best to use the Compare mode because doing so makes predicting your robot’s behavior easier. Regardless of the initial state of the sensor, the robot will always wait to change its behavior until the Touch Sensor reaches the state of your choice.

As you learned in Chapter 5, you can configure a Loop block to loop a certain number of times, loop for a specified number of seconds, or loop endlessly. You can also program a Loop block to stop repeating based on sensor input. For example, you can make your robot drive back and forth until the Touch Sensor is pressed. To configure the Loop block like this, select the Touch Sensor – State mode, as shown in Figure 6-10. As before, choose 1 in the Port setting.

Create the LoopUntilTouch program and run it to see how it works. You should notice that the program checks the sensor measurement only once each time the blocks inside the loop have completed. For the loop to end, the sensor will need to be pressed just after the robot moves forward. If the sensor is not pressed at this point in the loop, the robot moves back and forth once more before checking the state of the Touch Sensor again.

This is the expected behavior for the Loop block, but sometimes you’ll want the loop to end even if you don’t press the sensor at exactly the right time. To accomplish this, set the state setting to Bumped (2) and run the program again. In this configuration, the loop doesn’t check whether the Touch Sensor is pressed at the end of the loop; rather, it checks whether you bump (press and release) the sensor at any time during the loop. If you do, the blocks should stop repeating after they complete the current run. (The EV3 continuously monitors the state of the Touch Sensor while the loop runs so that you don’t have to worry about it.)

You can use a Switch block to have a robot make a decision based on a sensor measurement. For example, you can make your robot drive backward if the Touch Sensor is pressed or say “No Object” when it’s not pressed, as shown in Figure 6-11.

The Switch block checks whether a given condition (such as “The Touch Sensor is pressed”) is true or false, as shown in Figure 6-12.

The Switch block in this example contains two sequences of blocks; the switch decides which sequence to run based on whether the condition is true or not. If the condition is true, the block in the upper part of the switch is run, and the robot moves backward; if the condition is false, the lower blocks are run, and the robot should say “No Object.”

There’s no limit to the number of blocks you can place inside a Switch block. If one part of a switch has multiple blocks, they’re simply run one by one. You can also leave one of the two parts of a Switch block empty, as shown in Figure 6-14.

Run this modified program to see what happens. If the condition is true (the bumper is pressed), the robot should say “Object” and move backward, and the program should continue by playing the tone. If the condition is false (the sensor is not pressed), the program will find no blocks in the lower part of the switch and instantly move on to the Sound block after the switch.

If you use sensor input in any programs with Wait, Loop, and Switch blocks, you often have to choose among Compare, Change, and Measure when configuring a block’s mode. You’ll see more examples of these modes as you read on, but let’s take some time to look at them side by side to see how they work.

The text usually makes clear which mode you should use for the example programs in this book, but all of the information you need is also visible in the programming diagrams. If you’re not sure which mode to choose, just look at the icons on each block, as shown in Figure 6-18.

Once you’ve selected the sensor (1) and made a choice of Compare, Change, or Measure (2), you choose the sensor operation mode (3). The Touch Sensor measures just one thing (the state of the red button), but the Color Sensor has three operation modes, as shown in Figure 6-18. As you practice with each mode in the next chapters, you’ll be able to create your own programs with sensors in no time.

In this chapter, you’ve learned that robots use sensors to gather input from their environment. You’ve also learned how to create programs for robots with sensors using Wait, Loop, and Switch blocks.

You’ve been using only the Touch Sensor so far, but you can use all the programming techniques you’ve learned in this chapter when working with the other sensors. You’ll continue with the Color Sensor in Chapter 7, you’ll learn about the Infrared Sensor in Chapter 8, and you’ll meet the built-in Rotation Sensors and the Brick Buttons in Chapter 9. Before you continue, practice your sensor-programming skills by solving these Discoveries.