# COUNT

COUNT Example

Syntax: COUNT Pin, Duration, Variable

## Function

Count the number of cycles (0-1-0 or 1-0-1) on the specified pin during the Duration time frame and store that number in Variable.

- Pin is a variable/constant/expression (0 – 15) that specifies the I/O pin to use. This pin will be set to input mode.
- Duration is a variable/constant/expression (1 – 65535) specifying the time during which to count. The unit of time for Duration is described below.
- Variable is a variable (usually a Word) in which the count will be stored.

## Quick Facts

| BS2 / BS2e | BS2sx | BS2p | BS2pe | BS2px
Units in Duration | 1 ms | 400 µs | 287 µs | 720 µs | 287 µs
Duration range | 1 ms to 65.535 s | 400 µs to 26.214 s | 287 µs to 18.809 s | 720 µs to 47.180 s | 287 µs to 18.809 s
Minimum pulse width | 4.16 µs | 1.66 µs | 1.20 µs | 3.00 µs | 1.20 µs
Maximum frequency (square wave) | 120,000 Hz | 300,000 Hz | 416,700 Hz | 166,667 Hz | 416,700 Hz
Related Commands | PULSIN

## Explanation

The COUNT instruction makes the Pin an input, then for the specified duration, counts cycles on that pin and stores the total in a variable. A cycle is a change in state from 1 to 0 to 1, or from 0 to 1 to 0.

According to table above, COUNT on the BS2 can respond to transitions (pulse widths) as small as 4.16 microseconds (µs). A cycle consists of two transitions (e.g., 0 to 1, then 1 to 0), so COUNT (on the BS2) can respond to square waves with periods as short as 8.32 µs; up to 120 kilohertz (kHz) in frequency. For non-square waves (those whose high time and low time are unequal), the shorter of the high and low times must be at least 4.16 µs in width (on the BS2). Refer to the table for data on other BASIC Stamp modules.

If you use COUNT on slowly changing analog waveforms like sine waves, you may find that the value returned is higher than expected. This is because the waveform may pass through the BASIC Stamp’s 1.4-volt logic threshold slowly enough that noise causes false counts. You can fix this by passing the signal through a Schmitt Trigger, like one of the inverters of a 74HCT14. Or, you may use the BS2px’s built-in Schmitt-Trigger pin property; see CONFIGPIN for details.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012