Loading...
Searching...
No Matches
arduino/phyduino_pico/analog_differential/main.py

Measures the voltage between two analogue pins, e.g. across a resistor or a sensor bridge, twice a second.

Arduino has no differential reading, so this uses the phyduino.py functions.

  • A3 - A4: true differential. The on-board ADC samples both pins at the same instant, so noise common to both cancels. Range ±1024 mV in 0.5 mV steps (a larger difference is measured as two separate readings).
  • A0 - A1: computed from readings taken one after the other. Fine for slow signals, but noise on both pins does not cancel.
Wiring
Keep every pin between 0 V and its full scale (A0-A2: 3.3 V, A3-A5: 2.048 V).
Output
A3 - A4: 100.0 mV A0 - A1: 99.1 mV
# Measures the voltage between two analogue pins, e.g. across a resistor or a
# sensor bridge, twice a second.
#
# A3 - A4: true differential. The on-board ADC samples both pins at the same
# instant, so noise common to both cancels. Range +/-1024 mV in 0.5 mV steps
# (a larger difference is measured as two separate readings).
# A0 - A1: computed from readings taken one after the other.
# Fine for slow signals, but noise on both pins does not cancel.
#
# Keep every pin between 0 V and its full scale (A0-A2: 3.3 V, A3-A5: 2.048 V).
# Arduino has no differential reading, so this uses the phyduino functions.
from arduino import * # the Arduino-style functions: pinMode(), digitalWrite(), delay(), ...
import phyduino as mcu # the Phyduino Pico library, for mcu.analog_differential_millivolts()
def setup():
mcu.analog_averaging(16) # average 16 samples per reading, for steadier numbers
def loop():
# The voltage of the first pin minus the second, in millivolts. "%7.1f" shows
# each with one decimal place, right-aligned in 7 characters.
print("A3 - A4: %7.1f mV A0 - A1: %7.1f mV" % (
mcu.analog_differential_millivolts(A3, A4), # sampled at the same instant
mcu.analog_differential_millivolts(A0, A1))) # two readings, one after the other
delay(500) # twice a second
# Do not edit below this line: it runs setup() once, then loop() over and over.
def main():
setup() # runs once, at the start
while True: # repeats until stopped: Interrupt in PyCharm, or mpremote soft-reset
loop()
# True when this file is run, but not when another file imports it.
if __name__ == "__main__":
main()