I developed a quick custom PCB using the ATSAM3X8EA-AU as I wanted to use Arduino to program it as an Arduino DUE, I kept most of the schematic the same, however I used pin A14 pin 52 as an analog input as I was going by the Arduino Schematic and MCU ATSAM3X8EA-AU datasheet, stupidly without checking the product page or Arduino code first. Therefore I have PCBs on the way and no way to use A14 natively.
With the help of AI I have developed working code that allows me to use A14, it works as expected and doesn't seem to break anything. Id like to get peoples opinions on it, is it likely to break anything if I use other functions in the future or try to use it in other code? I will likely pull it out into a separate library.
I'm concerned because there must have been a reason why Arduino never chose to enable more than 12 ADC channels like they did with the Mega, the pin is a hardware SPI CS pin and I don't fully understand the code so it might break some other analog functionality. So my questions:
Why did Arduino only enable 12 analog pins from the outset? Is there something that I am not aware of that could cause issue with using A14? (The Mega has 16 analog inputs)
This pin is multiplexed with hardware chip select pin NPCS2, it is not clear to me if any of the Arduino libraries actually use this functionality, will me using this pin as an analog input break any of the SPI libraries? (I obviously wont use pin 52 as CS when using this)
My code works and I've tested the standard analog inputs with analogRead(), however could it cause any issues with the analog functionality that I am not aware of?
//#define A14_PIN 52 // No longer used
#define ADC14_CHANNEL ADC_CHANNEL_14 // <-- FIXED
void setup() {
Serial.begin(9600);
//pinMode(A14_PIN, INPUT);
//digitalWrite(A14_PIN, LOW); // Disable pull-up - This was not actually needed
analogReadResolution(12); // For consistency with rest of ADC usage
// Enable ADC peripheral - Not sure is this is needed as it works without, however AI tells me its an important failsafe.
pmc_enable_periph_clk(ID_ADC);
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST);
adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1);
adc_configure_trigger(ADC, ADC_TRIG_SW, 0);
adc_disable_interrupt(ADC, 0xFFFFFFFF);
adc_disable_all_channel(ADC);
// Configure PB21 (pin 52) for ADC
PIOB->PIO_PDR = PIO_PB21;
PIOB->PIO_ABSR |= PIO_PB21;
// Disable ADC write protection
ADC->ADC_WPMR = (0x414443u << 8); // "ADC"
}
uint16_t readA14() {
// Enable only ADC14
adc_disable_all_channel(ADC);
adc_enable_channel(ADC, ADC14_CHANNEL);
// Start conversion
ADC->ADC_CR = ADC_CR_START;
// Wait for conversion to complete
while ((ADC->ADC_ISR & ADC_ISR_DRDY) != ADC_ISR_DRDY);
// Read result
uint16_t result = ADC->ADC_LCDR;
// Disable ADC14 again to avoid conflicts
adc_disable_channel(ADC, ADC14_CHANNEL);
// Read and return result
return result;
}
void loop() {
uint16_t valA14 = readA14();
uint16_t valA0 = analogRead(A0);
uint16_t valA1 = analogRead(A1);
uint16_t valA11 = analogRead(A11);
Serial.print("A14 (PB21): ");
Serial.print(valA14);
Serial.print(" | A0: ");
Serial.print(valA0);
Serial.print(" | A1: ");
Serial.print(valA1);
Serial.print(" | A11: ");
Serial.println(valA11);
delay(500);
}
-
1what is the actual problem?jsotola– jsotola2025年08月03日 15:48:56 +00:00Commented Aug 3 at 15:48
1 Answer 1
PB21 is just a GPIO pin, GPIO means General Purpose IO so it can be configured as ADC channel 14 (as shown in section 43.5.5 of datasheet), or as Wake-up pin, or as a digital pin.
"ATSAM3X adc pins"
However, Arduino Due only provision CH0 - CH11 (12 channels) for analog read if the chip is a ATSAM3X8E(and 8H), see source code, so calling analogRead(A14)
won't work, but it doesn't stop you from reading it as an analog pin by writing your own analog read function as what the AI suggested.
You have to asked Arduino.CC why they assigned only 12 ADC channels for ATSAM3X8E(and 8H) but full 16-channel if it is a SAM3U4E chip.
There is nothing to be worry about until if you need to have a wakeup pin (the name implies that the pin is probably on the Power Saving or RTC power domain, but I didn't read the datasheet in further details), and in that case, you can't use it as an ADC pin.
(削除) BTW, I don't know where you get the information that the pin is "a hardware SPI CS pin", I can't find any reference to that. (削除ここまで)
-
3Re: "BTW, I don't know where you get the information that the pin is "a hardware SPI CS pin", I can't find any reference to that." check table 9-3 "Multiplexing on PIO Controller B (PIOB)", Peripheral B column, signal
SPI0_NPCS2
timemage– timemage2025年08月04日 04:40:28 +00:00Commented Aug 4 at 4:40