Shop OBEX P1 Docs P2 Docs Learn Events
Xbee (802.15.4) with Sharp GP2D12 Analog Sensor and BS2 — Parallax Forums

Xbee (802.15.4) with Sharp GP2D12 Analog Sensor and BS2

kvvanburekvvanbure Posts: 7
edited 2012-12-13 17:02 in Accessories
My goal is to use the Sharp GP2D12 Analog Sensor connected to the Xbee (802.15.4) to transmit the analog signal to another Xbee (802.15.4). From there the signal will be passed through an ADC0831 and then to the stamp to output the distance as measured from the sensor. So far I've been able to get the ADC and Sharp sensor working together using the following code supplied in the data sheet. Was also able to loop back info using the following code for the Xbee:
' {$STAMP BS2}
' {$PBASIC 2.5}
 
' Requires remote XBee in loopback mode (DIN connected to DOUT)
 
Baud  CON     84                        ' 9600 baud non-inverted
Rx    PIN     15                        ' XBee DOUT
Tx    PIN     14                        ' XBee DIN
temp  VAR     Byte                      ' Temporary variable
 
PAUSE 500                               ' 1/2 second pause to stabilize
 
DO
  DEBUG CLS, "Type a character: "       ' Prompt for character
  DEBUGIN temp                          ' Catch user response
  SEROUT Tx, Baud, [temp]               ' Send to transmitting XBee
  SERIN Rx, Baud, [temp]                ' Get echo from receiving XBee
  SEROUT 16, Baud,
    [CR, "You entered: " , temp]        ' Display result
  PAUSE 1500                            ' Wait 1-1/2 seconds
LOOP

My question is. Is it possible to transmit the signal as analog, receive it, pass it though the ADC, and then read it on the stamp? Is there any extra coding that needs to be done in order to get the analog signal transmitted? So far the attempt to get this to work is it use the analog output of the sensor connected to pin 20 of the transmitting Xbee with Vref connected to 3.3V. The receiving Xbee is on the breadboard on the Basic Stamp board of education. Pin 20 of the receiving Xbee is then connected to the V+ pin of the ADC and the output of the ADC is then connected to pin 13 of the stamp and then read by the program linked above.

Any help on this would be greatly appreciated.
Thanks in advance.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-07 19:08
    The XBees do the analog to digital conversion themselves. They don't transmit an analog value they transmit a number representing the measured voltage on the ADC pin. Section 2.2 (page 12) of the XBee Manual (at least the version I have) has information about this.
  • kvvanburekvvanbure Posts: 7
    edited 2012-12-08 08:49
    I see what you're referring to, but is it possible to transmit the analog signal coming out of the sharp sensor and then pass that signal through an external ADC?

    I guess if that's not possible I'd like to pass the signal through the ADC before inputting it into the Xbee and then transmitting it to the receiver on the basic stamp. The real problem I'm having is that I can't get a signal transmitted to the receiver to output to the basic stamp.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-08 08:57
    kvvanbure wrote: »
    is it possible to transmit the analog signal coming out of the sharp sensor and then pass that signal through an external ADC?

    I'm pretty sure that's a no.

    I think it's possible to use the XBee as your ADC and have it transmit the readings directly.

    I haven't ever used the XBee's ADC pins myself, but it looks like the manual covers how to do it. IIRC, only the "Pro" versions of the XBees can to this ADC themselves (at least that's what I thought the manual said).

    I'm pretty sure some of the other forum members have used the XBee's ADC feature. Hopefully they'll chime in to help you out.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-12-08 18:41
    "line passing" allows analog input on one XBee to be mapped through automatically to matching PWM output on another XBee. The PWM can be converted back to analog with an resis. For example, suppose the MY address of the analog input XB is $1 and the MY address of the pwm output XBee is $2.

    On the analog input side, need AT commands:
    MY 1 ' my network address
    DL 2 ' destination XB network address
    D0 2 ' configures pin 20 as an analog input
    IL 3E8 ' sample at one second intervals

    On the PWM output side, the AT commands:
    MY 2 ' my network address
    DL 1 ' destination XB network address, if necessary
    P0 2 ' configure pin 20 as PWM output, will update once per second.
    IA 1 ' to enable line passing from MY=1

    There are other settings that could affect that. For example, if the analog input side is using a sleep mode, the XBee can be configured to transmit an update whenever it wakes.

    But, why do you want to convert back to analog, then to an ADC, and then to the Stamp? Why not just have the XBee send the data in digital form directly to the Stamp?
  • kvvanburekvvanbure Posts: 7
    edited 2012-12-09 09:09
    If I cannot transmit the analog signal then I could use the ADC on the transmit side in order to input a digital signal to the transmitting Xbee. Then output the signal from the receiving Xbee to the stamp. The reason for the ADC is that it is a design requirement for this project.

    My attempt at sending and receiving the digital signal is to input the digital signal from the ADC to the DIN input of the transmitting Xbee. Then connect the DOUT pin on the receiving Xbee to pin 13 on the stamp to read the distance from the sensor. So far this doesn't work and I'm a little stumped on what to try from here.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-12-09 10:25
    Are you required to use an ADC separate from the XBee itself?

    Din (pin2) and Dout (pin3) are for transmission of RS232 data (3 volt levels). One scenario is:
    Sharp---ADC---Basic.Stamp---Din.XBee- - - - - -XBee.Dout---BASIC.Stamp---Display

    You see, that requires two BASIC Stamps, because the ADC can't talk directly to the XBee. On the other hand, if your assignment allows you to use the ADC that is built into the XBee, then it becomes simpler:
    Sharp---ADC.XBee- - - - - -XBee.Dout---BASIC.Stamp---Display
    The transmitting XBee is set up with the command
    ATD0 2
    to enable the XBee ADC on pin 20. The data in digital form comes out on pin3 of the other XBee, and the Stamp reads the data and formats it for display.

    Please diagram what is expected for your assignment.
  • kvvanburekvvanbure Posts: 7
    edited 2012-12-09 13:09
    The requirements for this project are listed below:

    Requirements – The system must meet the following requirements:
    • use two or more external devices (e.g., sensors, actuators, or interfaces)
    • use the Basic Stamp board
    • use one AD0831, 8-bit analog-to-digital converter (A/D converter or ADC)
    Our original block diagram is shown below and we planned on driving the clock of the ADC with a 555 timer (page 11).
    Slide1.jpg


    It would be ideal if we could incorporate the ADC in some way. At this point just getting the stamp to output the distance as measured from the sensor would be nice. Also listed below are the parts we currently have.

    (2) Xbee 802.15.4
    (1) BS2 and USB board of education
    (1) Sharp IR Sensor
    ADC0831
    (1) Xbee breakout adapter board (to convert pin alignment to fit on breadboard)
    (1) Xbee 5/3.3V breakout adapter board (provides 3.3V power for Xbee from 5V source and converts pin alignment to fit on breadboard)
    720 x 540 - 30K
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-12-09 18:04
    If you just have to use the ADC0831, that could be rearranged like this,
    alternative.jpg


    However, you would need a level inverter to go from the XBee directly into the PC.
    1024 x 464 - 24K
  • kvvanburekvvanbure Posts: 7
    edited 2012-12-09 18:34
    If you just have to use the ADC0831, that could be rearranged like this,
    alternative.jpg


    However, you would need a level inverter to go from the XBee directly into the PC.

    That's the other problem we've run into. We don't have the adapter to interface the Xbee with the pc. I'm assuming this is the level inverter you are referring to?

    32400-L.jpg


    It looks like the best option for us then is going to be to use the on-board ADC on the Xbee. I think we can work around having to use the external ADC.
    700 x 700 - 58K
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-12-10 10:01
    Yes, that level inverter is good for a direct XBee to PC-USB connection. However, given the materials you have on hand, it makes sense to use the ADC/XBee at the remote end and the Stamp with its USB connection to the PC end. You'll learn a lot by doing that.

    Since you have the parts, and this is all about learning, you might as well also get the ADC0831 ADC working directly with the Stamp--PC, to read the Sharp sensor without the XBee link. Good to know!
  • kvvanburekvvanbure Posts: 7
    edited 2012-12-10 11:17
    We got the sensor to work with tha ADC and stamp directly. Our issue has been incorporating the wireless transmission. Thanks for the input. Going to try using the Xbee ADC on Thursday after finals. Hopefully all goes well. Suppose to present this project Friday afternoon.
  • kvvanburekvvanbure Posts: 7
    edited 2012-12-13 17:02
    This is the code we used to get the sharp sensor working with the ADC and BS2 board of education. The issue I'm having now is removing the ADC and using the onboard ADC on the Xbee in order to convert the analog signal to digital on the transmitting side. Trying to get the received digital signal to output the correct distance to the stamp has been somewhat baffling to me. The manner I have the Xbee's connected is as follows:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    Adc0831            PIN    9                       ' ADC0831 Chip Select (ADC0831.1)
    AdcClock           PIN    8                       ' ADC0831 Clock (ADC0831.7)
    AdcData            PIN    11                      ' ADC0831 Data (ADC0831.6)
    
    ' -----[ Constants ]-------------------------------------------------------
    
    span               CON    5                       ' 5 cm Per Data Point
    
    ' -----[ Variables ]-------------------------------------------------------
    
    result             VAR    Byte                    ' ADC8031 Result
    volts              VAR    Word                    ' Volts (0.01 Increments)
    cm                 VAR    Byte                    ' centimeters
    index              VAR    Nib
    test1              VAR    Byte                    ' Values For
    test2              VAR    Byte                    ' Interpolation
    slope              VAR    Word                    ' mV/cm between test points
    
    ' -----[ EEPROM Data ]-----------------------------------------------------
    
    Vout    DATA 251, 179, 139, 114, 97
            DATA  85,  76,  67,  62, 57
            DATA  53,  50,  48,  46, 43
            DATA   0
    
    ' -----[ Initialization ]--------------------------------------------------
    
    HIGH Adc0831                                      ' Disable ADC0831
    
    ' -----[ Program Code ]----------------------------------------------------
    
    DO
      GOSUB Read_GP2D12                               ' Read Sensor Value
      GOSUB Calculate_Distance                        ' Convert Value To cm
      DEBUG HOME, "Distance = ", DEC cm, " cm "
      PAUSE 100
    LOOP
    END
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    Read_GP2D12:
      volts = 0                                        ' Reset Sensor Value
      FOR index = 0 TO 2                               ' Read 3 Times
        'LOW Adc0831                                    ' Enable ADC0831
        'SHIFTIN AdcData, AdcClock, MSBPOST, [result\9] ' Read The Voltage
        'HIGH Adc0831                                   ' Disable ADC0831
        volts = volts + result                         ' Add The Values
        PAUSE 10
      NEXT
      volts = volts / 3                                ' Average The Readings
      RETURN
    
    
    Calculate_Distance:
      FOR index = 0 TO 15                              ' Search DATA Table For Value
        READ (Vout + index), test2                     ' Get Value From DATA Table
        IF (test2 <= volts) THEN EXIT                  ' Found Value
      NEXT
      SELECT index
      CASE 0
        cm = 10                                        ' Set To Minimum Distance
      CASE 1 TO 14                                     ' Calculate Distance
        cm = 10 + (5 * index)
      IF (test2 < volts) THEN                          ' Estimate Using Interpolation
          READ (Vout + index - 1), test1
          slope = (test1 - test2) * 10 / span          ' Calculate Slope
          cm = cm - ((volts - test2) * 10 / slope)
         ENDIF
        CASE 15
          cm = 80                                      ' Set To Maximum Distance
      ENDSELECT
      RETURN
    

    Transmitter side:
    Xbee on sparkfun breakout board

    Sharp analog IR sensor -> AD0 (pin 20) on transmitter.
    Vcc (pin 1) connected to 3.3V power
    Vref (pin 14) connected to 2.3 V power (through voltage divider from pin 1)
    Gnd (pin 10) connected to gnd

    Receiver side:
    Xbee on 5V/3.3V adapter board from Parallax

    Pins 1 and 10 connected to gnd
    Dout (pin 3) connected to pin P12 on BS2
    Pin 11 connected to 5V power

    My attempt at modifying the code for the ADC to work with the built in ADC of the Xbee is shown below. I know this isn't right because it still uses P13 in the code which won't be connected in the working product (P13 is the analog input connected to the stamp for testing).
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' Requires remote XBee in loopback mode (DIN connected to DOUT)
    
    Baud      CON     84                        ' 9600 baud non-inverted
    AD0       PIN     12                        ' XBee DOUT
    Tx        PIN     13                        ' XBee DIN
    
    ' -----[ Constants ]-------------------------------------------------------
    
    span               CON    5                       ' 5 cm Per Data Point
    
    ' -----[ Variables ]-------------------------------------------------------
    
    result             VAR    Byte                    ' ADC8031 Result
    volts              VAR    Word                    ' Volts (0.01 Increments)
    cm                 VAR    Byte                    ' centimeters
    index              VAR    Nib
    test1              VAR    Byte                    ' Values For
    test2              VAR    Byte                    ' Interpolation
    slope              VAR    Word                    ' mV/cm between test points
    
    ' -----[ EEPROM Data ]-----------------------------------------------------
    
    Vout    DATA 251, 179, 139, 114, 97
            DATA  85,  76,  67,  62, 57
            DATA  53,  50,  48,  46, 43
            DATA   0
    ' -----[ Program Code ]----------------------------------------------------
    
    DO
      GOSUB Read_GP2D12                               ' Read Sensor Value
      GOSUB Calculate_Distance                        ' Convert Value To cm
      DEBUG HOME, "Distance = ", DEC cm, " cm "
      PAUSE 100
      LOOP
    END
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    Read_GP2D12:
      volts = 0                                        ' Reset Sensor Value
      FOR index = 0 TO 2                               ' Read 3 Times
        SHIFTIN Tx, AD0, MSBPOST, [result\9]           ' Read The Voltage
        volts = volts + result                         ' Add The Values
        PAUSE 100
      NEXT
      volts = volts / 3                                ' Average The Readings
      RETURN
    
    
    Calculate_Distance:
      FOR index = 0 TO 15                              ' Search DATA Table For Value
        READ (Vout + index), test2                     ' Get Value From DATA Table
        IF (test2 <= volts) THEN EXIT                  ' Found Value
      NEXT
      SELECT index
      CASE 0
        cm = 10                                        ' Set To Minimum Distance
      CASE 1 TO 14                                     ' Calculate Distance
        cm = 10 + (5 * index)
      IF (test2 < volts) THEN                          ' Estimate Using Interpolation
          READ (Vout + index - 1), test1
          slope = (test1 - test2) * 10 / span          ' Calculate Slope
          cm = cm - ((volts - test2) * 10 / slope)
         ENDIF
        CASE 15
          cm = 80                                      ' Set To Maximum Distance
      ENDSELECT
      RETURN
    
Sign In or Register to comment.