resuse pin ?!?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
jedie
Posts: 252
Joined: Fri Jan 29, 2016 12:32 pm
Contact:

resuse pin ?!?

Post by jedie » Fri Oct 19, 2018 6:26 pm

I get a ValueError: pin already used for adc if i call adc = ADC(pin) a second time in a other code part... (I'm on LoBo firmware)

What's a best practise in this case?

Should i made a instance of defaults pins somewhere and (re-)use the instances everywhere? Where is a common place for this?

Is there a "internal registry" of all Pin() instances?!?

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: resuse pin ?!?

Post by loboris » Fri Oct 19, 2018 6:47 pm

You can create ony one ADC instance on any pin. If you don't need it anymore, free it with adc.deinit(), than you can use the same pin again (for adc or any other purpose).
Read the ADC Wiki for more details.

jedie
Posts: 252
Joined: Fri Jan 29, 2016 12:32 pm
Contact:

Re: resuse pin ?!?

Post by jedie » Fri Oct 19, 2018 9:16 pm

Thanks!

So I have two choices:
  • A.) I create *one* instance and use it everywhere.
  • B.) I have to use deinit() when I don't need the ADC at the moment.

I'm assuming both variants have advantages and disadvantages, right?

Think "A" is good, if i use the object often in different places. I think B is good when i rarely need the object. Does deinit frees up resources?

Post Reply