Let's generate an analog signal!

Well, don't get too excited. There's only a single DAC and it's a whopping 5 bits! So don't expect super awesome high fidelity audio block rockin' beats or anything.

Here's a simple sine wave generator you can try:

import time
import math
import board
from analogio import AnalogOut

dac = AnalogOut(board.G3)

while True:
    dac.value = int((2**16 - 1) * (0.5 + 0.5 * math.sin(time.monotonic()*10)))

Save that as something like dac.py and run it with:

python3 dac.py

And connect the G3 pin to an oscilloscope. You should see something like this:

Note that even though the MCP221's DAC is only 5 bits, you set it using a 16 bit value to comply with the CircuitPython API.

This guide was first published on Dec 22, 2019. It was last updated on 2023-11-27 12:05:15 -0500.

This page (DAC) was last updated on Dec 17, 2019.

Text editor powered by tinymce.