1

I am a little bewildered about the analog pins on the Arduino ATTiny88.

First of all, am I correct in thinking the functions analogRead() and analogWrite() can use either the string A0 - A7 or a decimal number as inputs? Since I need to use the pins A7, A0, A1, A2m A3, and A4 - in that order - as analog inputs and A6 as an analog output, it would be much more convenient to use decimal digits instead of the aforementioned strings.

If so, then there also is a bit of a question in my mind about the numbers themselves. It looks to me as if the pin numbers start with 0, rather than 1, so the right hand pins start with 15, and the analog pins start with 17 (A6) through 24 (A5). So can I simply scan the analog pins starting with analogRead(18) and increment each pin by one through analogRead(23) and use analogWrite(17), or do I have to build a table of strings and iterate through the table?

I have looked through the documentation on various boards, and it just is not quite clear to me. The digital pins are a bit more consistent, although not fully so. D0, D1, and D2 are on what would normally be pins 30 - 32, adn digital pins 3 - 25 are all offset by 1 from the usual numbering scheme. Some other Arduino boards have even stranger numbering schemes. (And yes, I understand why the pinouts on the board do not match up exactly to those on the MCU chip.)

MH-Tiny ATTINY88

So far, I have been unable to get the analog inputs to work, no matter what I try. I have tried:

Value = analogRead(A0);
Value = analogRead(A7);
Value = analogRead(17);
for (uint8_t x=17; x < 24; X++)
{
    Value = analogRead(x);
}

Obviously, I prefaced all of these calls by defining the ports with pinMode(xx, INPUT) using identical target values. None of them appeared to return an appropriate value given the voltages on the inputs. (Roughly 3 volts on each pin, which should have returned a value around 600.)

2
  • You didn't mention what framework you are using. if you are referring to ATTinyCore Arduino framework, the A0-A7 is defined here, if you trace up a few lines, it matches the pin17-pin25.
    – hcheung
    Commented Apr 16 at 11:32
  • I am not sure what "framework" means. The board definition I am using in the IDE is "MH-ET LIVE Tiny88(16MHz)".
    – LesRhorer
    Commented Apr 16 at 15:14

3 Answers 3

2

I need to use the pins A7, A0, A1, A2, A3, and A4 - in that order

If you want to read the ADC from the channels in the order you want, all you need to do is to create an array that has the pins in the sequence as per your order and access each of it with an index.

int adcChannels[]{A7, A0, A1, A2, A3, A4};

for (int i=0; i<sizeof(adcChannles; i++) {
  int adcValue = analogRead(adcChannel[i]);
}

BTW, based on your comment, I have found the Arduino Core (a.k.a Arduino framework) that you are using on the internet. Based on the board definition for MH-ET LIVE Tiny88, the analog pins definition can be found here. They are partially in sequence with pin19-24 for A1-A5, and pin 17-18 for A6-A7.

One more thing, the naming of the pin A0 is not a string. It is just a name for a variable as

static const uint8_t A0 = 19;
1
  • I don't know why, but this does not seem to be correct. I was able to get it working, but not with those values. Please see my answer.
    – LesRhorer
    Commented Apr 18 at 18:07
1

This still seems quite odd, to me, but I have gotten an answer that works. In an effort to discover the correct ports to use for the analogRead() and analogWrite functions, I created this test sketch:

#include <Arduino.h>
#include <TM1637Display.h>

// Create a display object of type TM1637Display
TM1637Display display(4, 3);

int BatteryValue = 0;

void setup()
{
    // Note: Setting up the analog pins as inputs is not required
    display.setBrightness(0x0f);    // Initialize display
    display.clear();                // Clear display
}

void loop()
{
    // Move through battery cells
    for (int  x = 1; x < 26; x++)
    {
        display.showNumberDec(x, false);            // Using the TM1637 to show the pin number
        delay(1500);
        BatteryValue = analogRead(x);               // Read the value of the pin number
        display.showNumberDec(BatteryValue, false); // Display the analog value of the pin
        delay(2000);
    }
}

I first tried initializing the pins as inputs, but it turns out this is unnecessary, and as it turns out would cause problems. The output of the sketch made it perfectly clear which pins are the analog inputs. This board uses pins 11 - 16 in code to read the pins labeled as A7 and A0 - A4 on the board, respectively, and pin 10 to write to A6 on the board. This does make sense in a sort of twisted way, if we take the numbers to be Hexadecimal values, rather than Decimal. In any case, this code now works perfectly:

#include <Arduino.h>

int BatteryValue = 0;

void setup()
{
    analogWrite(10, 128);       // Send PWM to analog pin 10
}

void loop()
{
    // Move through battery cells
    for (int  x = 11; x < 17; x++)
    {
        BatteryValue = analogRead(x);
    }
}

All attempts to use A0, 17, etc failed.

1

Expectations

First of all, am I correct in thinking the functions analogRead() and analogWrite() can use either the string A0 - A7 or a decimal number as inputs?

This requires dissection to explain what is correct (or rather should have been correct) and what isn't. A0, A1, are "identifiers" rather than strings and typically evaluate to some value of int type. These pins are connected to usage of analogRead(). analogWrite() doesn't have any strict relationship to these pins. In some cases none of the A0, A1, etc pins are PWM or DAC output capable.

For analogRead() you are more or less correct. It's the norm for the numbers used A0, A1, etc to be larger than the largest ADC channel number. And that allows analogRead() to be written in such a way as to accept either an ADC channel number or one of those identifiers starting with an A.

Core bug

However, the MHEtLiveArduino core code to do that is broken. And if you're curios as to how:

#elif defined(__AVR_ATtiny88__)
    if (17 <= pin <= 18) pin -= 11; // allow for channel or pin numbers
    else if (19 <= pin <= 24) pin -= 19;
#else
    if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif

In the #else section you can see the code that is used for the more common UNO R3 (and a few others) which have ATmega8/168/328 chips with 14 (0 to 13 inclusive) "digital"-but-not-"analog" pins. The analog pins are mapped to numbers 14 upwards. They subtract off 14 to convert the pin number to an ADC channel number that later goes into the ADMUX hardware register.

The code that MHET bolted onto the original AVR core code (in the #elif defined(__AVR_ATtiny88__) section) contains some if-statements with conditions that take the form A <= B <= C. This is normal math notation that would work in Python, but for C (or C++) needs to be rewritten as A <= B && B <= C. C (and C++) treat A <= B <= C more literally as (A <= B) <= C; they check whether or not the truth-value of A <= B (a 0 or 1) is less than C which is rarely what someone would want.

So, MHET have if (17 <= pin <= 18), which is always true irrespective of the value of pin. 17 <= pin is either 0 or 1. Both 0 and 1 are <= 18. So, no matter what number you give for pin it will pin -= 11. pin is of uint8_t type. So, they subtract 11 from it and allow it to reduce modulo 0x100. Later the take the lower four bits for the ADC channel number.

Patching the core

If you want, you can find and fix your installed MHET core's wiring_analog.c by fixing the if conditions:

if      (17 <= pin && pin <= 18) pin -= 11;
else if (19 <= pin && pin <= 24) pin -= 19;

ATtinycore

It is probably better to switch to ATTinyCore. They seem to support the MHtiny pin mapping.

1
  • I am not sure I want to switch at this point. It might break something else. Assuming I do take that on, does it support the Micronucleus upload, which does not employ a serial port? Also, I am having some trouble with the PWM output on A6. So far I am not able to get it to work. You say in some cases none of them work. How can I know for certain which pins will work?
    – LesRhorer
    Commented Apr 24 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.