八色木

SRF02超声波测距模块的连接

srf02超声波模块

SRF02超声波测距模块的技术参数

范围: 16cm – 6m.
电源:5v, 4mA .
频率:40KHz.
外形尺寸:24mm(长) x 20mm(宽) x 17mm(高) .
通信协议:标准 I2C 总线或串行总线,最多可连接16个模块.
重量:4.6克.

目前,I2C总线可以在大多数控制器上使用,如Arduino、树莓派、香蕉派等等开发板,对于程序员来说,SRF02的使用与使用普通24xx系列芯片的EEPROM是一样简单,只不过I2C总线地址不同而已。SRF02 默认的总线地址是 0xE0 ,当然也可以更改,可使用的16位地址分别为:E0、E2、E4、E6、E8、EA、EC、EE、F0、F2、F4、F6、F8、FA、FC或FE,因此在一条I2C总线上我们最多可以使用16个超声波测距模块。(如果要使SRF02超声波测距模块工作在I2C模式下,MODE 引脚必须是断开的。)

SRF02超声波模块I2C总线接口示意

SRF02的引脚与SRF08和SRF10是完全相同的,需要注意:在I2C总线上需要在主控装置侧设置一对1.8K的上拉电阻分别连接至模块的SCL引脚和SDA引脚,I2C总线上只设置一对电阻,而不是每个模块设置一对,在I2C总线上SRF02只能作为从属设备。如果你采用的主控设备本身已内置了上拉电阻,则就没有必要再次设置了。

SRF02超声波测距模块的引脚定义

通常,我们只使用+5V、SDA、SCL和GND四个引脚,Mode引脚悬空。正如前面提到的I2C总线上需要在主控端串接一对上拉电阻,如下图:

SRF02与Arduino的连接方式和SRF08和SRF10等模块的连接方式也完全相同。图中为了后续测试便利同样采用串口方式接入了LCD1602显示器,可查阅:1602 LCD液晶屏引脚定义图了解LCD1602。普通1602显示屏要使用串口连接,需要用到LCD1602-串口转接板,可是我并未找到购买地址….只能从robot-electronics复制Arduino测试代码,供参考:

/*
Generic example for the SRF modules 02, 08, 10 and 235.
Only the SRF08 uses the light saensor so when any other 
range finder is used with this code the light reading will 
be a constant value. 
*/

#include <Wire.h>
#include <SoftwareSerial.h>

#define LCD_RX              0x02                                   // Software serial pin for rx
#define LCD_TX              0x03                                   // Software serial pin for tx
#define SRF_ADDRESS         0x70                                   // Address of the SRF08
#define CMD                 (byte)0x00                             // Command byte, values of 0 being sent with write have to be masked as a byte to stop them being misinterpreted as NULL this is a bug with arduino 1.0
#define LIGHTBYTE           0x01                                   // Byte to read light sensor
#define RANGEBYTE           0x02                                   // Byte for start of ranging data
#define LCD03_CLEAR         0x0C                                   // Byte to clear LCD1602 screen
#define LCD03_SET_CUR       0x02                                   // Byte to tell LCD1602 we wish to move cursor
#define LCD03_HIDE_CUR      0x04                                   // Byte to hide the cursor

SoftwareSerial lcd_03 =  SoftwareSerial(LCD_RX, LCD_TX);    // defines a new software serial port for lcd_1602

byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging

void setup(){
  
  lcd_03.begin(9600);                             // Begins serial port for LCD_03
  Wire.begin();                               
  delay(100);                                     // Waits to make sure everything is powered up before sending or receiving data
  
  lcd_03.write(LCD03_CLEAR);
  lcd_03.write(LCD03_HIDE_CUR);
  lcd_03.println("SRF02/08/10/235");
  
  int softRev = getSoft();                        // Calls function to get software revision
  lcd_03.write(LCD03_SET_CUR);
  lcd_03.write(61);                               // Move cursor
  lcd_03.print("Software version: ");
  lcd_03.print(softRev, DEC);                     // Print softRev to LCD03
    
}

void loop(){
  
  int rangeData = getRange();                     // Calls a function to get range
  lcd_03.write(LCD03_SET_CUR);
  lcd_03.write(21);                               // Move cursor to space 21
  lcd_03.print("Range = ");
  lcd_03.print(rangeData, DEC);                   // Print rangeData to LCD03
  lcd_03.print("   ");                            // Print some spaces to slear spaces after data
 
  int lightData = getLight();                     // Call function to get light reading and store in lightData
  lcd_03.write(LCD03_SET_CUR);
  lcd_03.write(41);                               // Move cursor to space 41
  lcd_03.print("light = ");
  lcd_03.print(lightData, DEC);                   // Display lightData
  lcd_03.print("  ");                             // Print some spaces to slear spaces after data 
  
 delay(100);                                      // Wait before looping
}

int getRange(){                                   // This function gets a ranging from the SRF08
  
  int range = 0; 
  
  Wire.beginTransmission(SRF_ADDRESS);             // Start communticating with SRF08
  Wire.write(CMD);                                 // Send Command Byte
  Wire.write(0x51);                                // Send 0x51 to start a ranging
  Wire.endTransmission();
  
  delay(100);                                      // Wait for ranging to be complete
  
  Wire.beginTransmission(SRF_ADDRESS);             // start communicating with SRFmodule
  Wire.write(RANGEBYTE);                           // Call the register for start of ranging data
  Wire.endTransmission();
  
  Wire.requestFrom(SRF_ADDRESS, 2);                // Request 2 bytes from SRF module
  while(Wire.available() < 2);                     // Wait for data to arrive
  highByte = Wire.read();                          // Get high byte
  lowByte = Wire.read();                           // Get low byte

  range = (highByte << 8) + lowByte;               // Put them together
  
  return(range);                                   // Returns Range
}

int getLight(){                                    // Function to get light reading
  
  Wire.beginTransmission(SRF_ADDRESS);
  Wire.write(LIGHTBYTE);                           // Call register to get light reading
  Wire.endTransmission();
  
  Wire.requestFrom(SRF_ADDRESS, 1);                // Request 1 byte
  while(Wire.available() < 0);                     // While byte available
  int lightRead = Wire.read();                     // Get light reading
    
  return(lightRead);                               // Returns lightRead
  
}

int getSoft(){                                     // Function to get software revision
  
  Wire.beginTransmission(SRF_ADDRESS);             // Begin communication with the SRF module
  Wire.write(CMD);                                 // Sends the command bit, when this bit is read it returns the software revision
  Wire.endTransmission();
  
  Wire.requestFrom(SRF_ADDRESS, 1);                // Request 1 byte
  while(Wire.available() < 0);                     // While byte available
  int software = Wire.read();                      // Get byte
    
  return(software);                               
  
}

树莓派的测试代码如下:

// SRF02 Raspberry pi 测试代码.
//
// 代码同样支持SRF02/ 10/ 235 和 08.
// By James Henderson, 2016.

#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

int main(int argc, char **argv)
{
	printf("**** SRF02/10/235 example program ****\n");
	
	int fd;														// File descrition
	// For older raspberry pi modules use "/dev/i2c-0" instead of "/dev/i2c-1" for the i2c port
	char *fileName = "/dev/i2c-1";								// Name of the port we will be using
	int  address = 0x70;										// Address of the SRF02 shifted right one bit
	unsigned char buf[10];										// Buffer for data being read/ written on the i2c bus
	
	if ((fd = open(fileName, O_RDWR)) 

	
Exit mobile version