# Atomvm for ADC ESP32

**URL:** <https://erlangforums.com/t/atomvm-for-adc-esp32/3580>\
**Category:** AtomVM Forum\
**Tags:** atomvm\
**Created:** [May 2, 2024, 2:22pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580 "2024-05-02T14:22:21Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![joserp93](https://erlangforums.com/user_avatar/erlangforums.com/joserp93/32/2345_2.png) [@joserp93](https://erlangforums.com/u/joserp93)\
**Post date:** [May 2, 2024, 2:22pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/1 "2024-05-02T14:22:21Z")

</div>

Hello @Winford , I was trying to use [atomvm\_adc](https://github.com/atomvm/atomvm_adc) but it turns out that all its functions were deprecated for versions higher than 5 of the espressif sdk, so I took a while to make a new compatible nif, I am new to this, I have just learned how to use it, I did a nif for nerves but it was easier. I need to include documentation and examples of use. But I have this error when compiling, let’s see if you can help me.

This is the repository. [GitHub - joserp93/atomvm\_adc](https://github.com/joserp93/atomvm_adc)

error:

```erlang
Linking CXX executable atomvm-esp32.elf
/Users/joserodriguez/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: unrecognized option '--whole-archive /Users/joserodriguez/atomvm/atomvm-v0.6.0/src/platforms/esp32/build/esp-idf/atomvm_adc/libatomvm_adc.a -Wl'
/Users/joserodriguez/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin/../lib/gcc/xtensa-esp-elf/13.2.0/../../../../xtensa-esp-elf/bin/ld: use the --help option for usage information
collect2: error: ld returned 1 exit status
make[2]: *** [atomvm-esp32.elf] Error 1
make[1]: *** [CMakeFiles/atomvm-esp32.elf.dir/all] Error 2
make: *** [all] Error 2

```

---

<div class="post-metadata">

**Author:** ![arpunk](https://erlangforums.com/user_avatar/erlangforums.com/arpunk/32/276_2.png) [@arpunk](https://erlangforums.com/u/arpunk)\
**Post date:** [May 2, 2024, 2:44pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/2 "2024-05-02T14:44:25Z")

</div>

> This is the repository. [GitHub - joserp93/atomvm\_adc](https://github.com/joserp93/atomvm_adc)

Hello, is this Fred’s codebase?

---

<div class="post-metadata">

**Author:** ![joserp93](https://erlangforums.com/user_avatar/erlangforums.com/joserp93/32/2345_2.png) [@joserp93](https://erlangforums.com/u/joserp93)\
**Post date:** [May 2, 2024, 2:46pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/3 "2024-05-02T14:46:07Z")

</div>

Hi, Yes, I took Fred’s code as a base.

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 2:53pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/4 "2024-05-02T14:53:45Z")

</div>

So before I go digging to deep, let me be sure I understand what you are doing. Is this code for IDF v5.x compatibility?

---

<div class="post-metadata">

**Author:** ![joserp93](https://erlangforums.com/user_avatar/erlangforums.com/joserp93/32/2345_2.png) [@joserp93](https://erlangforums.com/u/joserp93)\
**Post date:** [May 2, 2024, 2:56pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/5 "2024-05-02T14:56:37Z")

</div>

That’s right, I read the IDF v5.x documentation and made it compatible.

---

<div class="post-metadata">

**Author:** ![arpunk](https://erlangforums.com/user_avatar/erlangforums.com/arpunk/32/276_2.png) [@arpunk](https://erlangforums.com/u/arpunk)\
**Post date:** [May 2, 2024, 2:56pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/6 "2024-05-02T14:56:37Z")

</div>

Take a look at [atomvm\_adc/CMakeLists.txt at master · atomvm/atomvm\_adc · GitHub](https://github.com/atomvm/atomvm_adc/blob/master/CMakeLists.txt#L31-L34)

You might need to update the `LINK_OPTIONS` arguments, try:

```erlang
LINK_OPTIONS "-Wl,--whole-archive;${CMAKE_CURRENT_BINARY_DIR}/lib${COMPONENT_NAME}.a;-Wl,--no-whole-archive"

```

If you get it to work on IDF v5.x, I’d suggest you create a PR to the official repo instead.

---

<div class="post-metadata">

**Author:** ![joserp93](https://erlangforums.com/user_avatar/erlangforums.com/joserp93/32/2345_2.png) [@joserp93](https://erlangforums.com/u/joserp93)\
**Post date:** [May 2, 2024, 3:20pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/7 "2024-05-02T15:20:50Z")

</div>

I don’t have access to push the new branch.

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 3:26pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/8 "2024-05-02T15:26:21Z")

</div>

Ok, I started to work on IDF v5 compatibility, but got a bit waylaid working on some issues in the VM core. I plan on getting back into to it as soon as I finish working on some enhancements for the esp32 network driver.

If you want to work on this, be sure you are aware of all the breaking changes, the v5.x ADC driver and configuration bears almost no resemblance with the 4.x version. Now there are a lot of handles that need to be kept track of, so a simple nif implementation will not work. The driver will either need to be implemented as a gen\_server, or as a resource based nif. Resource based nifs are fairly new in AtomVM but give you the ability to maintain some resources in memory and have a handle to access them from a nif (basically giving you the ability to maintain State without a full blown gen\_server). This was the approach I was working on.

I would be happy to help you answer any questions you have if you want to continue working on it. If you don’t get it figured out, I was planning on getting back to this, hopefully by the middle of May. Since the APIs are completely different I was planning on submitting the IDF v5 ADC driver directly to AtomVM as a core component. Version 4.4.x of the ESP-IDF will be retired this summer and we do release builds from v5.x, so anyone needing a v4.4 could still build their own with the standalone atomvm\_adc.

---

<div class="post-metadata">

**Author:** ![arpunk](https://erlangforums.com/user_avatar/erlangforums.com/arpunk/32/276_2.png) [@arpunk](https://erlangforums.com/u/arpunk)\
**Post date:** [May 2, 2024, 3:29pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/9 "2024-05-02T15:29:54Z")

</div>

Same here, I tried to adjust the official ADC driver but changes were too deep and I’m not using v4.x anymore. I also have a PoC of a resource based NIF for ADC1/2, from scratch, but it needs more testing. @Winford maybe we can sync up and come up with a better one? 😃

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 3:29pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/10 "2024-05-02T15:29:57Z")

</div>

Yes, you can look at the link options from the esp32 CMakeLists.txt files to see how to link with v5.x.

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 3:31pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/11 "2024-05-02T15:31:34Z")

</div>

Absolutely!!! We should collaborate and submit a PR to AtomVM core, the only reason ADC isn’t there yet is because we don’t yet support v5.x.

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 3:34pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/12 "2024-05-02T15:34:55Z")

</div>

…and ADC unit2 is buggy in atomvm\_adc, It can only be used if WiFi is stopped, and the esp32 network driver was not stopping the network, only the controlling gen\_server when network:stop is used. I have a PR in right now under review to fix that problem.

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 3:38pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/13 "2024-05-02T15:38:19Z")

</div>

Adding the ability to put the WiFi driver to sleep while using unit\_2 would be sufficient, so the network can be brought back up quickly with a resume… This is on the list of previously mentioned enhancements I’m working on.

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [May 2, 2024, 5:53pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/14 "2024-05-02T17:53:43Z")

</div>

> [@Winford](#):
>
> The driver will either need to be implemented as a gen\_server, or as a resource based nif.

My apologies, I responded before finishing my coffee! I repeatedly used gen\_server when I mean to say **port driver**. A regular nif won’t work. The implementation needs to be either a port or a resource based nif.

---

<div class="post-metadata">

**Author:** ![deecanuwin](https://erlangforums.com/user_avatar/erlangforums.com/deecanuwin/32/2566_2.png) [@deecanuwin](https://erlangforums.com/u/deecanuwin)\
**Post date:** [September 8, 2024, 11:03pm UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/15 "2024-09-08T23:03:28Z")

</div>

Hey there! I was trying to make the OG atomvm/atomvm\_adc work with IDF v5 and stumbled upon your codebases joserp93/atomvm\_adc and arpunk/atomvm\_adc! Are those working right now or still in progress?

---

<div class="post-metadata">

**Author:** ![Winford](https://erlangforums.com/user_avatar/erlangforums.com/winford/32/1246_2.png) [@Winford](https://erlangforums.com/u/Winford)\
**Post date:** [September 9, 2024, 3:51am UTC](https://erlangforums.com/t/atomvm-for-adc-esp32/3580/16 "2024-09-09T03:51:40Z")

</div>

Your timing is excellent! I just submitted a draft PR that is fully functional and supports building with all ESP-IDF 5.x versions to the AtomVM repository. It is still in draft because I am trying to add Elixir native interfaces and examples as well. But I may back out on that and just mark the Erlang support as ready for review, and leave the Elixir side to someone who is more comfortable with Elixir.

You can see the draft PR here:

> <https://github.com/atomvm/AtomVM/pull/1264>
>
> This set of changes adds support for the ESP32 ADC peripheral, with documentatio…n and examples.
> 
> These changes are made under both the "Apache 2.0" and the "GNU Lesser General
> Public License 2.1 or later" license terms (dual license).
> 
> SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
