Bug? AnalogWrite pin G25 on ESP32 for true analog voltage output does not work

bussi04

Member
Licensed User
Longtime User
Most analog outputs for Arduino and ESP32 are digital outputs pulsed by pwm.
ESP32 has 2 real 8 bit analog outputs providing analog voltage levels.

Function calls for Arduino IDE does not seem to differ between true analog and pwm.



In B4R my code for ESP32 does not run. Output stays low all the time:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
    Private pinG25analog As Pin   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200) ' (256000)
    Log("AppStart")
    astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
    pinG25analog.Initialize(25, pinG25analog.MODE_OUTPUT)
    'pinG26analog.Initialize(26, pinG26analog.MODE_OUTPUT)

    pinG25analog.AnalogWrite(250) ' 3.2V
    pinG25analog.AnalogWrite(120) ' 1.5V
    pinG25analog.AnalogWrite(50)  ' 0.6V
   
End Sub

What´s going wrong?
 
Last edited:

bussi04

Member
Licensed User
Longtime User
Thank you Erel for your reply.

I tested as you told and it showed that analogWrite() actually is not implemented.
It took me some time to get that test done because I wanted to test it in the Arduino App version 1.8.13 and I had to overcome some access rights problems according to WinApps folder.

However - analogWrite() not implemented yet.

There is a discussion describing general ressource organization problems for analogWrite() function because arduino analogWrite is no real DAC analog output but instead historically a digital timer controlled pwm kind of output.
discussion here: https://github.com/espressif/arduino-esp32/issues/4

... and there is a workaround solution for that timer driven analogWrite here: Repository https://github.com/ERROPiX/ESP32_AnalogWrite

but ...

ESP32 has 2 real DAC controlled outputs G25 and G26 where no timers are needed for function implementation.

I posted a question according that subject: https://github.com/espressif/arduino-esp32/issues/4#issuecomment-768468383. Let´s see what comes from that direction ...


Original espressif IDE seems to support these true analogWrite as far as I understand.

Discussion here: https://docs.espressif.com/projects...2/api-reference/peripherals/dac.html#overview

Is there a way to implement "trueanalogWrite()" using their sources as template?
 
Last edited:

bussi04

Member
Licensed User
Longtime User
Thanks to bitluni (https://bitluni.net/oscilloscope-as-display) it showed that function was already there ... 😄

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
    Private n1, n2 As Int
End Sub

Private Sub AppStart
    Serial1.Initialize(115200) ' (256000)
    Log("AppStart")
    astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")

    RunNative("Setup", Null)

    Do While True
        n1=250
        n2=n1
        RunNative("Plot", Null)
        n1=120
        n2=n1
        RunNative("Plot", Null)
        n1=50
        n2=n1
        RunNative("Plot", Null)
    Loop

End Sub

Sub Astream_NewData (Buffer() As Byte)
    Log("Received: ", Buffer)
End Sub

Sub AStream_Error
    Log("error")
End Sub

#if C
#include <driver/dac.h>

void Setup(B4R::Object* o)
{
  dac_output_enable(DAC_CHANNEL_1);
  dac_output_enable(DAC_CHANNEL_2);
}

void Plot(B4R::Object* o)
{
//lower case variables
   dac_output_voltage(DAC_CHANNEL_1, b4r_main::_n1);
   dac_output_voltage(DAC_CHANNEL_2, b4r_main::_n2);
}
#End if
 
Last edited:
Top