Read this blog to be able to read multiple ADC channels on an ATtiny85.
ready your Multiple ADC Channel Circuit
Above you have a circuit with 3 potentiometers connected to ADC1(PB2), ADC2(PB4), and ADC3(PB3) of the ATTiny. A serial port is simulated through software on the ATTiny Tx line PB0 which connects to pin 1 of the UNO. The usual programming lines pin 13, 12, 11, and 10 of the UNO connects to PB2, PB1, PB0, and PB5 of the ATTiny,Â
The Program
#include
#include
uint16_t adc_value;
uint8_t i;
SendOnlySoftwareSerial Monitor (0); // use PB0 as Tx Pin
void setup() {
// put your setup code here, to run once:
// disable digital input
DIDR0 |= (1<
The firmware consists of the usual ADC setup only now, the MUXn bits of the ADMUX registers are manipulated in the looping code. Also, a SendOnlySoftwareSerial monitor library is used instead of SoftwareSerial to eliminate the need for an RX pin. This process keeps other pins available on the ATTiny.
Steps to Setup the ADC
There are lots of similarities in setting up the ADC like in the previous blog. However, in this setup, you specify the ADC channel to use before starting the ADC conversion (or after finishing it according to the datasheet). The MUXn bits in the ADMUX register are scanned before (or after) conversions as they are locked during the conversion process. This protects the integrity of your ADC data.
- There is an optional step to disable the digital inputs ADC0D – ADC3D in the DIDR0 register to save power generated by the digital input buffer.
- Begin the SendOnlySoftwareSerial Instance Monitor and set it to your desired baud rate.
- Set up the format of the ADC result (ADCH and ADCL registers). Here, a 10-bit format is used with left justification through the ADLAR register in ADMUX.
- Set the clock prescaler for the ADC. The ADC conversion clock can only go as high as 1MHz. Set it through the ADPSn bits in the ADCSRA register.
- Enable the ADC through the ADEN bit.
Steps to Make Multiple ADC Channel Conversions
After setup, you can now go through the looping code.
- Select the ADC channel you want to use by clearing the MUXn bits and then setting them on the ADMUX register. (Note that the order of the conversions seems one step behind in actual. It could be the MUXn bits were actually relayed after the conversion process).
- Start the ADC conversion by setting the ADSC bit in the ADCSRA register.
- Monitor the ADSC bit until it becomes zero. This means the ADC has finished conversion.
- You may now extract the value of the ADCL and ADCH registers and put it into your ADC variable (The 16-bit adc_value here). Note that there’s a two-step approach to get a 10-bit value from your 8-bit microcontroller. First, get the ADCL and then the ADCH value. After that, combine them. However, this example combined the process through operand precedence rules (left-to-right).
- The process repeats in a for-loop so that you can scan the 3 ADC channels consecutively.
The programming process starts with the Arduino UNO programmed as an Arduino ISP as in the previous blog and continues from there. To do serial monitoring, you can use the same Arduino UNO provided that you program it with a blank or blinky code that has serial monitor capability. Make sure to disconnect the serial monitor pins while programming. Also, disconnect programming lines that may interfere with your ADC readings.
Hope you’ve learned a lot on this topic about multiple ADC channels on an ATTiny85 tutorial :).
Circuit in Action
Upload ISP code to UNO
Upload using programmer to ATtiny85Â Â
Program Blinky Code to UNO and Start Serial MonitorÂ
Serial Monitor ADC values of the 3 potentiometers