Using the Pyboard's Analog Pins for ADC
Contents
Pyboard's ADC Pins
The Pyboard has two different types of analog pins. Analog-to-Digital Converter (ADC) converts a continuous physical voltage read at the Pyboard's analog pin into a discrete digital number.
Digital-to-Analog Converter (DAC) performs the opposite function to the ADC. It converts a discrete digital signal (a series of 0's and 1's) into a continuous analog voltage signal as an output on the Pyboard's analog pin.
This article deals exclusively with the Pyboard's ADC pins. If interested the article Using the Pyboard's Analog Pins for DAC discusses the Pyboard's DAC pins.
The STM32F405RGT6 which is the Pyboard's microcontroller has three × 12-bit ADCs which share a total of 16 external input pins. All 16 of the analog ADC pins are available on the Pyboard.
These pins are : X1..X8, X11, X12, X19..X22, Y11, Y12. Refer to the Pyboard V1.1 pinout.
MicroPython pyb.ADC Class
An ADC object is instantiated from the pyb.ADC class and associated with a Pin object created from the pyb.Pin class. The voltage at the pin can then be read with the ADC.read() method.
Step 1 : Declare a Pin object with analog mode. <pin_variable> = pyb.Pin(pin_name, mode=pyb.Pin.ANALOG) Step 2 : Declare an ADC object and associate it with the pin from Step 1. <ADC_variable> = pyb.ADC(pin) Step 3 : Read the voltage on the pin and return it as a discrete number. ADC.read() Example: import pyb # Assign pin X1 as analog. pin = pyb.Pin('X1', pyb.Pin.ANALOG) # Assign the pin to an ADC object. adc = pyb.ADC(pin) # Return the voltage on X1 pin as a digital value. raw = adc.read()
The hardware ADC on the Pyboard have 12-bit resolution. This means that the value returned by the ADC.read() method will be between 0 and 4095 inclusive.
The actual voltage at the pin can be calculated with the following formula:
- Voltage = (ADC_Value * 3.3) / 4095
ADXL355 Accelerometer Example
The ADXL335 is an analog accelerometer which measure acceleration in three axis (X, Y, Z) simultaneously. The sensor has three output pins: X_OUT, Y_OUT, Z_OUT. The ADXL335 takes measurements on the three axis and outputs the accelerations as a proportional voltage on the respective output pins.
FIG 1 - ADXL335 breakout board: [Left] front view, [Right] rear view
The Pyboard reads the analog voltage converting it to a digital value with its 12-bit ADC. The following formula is used to calculate the acceleration on an axis:
- Acceleration = (ADC_Value - 2048) / 422.4
The acceleration unit is g where 1g is the acceleration due to the gravitational pull of the Earth.
Pyboard to ADXL335 hookupThis example will only use the X-axis of the sensor so the hookup of the ADXL355 board to the Pyboard is simple and only involves three jumper cables.
| Pyboard | ADXL335 |
|---|---|
| 3.3V | VCC |
| GND | GND |
| Pin Y12 | X_OUT |
MicroPython code:
Copy the following code to the Pyboard. While the program is running tilt the board in the X-axis towards the vertical then back down till it is flat on the bench again. Halt the program by sending Ctrl-C (or Cmd-C) from the computer keyboard.
# Read the X-axis analog output of the ADXL335
# 3-axis accelerometer with the Pyboard's ADC.
# The digital value is converted to millivolts.
# The digital value is also converted to acceleration
# of the sensor's X-axis in units of g.
import pyb
# Assign pin Y12 as analog
pin = pyb.Pin('Y12', pyb.Pin.ANALOG)
# Assign pin to ADC
adc = pyb.ADC(pin)
# Endless loop
# Take a reading from the sensor every 2 seconds.
while True:
raw = adc.read()
volts = (raw * 3.3) / 4095 # Convert to Volts
accX = (raw - 2048) / 422.4 # Calculate X-axis acceleration
print('V:', round(volts, 3), ' X-axis (g):', round(accX, 4))
pyb.delay(2000)
Typical Output:
V: 1.646 X-axis (g): -0.0142
V: 1.76 X-axis (g): 0.322
V: 1.866 X-axis (g): 0.6345
V: 1.971 X-axis (g): 0.9422
V: 1.88 X-axis (g): 0.6747
V: 1.768 X-axis (g): 0.3456
V: 1.644 X-axis (g): -0.0189
Traceback (most recent call last):
File "<stdin>", line 22, in <module>
KeyboardInterrupt:
In this example the ADXL is measuring static acceleration that is a result of the Earth's gravitational force. When the board is lying flat on the bench the X-axis acceleration should be close to 0g. As it is tilted vertically along the X-axis the acceleration in that axis should increase towards 1g.