Shop OBEX P1 Docs P2 Docs Learn Events
Arduino to BasicStamp 2 Single Wire Serial Communication — Parallax Forums

Arduino to BasicStamp 2 Single Wire Serial Communication

simlcsimlc Posts: 8
edited 2015-03-24 20:00 in BASIC Stamp
I am trying to use an Arduino to send 0,1,or 2 to my Stamp 2. I have the Arduino pin 1 (TX) going to the Stamp pin 2 (SIN) via a 220 ohm resistor. I have the Stamp pin 1 (SOUT) going to my laptop's COM1 pin 2 (RX) in an attempt to see what the Arduino is sending the Stamp. Here is the Stamp's code:
'  ($STAMP BS2)
'  (PBASIC 2.5)
Image VAR Byte

Setup:
  Image = 0

Main:
  SERIN 16, 396, 20 Face_Image, [Image]
  SEROUT 16, 416, [Image]      'I read somewhere that you needed to add 20ms to the BAUDMODE number. Didn't make any difference in what I saw in my serial monitor.
  GOTO Main
END

And here is the Arduino code:
void setup()
{
  Serial.begin(2400, SERIAL_8N1)
}

void loop()
{
  Serial.print("0");
  delay(1000);
  Serial.print("1");
  delay(1000);
  Serial.print("2");
  delay(1000);
}

On my laptop I have a serial monitor running that is looking at COM1. What I hoped to see was some version of 0,1, and 2 (hex, decimal, binary, ASCII, something!)
What I consistently saw, in decimal, was 6, 103, 51. Not even close to 48, 49, 50.
Anyone have any ideas as to what I am doing wrong? The only thing I can think of is the level translation circuitry on the Stamp. The Arduino is sending out 0-5v (space = 0v). Supposedly, the BAUDMODE should be able to handle that.
I have spent 2 days tweaking various things and haven't figured it out.
Any thoughts would be appreciated!
Thanks.

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2015-03-17 09:45
    Welcome to the forums.

    Start first by getting the Arduino to talk to the laptop. I think the Arduino pin 1 is at ttl levels and not what the Stamp pin 16 or laptop are expecting. If you can confirm that, then you can set the Stamp to use another pin at ttl levels. You can also see if the Stamp can talk to the laptop by setting the Stamp in a loop to print out a known string, say "test". The Stamp Helpfile in the IDE has several sample programs. Look under SERIN or SEROUT.

    Now connect the Arduino to the Stamp pin (no, you don't have to add 20mS to the baud number)
  • simlcsimlc Posts: 8
    edited 2015-03-17 12:25
    Thanks, Tom. I have connected the Stamp to the laptop. Everything I send the Stamp gets sent back to the laptop okay. No problem there.
    I am concerned about connecting the Arduino directly to the laptop. As you pointed out, the Arduino is using TTL levels, while the laptop serial port will be using normal RS232 levels, +/- 12 volts. I do not want to fry my Arduino. I am using all the other pins on the Stamp to drive an 8x8 LED matrix, so I have no extra pins to play with, outside the SERIN and SEROUT pins.
  • stamptrolstamptrol Posts: 1,731
    edited 2015-03-18 07:44
    Good progress.

    For testing, you won't damage the Arduino if you connect it to the laptop through some 1K resistors. But, the Arduino will have to be set to an "inverted" baudmode to talk to the laptop's "real" rs-232 logic. I know Stamps can set the general pins to true or inverted mode but I am unsure about the Arduino.

    If you're scarce on Stamp pins and must use the pin 16 serial on the Stamp , you can run the Arduino ttl serial through a rs-232 interface chip MAX233 or ST232 to shift the levels to what the laptop expects.

    You're very close now!
  • simlcsimlc Posts: 8
    edited 2015-03-18 08:12
    Thanks, Tom. I came to the realization that I would have to use a MAX232-type chip to get the interfacing correct. I ran a quick TTL to TTL serial test between my Raspberry Pi and another Arduino board last night. There were no problems with the received data, it came through exactly as expected. It confirmed my suspicion that it was a translation issue between the TTL and RS232. Going out to get a chip today and try again. I was hoping to keep the chip count down, but you gotta do what you gotta do. :)
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2015-03-21 08:29
    simic,
    A MAX232 is unnecessary.
    If you are using the BoE/HWB D-Sub connector (pin 3) then all you need is a simple transistor-based inverter (invert the Arduino output.)
    If you need a sketch of that then Reply ("PJ, how about a sketch of that circuit?")

    If you SERIN using 0-15 then a "direct connection" is fine (add a 2K for "mistakes".)

    Either way, use a True BAUDMODE.

    I have verified all of this, using:
    void setup()
    {
      Serial.begin(9600);
      // output is from Digital pin 1
    }
    
    void loop ()
    {
      Serial.println("0");
      delay(2000);
      Serial.println("1");
      delay(2000);
      Serial.println("2");
      delay(2000);
      Serial.println("3");
      delay(2000);
      Serial.println("2");
      delay(2000);
      Serial.println("3");
      delay(2000);
    }
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    got_it VAR Byte
    OUTPUT 0
    OUTPUT 1
    
    Main:
      SERIN 16,84,[DEC got_it]    ' SERIN 10,  for direct conn.
    
      IF (got_it = 0) THEN
        GOTO twaszero
      ELSEIF (got_it = 1) THEN
        GOTO twasone
      ELSEIF (got_it = 2) THEN
        GOTO twastwo
      ELSE
        GOTO noneoftheabove
      ENDIF
      ' Look Out Below !!!
      GOTO Main
    
    twaszero:
      HIGH 0
      LOW 1
      GOTO Main
    
    twasone:
      LOW 0
      HIGH 1
      GOTO Main
    
    twastwo:
      HIGH 0
      HIGH 1
      GOTO Main
    
    noneoftheabove:
      LOW 0
      LOW 1
      GOTO Main
    
  • ercoerco Posts: 20,257
    edited 2015-03-21 15:05
    Great to see you're back, PJ, doing what you do best: helping out with great tech knowledge! We've missed you.
  • simlcsimlc Posts: 8
    edited 2015-03-21 19:34
    Thanks, PJ! I am using the BS2 by itself and am using the SERIN 16, as your code is set to do. I will try the transistor and see what happens. :)
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2015-03-22 06:53
    OK.

    To make things clear(er) --

    IMG_1818_zpsnwkvmttb.jpg

    The module on the left is the Arduino and that on the right is the Stamp.
    I used 1Ks because they were right there at the time. The transistor is a "you pick it" NPN.
  • simlcsimlc Posts: 8
    edited 2015-03-22 14:55
    AJ, yep, this is just how I envisioned it. I will try it tomorrow. Thanks!!
  • simlcsimlc Posts: 8
    edited 2015-03-24 08:46
    AJ, I tried the transistor and it appeared to work great. I was seeing the correct data. Something was still messed up with my code because the IF statements were not responding to the different data like they should have. It may be a moot point now: I think I fried my Stamp by inadvertently moving the SERIN to the RS232 connection while the circuit was under power. The Stamp doesn't appear to respond, or do, anything now. The LED matrix that was being driven by the Stamp doesn't even flicker now. I'll have to do some more tests. Thanks for your help!!

    Not sure how to mark this as resolved. The original problem has been resolved. Frying my Stamp wasn't part of that problem.

    Lynn
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2015-03-24 12:30
    You may want to try disconnecting everything, down to Stamp alone, to see if it can be programmed/re-programmed and then build it back up step by step.

    PJA

    [If you go back to your original post and press the Edit button you can then change Un/Resolved etc.]
  • simlcsimlc Posts: 8
    edited 2015-03-24 14:27
    AJ, I used a logic probe to check all the pins on my Stamp. The VDD and VSS pins showed high and low, respectively, as expected. The SERIN and SEROUT pins both showed low. All the other pins did not register on my probe, like they were all tri-stated. I removed some of the wires to see if the matrix was causing the problem. No change.

    I went back to my original post, edited it, but could not see anyway there to change the status to Resolved.

    Thanks for your responses!!
    Lynn
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2015-03-24 19:08
    simlc wrote: »
    AJ, ...
    I went back to my original post, edited it, but could not see anyway there to change the status to Resolved.

    Sorry we can't get your situation squared away.

    Otherwise, look for the little gadget labelled "Prefix", just north of "Title". There's a dropdown with 3 options: (none), Solved, and Unresolved.

    [I think it's there either way, but you may have to bop that "Go Advanced" button.]
    Edit Post then Go Advanced gets you access to the Prefix dropdown.

    And, it's PJ.
  • simlcsimlc Posts: 8
    edited 2015-03-24 20:00
    PJ, (really sorry about messing up the initials)
    Found the "solved" prefix. Thanks for all your help and patience.
    Lynn
Sign In or Register to comment.