Shop OBEX P1 Docs P2 Docs Learn Events
Connect Raspberry to Basic Stamp 2 — Parallax Forums

Connect Raspberry to Basic Stamp 2

satriaoosatriaoo Posts: 20
edited 2015-06-03 14:45 in BASIC Stamp
Excuse me, Sir.

I've got project to running hexapod using Raspberry pi and Basic stamp. But, i'm beginner.

I'm trying to connect Raspberry pi model B to Basic Stamp 2 using USB cable. But, always failed. I found the code on this forum about this. I try to copy and paste. But, failed. Is there any 'special' code for connect raspbery pi to basic stamp? So, what should i do? thank you. :)

btw, on Raspberry, i'm using Stain Phyton Editor.

Thank you, sir :')
«1

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-14 18:34
    It will likely help someone trying to help you, if you provided links to the code you're trying to use.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-14 18:41
    Duane Degn wrote: »
    It will likely help someone trying to help you, if you provided links to the code you're trying to use.


    I'm using this code (on attachment) that i got from this link: http://forums.parallax.com/showthread.php/154208-Basic-Stamp-serial-connection-Raspberry-Pi

    I only change pin number. :/
    968 x 576 - 156K
  • GenetixGenetix Posts: 1,740
    edited 2015-01-14 19:23
    Which BS2 board are you using and do you know what BS2 module is on it?
  • satriaoosatriaoo Posts: 20
    edited 2015-01-14 19:37
    Genetix wrote: »
    Which BS2 board are you using and do you know what BS2 module is on it?

    i'm using Board of Education Develompment Board - USB, sir. :/
    600 x 800 - 132K
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-14 19:55
    Is your tri power adapter thingy touching the 9V battery connectors and causing a short?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-14 20:13
    Is your tri power adapter thingy touching the 9V battery connectors and causing a short?

    I was wondering the same thing.

    I managed to blow out my PropBOE with a single terminal connector.

    attachment.php?attachmentid=91001&d=1332715119

    I now use heat shrink tubing over the 9V connectors on my boards.

    BTW, In case any of you are unaware, the PropBOE has a lifetime warranty. (Even if you do something dumb to blow it up.)
  • satriaoosatriaoo Posts: 20
    edited 2015-01-14 20:37
    Duane Degn wrote: »
    I was wondering the same thing.

    I managed to blow out my PropBOE with a single terminal connector.

    attachment.php?attachmentid=91001&d=1332715119

    I now use heat shrink tubing over the 9V connectors on my boards.

    BTW, In case any of you are unaware, the PropBOE has a lifetime warranty. (Even if you do something dumb to blow it up.)

    No, Sir. On hardware, there is nothing problem. When i still using only basic stamp (coding from mac and only connect to mac), the LED is on. (i use the standard code like "High 15 Pause 1000")

    And when i connect raspberry with Basic Stamp, and wanna turn on the LED from Raspberry, the LED cannot on. (in this case, i use the code from last thread. Look at the attachment. Hehe)
    968 x 576 - 156K
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-14 20:42
    Try replacing port='/dev/ttyUSB2' with port='/dev/ttyUSB0'

    /dev/ttyUSB2 is the third USB to serial converter plugged in to your Raspberry Pi. If you only plug one in, it will be /dev/ttyUSB0
  • satriaoosatriaoo Posts: 20
    edited 2015-01-14 21:59
    Try replacing port='/dev/ttyUSB2' with port='/dev/ttyUSB0'

    /dev/ttyUSB2 is the third USB to serial converter plugged in to your Raspberry Pi. If you only plug one in, it will be /dev/ttyUSB0

    Umm, iits okay. Thank you, sir.

    But, when i run the code, there is error. (on attachment)

    What should i do? :/ Is there any package that i must install? ://
    800 x 600 - 173K
  • GenetixGenetix Posts: 1,740
    edited 2015-01-15 01:17
    You have the LED connected to P15 but the code uses P9.

    Try this!
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    ' I/O Declaration (Added)
    LedPin PIN 15 ' Added
    SerialPin PIN 16 ' Changed from CON to PIN
    
    ' Constant Declaration (Added)
    Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted)
    LoopDelay CON 1000 ' Added
    
    ' Variable Declaration (Added)
    state VAR Byte
    
    
    HIGH LedPin ' Changed from 9 to LedPin
    PAUSE 200
    LOW  LedPin ' Changed from 9 to LedPin
    
    
    DO ' Changed from Main: to DO
      SERIN SerialPin, BAUD, [state]
    
      IF (state = 1)  THEN
        HIGH LedPin ' Changed from 9 to LedPin
      ELSEIF (state = 2)  THEN
        LOW LedPin ' Changed from 9 to LedPin
      ENDIF
    
      PAUSE LoopDelay
    LOOP ' Changed from Goto Main to LOOP
    
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-15 06:37
    satriaoo wrote: »
    Umm, iits okay. Thank you, sir.

    But, when i run the code, there is error. (on attachment)

    What should i do? :/ Is there any package that i must install? ://

    Is your indentation right? Indentation matters in Python. Are you consistently using only spaces or only tabs?
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 18:39
    Genetix wrote: »
    You have the LED connected to P15 but the code uses P9.

    Try this!
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    ' I/O Declaration (Added)
    LedPin PIN 15 ' Added
    SerialPin PIN 16 ' Changed from CON to PIN
    
    ' Constant Declaration (Added)
    Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted)
    LoopDelay CON 1000 ' Added
    
    ' Variable Declaration (Added)
    state VAR Byte
    
    
    HIGH LedPin ' Changed from 9 to LedPin
    PAUSE 200
    LOW  LedPin ' Changed from 9 to LedPin
    
    
    DO ' Changed from Main: to DO
      SERIN SerialPin, BAUD, [state]
    
      IF (state = 1)  THEN
        HIGH LedPin ' Changed from 9 to LedPin
      ELSEIF (state = 2)  THEN
        LOW LedPin ' Changed from 9 to LedPin
      ENDIF
    
      PAUSE LoopDelay
    LOOP ' Changed from Goto Main to LOOP
    


    wow! thank you, sir! i will try. :)
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 18:42
    Is your indentation right? Indentation matters in Python. Are you consistently using only spaces or only tabs?

    Aha! it was my fault. Now, my code can running. Thank you very much, sir. :)
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-15 18:57
    satriaoo wrote: »
    Aha! it was my fault. Now, my code can running. Thank you very much, sir. :)
    You're welcome!
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 19:09
    You're welcome!

    But, sir. My raspberry pi still cannot turn on the LED. :/ my LED is on the Basic Stamp 2. (so, i wanna turn on the LED from raspberry pi. In case, the LED is on the basic stamp)

    In my code on SPE (attachment hehe), i think i must calling the function, isn't it? but i dont know what should i type. :/:/
    600 x 800 - 124K
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-15 19:42
    satriaoo, it would be easier for us to read your code if you could paste the text between code tags.

    To use code tags, start with [noparse]
    and end with
    
    [/noparse]. This will allow your code to show up properly indented (or at least without the indentation changed my the forum software).

    Without code tags, when you type (or paste) the following text it will not appear properly formatted.

    ____if value == '1':
    ______print ('LEDON')
    ______ser.write('2/n')
    ____elif value == '2':

    The above indentation will not appear once the post is submitted. Instead of appearing indented as it does above, it will show up like the text below.

    if value == '1':
    print ('LEDON')
    ser.write('2/n')
    elif value == '2':

    As you can see the indentation was lost.

    However if you use code tags:

    [noparse]
    [/noparse]
    [color=white]____[/color]if value == '1':
    [color=white]______[/color]print ('LEDON')
    [color=white]______[/color]ser.write('2/n')
    [color=white]____[/color]elif value == '2':
    [noparse]
    
    [/noparse]

    Then your code will appear as:
        if value == '1':
          print ('LEDON')
          ser.write('2/n')
        elif value == '2':
    

    You should at least be able to cut and paste BS2 code this way. Getting the Python code from the RPi to the forum editor might not be as easy.

    By sharing your code as text, we can then use the same code you are using without the need to type the code from the photos of the screens. I for one would be much more willing to test the code if I didn't have to manually type it into the program editors.
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-15 19:51
    You have /n in your Python code. That just prints /n. If you want a newline, you need to write \n instead. I don't think this is the main problem, but it is a problem.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 19:53
    Duane Degn wrote: »
    satriaoo, it would be easier for us to read your code if you could paste the text between code tags.

    To use code tags, start with [noparse]
    and end with
    
    [/noparse]. This will allow your code to show up properly indented (or at least without the indentation changed my the forum software).

    Without code tags, when you type (or paste) the following text it will not appear properly formatted.

    ____if value == '1':
    ______print ('LEDON')
    ______ser.write('2/n')
    ____elif value == '2':

    The above indentation will not appear once the post is submitted. Instead of appearing indented as it does above, it will show up like the text below.

    if value == '1':
    print ('LEDON')
    ser.write('2/n')
    elif value == '2':

    As you can see the indentation was lost.

    However if you use code tags:

    [noparse]
    [/noparse]
    [color=white]____[/color]if value == '1':
    [color=white]______[/color]print ('LEDON')
    [color=white]______[/color]ser.write('2/n')
    [color=white]____[/color]elif value == '2':
    [noparse]
    
    [/noparse]

    Then your code will appear as:
        if value == '1':
          print ('LEDON')
          ser.write('2/n')
        elif value == '2':
    

    You should at least be able to cut and paste BS2 code this way. Getting the Python code from the RPi to the forum editor might not be as easy.

    By sharing your code as text, we can then use the same code you are using without the need to type the code from the photos of the screens. I for one would be much more willing to test the code if I didn't have to manually type it into the program editors.


    Oh, hehehe, im sorry, sir.

    So, this is my BS2 Code.
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    
    ' I/O Declaration (Added)
    LedPin PIN 15 ' Added
    SerialPin PIN 15 ' Changed from CON to PIN
    
    ' Constant Declaration (Added)
    Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted)
    LoopDelay CON 1000 ' Added
    
    ' Variable Declaration (Added)
    state VAR BYTE
    
    
    HIGH LedPin ' Changed from 9 to LedPin
    PAUSE 200
    LOW  LedPin ' Changed from 9 to LedPin
    
    
    DO ' Changed from Main: to DO
      SERIN SerialPin, BAUD, [state]
    
      IF (state = 1)  THEN
        HIGH LedPin ' Changed from 9 to LedPin
      ELSEIF (state = 2)  THEN
        LOW LedPin ' Changed from 9 to LedPin
      ENDIF
    
      PAUSE LoopDelay
    LOOP ' Changed from Goto Main to LOOP
    

    and my SPE code:
    import serial
    
    ser=serial.Serial(port='/dev/ttyUSB0', baudrate=9600, bytesize=8, parity=''N', stopbits=1, timeout=None)
    print("Starting!")
    while True:
          val=raw_input('Value: ')
          if val == '1':
              print('LED ON')
              ser.write('1/n')
          elif val =='2':
              print('LED OFF')
              ser.write('2/n')
          elif val=='exit':
               ser.close()
               exit()
    ser.close()
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-15 20:42
    satriaoo wrote: »
    So, this is my BS2 Code.

    . . .

    and my SPE code:

    That looks great. Thank you.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 20:44
    Duane Degn wrote: »
    That looks great. Thank you.

    but, still doesn't works, sir. :/ Led can't be on if i compile from Raspberry Pi. :/:/
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-15 20:48
    satriaoo wrote: »
    but, still doesn't works, sir. :/ Led can't be on if i compile from Raspberry Pi. :/:/

    Does the LED blink once quickly when you first turn on the BS2 board or reset the Stamp? It should with the code you're using.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 20:53
    Does the LED blink once quickly when you first turn on the BS2 board or reset the Stamp? It should with the code you're using.

    yap. only once. :/

    I wanna turn on if i press "1" button, and turn of if i press "2" button. But, it can't works. Are you know what should i do, sir? :/:/:/
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-15 20:53
    satriaoo wrote: »
    but, still doesn't works, sir. :/ Led can't be on if i compile from Raspberry Pi. :/:/

    But now we can try the code also. This should make it easier for others trying to help to find the problem.
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-15 20:58
    satriaoo wrote: »
    yap. only once. :/

    I wanna turn on if i press "1" button, and turn of if i press "2" button. But, it can't works. Are you know what should i do, sir? :/:/:/

    Try this:
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    
    ' I/O Declaration (Added)
    LedPin PIN 15 ' Added
    SerialPin PIN 15 ' Changed from CON to PIN
    
    ' Constant Declaration (Added)
    Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted)
    LoopDelay CON 1000 ' Added
    
    ' Variable Declaration (Added)
    state VAR BYTE
    
    
    HIGH LedPin ' Changed from 9 to LedPin
    PAUSE 200
    LOW  LedPin ' Changed from 9 to LedPin
    
    
    DO ' Changed from Main: to DO
      SERIN SerialPin, BAUD, [state]
    
      IF (state = "1")  THEN
        HIGH LedPin ' Changed from 9 to LedPin
      ELSEIF (state = "2")  THEN
        LOW LedPin ' Changed from 9 to LedPin
      ENDIF
    
      PAUSE LoopDelay
    LOOP ' Changed from Goto Main to LOOP
    

    You are sending ASCII 49 or 50 ("1" or "2") but your code is expecting ASCII 1 or 2. If you do state = "x" instead of state = x, it should work.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 21:07
    Try this:
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    
    ' I/O Declaration (Added)
    LedPin PIN 15 ' Added
    SerialPin PIN 15 ' Changed from CON to PIN
    
    ' Constant Declaration (Added)
    Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted)
    LoopDelay CON 1000 ' Added
    
    ' Variable Declaration (Added)
    state VAR BYTE
    
    
    HIGH LedPin ' Changed from 9 to LedPin
    PAUSE 200
    LOW  LedPin ' Changed from 9 to LedPin
    
    
    DO ' Changed from Main: to DO
      SERIN SerialPin, BAUD, [state]
    
      IF (state = "1")  THEN
        HIGH LedPin ' Changed from 9 to LedPin
      ELSEIF (state = "2")  THEN
        LOW LedPin ' Changed from 9 to LedPin
      ENDIF
    
      PAUSE LoopDelay
    LOOP ' Changed from Goto Main to LOOP
    

    You are sending ASCII 49 or 50 ("1" or "2") but your code is expecting ASCII 1 or 2. If you do state = "x" instead of state = x, it should work.


    ok, i will try, sir.

    But for my code on raspberry pi (SPE), is there any false? Should i add new code or calling the function? Because, i wanna turn on the LED from raspberry pi (SPE). :/
    import serial
    
    ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity=''N', stopbits=1, timeout=None)
    print("Starting!")
    while True:
          val=raw_input('Value: ')
          if val == '1':
              print('LED ON')
              ser.write('1/n')
          elif val =='2':
              print('LED OFF')
              ser.write('2/n')
          elif val=='exit':
               ser.close()
               exit()
    ser.close()
    
    
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-15 21:14
    ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity=''N', stopbits=1, timeout=None)
    
    should read
    ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None)
    
    You have an extra quote in the parity='N' that makes it not compile.

    You don't need to send /n, not to mention the fact that you probably meant \n if you want a newline. Just send "1" or "2" instead with no newline - it will be less stuff for the Stamp to process.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-15 22:46
    Try this:
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    
    ' I/O Declaration (Added)
    LedPin PIN 15 ' Added
    SerialPin PIN 15 ' Changed from CON to PIN
    
    ' Constant Declaration (Added)
    Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted)
    LoopDelay CON 1000 ' Added
    
    ' Variable Declaration (Added)
    state VAR BYTE
    
    
    HIGH LedPin ' Changed from 9 to LedPin
    PAUSE 200
    LOW  LedPin ' Changed from 9 to LedPin
    
    
    DO ' Changed from Main: to DO
      SERIN SerialPin, BAUD, [state]
    
      IF (state = "1")  THEN
        HIGH LedPin ' Changed from 9 to LedPin
      ELSEIF (state = "2")  THEN
        LOW LedPin ' Changed from 9 to LedPin
      ENDIF
    
      PAUSE LoopDelay
    LOOP ' Changed from Goto Main to LOOP
    

    You are sending ASCII 49 or 50 ("1" or "2") but your code is expecting ASCII 1 or 2. If you do state = "x" instead of state = x, it should work.

    when i running on raspberry, still not works, sir. The LED which i put on Basic Stamp, cannot turn on. :/
    800 x 600 - 116K
  • ElectrodudeElectrodude Posts: 1,614
    edited 2015-01-16 10:06
    Add this after the SERIN in your BS2 code:
    DEBUG HEX2 state, CR, LF
    
    Have the Pi send stuff to the Stamp. What does it print on the BS2 debug screen?
  • GenetixGenetix Posts: 1,740
    edited 2015-01-16 16:26
    Based on the SERIN statement the BS2 is expecting 1 byte of non-inverted serial data but is that what the Raspberry PI is actually sending?
    The newlines seem unneeded and why not just use binary values instead of ASCII.
    Using ASCII limits the program to 10 number values (0 to 9) for a 1 byte variable.
  • satriaoosatriaoo Posts: 20
    edited 2015-01-18 22:31
    Add this after the SERIN in your BS2 code:
    DEBUG HEX2 state, CR, LF
    
    Have the Pi send stuff to the Stamp. What does it print on the BS2 debug screen?

    Not work sir. :/

    am i must installing any software or package (maybe Pbasic) on raspberry pi? The LED doesn't works. If i push "1" button, it only show "LED ON" on the terminal, but the LED didn't on. -.-
Sign In or Register to comment.