Sensing Light, Surfaces, and Obstacles

Session 2

Nanica.io
Teaching Sparki
5 min readSep 18, 2015

--

We will be exploring how Sparki can sense light, the darkness of the surface underneath it, and its distance from obstacles.. This includes teaching Sparki to avoid or follow light sources.

This session (number 2!) is designed to be finished in about 30–40 minutes.

Sparki has many features that you can use and experiment with. In this article, we will be exploring three types of Sparki’s sensors. They detect light, changes in surfaces, and obstacles in its path. Let’s get started!

LIGHT SENSORS. INFRARED REFLECTANCE SENSORS. ULTRASONIC DISTANCE SENSOR.

INFRARED REFLECTANCE SENSORS

Sparki has 5 infrared reflectance sensors underneath it. Each sensor has a phototransistor and LED (Light Emitting diodes).

These acts like a pair of eyes and flashlight but the flashlight shines infrared light which is invisible to a human eye. The more light the infrared receives, the higher the voltage Sparki will read from it. You can use it to sense if the surface underneath it is white or black.

The commands below return a number the sensor read in the form of an integer. A bright white paper is about 1000 while a black line is below 400. Going off the edge of a surface is about 200. This can change depending on outside factors like how dark the room is and how far away the sensor is from the surface.

PROGRAMMING EXERCISE: Use the commands above to print the sensor readings from each of the five sensors and print it to the LCD screen. See how different surfaces affect the numbers.

There are different ways you can use these sensors such as edge avoidance and line following. We will explore this in session 3 and in succeeding sessions. :)

CODE FOR INFRARED REFLECTANCE SENSOR TEST

ULTRASONIC DISTANCE SENSOR

Sparki’s ultrasonic range finder measures distance with sound waves. It finds objects like a bat or dolphin does. One part of the sensor is a speaker that sends out a sound wave and the other part is a microphone that measures how long it takes for the object to come back. The longer it takes to come back, the further away the object.

Measure the time it takes for the sound to comeback, you can tell the distance. The speaker sends the object out in a cone, so only the objects within the cone will return sound and be detected. Objects in the center of the cone do best. This sensor cannot detect individual objects only how long it took the sound sent to return. It measures the closest object.

If an object absorbs sound, or if there is nothing for a long distance or if the object bounces the sound at an angle. There might not be enough sound returning to measure anything.

The command sparki.ping() returns the distance the sensor reads in centimeters in the form of a float.

PROGRAMMING EXERCISE: Write a program that will “PING” every three seconds with Sparki. Output the result on the LCD display.

EXPLORE ON YOUR OWN: Put a notebook in front of the sensor at varying distances. Measure the distances between Sparki and your hand with a ruler/measuring tape. Do the readings make sense?

EXPLORE ON YOUR OWN: Try putting a notebook far or at an angle, what angle and distance does the ultrasonic range finder fail? Can the sensor detect your pillow? How about a sponge?

CODE FOR ULTRASONIC RANGE FINDER TEST

LIGHT SENSORS

Sparki has three light sensors that detect the level of light that hits them. These sensors are phototransistors are connected to Sparki such that when more light hits them, the higher the voltage they send.

The commands below return the amount of light in terms of an integer. The more light, the higher the number. Each sensor is slightly different, so the same amount might not mean the same number for each sensor.

A) PROGRAMMING EXERCISE: PROGRAM SPARKI SUCH THAT IT FOLLOWS A FLASHLIGHT.

B) PROGRAMMING EXERCISE: PROGRAM SPARKI SUCH THAT IT AVOIDS A FLASHLIGHT.

Dark Turning
Light turning

CODE FOR LIGHT AVOIDANCE AND FOLLOWING

SUMMARY

In this session, we learned how the infrared reflectance sensors, light sensors and the ultrasonic rangefinder work. We used them to detect light, change in surfaces, and distance from potential obstacles. We also made a program such that Sparki will follow or avoid a light source.

In the next sessions we will use what we’ve learned about sensors and motors (session 1 and 2) to make Sparki autonomous. We will teach Sparki how to avoid walls and edges, follow lines, and push objects around!

--

--

Nanica.io
Teaching Sparki

The best and most fun way to learn and test your programming skills is with robots!