SRF10
SRF10
Ultrasonic sensors
SRF10 connector description
This module used I2C as a protocol to communication with MCU. This module support to use with more than one module in system up to 16 module. We can set ID address for each module before integrated to the system, About how to change ID address click here.
For distance to measure this module can measure from 6 cm to 6 m.
Sample code:
#include <Wire.h>
//For Ultrasonic
//ADDRESS
#define Front_Left 112
#define Front_Right 114
#define CMD 0x00
#define Read_inch 0x50
#define Read_cen 0x51
#define Read_micro_sec 0x52
#define Read_pointer 0x02
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
//Correct Data
Serial.println("----");
read_distance_ultrasonic(Front_Left);
delay(75);
read_distance_ultrasonic(Front_Right);
delay(75);
}
int read_distance_ultrasonic(int addr){
int reading = 0;
// step 1: instruct sensor to read echoes
Wire.beginTransmission(addr); // transmit to device addr
Wire.write(CMD); // sets register pointer to the command register (0x00)
Wire.write(Read_cen); // command sensor to measure in "inches" (0x50)
// 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(addr); // transmit to device
Wire.write(Read_pointer); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(addr, 2); // request 2 bytes from slave device
// 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.println(reading); // print the reading
return reading;
}
So in this code, We will read a distance from 2 SRF10 sensors and print output to a serial port.
Reference: SRF10 Document, Example code
Comments
Post a Comment