Skip to content
Boris Lovosevic edited this page Jun 5, 2018 · 6 revisions

machine Module

Class DAC


ESP32 has two 8-bit DAC (digital to analog converter) channels, connected to GPIO25 (Channel 1) and GPIO26 (Channel 2).
The DAC driver allows these channels to be set to arbitrary voltages.<br The DAC channels can also be driven with DMA-style written sample data, via the I2S driver when using the “built-in DAC mode”.



This class includes full support for using ESP32 DAC peripheral.

ESP32 DAC output voltage range is 0-Vdd (3.3 V), the resolution is 8-bits


Create the DAC instance object


dac = machine.DAC(pin)

pin argument defines the gpio which will will be used as dac output.
Only GPIOs 25 and 26 can be used as DAC outputs.
The pin argument can be given as pin number (integer) or the machine.Pin object.

>>> import machine
>>> dac=machine.DAC(25)
>>> dac
DAC(Pin(25), channel: 1)
>>> 

Methods


dac.write(value)

Set the DAC value. Valid range is: 0 - 255
The value of 255 sets the voltage on dac pin to 3.3 V


dac.waveform(freq, type [,duration=0] [,scale=0] [,offset=0] [,invert=2])

Generate the waveform on the dac output

Arg Description
freq the waveform frequency; valid range:
16-32000 Hz for sine wave
500-32000 Hz for noise
170 - 3600 Hz for triangle
170 - 7200 Hz for ramp and sawtooth
type the waveform type, use one of the defined constants:
SINE, TRIANGLE, RAMP, SAWTOOTH and NOISE
duration optional, is given, waits for duration ms and stops the waveform
scale optional, only valid for sine wave; range: 0-3; scale the output voltage by 2^scale
offset optional, only valid for sine wave; range: 0-255; ofset the output voltage by offset value
invert optional, only valid for sine wave; range: 0-3; invert the half-cycle of the sine wave

dac.beep(freq, duration [,scale=0])

Generate the sine wave beep on the dac output

Arg Description
freq fequency; valid range:
16-32000 Hz
duration the duration of the beep in ms
scale optional; range: 0-3; scale the output voltage by 2^scale
Clone this wiki locally