Edge Avoidance

Teach Sparki how to avoid edges and stay inside a shape


Lessons You Should Know


In this tutorial, you’ll learn how to make Sparki avoid falling off a table using the sensors on the underside of the robot:

What You’ll Need

  • A Sparki
  • A table (preferably a light colored non-transparent one).

How It Works

Sparki has an array of infrared sensors underneath that it uses to detect if there is anything underneath it. We’re going to program Sparki so that it knows it should stop moving and turn around if it detects something underneath it. This will help Sparki not fall off tables.Here you can see the sensors in the bottom of the robot:lineSensors.png

Infrared reflectance sensors on Sparki’s underside

Infrared reflectance sensors can be thought of as infrared light (which humans can not see), and some special eyes that look at that light. The eyes are made out of a photo-sensible material (a material which changes its electrical properties in response to changes in the incident light) and an LED (or Light Emitting Diode):

Infrared Sensor Diagram

Here are the parts again, but with pictures showing what you’d normally think of them as!

Infrared Sensor Diagram


When the infrared light shines off the edge of the table, almost nothing comes back!

Programming It With Sparki

Warning_Triangle Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki.This code requires integers, variables, basic programming, wheel code, if statements and infrared reflector sensors.The actual code is relatively simple.  We set a threshold beyond which we assume so little light is coming back that we are shining the lights off the table. Then, if the right sensor sees that, we go left, and if the left sensor sees that, we go right. After that we go forward. Looping this behavior causes Sparki to wander around a table without falling off.If you’re having trouble getting Sparki to tell the difference between the table and the edge, consider readjusting your threshold variable. Here is the code:Great job! You have now programmed Sparki to avoid running off edges!Taking a look at the previous code, there are two important numeric values that could be useful to experiment with:
  • The angle belonging to the turnLeft and turnRight commands, which has a value of 20 (degrees).
  • The distance belonging to the moveBackward command, which has a value of 5 (centimeters).
 So, as these are values that are used more than once, it could be a good idea to store them into a variable instead of typing the literal numbers each time. This will also let you experiment easily with these values, modifying them in order to see how the robot behavior changes: 

Extra Activities

As you might figure out, this code really does not like corners as those will trigger both of the sensors at the same time! Can you think of some code modifications that would help Sparki not get stuck on corners?  To learn more about the infrared sensors and other similar activities, take a look to the following lessons: