Posted By

digitalworld on 12/01/10


Tagged

arduino Sensor Ultrasonic


Versions (?)

Arduino SRF02 SFR08 SFR10 sensor reader


 / Published in: Other
 

  1. /*
  2.  
  3. SRF02 sensor reader
  4.  
  5. language: Wiring/Arduino Reads data from a Devantech SRF02 ultrasonic sensor.
  6.  
  7. Should also work for the SRF08 and SRF10 sensors as well.
  8.  
  9. Sensor connections:
  10.  
  11. SDA - Analog pin 4
  12.  
  13. SCL - Analog pin 5
  14.  
  15. created 5 Mar. 2007
  16.  
  17. by Tom Igoe
  18.  
  19. */
  20.  
  21. // include Wire library to read and write I2C commands:
  22.  
  23. #include <Wire.h> // Verificar libreria Wire
  24.  
  25. // the commands needed for the SRF sensors:
  26.  
  27. #define sensorAddress 0x70 //Constantes hexadecimales de memoria predefinida
  28.  
  29. #define readInches 0x50 //Constantes hexadecimales de memoria predefinidas
  30.  
  31. // use these as alternatives if you want centimeters or microseconds:
  32.  
  33. #define readCentimeters 0x51 //Constantes hexadecimales de memoria predefinidas
  34.  
  35. #define readMicroseconds 0x52 //Constantes hexadecimales de memoria predefinidas
  36.  
  37. // this is the memory register in the sensor that contains the result:
  38.  
  39. #define resultRegister 0x02 //Constantes hexadecimales de memoria predefinida
  40.  
  41. void setup()
  42.  
  43. {
  44.  
  45. // start the I2C bus
  46.  
  47. Wire.begin(); // inicia el programa por medio de la conexion
  48.  
  49. // open the serial port:
  50.  
  51. Serial.begin(9600); //Comunicacion para mostrar resultados en pantalla
  52.  
  53. }
  54.  
  55. void loop()
  56.  
  57. {
  58.  
  59. // send the command to read the result in inches:
  60. // sendCommand(sensorAddress, 0x02, 0x1F); //Configura rango de medicion
  61. //sendCommand(sensorAddress, 0x01, 0x10); //Establece una ganancia
  62. sendCommand(sensorAddress, 0x00, 0x51); //Enviar al sensor un comando lectura centimetros //0x00, 0x51
  63.  
  64. // wait at least 70 milliseconds for a result:
  65.  
  66. delay(70);
  67.  
  68. // set the register that you want to reas the result from:
  69.  
  70. setRegister(sensorAddress, resultRegister);
  71.  
  72. // read the result:
  73.  
  74. int sensorReading = readData(sensorAddress, 2);
  75.  
  76. // print it:
  77.  
  78. Serial.print("Distancia: ");
  79.  
  80. Serial.print(sensorReading);
  81.  
  82. Serial.println(" Microsegundos");
  83.  
  84. // wait before next reading:
  85.  
  86. delay(70);
  87.  
  88. }
  89.  
  90. /*
  91.  
  92. SendCommand() sends commands in the format that the SRF sensors expect
  93.  
  94. */
  95.  
  96. void sendCommand (int address, int Register, int command) { //int Register
  97.  
  98.  
  99. // start I2C transmission:
  100.  
  101. Wire.beginTransmission(address);
  102.  
  103. // send command:
  104.  
  105. Wire.send(Register); //Register
  106.  
  107. Wire.send(command);
  108.  
  109. // end I2C transmission:
  110.  
  111. Wire.endTransmission();
  112.  
  113. }
  114.  
  115. /*
  116.  
  117. setRegister() tells the SRF sensor to change the address pointer position
  118.  
  119. */
  120.  
  121. void setRegister(int address, int thisRegister) {
  122.  
  123. // start I2C transmission:
  124.  
  125. Wire.beginTransmission(address);
  126.  
  127. // send address to read from:
  128.  
  129. Wire.send(thisRegister);
  130.  
  131. // end I2C transmission:
  132.  
  133. Wire.endTransmission();
  134.  
  135. }
  136.  
  137. /*
  138.  
  139. readData() returns a result from the SRF sensor
  140.  
  141. */
  142.  
  143. int readData(int address, int numBytes) {
  144.  
  145. int result = 0; // the result is two bytes long
  146.  
  147. // send I2C request for data:
  148.  
  149. Wire.requestFrom(address, numBytes);
  150.  
  151. // wait for two bytes to return:
  152.  
  153. while (Wire.available() < 2 ) {
  154.  
  155. // wait for result
  156.  
  157. }
  158.  
  159. // read the two bytes, and combine them into one int:
  160.  
  161. result = Wire.receive() * 256;
  162.  
  163. result = result + Wire.receive();
  164.  
  165. // return the result:
  166.  
  167. return result;
  168.  
  169. }

Report this snippet  

You need to login to post a comment.