RE: ATTiny45 AREF

Hi Gang

I'm wanting some advice on how to use AREF on the ATTiny45. My go to page for programming the ATTiny has been High-Low Tech. I've checked their website thoroughly and there is no mention of using AREF. In fact, I can't even find a AREF pin. :frowning:

Naturally I've spread my wings and looked further afield. I managed to find the following Provide Your Own. The article does discuss using AREF. In fact it suggests that there are internal AREF options (INTERNAL2V56). Unfortunately, the article is two years old and it references an even older article on how to program the ATTiny using another (possibly dated) 'core'.

Has anyone had success using the Arduino IDE/Hi-Low Tech combination to make use of AREF? If so can you give me some pointers?

Cheers

Jase :slight_smile:

...and there is no mention of using AREF. In fact, I can't even find a AREF pin. :frowning:

I guess by now you're aware that the AREF pin on a tiny45 DIP chip is physical pin 5, but I thought I'd mention it anyway.

The datasheet for the tiny45 definitely says that there are two internal analogue reference voltages, 1.1V and 2.56V, so you're on the right track:-

Internal reference voltages of nominally 1.1V / 2.56V are provided on-chip. Alternatively, VCC can be used as reference
voltage for single ended channels. There is also an option to use an external voltage reference and turn-off
the internal voltage reference.

Using DrAzzy's cores, both of these compile:-
analogReference(INTERNAL1V1);
analogReference(INTERNAL2V56);
Whereas on a board without a 2.56V reference like an UNO, I get a compile error.

That's got to be an indication that it will probably work OK using either of the internal references, set as shown.

I don't know whose core you're using, but perhaps you could contact them and ask?
(Or do a test. Set the internal reference to 2.56V with 'analogReference()', then apply a known voltage to the ADC input pin and have the chip light an LED if the analogue value is within the expected range.)

Hi OldSteve

First and foremost, thanks for your response. After your prompting I checked the Hi-Low Tech site (yet again) and managed to locate the AREF pin (pin 5) with the help of my wife. :slight_smile:

Making use of the internal 2.56V AREF would be very attractive. I'm using the High-Low Tech Core but I can't see any mention of the internal AREF.

I've tried making contact with MIT Media Lab but I'm not confident I will get a response after reading their FAQ.

I will therefore try your suggestion of running a test as you outlined above. I will keep you posted.

Cheers

Jase :slight_smile:

Yeah, let us know how you go.
That's the David Mellis core that you're using. I used to use it, but switched to DrAzzy's because it supports a wider range of chips and configurations.

Hi OldSteve

Sorry to take so long to get back to you. I had a chance to sit down this evening and test whether a simple sketch would compile using analogReference(INTERNAL2V56).

void setup() {
  analogReference(INTERNAL2V56);
}

void loop() {

}

Unfortunately I get the following error.

exit status 1
'INTERNAL2V56' was not declared in this scope

If I however change the sketch.

void setup() {
  analogReference(INTERNAL);
}

void loop() {

}

My code compiles. 2.56V is my preference reference. :wink:

I've done some further searching and found the following thread on the Arduino forum.

analogReference on ATTiny85

If you check out specifically post #6 there is a odd suggestion involving defining a constant. I'm not quite sure what's happening here although if I copy and paste the code it does compile? :confused:

#define INTERNAL2V56_NO_CAP (6)

void setup( void )
{
  analogReference( INTERNAL2V56_NO_CAP );
}

What does the bracketed six represent?

Cheers

Jase :slight_smile:

PS I'm not sure what 'Core' means? I've tried reading up on this and have also spoken to a retired Electrical Engineering lecturer and the best I've come up with is it's the guts of the chip???

ilovetoflyfpv:
Hi OldSteve
Sorry to take so long to get back to you. I had a chance to sit down this evening and test whether a simple sketch would compile using analogReference(INTERNAL2V56).

void setup() {

analogReference(INTERNAL2V56);
}

void loop() {

}


Unfortunately I get the following error.

Right. That's a core-dependent thing then. The David Mellis core that you're using differs from DrAzzy's core in that respect. (I'll address what a core is further down. :slight_smile: )
Your code above compiles fine for me.

If I however change the sketch.

void setup() {

analogReference(INTERNAL);
}

void loop() {

}


My code compiles. 2.56V is my preference reference. ;)

Are you sure that the above will set the reference to 2.56V, and not 1.1V?
It's a bit ambiguous. Using an UNO board, for instance, that would set the reference to 1.1V. That's another good reason to use DrAzzy's core, besides the fact that it supports a wider range of chips and configurations, and is more up-to-date.
Personally, I like the fact that when using DrAzzy's core, you can use "INTERNAL2V56" or "INTERNAL1V1", because there's no ambiguity about the reference voltage, unlike when using "INTERNAL", when a chip has multiple internal references available.

I've done some further searching and found the following thread on the Arduino forum.
analogReference on ATTiny85

If you check out specifically post #6 there is a odd suggestion involving defining a constant. I'm not quite sure what's happening here although if I copy and paste the code it does compile? :confused: #define INTERNAL2V56_NO_CAP (6)

The above just defines "INTERNAL2V56_NO_CAP" to be equal to the value 6. It effectively means that you could use:-analogReference(6);(Which, incidentally, compiles fine for me.)

PS I'm not sure what 'Core' means? I've tried reading up on this and have also spoken to a retired Electrical Engineering lecturer and the best I've come up with is it's the guts of the chip???

In this context, "core" means the files that are used by the IDE to relate to a particular chip or board. For the ATtiny range, the two most commonly used cores are the one by David Mellis, and the newer one by DrAzzy that I'm using.
To avoid the ambiguities that you're experiencing with the ealier core, I'd highly recommend using DrAzzy's instead. Like the David Mellis tiny cores, it can be installed by adding a line to "Additional Boards Manager URLs" in ">File >Preferences".
If you're interested, there's more info here:- ATTinyCore
DrAzzy is a regular visitor to the forums, too, contributing on a daily basis.

Hi OldSteve

Once again thanks for answering my questions. So the most straight forward option would be to remove the David Mellis Core and replace it with the DrAzzy Core? Am I correct in thinking that the uploading process is the same?

Alternatively, if I stick with the David Mellis Core I need to work out whether setting analogReference(INTERNAL); sets the analog reference voltage to 1.1V or 2.56V. I strongly suspect it's 1.1V.

I'd love to know what analogReference(6); actually does? Looking at the ATtiny45 datasheet, specifically page 134, table 17-3, second last row, if Voltage Reference Selection Bits 2 and 1 are set high and Voltage Reference Selection Bit is set low you can select 2.56V. I wonder whether inserting the number 6 in analogReference(6) actually does this? 110 binary = 6 decimal?

Cheers

Jase :slight_smile:

Not sure how the core you are using is done, but one option is to mask off the reference bits (they are in the ADMUX register), and place the result into a tempory variable. Then "or" the desired reference bits with the tempory variable (local_ADMUX) and save that back into the ADMUX register.

void analogReference(uint8_t reference)
{
 analog_reference = reference;
#if defined(ADMUX)
    // clear the reference bits REFS0, REFS1[,REFS2]
    uint8_t local_ADMUX = (ADMUX & ~(ADREFSMASK));
    
    // select the reference so it has time to stabalize.
    ADMUX = local_ADMUX | reference ;
#else
#   error missing ADMUX register which is used to sellect the reference and channel
#endif
}

The number six might be like this INTERNAL_2V56WOBP define, but without the bit shifting stuff I added for reference.

#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#define ADREFSMASK (1<<REFS2) | (1<<REFS1) | (1<<REFS0)
#define EXTERNAL_AVCC 0
#define EXTERNAL_AREF (1<<REFS0)
#define INTERNAL_1V1 (1<<REFS1) 
#define INTERNAL_2V56WOBP (1<<REFS2) | (1<<REFS1) 
#define INTERNAL_2V56 (1<<REFS2) | (1<<REFS1) | (1<<REFS0)
#endif

Now that's just scary. :o

ilovetoflyfpv:
Hi OldSteve

Once again thanks for answering my questions. So the most straight forward option would be to remove the David Mellis Core and replace it with the DrAzzy Core? Am I correct in thinking that the uploading process is the same?

The upload process is identical, regardless of which core is used. It just uploads the finished *.hex file to the chip.

Alternatively, if I stick with the David Mellis Core I need to work out whether setting analogReference(INTERNAL); sets the analog reference voltage to 1.1V or 2.56V. I strongly suspect it's 1.1V.

So do I, and the problem is that since it isn't clear, there's the potnetial for it to confuse you again in the future. That's why I suggested DrAzzy's cores.

I'd love to know what analogReference(6); actually does?.....110 binary = 6 decimal?

Yes. One of the first things you clearly need to do is learn binary. It's an essential part of learning programming.

I didn't watch all of the video you linked, there was no need to, but I noticed that he showed how to install a core manually. You don't need to go through that process. That's what the "Boards Manager" is for.
If you install DrAzzy's core, all you need to do is add an entry to the "Additional Boards Manager URLs" line in the "Preferences" dialog, ">File >Preferences >Additional Boards Manager URLs". The IDE will take care of installation.
It's all covered under "Installation" in the DrAzzy ATtiny page that I linked.
The line to add is:-
http://drazzy.com/package_drazzy.com_index.json

After you do that, in ">Tools >Board", at the bottom, you'll see a whole bunch of new chips listed under "ATtiny Universal". When you select one, "ATtiny x5 Series" for example, there'll be extra options available under "Tools":- "Timer1 Clock", "B.O.D", "Chip" and "Clock", where you can select your specific chip and configuration options.

As I mentioned, with that core and an ATtiny45, you can use the "INTERNAL1V1" and "INTERNAL2V56" options for 'analogReference()', so there'll be no doubt what the reference is, especially in the future if you return to review, modify or copy part of the code to a new program.

DrAzzy's cores also add newer ATtiny boards that aren't covered by David Mellis' cores. There are 14 entries listed under "Board".

David Mellis' cores are good, but getting a bit long in the tooth, lacking features/chips. Time moves on. :slight_smile:

Hi OldSteve

Well I've taken the plunge and downloaded and installed ATtiny Universal. If I select Tools in the Arduino IDE there are few more options than the older ATtiny Microcontrollers. I've naturally read the README document but it makes no mention of what the following do.

Board: Obviously what flavour of ATtiny you have.

Timer 1 Clock: Timer interrupts? Related to clock?

B.O.D.: Brown Out Detection? So what happens here exactly? If VCC reaches 4.3V for example the chip restarts?

Clock: Simply speed of processor? Any correlation to Timer 1 Clock?

Chip: Obviously related to choice made in Board.

I know I may sound like a broken record but I do appreciate your help.

Cheers

Jase :slight_smile:

"Board" and "Chip" are self-explanatory, as you say.

Generally, just leave "Timer1 Clock" set to "CPU". It's the clock source for Timer1. Doesn't affect "Clock". I've never played with this, so can't say more with confidence.

"Clock" is the speed of the processor. If using an external crystal/resonator, choose the speed to match. Otherwise, when using "Internal", it's up to you. I usually run mine at "8MHz Internal". Nice and fast, and doesn't waste pins on an external resonator. An external oscillator will give better precision, at the cost of those pins. I'm sure you already know this.

"BOD" You have it. "Brown Out Detect" pulls RST low if the voltage drops below the defined level. You can set this to avoid unwanted behaviour if the voltage is low. Handy for battery operation.
For a 5V supply, I set it to 4.3V. For a 3.3V supply, 2.7V is probably appropriate.

In most other regards, there's no difference between the cores in actual use. (Except for the voltage reference we've been discussing.)

Hi OldSteve

Works a treat! Many, many thanks.

Cheers

Jase :slight_smile:

ilovetoflyfpv:
Hi OldSteve

Works a treat! Many, many thanks.

Cheers
Jase :slight_smile:

Good one, Jase. Glad to hear that it's all working. Have fun. :slight_smile:

ilovetoflyfpv:
Hi OldSteve

Well I've taken the plunge and downloaded and installed ATtiny Universal. If I select Tools in the Arduino IDE there are few more options than the older ATtiny Microcontrollers. I've naturally read the README document but it makes no mention of what the following do.

Board: Obviously what flavour of ATtiny you have.

Timer 1 Clock: Timer interrupts? Related to clock?

B.O.D.: Brown Out Detection? So what happens here exactly? If VCC reaches 4.3V for example the chip restarts?

Clock: Simply speed of processor? Any correlation to Timer 1 Clock?

Chip: Obviously related to choice made in Board.

I know I may sound like a broken record but I do appreciate your help.

Cheers

Jase :slight_smile:

Deficiencies of readme are a known issue. See Chip-specific doc pages · Issue #59 · SpenceKonde/ATTinyCore · GitHub for mention of the need for chip specific doc pages, which as yet are only done for a few boards.

I've updated the ATTiny85 and 861 chip doc pages to have a heading for the clock source option.

I added a section on BOD to readme.

Clock menu selects the clock source for the system clock. Fuses are only set when doing burn bootloader (so make sure your clock speed matches what you had when you did burn bootloader - this needs to be done even when no bootloader is in use which is normally the case for the tiny85). Unless otherwise noted, things work like they do on normal Arduino boards - all the timers are clocked off the prescaled system clock.

I added the no-cap option to the tiny x5 voltage reference options (will be in next release or you can clone the repo to hardware), and the reference page for tiny85 actually lists the voltage reference options now.

And yeah - the 6 or whatever is the pattern of bits for that reference source, applied by the normal bitwise manipulation techniques that are standard practice for Register manipulation.