Page 1 of 1

New to ESP32 Programming - Getting Weird ADC Readings and Can Only Run Code Once

Posted: Fri Nov 22, 2024 7:05 pm
by pmi2410
I am new to ESP32 programming. I am trying to write some micropython code, test it, edit it, test again. I can write the code, run it once, edit it, but it won't run again. I am not sure how to reset the ESP32 so the edited code runs as if it has never run before.

My code:

Code: Select all

from machine import DAC, Pin, ADC

dac = DAC(Pin(25))  
adc = ADC(Pin(36))

for i in range(0,255,100):
  dac.write(i)
  val = adc.read_u16()
  print("dac=%s, adc=%s" % (i,val))
It runs the first time

Code: Select all

>>> %Run -c $EDITOR_CONTENT

MPY: soft reboot
dac=0, adc=400
dac=100, adc=65535
dac=200, adc=65535
>>>
The ADC readings are weird. Not sure why.

The second time I run the code (no edits, just re-run the same code):

Code: Select all

 %Run -c $EDITOR_CONTENT

MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
OSError: (-259, 'ESP_ERR_INVALID_STATE')

The error is from reallocating the same GPIO pin for the DAC. Is there a way in the code to say "Hey, I'm done. Forget what just happened."

I tried using Stop/Restart and Send EOF/Soft reboot under the Run menu, but I cannot get the code to run again. Same with Disconnect. The only way to re-run the code is to disconnect power to the ESP32. I have been trying both Thonny and the Arduino micropython IDEs.

Is this the normal process for a micro-controller? Load the code, run it once, edit the code, remove power from the device, reload code and test again?

On a related issue, how do I release the GPIO pin from the DAC? Or, how to detect if it is in use already? I have encapsulated the DAC code in a class, and I want to reuse that class in different parts of my code. However, it will break every time unless I figure out how to re-run the DAC code.

Thanks!

Re: New to ESP32 Programming - Getting Weird ADC Readings and Can Only Run Code Once

Posted: Sat Nov 23, 2024 7:09 pm
by pmi2410
I was able to fix the weird ADC readings with

Code: Select all

adc.atten(ADC.ATTN_11DB)  # Set to 3.3V range
adc.width(ADC.WIDTH_12BIT)  # 12-bit resolution (0-4095, default)
and I get this output now:

Code: Select all

DAC voltage: 0.25 ADC voltage: 0.18
DAC voltage: 0.5 ADC voltage: 0.41
DAC voltage: 0.75 ADC voltage: 0.63
DAC voltage: 1.0 ADC voltage: 0.88
DAC voltage: 1.25 ADC voltage: 1.12
DAC voltage: 1.5 ADC voltage: 1.35
DAC voltage: 1.75 ADC voltage: 1.6
DAC voltage: 2.0 ADC voltage: 1.83
DAC voltage: 2.25 ADC voltage: 2.06
DAC voltage: 2.5 ADC voltage: 2.31
DAC voltage: 2.75 ADC voltage: 2.56
DAC voltage: 3.0 ADC voltage: 2.86
DAC voltage: 3.25 ADC voltage: 3.29
But I still have to remove power from the ESP-WROOM-32 board every time I change my code and re-run it. I have tried both the Arduino micropython and Thonny IDEs. I have tried all the "restart" buttons in both IDEs, and I cannot re-run my code without powering down the board. Any ideas as to why? Should I try an older micropython firmware?

My DAC_test code:

Code: Select all

from machine import Pin, ADC, DAC
import time

# Configure the DAC pin
dac_pin = 25
dac = DAC(Pin(adc_pin))

# Configure the ADC pin (use the correct pin number for your setup)
adc_pin = 36  # Example pin (GPIO36, also known as VP)
adc = ADC(Pin(adc_pin))

# Set the ADC attenuation (defines voltage range)
# ADC.ATTN_0DB: 0-1.1V (default)
# ADC.ATTN_2_5DB: 0-1.5V
# ADC.ATTN_6DB: 0-2.0V
# ADC.ATTN_11DB: 0-3.3V
adc.atten(ADC.ATTN_11DB)  # Set to 3.3V range

# Set ADC width (defines resolution)
# ADC.WIDTH_9BIT: 9-bit resolution (0-511)
# ADC.WIDTH_10BIT: 10-bit resolution (0-1023)
# ADC.WIDTH_11BIT: 11-bit resolution (0-2047)
# ADC.WIDTH_12BIT: 12-bit resolution (0-4095, default)
adc.width(ADC.WIDTH_12BIT)

# Output voltage from DAC and read with ADC
dac_voltage = 0
while True:
  dac_voltage += 0.25   # increase DAC voltage by 0.25V for each iteration
  dac_value = int((dac_voltage / 3.3) * 255) # convert voltage to 8 bit DAC value
  if dac_value > 256:
    break
  dac.write(dac_value)  # Write the DAC value to the DAC
  time.sleep(0.5)  # Delay for half a second
  adc_value = adc.read()  # Read the ADC value (0-4095 for 12-bit width)
  adc_voltage = adc_value / 4095 * 3.3  # Convert ADC value to voltage (based on 3.3V range)
  print("DAC voltage:", dac_voltage, "ADC voltage:", round(adc_voltage, 2))