
Introduction
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:
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):
Here are the parts again, but with pictures showing what you’d normally think of them as!
When the infrared light shines off the edge of the table, almost nothing comes back!
Programming It With Sparki

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <Sparki.h> // include the sparki library void setup() { } void loop() { int edgeLeft = sparki.edgeLeft(); // measure the left edge IR sensor int edgeRight = sparki.edgeRight(); // measure the right edge IR sensor int threshold = 200; // if below this value, no surface underneath if (edgeLeft < threshold) // if no surface underneath left sensor { sparki.moveBackward(5); sparki.moveRight(20); // turn right } if (edgeRight < threshold) // if no surface underneath right sensor { sparki.moveBackward(5); sparki.moveLeft(20); // turn left } sparki.moveForward(); // move forward delay(100); // wait 0.1 seconds } |

- 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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <Sparki.h> // include the sparki library int backwardDistance = 5; int angle = 20; void setup() { } void loop() { int edgeLeft = sparki.edgeLeft(); // measure the left edge IR sensor int edgeRight = sparki.edgeRight(); // measure the right edge IR sensor int threshold = 200; // if below this value, no surface underneath if (edgeLeft < threshold) // if no surface underneath left sensor { sparki.moveBackward(backwardDistance); sparki.moveRight(angle); // turn right } if (edgeRight < threshold) // if no surface underneath right sensor { sparki.moveBackward(backwardDistance); sparki.moveLeft(angle); // turn left } sparki.moveForward(); // move forward delay(100); // wait 0.1 seconds } |
