Managing an ultrasonic sensor SRF02
Finally it is time to get my hands on one of the ultrasonic sensors, which I keep in the drawers of the components, in particular I’m talking about the SRF02 sensor, an ultrasonic rangefinder that can make measurements between 16cm and 6m.
This sensor can be controlled through a serial or I2C, the choice of mode of control is administered through a 5-pin, which is connected to ground if you want to use the serial port, or left unconnected if you want to use the control via I2C. In this article I will speak of my experience with sensor controlled by a PIC 18F2620 I2C, and as you can see the management of the sensor is identical to that of a common I2C EEPROM. The details of the sensor and use them can be found at this link.
We begin to analyze the circuit diagram that is vary simple.

As you can see, in addition to the basic circuitry for the PIC (crystal, capacitors and resistor on MCLR) the sensor is connected to pins RC3 and RC4, respectively SCL and SDA, are used by the PIC MSSP module for communicating with I2C slave devices. Both of these pins are held at high level with two pull-up resistors from 1.8 K ohms, as required by the specifications of the I2C BUS. I also added a display to the scheme, in order to display the values returned by the sensor.
Now let’s see the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | void main(void) { unsigned char counter; unsigned int range=0, autotune=0; // PIC configuration config_system(); // Get 6 faster read of sensor for autotune srf02_init(); // Loop for(;;) { // Send to sensor a request to start a measurement in cm srf02_tx(SRF08, 0, 81); Delay10KTCYx(70); // Read the six register of sensor srf02_rx(SRF08, 0, 6); lcd_clear(); // Write the firmware version of sensor lcd_put_uchar(srf02_buffer[0],0,0); // Write the value of unused regiter lcd_put_uchar(srf02_buffer[1],0,4); // Write the value measured from the sensor range = (srf02_buffer[2]<<8); range += srf02_buffer[3]; lcd_put_uint(range,0,8); // Write the value of autotune autotune = (srf02_buffer[4]<<8); autotune += srf02_buffer[5]; lcd_put_uint(autotune,0,14); Delay10KTCYx(500); } } |
As you can see also the Code (the full one can be downloaded from this link) is very simple, line 15 calls the function that starts the measurement of the distance in centimeters from the sensor. Then a delay waiting for 70 milliseconds have elapsed, the time needed to receive the echo farther, then reads the sensor 6 registers, storing the values in srf02_buffer. As indicated in the guide of the sensor each register contains specific information as you can easily understand from the comments in the code, the part that interests us most is that the lines 25 and 26, where it reads the measured distance between the sensor and the ‘object that is in front. The display, educational purposes only, the code displays all other values, and as you can see the whole cycle is repeated every half second. At line 9 srf02_init() function is called, this does nothing but make six measurements in rapid sequence, as indicated in the specifications of the first 5/6 measurements are used to make self sensor calibration.
At first time, following the example on pages of documentation, I was going to read only the two registers that contain the measure of the distance, doing it this way but I had some strange measures, starting from a minimum readings began to increase by ten row and then start over. The smallest size was, however, be much more precise than that I get all 6 reading registers. Not finding useful information on this strange behavior, however, I decided to take the method of reading of all registers as more stable, and therefore as a system to use. If you have different experiences I would be curious to learn more then feel free to post comments with your experiences.
Soon I will also try to use the model SRF10, SRF02 which is managed similarly to, but being equipped with a transmitter and receiver capsule U.S. can maintain a lower minimum measure amounts to 6cm.
:: Download the complete code ::
Ti è piaciuto questo articolo? Perché non lasci un commento e continui la discussione, oppure iscriviti ai feed e riceverai gli articoli sul tuo feed reader.


(2 voti, media: 4.50 su 5)



Nel post successivo [http://www.it-robotix.net/2011/11/02/412/] potete trovare il nuovo codice che ora funziona correttamente.