Revision: 36937
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 1, 2010 01:43 by digitalworld
Initial Code
/*
SRF02 sensor reader
language: Wiring/Arduino Reads data from a Devantech SRF02 ultrasonic sensor.
Should also work for the SRF08 and SRF10 sensors as well.
Sensor connections:
SDA - Analog pin 4
SCL - Analog pin 5
created 5 Mar. 2007
by Tom Igoe
*/
// include Wire library to read and write I2C commands:
#include <Wire.h> // Verificar libreria Wire
// the commands needed for the SRF sensors:
#define sensorAddress 0x70 //Constantes hexadecimales de memoria predefinida
#define readInches 0x50 //Constantes hexadecimales de memoria predefinidas
// use these as alternatives if you want centimeters or microseconds:
#define readCentimeters 0x51 //Constantes hexadecimales de memoria predefinidas
#define readMicroseconds 0x52 //Constantes hexadecimales de memoria predefinidas
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02 //Constantes hexadecimales de memoria predefinida
void setup()
{
// start the I2C bus
Wire.begin(); // inicia el programa por medio de la conexion
// open the serial port:
Serial.begin(9600); //Comunicacion para mostrar resultados en pantalla
}
void loop()
{
// send the command to read the result in inches:
// sendCommand(sensorAddress, 0x02, 0x1F); //Configura rango de medicion
//sendCommand(sensorAddress, 0x01, 0x10); //Establece una ganancia
sendCommand(sensorAddress, 0x00, 0x51); //Enviar al sensor un comando lectura centimetros //0x00, 0x51
// wait at least 70 milliseconds for a result:
delay(70);
// set the register that you want to reas the result from:
setRegister(sensorAddress, resultRegister);
// read the result:
int sensorReading = readData(sensorAddress, 2);
// print it:
Serial.print("Distancia: ");
Serial.print(sensorReading);
Serial.println(" Microsegundos");
// wait before next reading:
delay(70);
}
/*
SendCommand() sends commands in the format that the SRF sensors expect
*/
void sendCommand (int address, int Register, int command) { //int Register
// start I2C transmission:
Wire.beginTransmission(address);
// send command:
Wire.send(Register); //Register
Wire.send(command);
// end I2C transmission:
Wire.endTransmission();
}
/*
setRegister() tells the SRF sensor to change the address pointer position
*/
void setRegister(int address, int thisRegister) {
// start I2C transmission:
Wire.beginTransmission(address);
// send address to read from:
Wire.send(thisRegister);
// end I2C transmission:
Wire.endTransmission();
}
/*
readData() returns a result from the SRF sensor
*/
int readData(int address, int numBytes) {
int result = 0; // the result is two bytes long
// send I2C request for data:
Wire.requestFrom(address, numBytes);
// wait for two bytes to return:
while (Wire.available() < 2 ) {
// wait for result
}
// read the two bytes, and combine them into one int:
result = Wire.receive() * 256;
result = result + Wire.receive();
// return the result:
return result;
}
Initial URL
Initial Description
Initial Title
Arduino SRF02 SFR08 SFR10 sensor reader
Initial Tags
Initial Language
Other