CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Maxbotix ultrasonic sensor with 16f877a
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
marcus02



Joined: 22 Jan 2013
Posts: 7

View user's profile Send private message

Maxbotix ultrasonic sensor with 16f877a
PostPosted: Tue Feb 05, 2013 11:25 am     Reply with quote

Hi experts,

I'm currently working on a project by using Maxbotix ultrasonic sensor which is an analogue sensor and PIC16f877A microcontroller.
I'm using two analogue sensors with only 1 PIC.
The problem that i'm facing now is my sensors are unstable with the long range.
It can only works perfectly within the range of 50cm instead of 6meters.
Once the object is detected in range then it will trigger on a LED.
But now the situation is like the LED simply blinking once i set the range >50cm for example 100cm.
Please help as i need a stable long range sensing for my project.

Code:

#include <16f877a.h>
#device ADC=10
#include <stdio.h>
#use delay(clock = 20000000)
#fuses hs,noprotect, nowdt,nolvp, nobrownout, nowrt
void sensor11();
void sensor22();
float sensor1,sensor2,value1,value2;
int y,z;

void main()
{   setup_adc_ports(AN0_AN1_AN3);   
      setup_adc(ADC_CLOCK_INTERNAL);
      y=0;
      z=0;
      output_high(PIN_C0);
      output_high(PIN_C1);
      output_high(PIN_C2);
      output_high(PIN_C3);
      output_high(PIN_D0);
   while(TRUE)
   { 
   
      sensor11();
      sensor22();
   
   }  //end while
 
}  //end main

void sensor11()
{
//sensor 1
     
      set_adc_channel(0);
      delay_us(100);
      value1=read_adc();    //read adc value from sensor 1
      sensor1=(value1*1.240234375);     //convert value to cm
     
      if ((sensor1<100)&&(y==0))
      { 
         y=1;
         output_high(PIN_B0); //sensor 1 led ON
         delay_ms(100);
      }
      else if ((sensor1>100)&&(y==1))
      {
         y=0;
         output_low(PIN_B0); //sensor 1 led OFF
         delay_ms(100);
      }
}

void sensor22()
{
//sensor 2
     
      set_adc_channel(1);
      delay_us(100);
      value2=read_adc();    //read adc value from sensor 1
      sensor2=(value2*1.240234375);     //convert value to cm
     
      if ((sensor2<100)&&(z==0))
      { 
         z=1;
         output_high(PIN_B1); //sensor 2 led ON
         delay_ms(100);
      }
      else if ((sensor2>100)&&(z==1))
      {
       
         z=0;
         output_low(PIN_B1);  //sensor 2 led OFF
         delay_ms(100);
      }
}


Regards,
Marcus
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Feb 05, 2013 12:15 pm     Reply with quote

Ive played with these sensors...

my suggestion is to test your circuit in a different room or outside preferably in open space...

indoor testing for me resulted in many false echos and redundant signal return paths (fancy way of saying More Echos)


after that the sensors got wierd, and spat out serial data in an unknown baud rate...

in addition you could try to clean up your data by using an "olympic" filter...
there are several examples in the forum.

g.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Feb 05, 2013 12:16 pm     Reply with quote

Multiple things are not helping:

1) Is ADC_CLOCK_INTERNAL recommended with your clock?.....
2) Just how stable is the supply voltage?. This is being used as your ADC reference. It won't be good when you are looking for small changes.
3) Consider taking a short 'burst' of readings, and averaging them. Ultrasonic readings are very erratic, with some objects being detected yards away, while others are hard to resolve even at short distances. You'll often get reflections off flat objects and pick up other things from the reflections.

Give the sensor model, or better a link to it's data sheet. Maxbotic's do dozens of different models.

Then seriously, get rid of the float arithmetic. You don't need it.

Best Wishes
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Feb 05, 2013 4:51 pm     Reply with quote

got a complete schematic of what you've built that you can post ???
ALSO

give 10 bit resolution , the floating point calc of
value1*1.240234375 ... seems a bit out of whack.

if it were me i'd do something like

(unsigned int32 ) value_out = ((unsigned int32 ) (value1*1240) / 1000);

and do any scalar fprint() formatting as needed to return the result
with all the accuracy/resolution the ADC measurement is actually capable of.
can you see why i say that ??

EDIT: Just realized that this too, is along the line of what Ttelmah was kind
of enough to mention.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 6:46 am     Reply with quote

BTW a good test target is a hard surfaced ball. A ping pong ball, a billiards ball, or a soccer ball work well depending on range. Flat surface targets are highly dependent on angle.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 7:16 am     Reply with quote

SherpaDoug wrote:
BTW a good test target is a hard surfaced ball. A ping pong ball, a billiards ball, or a soccer ball work well depending on range. Flat surface targets are highly dependent on angle.

How about a corner reflector, like the one the left on the moon?
(But made from, say, hard wood.)

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 7:58 am     Reply with quote

It depends what you want to 'prove'. A 'good' target ( in terms of finding how well the system will cope with difficult targets), is a tennis ball. The combination of a furry surface, and resilient interior, make these quite hard to resolve. You'll often find the ultrasonic's missing these, and instead picking up the corner of a bookcase several feet further away....

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 9:25 am     Reply with quote

Agreed.

Mike
marcus02



Joined: 22 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 10:06 am     Reply with quote

Gabriel wrote:
Ive played with these sensors...

my suggestion is to test your circuit in a different room or outside preferably in open space...

indoor testing for me resulted in many false echos and redundant signal return paths (fancy way of saying More Echos)


after that the sensors got wierd, and spat out serial data in an unknown baud rate...

in addition you could try to clean up your data by using an "olympic" filter...
there are several examples in the forum.

g.


http://www.ccsinfo.com/forum/viewtopic.php?t=29018
Bro.. is it this thread that you mean? calibration of analogue input?

regards,
marcus
marcus02



Joined: 22 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 10:10 am     Reply with quote

Ttelmah wrote:
Multiple things are not helping:

1) Is ADC_CLOCK_INTERNAL recommended with your clock?.....
2) Just how stable is the supply voltage?. This is being used as your ADC reference. It won't be good when you are looking for small changes.
3) Consider taking a short 'burst' of readings, and averaging them. Ultrasonic readings are very erratic, with some objects being detected yards away, while others are hard to resolve even at short distances. You'll often get reflections off flat objects and pick up other things from the reflections.

Give the sensor model, or better a link to it's data sheet. Maxbotic's do dozens of different models.

Then seriously, get rid of the float arithmetic. You don't need it.

Best Wishes


Here is the datasheet of the ultrasonic sensor that i used.
www.maxbotix.com/documents/MB1010_Datasheet.pdf

I am not looking for small changes... I wish to make it detect as far as 1meter long but it will only stable within the range of 50cm. =(
sorry for my bad english.

regards,
marcus
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 10:28 am     Reply with quote

... i mean this thread:

http://www.ccsinfo.com/forum/viewtopic.php?t=19509&highlight=filter

Search for "filter" in the code library ... and "Olympic"

...

What Ttelmah suggest has nothing to do with your sensor...

it means you have a 20mhz crystal on the chip and you are using the INTERNAL setting... this is wrong... read the DATASHEET... search for TAD...

Id take his advise seriously... and try to understand what he is telling you... he is one of the more active/respected forum users...



br.
G
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 2:13 pm     Reply with quote

You do realise that to use two of these sensors, you need to turn the one you are not using 'off', while you read the other?. They work on the same frequency, and if left in automatic mode, will be sending pulses every 50mSec or so, that the other sensor _will_ receive, giving unexpected distances all the time....
You need to have the PIC operating the RX line of each unit, and pull the one not being read 'high'. Then pull the one you want to read low, wait 49mSec, and read the analog.

Best Wishes
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 3:40 pm     Reply with quote

Ttelmah is right... again.

The reason he points out is precisely the reason why Maxbotics has an application note for multiple sensor operation on their site!

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
marcus02



Joined: 22 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Wed Feb 06, 2013 6:17 pm     Reply with quote

Ttelmah wrote:
You do realise that to use two of these sensors, you need to turn the one you are not using 'off', while you read the other?. They work on the same frequency, and if left in automatic mode, will be sending pulses every 50mSec or so, that the other sensor _will_ receive, giving unexpected distances all the time....
You need to have the PIC operating the RX line of each unit, and pull the one not being read 'high'. Then pull the one you want to read low, wait 49mSec, and read the analog.

Best Wishes


Hi,

I do know that these two sensors are using the same frequency..
But i have no idea on how to turn the one i am not using 'off'.
What do you mean by having the PIC operating the RX line of each unit and pull the one not being read 'high'?
Sorry i am newbie in this field couldn't understand that much.
Thanks.

Regards,
marcus
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Feb 07, 2013 6:40 am     Reply with quote

Like i said:

Quote:
Maxbotics has an application note for multiple sensor operation on their site


I just checked their website again... they even have code samples...

good thing is that the sensors are built using PICs so the code should be ease to port to CCS...

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group