Saturday, October 31, 2015

Sharp GP2Y0A02YK0F IR Distance Sensor (20-150 cm) Arduino Library

Distance Measuring Options


If you are building an autonomous robot then you need to have some sort of obstacle avoiding sensors. I have attached a Parallax PING ultrasonic sensor to the front of AVA using a servo (so that I can scan 180 degrees). Ultrasonic sensors are generally pretty accurate but since they use reflected sound to calculate distance, they don't perform well if the obstacle is sound absorbing. Ultrasonic sensors can also miss thin objects or objects that reflect the sound away from the sensor. However, the range of ultrasonic sensors is much better than IR. For the PING, the available sensing range is 2 cm to 3 m.

To address the ultrasonic issues, I also mounted a Sharp IR Distance Sensor (GP2Y0A02YK0F) above the PING. The IR sensors don't perform well outside but indoors there accuracy is good enough as long as you stay within the quoted detection limits. IR sensors are generally cheaper than ultrasonic, their beams are more directional (narrower) and reflectivity of the surface is more important than the sound absorbing properties of potential obstacles.

Putting the two sensors together is complementary and allows the short comings of both sensors to be addressed (to an extent).

Sharp GP2Y0A02YK0F IR Distance Sensor (20-150 cm) 



Sharp manufactures a range of IR Distance Sensors. For the front sensor I selected the GP2Y0A02YK0F, which has a usable detection range of 20 to 150 cm's.

The Sharp GP2Y0A02YK0F measures distances in the 20–150 cm range using a reflected beam of infrared light.  By using triangulation to calculate the distance measured, this sensor can provide consistent readings that are less influenced by surface reflectivity, operating time, or environmental temperature.  The Sharp GP2Y0A02YK0F outputs an analog voltage corresponding to the distance to the reflecting object.

If you have a look at the GP2Y0A02YK0F datasheet, you will see that the analog voltage output does not have a linear relationship to distance. You can also see that the values go crazy below about 20 cm.


Noah over at the Arduino Mega Blog has reversed engineered this plot to work out the relationship between distance and the output voltage.

distance = 10650.08 * sensorValue ^ (-0.935) - 10 cm

Sharp GP2Y0A02YK0F IR Distance Sensor Arduino Library


To connect to an Arduino and get a distance you could just use Noah's formula above, but sometimes it is easier to wrap the complexity up in a library. I did a search and didn't find an existing library, so I decided to do one myself. Partly because I haven't done one before.

I did find a library for the GP2Y0A21YK IR Distance sensor (10 - 80 cm), but the characteristics must be different to the GP2Y0A02YK0F as the distances provided by the library are way off. Noah's formula on the other hand, provides very good correlation with the distances measured by the PING. For consistency, I based my library on what jeroendoggen did for his.

You can download the Sharp GP2Y0A02YK0F IR Distance Sensor (20-150 cm) Arduino Library files, and then follow these instructions to use it:

Instructions:

  • Create a directory called GP2Y0A02YK0F within the libraries sub directory where your Arduino sketches are saved.
  • Copy GP2Y0A02YK0F.h, GP2Y0A02YK0F.cpp and keywords.txt into the GP2Y0A02YK0F directory.
  • Within the GP2Y0A02YK0F directory, create a sub directory called examples.
  • Copy DisplayCM.ino into the examples sub directory.
  • Restart the Arduino IDE to see the new library.

Sharp GP2Y0A02YK0F Mounted on AVA


The following photo shows the Sharp IR sensor mounted above the front PING on AVA. I have fitted a sensor shield to the Arduino Mega which makes it very easy to connect the various sensors to the micro controller.



Friday, October 23, 2015

Connecting the Duinotech 3 Axis Compass to an Arduino

1. Duinotech 3 Axis Compass Magnetometer Module


At a recent mini maker faire, I purchased the duinotech 3 Axis Compass Magnetometer Module (Part Number: XC-4496) from the Jaycar Electronics stand. I thought this would be handy for calculating heading information for AVA the robot.

Unfortunately, the module comes with no instructions and just these very brief specifications:
  • Operating Voltage: 5VDC (or 3.3 VDC)
  • Resolution: 12 bits
  • Interface: I2C
  • Includes: 5V to 3.3V level shifter (so you can operate it from 5 VDC)
  • Dimensions: 20(L) x 16(W) x 5(H) mm
We know that the module communicates via I2C but without knowing the module address or the available instructions, this isn't much use. No problem, I'll head over to the Jaycar website and look up the technical specs. Bzzzzzzzt! If you click on the link above, you will see the only information provided is the specifications printed on the packaging. It appears that Jaycar are moving away from Freetronics for their Arduino compatible boards, shields and sensors to duinotech a Chinese manufacturer. I assume they are doing this based on cost, the Freetronics stuff is good quality and very well documented. 

2. Honeywell HMC5883L 3-Axis Digital Compass IC


If you have good eye sight you will see HMC5883L printed on the PCB. A quick google will determine that this is a 3-Axis Digital Compass IC produced by Honeywell. The HMC5883L Data Sheet is very comprehensive and requires everything that you need to interface the chip to an Arduino via I2C. The duinotech module is apparently a break out board for the HMC5883L, similar to the one produced by SparkFun. It is in fact an improvement on the SparkFun version in that it can be driven from 5VDC and the interrupt line is also broken out.

3. HMC5883L Arduino Library


Armed with the data sheet you could work out how to get the module talking to an Arduino but as usual, a number of people have already done the hard work for you. I ended up using a library written by Korneliusz Jarzebski, a Polish chap. The HMC5883L Arduino Library is accompanied by a tutorial, which is in Polish but google translate does a pretty good job of converting this to English.

One thing that the library is missing is the keyword definition file which the Arduino IDE uses to colour highlight defined library names and methods. I have produced a keywords.txt file for the Jarzebski HMC5883L Library which you are free to download and use. It includes all of the public methods exposed by the HMC5883L class.

To use the library, create a folder called HMC5883L in the Arduino libraries folder. Within this copy the HMC5883L.h and HMC5883L.cpp files from the Jarzebski Github repository and the keywords.txt file. Also create a sub folder called examples and copy the HMC5883L_calibrate,  HMC5883L_compass, and HMC5883L_simple files into it. You will need to restart the Arduino IDE to see the new library. The HMC5883L_simple sketch example shows how to use the library and test your module.

Here is a quick video which shows the HMC5883L mounted on my robot AVA. On the OLED screen you can see when the compass is connected and the observed heading when the text scrolls up.