KLCarithers Robotics

SRF05 single pin mode

Kyle L. Carithers

 

 

The SRF05 is an excellent sonar module that I highly recommend.  The module is accurate and easy to interface.  The code shown below was designed for use with up to eight SRF05 sonar modules in mode two attached to a single port of an Atmega128 with a 16MHz clock.  The code was developed in C using WinAVR’s Programmer’s Notepad.  This code is simple and easy to use and works well if the values do not need to continuously update. 

 

However, sonar is typically used for obstacle avoidance and the values do need to continuously update.  This function has the processor counting until the falling edge of the sonar output and the processor cannot do anything else during this time.  A slightly more complicated but much more efficient way to check the sonar uses an external interrupt to update the sonar values.  Click for an explanation of this method and example code.   

 

This code stores the most recent sonar reading for each sonar module as a global integer.  The variable can updated using the following:

 

sonar_distance_X = sonar(X);

 

 where X is the pin location of the sonar module to be read.

 

There should be a 50ms delay between each trigger.  This time is necessary to allow the sonar to settle and avoid false reading.

 

/*The next three line define port A as the port that will be used for the sonar modules,

other ports can be used but be sure to check the Atmega128 datasheet to find out alternate

functions of the selected port*/

#define SONAR_PORT PORTA

#define SONAR_DDRX DDRA

#define SONAR_PINX PINA

 

int sonar_distance_0;

int sonar_distance_1;

int sonar_distance_2;

int sonar_distance_3;

int sonar_distance_4;

int sonar_distance_5;

int sonar_distance_6;

int sonar_distance_7;

 

int sonar(int sonar_number)

            {

                        int i=0;

                        int counter=0;

                        int distance=0;

           

                        unsigned int sonar_unsigned=0b00000001;

           

                        ///Shifts to the desired pin///      

                        sonar_unsigned = sonar_unsigned<<(sonar_number);

 

                        ///Set pin to output///

                        SONAR_DDRX = sonar_unsigned;

 

                        ///Set pin high, this is the trigger///

                        SONAR_PORT = sonar_unsigned;

           

                        ///Wait for atleast 10 microseconds///

                        while(i<1000)

                                    {i=i+1;}

           

                        ///Set pin low///

                        SONAR_PORT = 0x00;

                                   

                        ///Set the pin to input///

                        SONAR_DDRX = 0x00;

           

           

                        ///Wait for the high signal from the sonar output///

                        while((SONAR_PINX & sonar_unsigned)==0){}

           

                        ///Wait for the end of the high signal///   

                        while(counter>=0))

                        {

                                    ///increment counter///

                                    counter++;

           

                                    ///check for end of high signal and break if low///

                                    if((SONAR_PINX & sonar_unsigned)==0) break;

                        }

 

                        /*Makes distance equal to counter.  Counter can be divided by a number

                        found experimentally to set distance to a value in centimeters, inches, etc.*/

                        distance=counter;

           

            return distance;}

 

 

int main (void)

{

            sonar_distance_0=sonar(0);

}