pxt-calliope/docs/reference/pins/analog-read-pin.md

42 lines
1.3 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Analog Read Pin
2016-06-23 01:24:14 +02:00
Read an **analog** signal (`0` through `1023`) from the
[pin](/device/pins) you say.
2016-03-26 00:47:20 +01:00
```sig
pins.analogReadPin(AnalogPin.P0)
2016-03-26 00:47:20 +01:00
```
## Parameters
2016-03-26 00:47:20 +01:00
* ``name`` is a [string](/types/string) with the name of the pin
you say (`P0` through `P4`, or `P10`)
2016-03-26 00:47:20 +01:00
## Returns
2016-03-26 00:47:20 +01:00
* a [number](/types/number) from `0` through `1023`
2016-03-26 00:47:20 +01:00
This program reads pin `P1` and shows the number
on the LED screen.
2016-03-26 00:47:20 +01:00
```blocks
basic.forever(() => {
let value = pins.analogReadPin(AnalogPin.P1)
basic.showNumber(value)
2016-03-26 00:47:20 +01:00
});
```
### ~hint
2016-11-02 01:44:37 +01:00
If you are using **analog read pin** with another @boardname@ running **analog write pin**, then things can get tricky. Remember that the @boardname@ that runs **analog set pin** writes 0's and 1's at a very high frequency to achieve an average of the desired value. Sadly, if you try to read that average from another @boardname@, then the @boardname@ will either read 0 or 1023. You could try to read a higher number of values (e.g. a million) in a loop, then computer then average. Alternatively, you can plug in a capacitor in-between the two @boardname@s.
### ~
## See also
2016-03-26 00:47:20 +01:00
2016-11-02 01:44:37 +01:00
[@boardname@ pins](/device/pins),
2016-06-23 01:24:14 +02:00
[on pin pressed](/reference/input/on-pin-pressed),
[analog write pin](/reference/pins/analog-write-pin),
[digital read pin](/reference/pins/digital-read-pin),
[digital write pin](/reference/pins/digital-write-pin)