LEGO Mindstorms Class:  Lesson 3

Why Use Sensors?

1:  Keep the Robot on Track:

In Lesson 2 we saw that it can be difficult to get the robot to navigate around its environment by just telling it where it needs to go.  Part of the problem is that small variations in its path can become really big errors once the robot has travelled a long distance.  For example, if you are trying to get your robot to move straight ahead, and it is angled off path by just a little, it may only be a small amount off course after a few inches, but will be much further off course after it has travelled a few feet.  You can see this in the picture below.        

1) Starting off course by a small amount can grow to a big error in a short while.

We can fix some of this inaccuracy by having the robot get information about its surroundings via sensors.  This allows the robot to find out where it is by "seeing" what is around it, figure out if it is on the right path, and make corrections if needed.

2:  Allow the Robot to interact with users and its environment

Sometimes you want the Robot to respond to a person or thing that it meets, or to give you information about the outside world.  A robot will only know about its environment if it can sense it in some way.  With  the right sensors, a robot can tell if it has come across a dangerous object or what the conditions around it are like (for example how hot or cold it is).  For example, sensors allowed the Mars Rover, a robot that landed on the surface of Mars, to drive around the surface of the planet, without falling into a deep hole, or running into a big rock.  Sensors also allowed the robot to detect and communicate the conditions on Mars back to scientists on Earth.

The Touch Sensor and Bumper:

Today's lesson focuses on the touch sensor.  It is a simple device that can detect whether someone or something touches, releases or "bumps" the orange button on its front.  The button on the touch sensor is rather small, however, and it can be hard to press unless your robot is lined up perfectly with its target.  

To make the sensor easier to use, we will build and attach a "bumper" to it.   The bumper simply consists of a bar which will trigger the touch sensor when any part of it is pushed.  That way an object doesn't have to be touching the orange button directly to trigger the touch sensor.

Programming with the Touch Sensor:

The touch sensor can detect three different conditions.  It can notice if it has been:

[1] pressed (the orange button has been pushed inwards)
released (the orange has moved from being pushed in to its original position)
bumped (the orange button was pushed and released in a short period of time)

There are two main ways to program with the touch sensor.  The Touch Sensor block, checks in with the sensor, and waits to see if it has been triggered.


4) The Touch Sensor Block

The Wait for Touch Sensor will not do anything until the action specified (pressed, released or bumped) happens.


5) The Wait for Touch Sensor Block

 Now that we know how it works, let's add a touch sensor to our 5 Minute Bot from the last lesson, and program it to perform some tasks…

Adding a Touch Sensor to "5-Minute Bot" to make "Bumper Bot":

We Start with 5-minute Bot from last time.  (The motor wires have been removed to make it easier to see)

Attach the Touch Sensor to a Bumper:

Step 1:

Step 2:

Attach rails to 5-minute Bot:

Attach the Bumper to the 5-Minute Bot:

Attach the Wires (Connect the Touch Sensor to Port 1):

You're done with Bumper Bot!  

Programming Exercises:

Exercise 1:  Write a simple program to say "ouch" when the touch sensor is pressed and "hooray" when it is released.   The pseudocode (program steps written in plain language) for this program looks like this:

1)  We will want our program to repeat itself so that it can say "ouch"/"hooray" over and over again.  Most of the time when you repeat actions inside a program it's a good idea to use a loop.  


2) Wait for the touch sensor to get pressed.


3) Make an "ouch" sound.


4) Now wait for the touch sensor to get released

5) Say "hooray", and the loop will return you to the beginning of the cycle

Exercise 2:  Write your own program to have the "Bumper Bot" drive until the bumper hits something, then stop, back up about 6 inches, turn right and start over again.  The pseudocode would look something like this:

Challenges for You

Challenge 1:  See if you can get Bumper Bot drive up to a block, and stop when it touches it without knocking the block over.

Challenge 2:  Use the touch sensor to get Bumper Bot to knock a block off the edge of a low platform (your instructors will set one up for you) without falling over the edge of the platform itself.

Challenge 3: (Advanced!):  Have Bumper Bot travel to a wall, stop when it hits it and then travel backwards to the exact same starting place.  You need to use the rotation sensor and data wires to do this.