Shop OBEX P1 Docs P2 Docs Learn Events
PWM as analogue input — Parallax Forums

PWM as analogue input

soulcalibresoulcalibre Posts: 2
edited 2011-11-22 09:41 in BASIC Stamp
Is there any way of using PWM as an input, rather than just an output?

We have an IR receiver, and we wish to use it as an analogue sensor, rather than a digital one. We've only found a way to use PWM as an output though.

Any help? :cool:

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-22 07:50
    Go to this website and click on the "app-notes" link. One of the application notes is on using a Stamp to measure voltages (specifically battery voltage) which you could use in your application. Look under "BASIC Stamp RCtime command".
  • soulcalibresoulcalibre Posts: 2
    edited 2011-11-22 08:14
    Here's the code we're trying out:
    [COLOR=#000000][FONT=monospace]rct var word ' RCtime value[/FONT][/COLOR]
    Vx var word ' voltage value calculated
     ' we want this to = applied voltage Vx
    Cn1 con 48576 ' first constant, see below
    Cn2 con 8 ' second constant, ditto
    ' circuit attached to P0
    low 0
    loop:
     RCtime 0,0,rct
     low 0
     Vx=Cn1 / rct + Cn2
     debug home,dec? rct,"Vx=",dec Vx/10,".",dec1 Vx
     pause 500
    goto loop
    

    Not sure which pin should be which though. What we've done is replace "rct" with the pin # of one of our IR sensors. Are we at least on the right path, or completely messed up?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-22 09:41
    Partly messed up. Please read the chapter in the Stamp Manual on the RCTIME statement so you know which parameter is which and how the statement works. Also reread the application note. I don't know what sort of analog signal you're trying to measure. This technique doesn't work well at low voltages (around 1V and below). There's a suggestion for measuring low voltages at the end of the application note. You might need some amplification (like a simple op-amp). You also might be better off with an external analog to digital converter like the ones Parallax sells.

    Build the circuit shown in the application note and try the program shown. Experiment with different voltages, then maybe try it with your IR sensor and see what happens. Only then should you change the circuit around to fit your project. It's generally a bad idea to change someone's demo circuit / program unless you understand it. You can usually add DEBUG statements to look at temporary or intermediate values (like rct) to help you understand what's happening. You can also use a multimeter or oscilloscope to look at the signals themselves.
Sign In or Register to comment.