SRF02超声波传感器

产品概述

技术规格

管脚定义

<File:srf02.jpg>

连线图(I2C模式)

样例代码(I2C模式)

    /*
    Sample code for test the SRF02 with the I2C mode based on Arduino UNO!
    Command for reference:http://robot-electronics.co.uk/htm/srf02techI2C.htm
    Connection:
    SRF02       Arduino
    5v Vcc    -> 5V
    SDA       -> A4
    SCL       -> A5
    Mode      -> no connection
    0v Ground -> GND
    updated by youyou.
    20140418
    */
    #include <Wire.h>

    void setup()
    {
      Wire.begin();                // join i2c bus (address optional for master)
      Serial.begin(9600);          // start serial communication at 9600bps
    }

    int reading = 0;

    void loop()
    {
      // step 1: instruct sensor to read echoes
      Wire.beginTransmission(112); // transmit to device #112 (0x70)
      // the address specified in the datasheet is 224 (0xE0)
      // but i2c adressing uses the high 7 bits so it's 112
      Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)
      Wire.write(byte(0x51));      // command sensor to measure in "centimeters" (0x51)
      // use 0x51 for centimeters
      // use 0x52 for ping microseconds
      Wire.endTransmission();      // stop transmitting

      // step 2: wait for readings to happen
      delay(70);                   // datasheet suggests at least 65 milliseconds

      // step 3: instruct sensor to return a particular echo reading
      Wire.beginTransmission(112); // transmit to device #112
      Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
      Wire.endTransmission();      // stop transmitting

      // step 4: request reading from sensor
      Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

      // step 5: receive reading from sensor
      if (2 <= Wire.available())   // if two bytes were received
      {
        reading = Wire.read();  // receive high byte (overwrites previous reading)
        reading = reading << 8;    // shift high byte to be high 8 bits
        reading |= Wire.read(); // receive low byte as lower 8 bits
        Serial.print(reading);   // print the reading
        Serial.println("cm");
      }

      delay(250);                  // wait a bit since people have to read the output :)
    }

连线图(UART模式)

样例代码(UART模式)

    /*
    Sample code for test the SRF02 with the UART mode based on Leonardo!
    Command for reference:http://robot-electronics.co.uk/htm/srf02techSer.htm
    Connection:
    SRF02       Arduino
    5v Vcc    -> 5V
    Rx        -> 1(TX)
    Tx        -> 0(RX)
    Mode      -> GND
    0v Ground -> GND
    Designed by youyou.
    20140418
    */

    void SendCmd(unsigned char address,unsigned char cmd)
    {
      Serial1.write(address);//set the address of SRF02(factory default is 0)
      delayMicroseconds(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit
      Serial1.write(cmd);//send the command to SRF02
      delayMicroseconds(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit
    }
    void setup(void)
    {
      Serial.begin(9600);
      Serial1.begin(9600);
      Serial.println("SRF02 TEST!");
    }
    void loop(void)
    {
      unsigned int reading;
      SendCmd(0x00,0x51);//Real Ranging Mode - Result in centimeters
      delay(70);//time for SRF02 to measure the range
      SendCmd(0x00,0x5E);//Get Range, returns two bytes (high byte first) from the most recent ranging.
      delay(10);//wait for some time,let the Arduino receive 2 bytes data from the TX pin of SRF02
      if(Serial1.available()>=2)//if two bytes were received
      {
        reading = Serial1.read()<<8;//receive high byte (overwrites previous reading) and shift high byte to be high 8 bits
        reading |= Serial1.read(); // receive low byte as lower 8 bits
        Serial.print(reading); // print the reading
        Serial.println("cm");
      }
      delay(250); // wait a bit since people have to read the output :)
    }

Arduino Leonardo的串口1让串口设备使用起来很方便。当然,如果你没有Leonardo板子,你也可以使用Arduino UNO软串口。只需要把以上代码的 "Serial1" 改成 "mySerial" ,然后在代码开头处加上这些语句:

#include <SoftwareSerial.h>  

SoftwareSerial mySerial(10, 11); // RX, TX

再根据软串口管脚定义,修改Tx和Rx的连接方式。 使用软串口一样很方便,经测试可用!

相关文档

SRF02超声波测距传感器功能说明 [http://www.robot-electronics.co.uk/htm/arduino_examples.htm#SRF02, SRF08, SRF10, SRF235 使用样例]

<File:nextredirectltr.png>购买SRF02超声波测距传感器(SKU:SEN0005)