Jump to content
43oh

HCSR 04 Ultrasonic Sensor Problem


Recommended Posts

Hi I write a code. But it didnt work. Led is burning and burning out not with range.

Where is the problem

 

#include "msp430g2452.h"
#define tr BIT0
#define ec BIT1
#define led BIT6
 
int range=0;
void main( void )
{
       WDTCTL = WDTPW + WDTHOLD; //watchdog stop
       DCOCTL=CALDCO_1MHZ;  //frequency setup
       BCSCTL1=CALBC1_1MHZ;     
        P1DIR = tr+led;
        P1OUT=0x00;   //all are zero
      for(;;)
 {
     P1OUT^=tr;                       //Trigger is on for 10 us
     __delay_cycles(10);
     P1OUT^=tr;
   if(P1IN&ec)
   {
     range++;            //when echo coming range is increasing
   }
   if(range>50)
   {
   P1OUT=led;
   range=0;
   }
   
   __delay_cycles(1000000);    //waiting some
   P1OUT^=led;
}
}
Link to post
Share on other sites

You might try out this code.  It's written for an Ultrasonic backpack that I'm working on.  It is designed for the QFN version of the 2553, so it has P3 available - but that part of the code can easily be changed.  The Echo pin from the device is connected to P2.0 through a voltage divider (since the device works on 5v which would damage the pins on the MSP430).  The trigger pin is connected to 2.3.

 

I *think* the code is figuring out the distance in CM - I'd have to double check - I think it was easier to calculate than inches.  

UBackpack.zip

Link to post
Share on other sites

 

 

   if(P1IN&ec)
   {
     range++;            //when echo coming range is increasing
   }
 

 

I think you meant to increment "range" until "echo" pin becomes high.

 

In such case it should be a while condition. Otherwise range can only be 0 or 1 and it would loop thru and do another trigger.

    while (P1IN&ec)
   {
     range++;            //when echo coming range is increasing
   }
Link to post
Share on other sites

 

I think you meant to increment "range" until "echo" pin becomes high.

 

In such case it should be a while condition. Otherwise range can only be 0 or 1 and it would loop thru and do another trigger.

    while (P1IN&ec)
   {
     range++;            //when echo coming range is increasing
   }

thanks but it didnt solve

Link to post
Share on other sites
  • 4 weeks later...

Unless i'm missing something, your code will create about a 10us pulse every 1 second. It will also sample the ec pin once every 1 second. Thats quite a ways away from what you need to do in order to get the module to  work.  Also, if your LED is burning, its probably not your software. Perhaps a resistor might help? Seems you need abit better understanding about how the module works and how code works in general.

 

 

Code examples

http://forum.43oh.com/topic/4027-hc-sr04-ultrasound-module-driver-code/

http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...