Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Very confused about the DAC
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Very confused about the DAC

by Bekar_T on Wed Apr 21, 2021 12:56 am

I feel weird just for writing this post but I'm very confused about the DAC behavior on every CircuitPython SAMD board I have.

From https://learn.adafruit.com/circuitpython-basics-analog-inputs-and-outputs/digital-to-analog-converters-outputs I copied this basic block of code to dip the toe in dac control:

Code: Select all | TOGGLE FULL SIZE
import board
import analogio

led = analogio.AnalogOut(board.A0)

led.value = 54000

From the text, it seems like that's all that's needed to set DAC voltage to a specific value but in my case, that code only produces a single flash.

Only way I managed to light up the led was to keep setting the value over and over in the main loop.

Is this how it's supposed to work? Does DAC output need to be maintained actively? Is there no way to set and forget?

Bekar_T
 
Posts: 22
Joined: Wed Apr 21, 2021 12:41 am

Re: Very confused about the DAC

by blnkjns on Wed Apr 21, 2021 1:21 am

Guess it is a bug in Circuitpython.
In Arduino this works fine, so your short blink is not expected port behaviour:
Code: Select all | TOGGLE FULL SIZE
void setup() {
  analogWrite(A0,600);
}

void loop() {
}

I'll put it on my argument list I collect why not to use Python for embedded. The values from 0-65535 are equaly weird to get kids to understand DAC's that eat values 0-1023.....
Last edited by blnkjns on Wed Apr 21, 2021 1:55 am, edited 1 time in total.

blnkjns
 
Posts: 429
Joined: Fri Oct 02, 2020 3:33 am

Re: Very confused about the DAC

by Bekar_T on Wed Apr 21, 2021 1:41 am

an update:

I decided to check if setting the value of dac was producing a spike to the specified voltage for certain amount of time or if dac was being shut down when the code finished running.

Turns out that it's the latter.

After adding time.sleep() in the main loop, the led is still lit up solid. So the dac will be active and output set voltage as long as the code is actively running.

I guess that's reasonable, because who wants an mcu for not running the code on it, but imho at least the guide should be updated.

Felt like a dumdum for a solid hour yesterday until I finally gave up.

Bekar_T
 
Posts: 22
Joined: Wed Apr 21, 2021 12:41 am

Re: Very confused about the DAC

by blnkjns on Wed Apr 21, 2021 1:59 am

Even without a loop I think ending the code should not reset the MCU. Now I am curious what the board would do if you remove the loop from main.cpp in the Arduino code...
Tried it, the LED stays on.

blnkjns
 
Posts: 429
Joined: Fri Oct 02, 2020 3:33 am

Re: Very confused about the DAC

by Bekar_T on Wed Apr 21, 2021 2:05 am

blnkjns wrote:Even without a loop I think ending the code should not reset the MCU.


To me, it's not a big deal as long as you're aware of it.

Confusing as hell if you are't though.

Bekar_T
 
Posts: 22
Joined: Wed Apr 21, 2021 12:41 am

Re: Very confused about the DAC

by kevinjwalters on Wed Apr 21, 2021 11:42 am

It is confusing the first time, see Code only works in REPL for another case of it.

The guide is showing interactive commands typed over the serial console to REPL. Those are all running within a single "session" (there might be a different term for it). As you note, when a program terminates it resets the majority of the onboard peripherals. An interesting exception to this is devices with screens where the screen is left active once its initialised to show console output.

Infinite loops in normal programming are an anathema but you often see

Code: Select all | TOGGLE FULL SIZE
while True:
    pass


at the end of a CircuitPython program to stop it terminating.

There are also some more obscure cases of understanding the lifecycle of hardware configuration, e.g. Adafruit Forums: Behaviour of deleted DigitalInOut without deinit.

kevinjwalters
 
Posts: 933
Joined: Sun Oct 01, 2017 3:15 pm

Re: Very confused about the DAC

by Bekar_T on Wed Apr 21, 2021 1:19 pm

kevinjwalters wrote:Infinite loops in normal programming are an anathema but you often see

while True:
pass

at the end of a CircuitPython program to stop it terminating.

I guess the peripherals have to be reset at some point, either start or the end and probably both of the options have their pros and cons.

Bekar_T
 
Posts: 22
Joined: Wed Apr 21, 2021 12:41 am

Please be positive and constructive with your questions and comments.