Monday, 23 July 2018

Interface a Ultrasonic Sensor with ARM Microcontroller (LPC2148)

Aim:

In this article I will describe how to interface a Ultrasonic Range Finder Module with a ARM LPC2148 microcontroller. I will provide a HEX file which you can burn into your LPC2148 directly to quickly test this whole setup.
ultrasonic

Description:

We know that sound vibrations can not penetrate through solids. So what happens is, when a source of sound generates vibrations they travel through air at a speed of 220 meters per second. These vibrations when they meet our ear we describe them as sound. As said earlier these vibrations can not go through solid, so when they strike with a surface like wall, they are reflected back at the same speed to the source, which is called echo.
Ultrasonic sensor “HC-SR04” provides an output signal proportional to distance based on the echo. The sensor here generates a sound vibration in ultrasonic range upon giving a trigger, after that it waits for the sound vibration to return. Now based on the parameters, sound speed (220m/s) and time taken for the echo to reach the source, it provides output pulse proportional to distance.
Ultrasonic Sensor Timing Diagram
As shown in figure, at first we need to initiate the sensor for measuring distance, that is a HIGH logic signal at trigger pin of sensor for more than 10uS, after that a sound vibration is sent by sensor, after a echo, the sensor provides a signal at the output pin whose width is proportional to distance between source and obstacle.

Block Diagram

ultrasonic

Schematic


Code

// ****************************************************** 
// Project: Ultrasonic sensor using LPC2148 
// Author: Hack Projects India 
// Module description: Operate ultrasonic sensor
// ******************************************************
#include <LPC214x.H>                       /* LPC21xx definitions */
#define RS (1<<16)  //Set/Reset Pin (Control_Pin)
#define EN (1<<17)

void Delay(unsigned int time)
{
 unsigned int i,j;
 for(i=0;i<time;i++)
 for(j=0;j<250;j++);
}

void cmd_lcd(unsigned int cmd)
{
 IOCLR1=RS;  
 IOSET1=EN;
 IOSET1=((cmd)&(0xff))<<18; 
    Delay(50);
    IOCLR1=EN; 
 IOCLR1=RS; 
 IOCLR1=((cmd)&(0xff))<<18;
}
void data_lcd(unsigned int data)
{
    IOSET1=RS; 
 IOSET1=EN;
 IOSET1=((data)&(0xff))<<18;   
    Delay(50);
    IOCLR1=EN;   
    IOCLR1=RS;
 IOCLR1=((data)&(0xff))<<18;
}

void init_lcd()
{                  
    cmd_lcd(0X38);              //8 BIT LCD
    cmd_lcd(0X06);              //DISPLAY OFF CURSOR OFF
    cmd_lcd(0X0C);              //DISPLAY ON CURSOR OFF
    cmd_lcd(0X01);              //CLEAR THE LCD
} 
void LCD_String(unsigned char *data)
{
 while(*data)
 {
  data_lcd(*data);
  data++;
 } 
} 
void LCDWriteInt(int val,unsigned int field_length)
{

 char str[5]={0,0,0,0,0};
 int i=4,j=0;
 while(val)
 {
 str[i]=val%10;
 val=val/10;
 i--;
 }
 if(field_length==-1)
  while(str[j]==0) j++;
 else
  j=5-field_length;

 if(val<0) data_lcd('-');
 for(i=j;i<5;i++)
 {
 data_lcd(48+str[i]);
 }
}
unsigned int ultrasonic_read()
{
    int b,c;   
 T1TC=0;   T1PR=32;
 T1TCR = 0x00;
      
    IO0SET =(1<<0);
    Delay1(100);
    IO0CLR =(1<<0);
    while(!(IO0PIN &(1<<1)));        
 T1TCR = 0x01;
 c=T1TC;
 while((IO0PIN &(1<<1)));
 T1TCR = 0x00;
 b=T1TC;
 return (b-c);
}

int main(void)
{  
 unsigned int a;
 PINSEL0 = 0x00000000;  // Enable GPIO on all pins
 PINSEL1 |= 0x15000000;
 PINSEL2 = 0x00000000;
 Delay(20);
 IO1DIR = 0xfff0000;
 IO0DIR =(1<<0)|(0<<1);
 init_lcd();
 Delay(20);
     
 Delay(200);
 cmd_lcd(0x01);
 LCD_String("value:");

  while(1)
  {
   a=ultrasonic_read();
   cmd_lcd(0xc0);
   LCDWriteInt(a,3);
  }
}


Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment