How to use the
Learning Portal

Learning Portal

Our hands-on, comprehensive lesson plans span a range of levels. Browse our free STEM and coding learning resources.

Marty Image

Guided By the Light

What you will do:

Instruct Marty to find the best lit area.

What you need to show:

That Marty can react to a light source and turn to where the light is. Then, when facing the light, Marty will walk forwards.


Artificial Intelligence - intelligence demonstrated by machines, as opposed to natural intelligence displayed by animals including that of humans.

Sensing Element - the part of the sensor which receives an input - like light, sound, or heat - and converts this into an electrical signal with a numerical value, which can then be measured.

One idea about artificial intelligence (1) is that it is based on a system of sensing, planning and acting. In our activity today, we are going to teach Marty to

  • sense something - light
  • plan a movement - turn to the light
  • act - walk toward the light.


Marty V2

A device with Marty the Robot V2 app

Planning paper and pencils

A light source, like the one built into a mobile phone.

Marty's light sensor installed on one foot

Remember this:

You may have used light sensors before. Marty can perform different movements with an if statement or more than one if statement: When the room is bright, Marty might wiggle his eyebrows and dance when the room is darker.

Can you think of different ways to have Marty move depending on the amount of light that is sensed?

Challenge 1:

Here is some code that will instruct Marty to wave one arm, depending on the level of light that is shining. Marty will only wave once and only if the condition is met. Can you write code to have Marty wave either their right or left arm depending on how bright the light is?

Have a think about what you would need to compare to allow for one arm to move with a certain amount of brightness and for the other arm to move with a different amount of brightness.

Compare your Marty to ours

We added a bit of code that tells Marty to walk away when the light becomes too bright.

What about having Marty turn, depending on where they sense the light? Take a close look at the light sensor installed on Marty's foot for a clue.

Click here for some ideas about the light sensor

The light sensor has three parts that stick up, they are often referred to as sensing elements. Notice that they point in slightly different directions. Now notice the block of code for the light sensor and that there are 3 numbers in the right-hand-side dropdown. Think about how you could use them to have Marty think about where the light is strongest.

Challenge 2:

The following code causes Marty's arms to move in a slightly different way.

Create this code in the app to see how Marty responds. In this example, we used the left and right sensing elements on the light sensor to identify if the light was stronger on Marty's left or right and instructed Marty to raise that arm.

Here is a video of Marty responding to the light.

Can you think of a way to have Marty turn themselves to the light instead of just raising an arm. More than that, can you think of a way to turn Marty to where the light is strongest and then have Marty move close to the light when they are facing the correct way?.

Here is a video of Marty turning and moving


Feedback (Here are some questions to think about):

What did you have to compare in order to control the movements?

Were you able to solve the challenges with one if statement or did you need more?

How did you help Marty decide when they were most accurately facing the light?

What did you enjoy most about the challenges?


Click to have a look at some suggested answers. These are not the only answers, they are only ideas for what you can show.

Challenge 1 ideas

This code shows a way to have Marty move different arms depending on the level of light, with an extra feature when the light was too bright. We used a nested conditional, which is an if statement inside an if statement. Two statements need to be true for some of the actions to occur. We could also have used logic blocks, instead.

Challenge 2 ideas

This code shows a way of determining which sensing element is receiving the most light. We used variables to store the values from the sensors to tidy up the code but you don\'t have to have this; you could use the sensing blocks to record what Marty senses and to compare that value from the different sensing element each time. After determining where the light is brighter, Marty turns in that direction and walks forward toward the light when the different sensors have a very similar value. This is a basic example of artificial intelligence. One thing we didn\'t include is what happens when the light is behind Marty. You will need to figure that one out.

Why do you think we used +2 and -2 as divisors in determining how far to turn?


PHAM, D. T., Introduction to AI Robotics, by R. R. Murphy, Bradford Book, MIT Press, Cambridge, MA, 466 pp., 2001, ISBN 0-262-13383-0, 2001 (Hardback, £34.50)