New to ESP32 Programming - Getting Weird ADC Readings and Can Only Run Code Once
Posted: Fri Nov 22, 2024 7:05 pm
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:
It runs the first time
The ADC readings are weird. Not sure why.
The second time I run the code (no edits, just re-run the same code):
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!
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))
Code: Select all
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
dac=0, adc=400
dac=100, adc=65535
dac=200, adc=65535
>>>
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!