Analog Read Pin

Read an analog signal (0 through 1023) from the pin you say.

pins.analogReadPin(AnalogPin.P0)

Parameters

  • name: is a string with the name of the pin you say (P0 through P4, or P10).

Returns

  • a number from 0 through 1023

Example

This program reads pin P1 and shows the number on the LED screen.

basic.forever(function() {
    let value = pins.analogReadPin(AnalogPin.P1)
    basic.showNumber(value)
})

If you are using analog read pin with another micro:bit running analog write pin, then things can get tricky. Remember that the micro:bit 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 micro:bit, then the micro:bit 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 micro:bits.

See also

micro:bit pins, on pin pressed, analog write pin, digital read pin, digital write pin