How to Interface ds1820 with PIC16F877A

ds1820 with PIC16F877A Slicker

Tags: Interfacing ds1820 with pic16f877a, ds1820 c source code, schematics for ds1820 interface with PIC16F877A, C Program to read temperature using PIC16F877A, Circuit Diagram to Interface ds1820 with PIC16F877A, Testing the ds1820 with PIC16F877A,
Overall rating
PIC16F/18F Slicker Board

The PIC16F/18F Slicker 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 USB port.

Microchip’s PIC (PIC16F877A), PIC16F/18F Slicker 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 PIC16F877A

We now want to read the temperature in PIC16F/18F Slicker Board from temperature sensor ds1820. The PIC16F/18F Slicker board uses the ADC pin for reading temperature from temperature sensor ds1820. The reading output is displayed into PC through UART.

The 10 bit ADC used to read temperature. Basic clocking for the A/D converters is provided by the VPB clock. A programmable divider is included in each converter, to scale this clock to the 4.5 MHz (max) clock needed by the successive approximation process. A fully accurate conversion requires 11 of these clocks.

Pin Assignment with PIC16F877A



Temp Sensor

PIC16F/18F Lines

Temperature Sensor








*Turn ON RXD, TXD and DS1820 Pin of CONFIG Switch SW1


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

Data array and Temperature

values will be displayed

in HyperTerminal


Circuit Diagram to Interface ds1820 with PIC16F877A


Source Code

The Interfacing ds1820 with PIC16F877A 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 PIC16F877A


Title : Program to read temperature






#include "delay.c" __CONFIG(0x3f72);

#define DQ         RC0

#define DQ_DIR     TRISC0

#define FOSC       10000

#define BAUD_RATE  9.6      // 9600 Baudrate

#define BAUD_VAL   (char)(FOSC/ (16 * BAUD_RATE )) - 1;     

// Calculation For 9600 Baudrate @10Mhz

unsigned char i=0;

float Temerature=0;

void ds1820_init();

void Reset(void);

void write(unsigned char);

unsigned char Read(void);

void Serial_init();

void main()


   unsigned char Temp[9];





   printf("\033[2J");   // Clear the Hyper terminal;






      DQ_DIR    = 1;



  // this will be raised after finishing conversion





      Temp[i] = Read();      // Read 9 bytes 


      printf("%d ",Temp[i]);    










void ds1820_init()


      DQ    = 1;

      DQ_DIR = 1;         // pull up



void Reset(void)


      DQ_DIR    = 0;

      DQ     = 0;


      DelayUs(250);      // 500us

      DQ_DIR    = 1;

      DelayUs(100);      // 40us

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


      DelayUs(250);      // 500us



void write(unsigned char cmd)



      DQ_DIR   = 0;                // pull down

      DQ     = 0;

      DelayUs(25);                 // 10us

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

      cmd      = cmd >> 1;

      DelayUs(120);                // >45us

      DQ_DIR   = 1;                // release




unsigned char Read(void)


      unsigned char temp=0,RecDat=0;



         DQ_DIR   = 0;

         DQ     = 0;        // pull down

    DelayUs(25);       // 10us

    DQ_DIR   = 1;      // release

        DelayUs(25);       // 10us

        temp   = DQ;       // read bit

        temp   = temp<<i;

       RecDat |= temp;

        DelayUs(70);       // 30us


      return RecDat;     


void Serial_init()


      TRISC=0xc0;        // RC7,RC6 set to usart mode(INPUT)

      TXSTA=0x24;        // Transmit Enable

      SPBRG=BAUD_VAL;    // 9600 baud at 10Mhz

      RCSTA=0x90;  // USART Enable, Continuous receive enable

      TXIF=1;            // Start Transmit



void putch(unsigned char Data) // data TX required for printf



         TXREG = Data;





To compile the above C code you need the Mplab software & Hi-Tech Compiler. They must be properly set up 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 Mplab, you want to 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 Slicker Board.

The PICKIT2 software is used to download the hex file into your PIC16F/18F Slicker Board through USB port line.

Testing the ds1820 with PIC16F877A

Give +12V power supply to PIC16F/18F Slicker 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 Mplab. If you want to see more details just see the videos in below link.

   -  How to create and Debug a Project in Mplab using PIC16F using Hi-Tech Compiler.


General Information

Join the World's Largest Technical Community

we respect your privacy.