{"id":39,"date":"2007-03-06T20:04:46","date_gmt":"2007-03-07T01:04:46","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/39"},"modified":"2007-08-07T09:23:46","modified_gmt":"2007-08-07T14:23:46","slug":"devantech-ultrasonic-ranger-reader","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/39\/","title":{"rendered":"Devantech SRF02, 08, 10 Ultrasonic Ranger reader"},"content":{"rendered":"<p>This code reads a Devantech SRF02 ultrasonic ranger (and probably an SRF08 and SRF10 as well)  It uses the Wire library for Wiring and Arduino.<\/p>\n<p><!--more--><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.tigoe.com\/pcomp\/code2\/wp-content\/uploads\/2007\/08\/srf02_arduino_schem.png\" alt=\"SRF02 sensor and Arduino schematic\" \/><\/p>\n<pre>\r\n\/*\r\n\r\n  SRF02 sensor reader\r\n\r\n language: Wiring\/Arduino Reads data from a Devantech SRF02 ultrasonic sensor.\r\n\r\n Should also work for the SRF08 and SRF10 sensors as well.\r\n\r\nSensor connections:\r\n\r\n SDA - Analog pin 4\r\n\r\n SCL - Analog pin 5\r\n\r\ncreated 5 Mar. 2007\r\n\r\n by Tom Igoe\r\n\r\n*\/\r\n\r\n\/\/ include Wire library to read and write I2C commands:\r\n\r\n#include <wire.h><\/wire.h>\r\n\r\n\/\/ the commands needed for the SRF sensors:\r\n\r\n#define sensorAddress 0x70\r\n\r\n#define readInches 0x50\r\n\r\n\/\/ use these as alternatives if you want centimeters or microseconds:\r\n\r\n#define readCentimeters 0x51\r\n\r\n#define readMicroseconds 0x52\r\n\r\n\/\/ this is the memory register in the sensor that contains the result:\r\n\r\n#define resultRegister 0x02\r\n\r\nvoid setup()\r\n\r\n{\r\n\r\n  \/\/ start the I2C bus\r\n\r\n  Wire.begin();\r\n\r\n  \/\/ open the serial port:\r\n\r\n  Serial.begin(9600);\r\n\r\n}\r\n\r\nvoid loop()\r\n\r\n{\r\n\r\n  \/\/ send the command to read the result in inches:\r\n\r\n  sendCommand(sensorAddress, readInches);\r\n\r\n  \/\/ wait at least 70 milliseconds for a result:\r\n\r\n  delay(70);\r\n\r\n  \/\/ set the register that you want to reas the result from:\r\n\r\n  setRegister(sensorAddress, resultRegister);\r\n\r\n\/\/ read the result:\r\n\r\n  int sensorReading = readData(sensorAddress, 2);\r\n\r\n  \/\/ print it:\r\n\r\n  Serial.print(\"distance: \");\r\n\r\n  Serial.print(sensorReading);\r\n\r\n  Serial.println(\" inches\");\r\n\r\n  \/\/ wait before next reading:\r\n\r\n  delay(70);\r\n\r\n}\r\n\r\n\/*\r\n\r\n  SendCommand() sends commands in the format that the SRF sensors expect\r\n\r\n *\/\r\n\r\nvoid sendCommand (int address, int command) {\r\n\r\n  \/\/ start I2C transmission:\r\n\r\n  Wire.beginTransmission(address);\r\n\r\n  \/\/ send command:\r\n\r\n  Wire.send(0x00);\r\n\r\n  Wire.send(command);\r\n\r\n  \/\/ end I2C transmission:\r\n\r\n  Wire.endTransmission();\r\n\r\n}\r\n\r\n\/*\r\n\r\n  setRegister() tells the SRF sensor to change the address pointer position\r\n\r\n *\/\r\n\r\nvoid setRegister(int address, int thisRegister) {\r\n\r\n  \/\/ start I2C transmission:\r\n\r\n  Wire.beginTransmission(address);\r\n\r\n  \/\/ send address to read from:\r\n\r\n  Wire.send(thisRegister);\r\n\r\n  \/\/ end I2C transmission:\r\n\r\n  Wire.endTransmission();\r\n\r\n}\r\n\r\n\/*\r\n\r\nreadData() returns a result from the SRF sensor\r\n\r\n *\/\r\n\r\nint readData(int address, int numBytes) {\r\n\r\n  int result = 0;        \/\/ the result is two bytes long\r\n\r\n\/\/ send I2C request for data:\r\n\r\n  Wire.requestFrom(address, numBytes);\r\n\r\n  \/\/ wait for two bytes to return:\r\n\r\n  while (Wire.available() &lt; 2 )   {\r\n\r\n    \/\/ wait for result\r\n\r\n  }\r\n\r\n  \/\/ read the two bytes, and combine them into one int:\r\n\r\n  result = Wire.receive() * 256;\r\n\r\n  result = result + Wire.receive();\r\n\r\n  \/\/ return the result:\r\n\r\n  return result;\r\n\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code reads a Devantech SRF02 ultrasonic ranger (and probably an SRF08 and SRF10 as well) It uses the Wire library for Wiring and Arduino.  \/* SRF02 sensor reader language: Wiring\/Arduino Reads data from a Devantech SRF02 ultrasonic sensor.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}