chipKIT® Development Platform

Inspired by Arduino™

analogRead compatiblity

Created Sat, 18 Jun 2011 14:32:13 +0000 by jjsd858


jjsd858

Sat, 18 Jun 2011 14:32:13 +0000

Using an Uno32. Loaded the first example program AnalogReadSerial and it doesn't work. Turns out that in Arduino land A0-A5 are defined as integers 14-19. I changed the code from analogRead(A0) to analogRead(0) and it correctly reads the A0 pin. A problem for any existing code using A0-A5 rather than 0-5.

It appears the maximum adc count corresponds to a 3.3V input instead of 5V like the Arduino. Therefore any existing shields that operate to 5V will have a large flat spot at the top end. The reasoning for this is clear enough but it makes you think that for true compatibility resistive dividers should have been added.


Mark

Sat, 18 Jun 2011 14:59:40 +0000

This has been addressed and fixed, it is in the next release. I just verified that the fix is in github. If you want to fix it now, download wiring_analog.c from github and replace it in your copy.

OR just put these lines at the beginning of analogRead

#if defined(BOARD_MEGA) || defined(BOARD_MAX32) if (pin >= 54) pin -= 54; // allow for channel or pin numbers #else if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif


Trev

Sat, 09 Jul 2011 06:44:36 +0000

I don't think this has been fixed for the Uno32. A0-A7 work, but not A8-A11.


Mark

Sun, 10 Jul 2011 00:32:05 +0000

Thanks for catching this, I have added it to the github repository

HOWEVER, I recommend NOT using the "A0" etc definitions, let me explain why

For the mega board, they are defined as

A0=54 A1=55 etc

for the uno32 (and the regular Arduino Uno)

A0=14, A1=15, etc

The first line of code in the analogRead function checks to see if its above a certain value, then subtracts that value. It is a rather round about way to get to the analog channel that you want. I had to put this in for Arduino compatiblity but now if you are using a board that is NOT a mega32 or a UNO32, you are limited to 14 Analog read inputs.

#if defined(BOARD_MEGA) || defined(BOARD_MAX32) if (pin >= 54) pin -= 54; // allow for channel or pin numbers #else if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif

Mark


razabashir

Mon, 16 Feb 2015 05:46:19 +0000

Using an Uno32. Loaded the first example program AnalogReadSerial and it doesn't work. Turns out that in Arduino land A0-A5 are defined as integers 14-19. I changed the code from analogRead(A0) to analogRead(0) and it correctly reads the A0 pin. A problem for any existing code using A0-A5 rather than 0-5.