Skip to main content

Arduino: Ultrasonic sensor


One of the easiest methods to measure distances in an embedded project is to use an ultrasonic sensor. They provide sufficient accuracy to most applications.It is exceptionally useful in robotics as well.

So how do we interface ultrasonic sensors with arduino? It`s fairly simple.

let`s briefly look how it works.Ultrasonic sensor consists of a transmitter and receiver



The transmitter emits ultrasonic pulse for short duration and waits for the echo pulse on the receiver. The time delay is a measure of the distance from sensor to the object.Roughly we could assume sound waves travels at the rate of 1cm per 29 microseconds.

first we have to pull the trigger pin high for a short duration say 10us.Then wait till the receiver gets the echo pulse.If your sensor is PWM type (common type like parallax PING)) ) we get a pulse whose width corresponds to delay of the ultrasound pulse.All we have to do is to calculate the pulse duration and print suitably, here is the sample code,

// initialize
int PingPin = 7 ;          // pin used for the Ping Sensor
int BaudRate = 9600 ;      // Baud rate

// set up
void setup() {
  Serial.begin(BaudRate) ; // Setup Serial
}

void loop() {
  pinMode(PingPin, OUTPUT)    ;
  digitalWrite(PingPin, LOW)  ;  // init sensor to ensure clean HIGH pulse
  delayMicroseconds(2)        ;
  digitalWrite(PingPin, HIGH) ;  // make the sensor send a pulse
  delayMicroseconds(5)        ;
  digitalWrite(PingPin, LOW)  ;  // Set LOW again
  pinMode(PingPin, INPUT)     ;  // Get ready to capture the duration of the resulting pulse
  // Capture how long the pin stays in HIGH state.
  unsigned long Duration = pulseIn(PingPin, HIGH) ;
  if (Duration == 0) {
    Serial.println ("No Pulse received from the sensor") ;
  }
  else
  {
    Serial.print ("Distance : ") ;
    Serial.print(Convert_Time_Space(Duration)) ;  // convert the duration into distance
    Serial.println (" cm");
  }
  delay (1000) ;
}

unsigned long Convert_Time_Space(const unsigned long fnDuration )
{
  // This function could be more precise by using floats
  // and taking into account temperature and humidity
  // I used 29 microseconds per cm.
  return fnDuration / 29 / 2 ;
}

hope this helps

Comments

Popular posts from this blog

STM32F4 Discovery: Clock frequency

So you have got an STM32F4 discovery board in your hands. I can surely assume one of the reason you got this nifty little development board is it's computing capability at 168MHz. However if you are not cautious, you may not achieve the maximum clock speed the device could achieve.If u are not careful (sometimes it happens) the controller would run at lower clock.Obviously that is not a bad thing in power critical applications.Here are some steps to ensure the system is running at its peak performance STEP 1: Download clock configuration tool from ST, file named AN3988 http://www.st.com/internet/mcu/product/252149.jsp STEP 2: Open the clock configuration tool using excel and change HCLK to 168 MHz.You can select on other options and PLL frequencies also (Know what you are doing else leave it as it is).Click generate.If values are OK then no errors would be shown.Then click on generate.Then close the excel file.You should have got a system_stm32f4xx.c file in the folder.U

Arduino as USB to Serial converter

Electronics prototyping was taken to new heights by the introduction of arduino. The small board could do many wonderful things,but the beauty that makes it stand apart from others is it`s versatility and easiness. Most of us while doing our projects would have searched for a communication method with PC, and ended up using USART by level converters (MAX232) or perhaps a USB-Serial cable.Serial ports are disappearing from systems these days. Obviously USB is the only choice..So what to do? First thing you would say is to use a USB to serial converter cable that is cheap to buy. Unfortunately as many of you would have observed these are not  reliable many a times. next option is to use a FTDI breakout boards like this https://www.sparkfun.com/products/9115 . So if you can`t reach out for an FTDI board or any of those serial converter cables, you`l think you are doomed just like i did at that time.So no way out? Nope there is a way! Do you have an arduino sitting around