Analog IO

This chapter introduces how to use analog input and output on the mPython Board pins. Pins are the way your board communicates with external devices connected to it. The mPython Board can expand its IO pins through an expansion board to connect and control or read other components or modules.

Attention

You can refer to mPython Board interface pin description to learn about the available analog pins.

Analog Input

The analog input pins available on the mPython Board are P0, P1, P2, P3, P4, P10, of which P4 and P10 are used by the mPython Board’s light and microphone sensors respectively.

What is analog input?

Analog input converts an analog signal into a digital signal, abbreviated as ADC.

The following reads analog input using the P0 pin:

from mpython import *           # Import the mpython module

p0=MPythonPin(0,PinMode.ANALOG)     # Instantiate MPythonPin, set P0 to "PinMode.ANALOG" mode
while True:
    value=p0.read_analog()          # Read the analog value of the P0 pin
    print("analog:%d" %value)
from mpython import *
p0=MPythonPin(0,PinMode.ANALOG)

Note

MPythonPin instantiation. mode is set to PinMode.ANALOG analog input mode.

Read analog input:

p0.read_analog()

Note

Because the ADC sampling data width is 12 bits, the full scale is 4095.

EXT Alligator Clip

Next, you can connect a resistive component (such as a light-dependent or thermistor) to the EXT and GND pads of the mPython Board with an alligator clip wire, to measure the change in the sensor’s input value…

The EXT connection is the P3 pin of the mPython Board:

from mpython import *           # Import the mpython module

    p3=MPythonPin(3,PinMode.ANALOG)     # Instantiate MPythonPin, set P3 to "PinMode.ANALOG" mode
    while True:
        value=p3.read_analog()          # Read the analog value of the EXT(P3) pin
        print("analog:%d" %value)
../../../../../../_images/ext.png

Analog Output

What is analog output?

The board’s pins cannot output an analog signal like an audio amplifier - by modulating the voltage on the pin. These pins can only enable a full 3.3V output, or pull it down to 0V. However, you can still control the brightness of an LED or the speed of a motor by turning this voltage on and off very quickly, and controlling its on time and off time. This technique is called pulse width modulation (PWM), which is how write_analog works.

Output a PWM signal of a certain voltage:

from mpython import *           # Import the mpython module

p0=MPythonPin(0,PinMode.PWM)     # Instantiate MPythonPin, set P0 to "PinMode.PWM" mode

voltage=2.0                      # Voltage 2V
p0.write_analog(int(voltage/3.3*1023))    # Calculate the duty cycle of the PWM for the corresponding voltage

Note

  • value in write_analog(value) is the duty cycle of the PWM signal.

  • Since the IO pin voltage is 3.3V and we need to output a voltage of 2V, the mapped value is 2/3*1023.

  • Since the calculated result is a floating-point number, we also need to use int() to convert it to an integer.

../../../../../../_images/pwm.png

You can see diagrams of three different PWM signals above. They all have the same period (and therefore frequency), but they have different duty cycles.

  • The first one produces write_analog(511), because it has exactly a 50% duty cycle - power is on half the time and off half the time. The result is that the total energy of this signal is the same as if it were 1.65V instead of 3.3V.

  • The second signal has a 25% duty cycle and can be produced with write_analog(255). It has a similar effect, as if 0.825V were output on that pin.

  • The third signal has a 75% duty cycle and can be generated with write_analog(767). Its energy is three times that of the second signal, equivalent to outputting 2.475V on the second pin.