Skip to content

Due bug: Cannot use pin as digital after analogRead #2198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trickedj opened this issue Jul 20, 2014 · 6 comments
Closed

Due bug: Cannot use pin as digital after analogRead #2198

trickedj opened this issue Jul 20, 2014 · 6 comments
Assignees

Comments

@trickedj
Copy link

Once a pin has been used by analogRead it cannot be returned to digital use with pinMode(xxxx, OUTPUT) because analogRead only frees up the pin when another channel is read. pinMode needs to check if the pin is currently selected for the ADC and if so free it up.

A work around is to free up the pin by selecting a spare with analogRead

I discovered this while porting (from a Uno to Due) a TFT+touchpanel screen which shares digital TFT functions with raw touchpanel resistors.

@agdl
Copy link
Member

agdl commented Dec 5, 2014

@trickedj I'll open a pull request for this in order to fix it

@agdl
Copy link
Member

agdl commented Dec 5, 2014

here the pull request

#2486

agdl added a commit to agdl/Arduino that referenced this issue Dec 5, 2014
If an analog input is used and during the sketch modified in a digitalOutput
the pinMode(AX, OUTPUT) isn't sensed because of this line of codes:

wiring_analog.c line 135
// Enable the corresponding channel
	if (ulChannel != latestSelectedChannel) {
		adc_enable_channel( ADC, ulChannel );
		if ( latestSelectedChannel != (uint32_t)-1 )
			adc_disable_channel( ADC, latestSelectedChannel );
		latestSelectedChannel = ulChannel;

The channel is infact enabled only if different from the previous one because
of speed problems.

With my patch:

when pinMode(PIN, OUTPUT) is declared a control about if the pin
is an analog one is done and if so the ADC is released

when pinMode(PIN, INPUT) is declared a control is as well done in order to
enable the ADC.
@trickedj
Copy link
Author

trickedj commented Dec 5, 2014

I am not sure that calling "adc_enable_channel" for the input mode is
correct. Input mode is for digital input and enabling the ADC may cause
a conflict. I think the ADC should only be enabled by AnalogRead.

@agdl
Copy link
Member

agdl commented Dec 6, 2014

I wasn't sure too, but i tested it and it works. That if that checks if the channel is changed is a problem.

@facchinm facchinm self-assigned this Mar 23, 2015
@facchinm
Copy link
Member

I've added some thoughts and code on https://groups.google.com/a/arduino.cc/forum/#!topic/developers/IdhTIT0V5GY ,
I'd be very glad to have some discussion before committing any fix 😉

facchinm added a commit to facchinm/Arduino that referenced this issue Mar 27, 2015
to avoid the bug arduino#2198 simply reconfigure the pin -> no additional overhead if pinMode configuration is performed at the beginning of the sketch, 4 to 25% overhead on all analogRead() due to the additional check
facchinm added a commit to facchinm/Arduino that referenced this issue Mar 27, 2015
to avoid the bug arduino#2198 simply reconfigure the pin -> no additional overhead if pinMode configuration is performed at the beginning of the sketch, 4 to 25% overhead on all analogRead() due to the additional check
facchinm added a commit to facchinm/Arduino that referenced this issue Jul 14, 2015
to avoid the bug arduino#2198 simply reconfigure the pin -> no additional overhead if pinMode configuration is performed at the beginning of the sketch, 4 to 25% overhead on all analogRead() due to the additional check
@facchinm
Copy link
Member

facchinm commented Jan 7, 2016

Solved by #3524

@facchinm facchinm closed this as completed Jan 7, 2016
ollie1400 pushed a commit to ollie1400/Arduino that referenced this issue May 2, 2022
to avoid the bug arduino#2198 simply reconfigure the pin -> no additional overhead if pinMode configuration is performed at the beginning of the sketch, 4 to 25% overhead on all analogRead() due to the additional check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants