SRF-10 Ultrasonic sensor with Arduino


pecification
  • Voltage - 5v only required
  • Current - 15mA Typ. 3mA Standby.
  • Frequency - 40KHz
  • Range - 6cm - 6m.
  • Max Analogue Gain - Variable 40 to 700 in 16 steps.
  • Connection - Standard I2C Bus.
  • Timing - Fully timed echo, freeing host controller of task.
  • Units - Range reported in uS, cm or inches.
  • Small Size - 32mm x 15mm x 10mm height.

Connection Diagram




Arduino Sketch



/*

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>

#include "SPI.h"

#include "TFT_22_ILI9225.h"



#define SRF_ADDRESS         0x70                                   // Address of the SRF08
#define CMD                 (byte)0x00                             // Command byte
#define LIGHTBYTE           0x01                                   // Byte to read light sensor
#define RANGEBYTE           0x02                                   // Byte for start of ranging data



// SPI TFT LCD

#define TFT_RST 8
#define TFT_RS  9
#define TFT_CS  10  // SS
#define TFT_SDI 11  // MOSI
#define TFT_CLK 13  // SCK
#define TFT_LED 3   // 0 if wired to +5V directly
#define TFT_BRIGHTNESS 200 // Initial brightness of TFT backlight (optional)



// Use hardware SPI (faster - on Uno: 13-SCK, 12-MISO, 11-MOSI)

TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, TFT_BRIGHTNESS);


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

int  rangeTemp = 0;



void setup(){



  Serial.begin(9600);                             // Begins serial port for LCD_03

  Wire.begin();                             

  delay(100);                                     // Waits to make sure everything is powered up before sending


  tft.begin();

 Serial.println("SRF02/08/10/235");

 }



void loop(){

  char buff[10];

  int rangeData =  getRange();                     // Calls a function to get range

  Serial.print("Range = ");
  Serial.print(rangeData, DEC);               
  Serial.println("   ");



 if( rangeTemp != rangeData ){


  rangeTemp = rangeData;
   tft.setFont(Terminal12x16);

  tft.drawText(10, 45, "Distance");

  snprintf (buff, sizeof(buff), "%d", rangeData);

   tft.setFont(Trebuchet_MS16x21);

   tft.fillRectangle(8, 67, 100, 95, COLOR_BLACK);

  tft.drawText(10,70,buff);

  tft.setFont(Terminal12x16);

  tft.drawText(50,70,"inch");

 }


 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);     // CMD                      // Send Command Byte
  Wire.write(0x51);     // command sensor to measure in "inches" (0x50)
                                   // use 0x51 for centimeters
                                   // use 0x52 for ping microseconds

  Wire.endTransmission();

  delay(70);                                      // 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 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);                             


}

ความคิดเห็น