How to use internal temperature sensor of ATmega328pb

Posted

in

by

ATmega328PB is a new semiconductor microcontroller from Microchip semiconductors. I have used its previous generation which is ATmega328 and ATmega328P. They were usually found on Arduino Uno and Arduino nano.

This new IC has a temperature sensor built into it. Which is handy for measuring the die temperature. Which can make device stable in high-temperature design. It is not accurate as a dedicated temperature sensor. But it gives you a rough idea. Using this you can the processes.

It is not an Ambient temperature Sensor.

/*
 * main.c
 *
 * Created: 8/15/2022 4:06:41 PM
 *  Author: abhay
 */ 
#define F_CPU 16*1000000
#include <xc.h>
#include "uart.h"
#include "util/delay.h"
#include <stdlib.h>
long Ctemp;
unsigned int Ftemp;

int main(void)
{
		DDRD &= ~(1 << DDD0);							// PD0 - Rx Input
		DDRD |= (1 << DDD1);							// PD1 - Tx Ouput
		USART_Init();
		
	/* Replace with your application code */
	ADMUX = (1<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (0<<MUX2) | (0<<MUX1) | (0<<MUX0);
	ADCSRA =  (1<<ADPS2) |(1<<ADPS1) | (1<<ADEN);
	
	ADCSRA |= (1<<ADSC);
	
	while ((ADCSRA & (1<<ADSC)) !=0);
	
	while (1)
	{
		ADCSRA |= (1<<ADSC);
		while ((ADCSRA & (1<<ADSC)) !=0);
		
		Ctemp = ((ADC - 247)/1.22)*1000;
		Ftemp = (Ctemp * 1.8) + 32;
		
		USART_Transmit( (((int)Ctemp/100000)%10) + 48);
		USART_Transmit( (((int)Ctemp/10000)%10) + 48);
		USART_Transmit( (((int)Ctemp/1000)%10) + 48);
		USART_Transmit('.');
		USART_Transmit( (((int)Ctemp/100)%10) + 48);
		USART_Transmit( (((int)Ctemp/10)%10) + 48);
		USART_Transmit( ((int)Ctemp%10) + 48);
		
		USART_Transmit('\n');
		_delay_ms(1000);
	}
	return -1;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *