Shop OBEX P1 Docs P2 Docs Learn Events
PING Sonar Sensor with PIC18F452 — Parallax Forums

PING Sonar Sensor with PIC18F452

Joseph123Joseph123 Posts: 1
edited 2007-03-25 03:07 in BASIC Stamp
Folks,

I am trying to interface the PING Sonar Sensor with the PIC18F452. I have been able to program the PIC to produce a trigger pulse of 5us ( verified that on the scope) and also been able to capture the waveforms and compute the ON pulse width (supplying a 20ms pulse using the function generator). I am using the in-built Capture/Compare module (CCP1) which allows for effective timing control.

Here's the problem: The PIC configures CCP1 pin as output and produces a 5us trigger pulse. I can see the LED on the PING to blink. I then configure the CCP1 module to change the pin to an input and wait for the return waveform. Well, the moment I change the pin to an input, the sensor shuts off. My subroutine is waiting for the return pulse to capture, it never comes. I have tried using a pullup resistor to 5V to keep it ON, connected to another pin that supplies 5V, etc but nothing seems to work. The moment the pin is changed from output to input, the module shuts down. The only way to restart the module is to power down the circuit and power it up.

Any ideas on what's happening?

-Joe

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-03-25 02:55
    This is how it's done with the stamp. you can convert this to pic code and try it.

    Get_Sonar:
    
      Ping = IsLow                                  ' make trigger 0-1-0
    
      PULSOUT Ping, Trigger                         ' activate sensor
    
      PULSIN  Ping, IsHigh, rawDist                 ' measure echo pulse
    
      rawDist = rawDist */ Scale                    ' convert to uS
    
      rawDist = rawDist / 2                         ' remove return trip
    
      RETURN
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-25 03:07
    Joe,
    The coding for the Propeller is much more like that for the PIC
    PRI pingProcess | initial, uS
       uS := clkfreq / 1_000_000
       repeat
          if pingTime == 0
             outa[noparse][[/noparse]pingData]~                      ' Clear I/O pin & make an output
             dira[noparse][[/noparse]pingData]~~             
             outa[noparse][[/noparse]pingData]~~                     ' Set I/O Pin & hold for at least 5uS
             outa[noparse][[/noparse]pingData]~~ 
             outa[noparse][[/noparse]pingData]~                   
             dira[noparse][[/noparse]pingData]~                      ' Make I/O pin an input and
             waitpne(0, |< pingData, 0)           '  wait For Pin To Go HIGH
             initial := cnt        
             waitpeq(0, |< pingData, 0)           ' Wait for pin To Go LOW 
             pingTime := ((cnt - initial)/uS)>>1  ' Compute one-way pulse width
          else
             waitcnt(clkfreq / 50 + cnt)          ' Pause about 20ms if not PINGing
    
    


    This does much the same thing as you're trying and it does work. You might try checking your pulse width, maybe just make it a little bit wider. This sets up the control pin as a low output, maybe 2us later sets it to high for a little more than 5us, sets it to low for about 2us, then changes it to input and waits for the leading edge of the echo timing pulse.
Sign In or Register to comment.