2277 views|3 replies

1w

Posts

25

Resources
The OP
 

[SAMR21 New Gameplay] 20. Analog Input and Analog Output [Copy link]

 

In CircuitPython, analog input and analog output functions are supported, that is, ADC and DAC. However, its usage is somewhat different from MicroPython, and the MicroPython method cannot be used.

Analog Input:

import analogio
from board import *

adc = analogio.AnalogIn(A1)
val = adc.value

First, set the pin used by the adc, and then read the analog input value through value. Note that it is read as an integer, not a function, so there are no brackets after it.

In addition to using the predefined A0/A1 pins, you can also specify the port directly, such as:

import analogio
from microcontroller import pin

adc=analogio.AnalogIn(pin.PA04)
adc.value





This content is originally created by dcexpert , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

 
 

1w

Posts

25

Resources
2
 

For SAMR21, there is no DAC function, so analog output is not supported. For SAMD21, the DAC output is PA02; SAMD51 is PA02/PA05 (DAC pin is related to the specific MCU).

import analogio
from microcontroller import pin

dac = analogio.AnalogOut(pin.PA02)     # output on pin PA02
dac.value = 32768              # makes PA02 1.65V

If a pin that does not support DAC is specified, an error will be displayed:

ValueError: AnalogOut not supported on given pin

 
 
 

1w

Posts

25

Resources
3
 

Compare the usage on MicroPython:

from pyb import Pin, ADC

adc = ADC(Pin('X19'))
adc.read() # read value, 0-4095



from pyb import Pin, DAC

dac = DAC(Pin('X5'))
dac.write(120) # output between 0 and 255

Here we can see that CircuitPython and MicroPython have obvious differences in details.

 
 
 

1w

Posts

25

Resources
4
 

The advantage over MicroPython is the addition of a deinit() function that can release objects after use.

adc.deinit()

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list