ATtiny85
How to Read ATtiny85 VCC Voltage Without Connecting to ADC – Today I am trying to implement an ADC to read the VCC voltage of an ATtiny85.
This does not require a cable or wire connected between the ADC pin and the VCC pin, but rather reads using the internal registers on the chip.
The VCC voltage read by ATtiny85 will be sent to the computer via serial communication and displayed on the Arduino Serial monitor.
ATtiny85 by default has no Tx Rx pins. To be able to outsmart this we use the SoftwareSerial.h library.
Schematic
In this experiment, I used 2 circuits. The first circuit is Arduino as an ISP which is connected to ATtiny85. It is used to program the ATtiny85. Read here.
The second circuit is, the ATtiny85 circuit is connected to the FT232. This is used to receive ATtiny data and will be forwarded to the computer. Read here.
Code Program
To program the VCC voltage reading using the internal ADC, please use the following program:
#include "SoftwareSerial.h"
int Rx = 3;
int Tx = 4;
SoftwareSerial mySerial(Rx, Tx);
void setup() {
pinMode(Rx, INPUT);
pinMode(Tx, OUTPUT);
mySerial.begin(9600);
}
void loop() {
float supply = readVcc() / 1000.0;
mySerial.print ("VCC: ");
mySerial.println (supply);
delay(100);
}
long readVcc() {
long result;
// Read 1.1V reference against AVcc
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1126400L / result; // Calculate Vcc (in mV); 1126400 = 1.1*1024*1000
return result;
}
Once uploaded, open the serial monitor. Make sure:
- You select port FT232 FTDI
- Set the baud rate of the serial monitor at 9600.
You will see a response from ATTiny85 in the form of a voltage that is read in the Arduino IDE Monitor serial.
If you don’t receive the response, check the circuit again. Thank you for visiting the website Chip Piko.
I hope this article is useful.