Skip to content
  • Home
  • Account
  • Restricted content
  • OBEX
  • Chips Tips
  • Logout
  • Shop
  • Blog
Propeller OBEX
  • OBEX
  • Forum
Login
Propeller OBEX

Propeller 1

  • How to pause in PASM
  • How to choose between VAR and DAT
  • WAITPEQ and WAITPNE with PASM
  • IO pin manipulation using PASM
  • Control IO pins from any cog
  • 3D Models for Parallax Products
  • Implementing Abstract Data Structures with Spin Objects
  • Making in USA - The Parallax 8 core microcontroller
  • Simultaneous pin group control
  • Programming the FLiP module over WiFi
  • Keep any value in range
  • Handy Diagnostic Heartbeat
  • Customer Code Deployment in one click

Propeller 2

  • Floating point operators in SPIN2
  • 64/96/128-Bit Unsigned Integer Math
  • 3D Models for Parallax Products
  • DEBUG Plot Layer and Crop
  • Floating point
  • Getting Started Video Tutorials
  • Using the counter for timeouts
  • asmclk and the automatic clock setter
  • Sharing variables between SPIN2 and PASM2
  • Keep any value in range
  • Determine pin state - High, Low or Floating?
View Categories
  • Home
  • Docs
  • Chips Tips
  • Propeller 2
  • Determine pin state - High, Low or Floating?

Determine pin state - High, Low or Floating?

Reading Time: 1 min read

SPIN2

Introduction #

A common requirement for programmers is to determine the external state of an IO pin.

The Propeller 2 smart IO pins make this simple without needing any external components, and allow the programmer to determine if the pin is connected to an external signal that has a high, low, floating or has a rapidly changing state.

The attached SPIN2 code method "check_pinstate" returns a value from 0 to 3 to indicate the pin state.

The method enables a weak 10uA internal pull resistor and then reads the input. Any stronger external signal is able to pull the IO pin in either direction, and so by testing both the low and high states, the external signal can be determined.

A complete demo can be found at the OBEX link below.

Code Snippet #

PRI check_pinstate(pin) : result

    ''' Check_pinstate will reply with a value indiciating the state of the io pin
    '''
    ''' 0 = pin is floating
    ''' 1 = pin is low
    ''' 2 = pin is high
    ''' 3 = pin is changing, high/low

    result := 0                         ' clear result

    ''' Test for low external state
    PINCLEAR(pin)
    WRPIN   (pin, P_HIGH_10uA)          ' enable weak internal pullup
    PINHIGH (pin)
    WAITUS  (10)                        ' allow pin to settle

    IFNOT PINREAD (pin)                 ' an external pulldown will pull pin low
        result |= %01

    ''' Test for high external state
    PINCLEAR(pin)
    WRPIN   (pin, P_LOW_10uA)           ' enable weak internal pulldown
    PINLOW  (pin)
    WAITUS  (10)                        ' allow pin to settle

    IF PINREAD (pin)                    ' an external pullup will pull pin high
        result |= %10

    PINCLEAR(pin)                       ' reset pin before returning result

Resources #

IO Pin State Detection Demo

Was this useful ?
Share This Article :
  • Facebook
  • X
  • LinkedIn
Still stuck? How can we help?

Still stuck? How can we help?

Updated on November 6, 2024
Table of Contents
  • Introduction
  • Code Snippet
  • Resources

Copyright © 2025 - Parallax Inc.
599 Menlo Drive | Rocklin, CA 95765 | USA