The DS18B20 is a digital thermometer that able to convert the temperature from -55 to +125 degree Celsius. The controller communicates with this temperature sensor using a 1-Wire bus. So this device requires only three wires including GND, VCC, and data line. Furthermore it can use data line to power the device eliminating the VCC.
This device also work in TTL logic level that could interface with the dsPIC30F2010 using its digital bi-directional I/O.
![]() |
DS18B20 Pin Configuration |
![]() |
DS18B20 In TO-92 Package |
For more information and interfacing this sensor with a micro-controller please check this post.
The newer version of CCS PICC (v5.119) have a C driver "ds18b20.c"for this device that allow the programmer to call it using a few lines of code. It's effective and fast.
The following example I use RB0 of dsPIC30F2010 to read the temperature sensor from a single wire ds18b20 temperature sensor. The sensor's data will be send over the RS-232 between the micro-controller and the host PC. I also added a TC1604A-04 16x4 LCD to display the temperature data in degree Celsius and Fahrenheit.
- CCS PICC Source Code (v5.119)
/*CCS PICC Compiler version 5.119*/- #include <30F2010.h>
- #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
- #use delay(clock=20M)
- #use rs232(UART1,BAUD=9600)
- #define PIN_DS18B20_DATA PIN_B0
- #include "ds18b20.c"
- #define LCD_RS_PIN PIN_E4
- #define LCD_RW_PIN PIN_E5
- #define LCD_ENABLE_PIN PIN_E5
- #define LCD_DATA4 PIN_E0
- #define LCD_DATA5 PIN_E1
- #define LCD_DATA6 PIN_E2
- #define LCD_DATA7 PIN_E3
- #include "lcd.c"
- #define lcd_clear() lcd_putc('\f')
- #define lcd_home() lcd_putc('\a')
- /*For 16x4 LCD Only*/
- #define line_1() lcd_send_byte(0,0x80);
- #define line_2() lcd_send_byte(0,0xC0);
- #define line_3() lcd_send_byte(0,0x90);
- #define line_4() lcd_send_byte(0,0xD0);
- void main(){
- signed int16 val;
- printf("\n\rdsPIC30F2010 Prototype Board.");
- printf("\n\rSaturday 26th September 2025");
- printf("\n\rds18b20 Humidity Sensor Example\n\r");
- printf("\r\n\r\ds18b20.c - DHT11 example starting\r\n\r\n");
- ds18b20_init();
- lcd_init();
- printf(LCD_PUTC,"dsPIC30F2010 LCD");
- line_2();
- printf(LCD_PUTC,"ds18b20 Sensor");
- line_3();
- printf(LCD_PUTC,"Programming");
- line_4();
- printf(LCD_PUTC,"CCS PICC v5.049");
- delay_ms(5000);
- lcd_clear();
- while(1){
- ds18b20_read(&val);
- printf("temperature = %ldC\r\n", val/(signed int16)16);
- lcd_home();
- printf(LCD_PUTC," ds18b20 Sensor");
- line_2();
- printf(LCD_PUTC," Temperature");
- line_3();
- printf(LCD_PUTC," %ld%cC",val/(signed int16)16,0xDF);
- line_4();
- printf(LCD_PUTC," and %f%cF",1.8*(val/(signed int16)16)+32,0xDF);
- delay_ms(1000);
- }
- }
I modified the "lcd.c" C driver file to make it works with my TC1604A-04 LCM module. So I need to copy this driver file to my project folder. The "ds18b20.c" C driver file is already existed in the driver folder of CCS PICC v5.119. But I also copied to my project folder.
This program needs 51% of ROM and 36% of RAM.
![]() |
Serial Input/Output Monitor |
I use my DIY dsPIC30F2010 Prototype Board offered by PCBWAY to test this programming example.
![]() |
Start Up Program |
![]() |
ds18B20 Temperature Reading |
Click here to download this example.
No comments:
Post a Comment