Skip to content

How can I measure source voltage using ATTINY804. #1081

Answered by hmeijdam
krishank652 asked this question in Q&A
Discussion options

You must be logged in to vote

This will print the actual VCC in the serial monitor.

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("VCC: ");
Serial.println (readVDD());
delay(1000);
}

uint16_t readVDD(void){ //returns Vdd volts*100 (3.21v = return 321)
    //adc0 VREF = 1v1
    VREF.CTRLA = (VREF.CTRLA & ~VREF_ADC0REFSEL_gm) | VREF_ADC0REFSEL_1V1_gc;
    ADC0.CTRLB = ADC_SAMPNUM_ACC64_gc; //64 samples
    ADC0.CTRLC = ADC_REFSEL_VDDREF_gc | ADC_PRESC_DIV16_gc | ADC_SAMPCAP_bm;
    ADC0.CTRLD = ADC_INITDLY_DLY64_gc;
    ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc;
    ADC0.CTRLA = 1; //enable adc
    ADC0.COMMAND = 1; //start
    while( ADC0.COMMAND ){} //wait
    return (1023*1.1*100ul*64) / ADC0.RES;
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@krishank652
Comment options

Answer selected by krishank652
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants