Shop OBEX P1 Docs P2 Docs Learn Events
I want to treat Basic Stamp 2 purely as an interface, driven from a Visual Basi — Parallax Forums

I want to treat Basic Stamp 2 purely as an interface, driven from a Visual Basi

LittleTykeLittleTyke Posts: 34
edited 2007-08-28 12:30 in BASIC Stamp
New to Basic Stamps (but not Visual Basic), I want to treat my Basic Stamp 2 (on a Super Carrier board) purely as an interface to up to 8 LEDs, any of which may be switched on or off individually. I thought I could just connect the Stamp to a PC's COMM port and send some appropriate data down to the stamp to get it to switch its various pins high or low.

But I now realise that the Stamp has to be running a program downloaded from the Basic Stamp Development System (BSDS).

I have already worked through the initial Activities involving LEDs in "What's a Microcontroller".

Keeping it simple to begin with, imagine I have 8 low-current LEDs connected to Stamp pins P0 - p7, here's how I see the project:

Write a Stamp program to switch each LED on or off individually on receipt of certain data from the PC. Load the program into the Stamp, then unload the BSDS.

Fire up VB5 or 6 and write a VB program to send suitable data. One possible data format could be: Send a "0", "1", "2" etc to denote the LED required, followed by a "1" or "0" to denote ON or OFF.

Run the VB program. The program uses the MSComm control, opens the relevant COM port, sends the data, e.g. "3", "1", "6", "0" whereupon the Stamp program receives the data, switches LED #3 ON and LED #6 OFF.

Important: The VB program would use the *same* PC-to-Stamp link as the BSDS, namely Sout/Sin via the Super Carrier on-board 9-pin serial connector. That is, I would run BSDS to update the Stamp's running program when required, or run VB to communicate with the Stamp. By the way, I'm not (at least initially) interested in receiving any data FROM the Stamp.

The Stamp would never be operating "standalone", but only when connected to a PC and only to receive data from the PC and act upon it.

Comments? Book recommendations?

Thanks!

Comments

  • QuattroRS4QuattroRS4 Posts: 916
    edited 2007-08-26 10:54
    LittleTyke,


    Option1:
    This topic has come up a lot .... All you need to do is write a small program with the basic stamp IDE to react to a serial string sent from the P.C (have a look at SERIN and SEROUT and SERIN pin,Baud,[noparse][[/noparse]str yourstring\length] ) and IF it receives a specific string THEN you can goto a subroutine which changes the state of the I/O's......VB MSComm on the PC to serial port ...

    Option2:
    You mentioned changing the state of 8 I/O's - there is a DLL written which can be referenced from a VB application which allows you to change the state of D0-D7 - 8 I/O's on a PC parallel port pins 2 to pins 9 (if you have a parallel port).. I have used this previously with addin cards on P.C's with no original parallel port and multiple parallel ports . This was easy in DOS and win95/98 - from win2k and above it became more of an issue so this DLL is the answer .. the DLL is called inpout32.dll - I just wrote a simple VB6·sample for you·- make sure the DLL is in the application path or else copy it to 'system32' folder and register it (regsvr32).. Basically you should be able to address and read any port that is available...you can read back the values using inp ... see sample app..

    If you do 'Out(888),255' in the sample app. D0-D7 will be high (8 Pins·logic 1)
    If you do 'Out(888),0' in the sample app. D0-D7 will be·low (8 Pins·logic 0)

    and all values in between ... remember 0,1,2,4,8,16,32,64,128... for individual O/P's ...... and of course any·value in between .. for combinations ....


    look at these...
    www.lvr.com/parport.htm

    www.logix4u.net/inpout32.htm

    for pinout of parallel port -
    www.diyha.co.uk/electronics/parallel.html

    Hope this helps ...

    Regards,
    John Twomey

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'

    Post Edited (QuattroRS4) : 8/26/2007 11:29:07 AM GMT
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-26 12:02
    Thanks, John, but I really want to use a serial link, especially as the PC may well be some distance away from the interface. Also, many PCs (desktops) have two COM ports (mine has four), but only one parallel port.
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2007-08-26 12:48
    LittleTyke,
    OK - have a look at SERIN and SEROUT and SERIN pin,Baud,[noparse][[/noparse]str yourstring\length] in the BASIC STAMP manual .. I have used a stamp in such a manner a number of years back ..

    have a look at SERIN / SEROUT ...
    www.parallax.com/dl/docs/prod/stamps/web-BSM-v2.2.pdf

    Similar Project (serin/Serout to/from PC serial port) during prototyping ..HERE

    and more details

    www.parallax.com/html_pages/resources/custapps/app_barracuda.asp





    Regards,
    John

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'

    Post Edited (QuattroRS4) : 8/26/2007 1:12:09 PM GMT
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-26 14:33
    I've also found the following: "Pulling out 1 digit from binary number" posted by Jon Williams 11/8/2004 7:24 in which Jon explains how to map a byte received from VB to OUTL or OUTH. My program in the Stamp could be very minimalist! E.g. (based on Jon's post):

    DO
    SERIN 16, baud, [noparse][[/noparse]cmdByte]
    OUTL = cmdByte
    LOOP

    Somewhere in the Stamp docs it says I must specify the pin as 16 to designate the Sin pin on the Stamp. I do not want to faff about with a separate serial link (e.g. with a MAX232) if the onboard 9-pin connector will work from VB (which it apparently will on the Super Carrier board I have).

    If only I had my eight low-current LEDs right now, I could try it this very minute (they're on order)! But I only have standard LEDs and I've read somewhere that I should only connect a few of these to the Stamp I/O pins to avoid overloading the Stamp. Not sure what "a few" is, though. Maybe three or four, but I don't want to risk blowing the Stamp up after only using it for a day!
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-26 14:46
    Quick addendum to that last post: I may need to use DIRS = %11...11 to set the pins to outputs prior to the loop, and a WAIT in there somewhere so that the Stamp waits until a fresh byte is received.

    Post Edited (LittleTyke) : 8/26/2007 2:51:29 PM GMT
  • FranklinFranklin Posts: 4,747
    edited 2007-08-26 14:51
    You will need to install a resistor in line with the LED and stamp pin to limit the current to the LED so why not calculate the max current you want from each pin and use that resistor with the LEDs you have? They may not be as bright but they would work to prove your concept.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-26 15:09
    Yes, I currently have just one LED connected via a 470 Ohm resistor to pin 14 as per Activity #1 in the "What's a Microcontroller" tutorials. But I didn't realise I could connect several LEDs (to different pins, naturally!) by upping the resistance. (Although I know a fair bit about VB programming, I am a bit of a newbie when it comes to hardware, although I did solder together some rudimenatry circuits many years ago from Forest Mimms' hand-drawn electronics books.) What do you reckon I'd need resistance-wise if I connected a standard LED to each of pins 0 through 7? As you say, they don't need to be very bright for testing, just visibly on or off.
  • pwillardpwillard Posts: 321
    edited 2007-08-26 16:13
    You should only need about 220 Ohm or so (see example attached)· The BOE actiually has these resistors on board already.· Keep in mind that MAX source/sink current is 50 MA. My pin assignments in the example were arbitrary, change at will.

    Note:· LED's Connected to Vdd use negative logic, LOW PIN = Lit LED.·

    ALSO: $15.00 will by you a pre-assembled RS232 Level converter Building Block from SCHMARTBOARD to save you the hassle of building it yourself.










    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    There's nothing like a new idea and a warm soldering iron.

    Post Edited (pwillard) : 8/26/2007 4:21:57 PM GMT
    776 x 408 - 9K
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-08-26 16:48
    Hi Little Tyke, if you check this link out http://forums.parallax.com/showthread.php?p=654587 it uses the same method as you are suggesting.

    Each switch adjusts the value of a byte at the PC end although in my case I only used 6 switches. I did the manipulation·by subtracting or adding ·integers when a switch was operated. +-1 , +-2 ,+-4 ,+-8 ,+-16 ,+-32. At the Stamp this can be interpreted however you want, OUTL or OUTH·for direct output or reading individual bits BIT0 BIT1 etc for use internal to the Stamps code.

    Using only 6 switches if you wanted to implement OUTX you might mask the input:- Result=In_data & 63

    One possible drawback with using the default programming port is that the characters are echoed back to the PC after each transmission and need to be parsed out before transmission of the next data.

    Jeff T.
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-26 17:07
    Jeff, I would just ignore the data echo at the PC end. My VB prog would not have any RX logic, only TX. The VB prog would be "the brainz" behind the whole project - keeping track of which LEDs are on or off, switching them accordingly, using AND and OR as appropriate.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-08-26 18:20
    Here is another thread on the same topic:

    http://forums.parallax.com/showthread.php?p=667413

    For the current calculations, take a look in the WaM manual and read the section on Ohms Law. You can use it to determine how much resistance is needed to limit the current at the pins to the correct level.

    Also, the BS2 has 4 "banks" of 4 pins each. Each pin has a current limit, and each bank has a limit when usiing multiple pins on the bank. Since you are using 8 LEDs, placing 2 per bank will give you more room for error than 4 per bank.

    The room for error factor comes into play in that electronic components generally have a +/- tolerance range that they fall within, especially with resistors.

    Post Edited (Kevin Wood) : 8/27/2007 1:01:49 AM GMT
  • D FaustD Faust Posts: 608
    edited 2007-08-27 00:15
    I don't think that the boe has built in resistors, but the homework board does.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    LOOKDOWN ThisThread, [noparse][[/noparse]Your_?, My_?, Cool_Thing], looknum
    LOOKUP looknum, [noparse][[/noparse]1, 2, 3], subnum
    ON subnum GOTO Hope_this_helps, Thanks!, WOW!!
    END 
    
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-27 13:02
    General question: Is it better to connect a LED:

    (a) cathode to Vss, anode to Stamp I/O pin (turned on with logical one)

    or

    (b) anode to Vdd, cathode to Stamp I/O pin (turned on with logical zero)

    Use of appropriate resistor is assumed in both cases.

    Post Edited (LittleTyke) : 8/27/2007 1:09:26 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-08-27 13:33
    Historically, there's been chips that are much more able to deal with "sinking" current (providing a 'low' output, assuming the other end of your LED is attached to +5) than "sourcing" current (providing a 'high' output, assuming the other end of your LED is connected to ground). And if you use an 'Open Collector' chip, this is the only alternative you get, as well as using a Darlington chip.

    Thus, 'old-timers' like myself will tend to prefer anode to Vdd, cathode toward BS2 I/O pin, and use a "LOW" to turn "ON" the LED.

    Now, the PIC chip the BS2 is based on has been engineered to be able to 'source' or 'sink' about the same amount of current. And frankly, it makes more intuitive sense to use a 'HIGH' to turn "ON" the LED. So the bottom line is, with the PIC chip (and BS2) it doesn't really matter which you use. For flexibility and historical purposes, I'd still recommend using the BS2 to 'sink' current.
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-27 15:00
    allanlane5, brilliant exposition! Now I know what the difference is between sourcing and sinking!

    Another comment: I have just been experimenting with different value resistors, 470 Ohm, 680, 820, 4K7 and there is no appreciable difference in the brightness of the LED. I expected to notice a definite weakness between 470 and 4K7.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-08-27 15:23
    An LED is a very non-linear device. What I mean by that is -- once you have a certain amount of current through it (usually about 8 mA, but this depends heavily on the color and current rating of the LED) any current above that doesn't really make the LED brighter.

    And there's a current-to-brightness 'knee' at that 8 mA (or whatever) which means a small increase or decrease in current has large changes in brightness.

    Ways to get around this are with "PWM" (Pulse Width Modulation) -- basically you make it 'full bright' for 50% of the time, and 'off' for the other 50%, so the human eye 'averages' this as a 50% 'dim-ness'. That's pretty easy to do with digital circuitry. The other way to get around this is to use a D-to-A converter to lower the voltage (and thus current) through the LED around that "knee". This is much harder to get right, depends on exactly what LED's you're using, and takes more hardware. So most people use "PWM" to change LED brightness.

    Now, you can do this with a BS2, but since it only does 'single tasking', you can't use the BS2 "PWM" command to light more than one LED at a time. Bummer. However, since the BS2 runs at about 2000 Instructions Per Second, you CAN use "HIGH" and "LOW" instructions on multiple LED's to implement a "PWM" in PBasic code. Then you just have to worry about your "flash rate" -- how long you leave it on and off.

    If you keep your "flash rate" above 60 Hz (8 mSec on, 8 mSec off), then the human eye won't 'see' the separate 'blinks', and will average the LED light into a 'dimness'. This should be very doable with the BS2.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-08-27 15:25
    Oh, one more thing. The BS2 can't really put out enough current (or sink it, for that matter) to light 8 LED's at 15 mA continuously -- but it's simple to add a single Darlington chip array which can.

    Edit:· Some simple math suggests that with a 4.7 KOhm resistor in series with the LED, you'd be putting about 700 uA through the LED.· This really shouldn't light the LED, so something else must be happening.

    The equation is:

    5 volts - 1.4 volts(LED voltage) == Voltage across the current limiting resistor, Vr.· == 3.6 volts.

    Now, V = IR, so Vr = I * R, or Vr / R == I.· 3.6 / 470 == 7.6 mA, which lights the LED.

    3.6 / 4.7K == 765 uA, which shouldn't light the LED.



    Post Edited (allanlane5) : 8/27/2007 3:33:20 PM GMT
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-27 16:38
    Maybe these red 5mm LEDs I'm using - they're from a Maplin "running lights" kit (http://preview.tinyurl.com/3ax3hl) - are low-current. The 4K7 resistor is colour-coded yellow - violet - red - gold.
  • LittleTykeLittleTyke Posts: 34
    edited 2007-08-28 12:30
    I sent an email to Velleman in Belgium (who make the running lights kit) and they have just confirmed that these ARE low-current LEDs! So I do have the eight I need.
Sign In or Register to comment.