How to Interface ds1820 with AVR Primer

Interfacing ds1820 with AVR Primer

Tags: How to interface temperature sensor with AVR,how to interface ds1820 with AVR micro controller,
Overall rating
AVR Primer Board

The AVR Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports In-system programming (ISP) which is done through Parallel/Printer port.

ATMEL’s AVR (Atmega8535), AVR Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.

Temperature Sensor

DS1820 is a temperature sensor which is small sensor. The output of sensor converted to digital that easy connecting with microcontroller.

Interfacing ds1820

Fig. 1 shows how to interface the ds1820 to microcontroller. As you can see the first pin is connected to GND, the third pin is connected to VCC & the second pin is connected to the Microcontroller. So when the temperature is sensing, it give the sensor reading to controller.


Fig. 1 Interfacing ds1820 to Microcontroller

Interfacing ds1820 with AVR Primer

We now want to read the temperature in AVR Primer Board from temperature sensor ds1820. AVR Primer board uses the PORTA pin for reading temperature from temperature sensor ds1820. The reading output is displayed into PC through UART.

The PORTA pin is used to read temperature.

Pin Assignment with AVR Primer






Connect Port pin PORTA.2 with JH1(anyone) of DS1820 section by jumper or bus

Connect Serial cable between USART Section in the Board and PC.

Data array and Tempera- ture values will be dis- played in HyperTerminal


Circuit Diagram to Interface ds1820 with AVR


Source Code

The Interfacing ds1820 with AVR Primer program is very simple and straight forward, that reading temperature from temperature sensor ds1820 and it display into PC through serial port.

C Program to read temperature using AVR Primer


Title : Program to read temperature








#define DQ      PORTA.2

#define DQ_DIR  DDRA.2

#define DQ_READ PINA.2   

#define TXIF    UCSRA.5


#define FOSC        16000000L                        //Oscillator Frequency

#define USART_Baud  9600                                                

#define BaudH       ((unsigned int)(((FOSC/16/USART_Baud)-1) >> 8))

#define BaudL       ((unsigned int)(((FOSC/16/USART_Baud)-1) & 0xFF))


unsigned char i,Temp[9]={0,0,0,0,0,0,0,0,0};

float Result;


void ds1820_init(void);

void Reset(void);

void write(unsigned char);

unsigned char Read(void);

void Serial_init(void);   


void main()











                DQ_DIR = 0; 

                DQ     = 1;










                            printf("%d ",Temp[i]);                  //this is the reading, uncomment

                                                        //here to see reading                    







void ds1820_init()


                        DQ    = 1;

                        DQ_DIR = 0;                                    //pull up



void Reset(void)


        DQ          = 0;

                        DQ_DIR         = 1;

                        delay_us(500);       //500us

        DQ          = 1;

                        DQ_DIR         = 0;

                        delay_us(40);                      //40us

                        while(DQ_READ==1);       //wait until presence pulse

                        delay_us(500);        //500us




void write(unsigned char cmd)




                            DQ_DIR     = 1;                                         //pull down

                            DQ              = 0;

                            delay_us(10);                                          //10us

                            DQ = (cmd & 0x01)?1:0;     //Send bit

                            cmd            = cmd >> 1;

                            delay_us(60);                                          //>45us

                            DQ_DIR     = 0;                                         //release





unsigned char Read(void)


                        unsigned char temp=0,RecDat=0;



                            DQ_DIR     = 1;

                            DQ              = 0;                 //pull down

                            delay_us(10);                  //10us

                            DQ_DIR     = 0;                 //release

                            delay_us(10);                  //10us

                            temp          = DQ_READ;            //read bit

                            temp          = temp<<i;

                            RecDat |= temp;

                            delay_us(30);                  //30us


                        return RecDat;                   



void Serial_init(void)


        UCSRA = 0x00;                //Status Register

        UCSRB = 0x18;                //Transmit Enable,Receive enable,interrupts disabled   

        UCSRC = 0x86;                //8 databits,no parity,single stop   

        UBRRH = BaudH;

        UBRRL = BaudL; 




#define _ALTERNATE_PUTCHAR_       


void putchar(unsigned char Data)


   UDR = Data;     



To compile the above C code you need the CodeVision AVR software. The software has it’s own IDE and built-in AVR gcc- Compiler. They must be properly installed and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In CodeVision AVR software, you can develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without microcontroller Board.

The AVR Code Blaster software is used to download the hex file into your microcontroller through Parallel port.

Testing the ds1820 with AVR

Give +12V power supply to AVR Primer Board; the serial cable is connected between the controller and PC. Open the Hyper Terminal screen, select which port you are using and set the default settings. Now the screen should show the current temperature readings.

Bring a Hot soldering iron tip near the ds1820's pins, don't touch it keep it 1 or 2mm away. The screen should update with the rising temperature. Now finally touch the pins of ds1820 with the tip of iron, the temperature should rise quickly. Keep it there until temperature rise to 80 degrees, and then remove the iron.

If any data is not coming in Hyper Terminal, then you just check the serial cable is working or not. Otherwise you just check the code with debugging mode in CodeVisionAVR.

General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.
  • More instructions are available in following articles,
       -  User Manual of AVR Primer Board.
       -  How to create & Debug a Project in CodeVision AVR.

Join the World's Largest Technical Community

we respect your privacy.