[SOLVED] I need to tweak the analog input ports

I need to redefine the A0 - A3 ports to analog inputs. I tried to just do

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

…at the end of setup() and the just do
a0 = analogRead(A0);
…but I only got zero from the port.
When I wrote a completely separate sketch (without the Arduboy library, just something for the Leonardo), I got perfect readings from the ports. I probably need to change something in the library to make this work with the rest of the Arduboy lib. Where exactly can I find the important routines for the button pins?
This video shows how it works. I have a 10k pullup resistor. And I have this conductive plastic foam pulling down. Since the RGB LED has a common anode, the catodes go to the PWM pins to sink the current. The harder press on the foam, the lower voltage on the analog pins, the brighter led.


In pins_arduino.h I found this:

// Mapping of analog pins as digital I/O
// A6-A11 share with digital pins
#define PIN_A0   (18)
#define PIN_A1   (19)
#define PIN_A2   (20)
#define PIN_A3   (21)
#define PIN_A4   (22)
#define PIN_A5   (23)
#define PIN_A6   (24)
#define PIN_A7   (25)
#define PIN_A8   (26)
#define PIN_A9   (27)
#define PIN_A10  (28)
#define PIN_A11  (29)

static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;	// D4
static const uint8_t A7 = PIN_A7;	// D6
static const uint8_t A8 = PIN_A8;	// D8
static const uint8_t A9 = PIN_A9;	// D9
static const uint8_t A10 = PIN_A10;	// D10
static const uint8_t A11 = PIN_A11;	// D12

Would this be the right place to disable the redefining of analog pins? If I just comment out all lines with A0 to A3?
I do know that I would disable my Arduboy to work with standard Arduboy software, but this is a prototype for a new concept. Later on I’ll figure out how to get it work with standard Arduboy games.

Just my two cents, but the begin() function from the Arduboy2 library will define those pins when called, so with that in mind, maybe you should make a similar begin() function, to be called after arduboy.begin(), so that no modification has to be made to the standard arduboy library, just a couple lines of code in the sketch itself. I did something similar when trying to implement a rotary encoder for my Pip-Boy32u4


That could work. But it seems the Arduboy lib is redefining what A0 points to, not only stating it’s digital. I’m not quite familiar with how pin names work, which names are “real” and which are macros or something else.

You can still refer to the pins as usual after arduboy.begin() using pinMode(pin, mode). The arduboy library defines the pins using low-level Port Manipulation, but can be repurposed easily so long as pinMode() is called after arduboy.begin().

in your setup() code, try this order:

void setup(){
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

additionally, these pins have internal pullup resistors, so I’m not sure an external one is necessary. When I used a rotary encoder with my arduboy, I had to call each as a pullup in pinMode() before I could get the encoder to read properly. So if the above code block still isn’t reading (which is likely. I think it has something to do with how the arduboy lib manipulates the ports), try this one instead:

void setup(){
  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
1 Like

Analog pins can be used as normal digital inputs, which is how they are used in the Arduboy by default.

By being analog inputs just means that they support the analog input function. What this does is provide a 0 to 255 1023 integer relative to a 0 to 5v signal respectively.

When polled as a digital input, it returns a boolean 0 or 1 depending on if the voltage is over a certain threshold, I’m not sure exactly what it is though but I’m sure referencing the datasheet can tell you exactly.

By enabling the internal pullup, this means digital input will return a 1 and analog input will return a 255 1023 (or close to it). When the button is pressed and pulls the signal to ground, digital and analog input will both read 0.

1 Like

Ah, then if I understand correctly, for this application you would just want to call it as INPUT not INPUT_PULLUP

Apparently it’s actually in the 0-1023 range because the ADC is actually 10 bits for some reason.
Some boards support up to 12 bits, but Leonardo is definitely referenced as being 10 bits wide.

Numbers are hard

1 Like

I found the solution. Nothing to do with port manipulation or redefining A0 or other pin names.
Arduboy lib disables the ADC, which reads the analog value and converts it to 0 … 1023.

After arduboy.begin(); you have to call power_adc_enable(); to turn it on again.

After that you can use analogRead(A0);

arduboy.begin(); disables the ADC to save power. And probably to gain speed, too.

Thaks to this thread and to MLXXXpScott.