728x90

Showing posts with label Sensor. Show all posts
Showing posts with label Sensor. Show all posts

Saturday, September 27, 2025

dsPIC30F2010 and ds18B20 Temperature Interfacing

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. 

dsPIC30F2010 and ds18B20 Temperature Interfacing

 

This device also work in TTL logic level that could interface with the dsPIC30F2010 using its digital bi-directional I/O. 

PIC16F84A DS18B20 1-Wire Temperature Reading And Multiplexing Display Example Using XC8
DS18B20 Pin Configuration
PIC16F84A DS18B20 1-Wire Temperature Reading And Multiplexing Display Example Using XC8
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) 

 


  1. /*CCS PICC Compiler version 5.119*/
  2. #include <30F2010.h>
  3. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  4. #use delay(clock=20M)
  5. #use rs232(UART1,BAUD=9600)


  6. #define PIN_DS18B20_DATA PIN_B0
  7. #include "ds18b20.c"

  8. #define LCD_RS_PIN PIN_E4
  9. #define LCD_RW_PIN PIN_E5
  10. #define LCD_ENABLE_PIN PIN_E5
  11. #define LCD_DATA4 PIN_E0
  12. #define LCD_DATA5 PIN_E1
  13. #define LCD_DATA6 PIN_E2
  14. #define LCD_DATA7 PIN_E3

  15. #include "lcd.c"

  16. #define lcd_clear() lcd_putc('\f')
  17. #define lcd_home() lcd_putc('\a')
  18. /*For 16x4 LCD Only*/
  19. #define line_1() lcd_send_byte(0,0x80);
  20. #define line_2() lcd_send_byte(0,0xC0);
  21. #define line_3() lcd_send_byte(0,0x90);
  22. #define line_4() lcd_send_byte(0,0xD0);



  23. void main(){
  24. signed int16 val;
  25. printf("\n\rdsPIC30F2010 Prototype Board.");
  26. printf("\n\rSaturday 26th September 2025");
  27. printf("\n\rds18b20 Humidity Sensor Example\n\r");
  28. printf("\r\n\r\ds18b20.c - DHT11 example starting\r\n\r\n");
  29. ds18b20_init();
  30. lcd_init();
  31. printf(LCD_PUTC,"dsPIC30F2010 LCD");
  32. line_2();
  33. printf(LCD_PUTC,"ds18b20 Sensor");
  34. line_3();
  35. printf(LCD_PUTC,"Programming");
  36. line_4();
  37. printf(LCD_PUTC,"CCS PICC v5.049");
  38. delay_ms(5000);
  39. lcd_clear();
  40. while(1){
  41. ds18b20_read(&val);
  42. printf("temperature = %ldC\r\n", val/(signed int16)16);
  43. lcd_home();
  44. printf(LCD_PUTC," ds18b20 Sensor");
  45. line_2();
  46. printf(LCD_PUTC," Temperature");
  47. line_3();
  48. printf(LCD_PUTC," %ld%cC",val/(signed int16)16,0xDF);
  49. line_4();
  50. printf(LCD_PUTC," and %f%cF",1.8*(val/(signed int16)16)+32,0xDF);
  51. delay_ms(1000);
  52. }
  53. }

 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.

dsPIC30F2010 and ds18B20 Temperature Interfacing
Serial Input/Output Monitor

I use my DIY dsPIC30F2010 Prototype Board offered by PCBWAY to test this programming example.

dsPIC30F2010 and ds18B20 Temperature Interfacing
Start Up Program

dsPIC30F2010 and ds18B20 Temperature Interfacing
ds18B20 Temperature Reading


Click here to download this example.

 

Friday, September 26, 2025

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing

The DHT11 temperature and humidity sensor features a temperature and humidity sensor
complex with a calibrated digital signal output. By using the exclusive digital-signal-acquisition
technique and temperature and humidity sensing technology, it ensures high reliability and
excellent long-term stability. This sensor includes a resistive-type humidity measurement
component and an NTC temperature measurement component, and connects to a high performance 8-bit micro-controller, offering excellent quality, fast response, anti-interference
ability and cost-effectiveness.

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing

Its standard TTL bi-directional I/O suitable for dsPIC30F2010 Interfacing and Programming. It needs only three wires, +5VDC, DATA and GND. Its DATA pin requires a weak pull up resistor with a resistance around 4.7k. 

I use my dsPIC30F2010 DIY Prototype Board to test this program. 

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
DHT11 Humidity & Temperature
Sensor

Parameters

Relative Humidity
Resolution:             16Bit
Repeatability:         ±1%RH
Accuracy:               25℃ ±5%RH
Interchangeability:  Fully interchangeable
Response time:       1/e (63%)25℃ 6s
                                1m/s Air 6s
Hysteresis:             <±0.3%RH
Long-term stability: <±0.5%RH/yr


Temperature
Resolution:             16Bit
Repeatability:         ±1℃
Accuracy:               25℃ ±2℃
Response time:       1/e (63%) 10S


Electrical Characteristics
Power supply:         DC 3.3~ 5.5V
Supply current:       Measure 0.3mA Standby 60μA
Sampling period:     Secondary Greater than 2 seconds 

For more detail about interfacing this sensor with a micro-controller please see this post. 

dsPIC30F2010 CCS PICC Programming

The newer version of CCS PICC (v5.119) has a C driver for the DHT-11 environmental sensor that could be called using a few lines of code. The "dht11.c" C driver locates in the drivers directory in program files. I use pin RB0 of dsPIC30F2010 to interface with this sensor.

The following example shows a simple sensor reading and data displaying over the RS-232.

  • board.h 
  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  4. #use delay(crystal=20000000)

  5. #define LED0 PIN_D0
  6. #define LED1 PIN_D1
  7. #define SW0 PIN_C13
  8. #define SW1 PIN_C14

  9. #define DELAY 500

  10. #use rs232(UART1, baud=9600, stream=UART_PORT1)

 

  •  main.c
  1. #include "board.h"

  2. #define PIN_DHT11_DATA PIN_B0
  3. #include <dht11.c>

  4. void main(){
  5. unsigned int8 relativeHumidity;
  6. unsigned int8 tempC;
  7. printf("\n\rdsPIC30F2010 Prototype Board.");
  8. printf("\n\rWednesday 25 September 2025");
  9. printf("\n\rDHT-11 Humidity Sensor Example\n\r");
  10. printf("\r\n\r\dht-11.c - DHT11 example starting\r\n\r\n");
  11. dht11_init();
  12. delay_ms(1000);
  13. while(1){
  14. dht11_read(&relativeHumidity, &tempC);
  15. printf("HUMIDITY=%03u%%, TEMPERATURE=%02uC\r\n", relativeHumidity, tempC);
  16. delay_ms(2000);
  17. }
  18. }

 

  • Memory Use 


  •  Serial Input/Output Monitor

 


 
 Click here to download this example.

I add a 16x4 LCD (TC1604A-04) to display temperature and humidity data. Using a character LCD is very conventional for most small micro-controller programming and interfacing. I copied the "lcd.c" 16x2 LCD C driver to my project folder that I can modify it without changing the original file. I disable the LCD R/W pin (wire to GND) and I upgrade this LCD driver to a 16x4 LCD.

Its "board.h" header file remains the same to above example. 

  • main.c

 

  1. #include "board.h"

  2. #define PIN_DHT11_DATA PIN_B0
  3. #include <dht11.c>

  4. #define LCD_RS_PIN PIN_E4
  5. #define LCD_RW_PIN PIN_E5
  6. #define LCD_ENABLE_PIN PIN_E5
  7. #define LCD_DATA4 PIN_E0
  8. #define LCD_DATA5 PIN_E1
  9. #define LCD_DATA6 PIN_E2
  10. #define LCD_DATA7 PIN_E3

  11. #include "lcd.c"

  12. #define lcd_clear() lcd_putc('\f')
  13. #define lcd_home() lcd_putc('\a')
  14. /*For 16x4 LCD Only*/
  15. #define line_1() lcd_send_byte(0,0x80);
  16. #define line_2() lcd_send_byte(0,0xC0);
  17. #define line_3() lcd_send_byte(0,0x90);
  18. #define line_4() lcd_send_byte(0,0xD0);


  19. void main(){
  20. unsigned int8 relativeHumidity;
  21. unsigned int8 tempC;
  22. printf("\n\rdsPIC30F2010 Prototype Board.");
  23. printf("\n\rFriday 26 September 2025");
  24. printf("\n\rDHT-11 Humidity Sensor Example\n\r");
  25. printf("\r\n\r\dht-11.c - DHT11 example starting\r\n\r\n");
  26. dht11_init();
  27. lcd_init();
  28. printf(LCD_PUTC,"dsPIC30F2010 LCD");
  29. line_2();
  30. printf(LCD_PUTC,"DHT-11 Sensor");
  31. line_3();
  32. printf(LCD_PUTC,"Example Using");
  33. line_4();
  34. printf(LCD_PUTC,"CCS PICC v5.119");
  35. delay_ms(5000);
  36. lcd_clear();
  37. while(1){
  38. dht11_read(&relativeHumidity, &tempC);
  39. printf("HUMIDITY=%03u%%, TEMPERATURE=%02uC\r\n", relativeHumidity, tempC);
  40. lcd_home();
  41. printf(LCD_PUTC," DHT-11 Sensor");
  42. line_2();
  43. printf(LCD_PUTC," Reading:");
  44. line_3();
  45. printf(LCD_PUTC,"Humidity: %03u%%",relativeHumidity);
  46. line_4();
  47. printf(LCD_PUTC,"Temperature:%02u%cC",tempC,0xDF);
  48. delay_ms(1000);
  49. }
  50. }

 The TC1604A-04 works fine even it's too old.

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing
Start-Up Screen


dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing
DHT-11 Sensor Reading and Displaying

Click here to download this example.
 


Friday, July 11, 2025

DIY PICMicro Low Pin Count DIP Prototype Board

Overview 

Using a prototype board for micro-controller firmware testing could save time and safer. Putting an on-board device programmer with prototype board could be more satisfy for electronic hobbyists.

I have some PIC18 and PIC16 series of micro-controllers left from previous projects. I don't know what to do with them anymore. So I put them on single board to PIC program testing next time I need them without checking their pin diagram, and wiring them on bread board. 

DIY PICMicro Low Pin Count DIP Prototype Board
PCB Front View

DIY PICMicro Low Pin Count DIP Prototype Board
PCB Back View
 

I designed a PCB with a 

  1. PICKit2 device programmer (with AVR ISP header)
  2. +5VDC and +3.3VDC low drop out power supply
  3.  RS-232 to TTL logic converter
  4. I2C DS1307 RTC and 24LC08 EEPROM 
  5. 4-bit LCD (HD4478)
  6. 3-digit 056'common cathode multiplexing display 
  7. One passive buzzer with transistor driver (using CCP1 PWM output pin of PIC16F876A)
  8. 8-LED that connects to PORTC of PIC16F876A
  9. A 4x4 keypad matrix that connects to PORTB of PIC16F876A
  10. Three analog inputs (one LM35 and two potentiometers) that connect to RA0...RA1 of PIC16F876A. 
  11. A 28-pin IC socket for 28-pin PIC devices
  12. A 20-pin IC socket for 20-pin PIC devices
  13. A 18-pin IC socket for 18-pin PIC devices
  14. A 14-pin IC socket for 14-pin PIC devices
  15. And a 8-pin IC socket for 8-pin PIC devices

This board seem to be a large PCB with two copper layer near a size of an A4 paper that I'm not yet fabricate it. It need a PCB fabrication service.

Schematic

I use Protues VSM Release 8.16 SP3 to design draw its circuit diagram. Some components are not in its original libraries. So I find and download some devices symbol, footprints and 3D objects from snapeda website. I separate its schematic into A4 sheets. 



DIY PICMicro Low Pin Count DIP Prototype Board
Sheet #1

DIY PICMicro Low Pin Count DIP Prototype Board
Sheet #2

DIY PICMicro Low Pin Count DIP Prototype Board
Sheet #3

DIY PICMicro Low Pin Count DIP Prototype Board
Sheet #4

DIY PICMicro Low Pin Count DIP Prototype Board
Sheet #5

This board could fit,

  1. 28-pin PIC microcontrollers: PIC16F876A, PIC16F886, etc.
  2. 20-pin PIC microcontrollers: PIC16F1459(USB), PIC16F690, etc.
  3. 18-pin PIC microcontrollers: PIC16F1827, PIC16F84A, PIC16F818, etc.
  4. 14-pin PIC microcontrollers: PIC16F630, PIC16F676, etc.
  5. 8-pin PIC microcontrollers: PIC12F629, PIC12F675, PIC12F683, etc.

These are some mid-range PIC micro-controllers I have at my own workshop.

Printed Circuit Board (PCB)

This board size is 8.02x6.30 inches that could be a little bit expensive to order from any professional PCB fabrication service. But if we need to use it with classmate or friend the share cost is cheaper.

DIY PICMicro Low Pin Count DIP Prototype Board
Top Copper non-mirror


DIY PICMicro Low Pin Count DIP Prototype Board
Bottom Copper


DIY PICMicro Low Pin Count DIP Prototype Board
Top Silk

I preview this PCB on an online Gerber viewer software.

DIY PICMicro Low Pin Count DIP Prototype Board
Gerber View Top Copper

DIY PICMicro Low Pin Count DIP Prototype Board
Gerber View Bottom Copper


 Click here to download its design file.


PCB Fabrication

I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
10 PCBs for only 5USD
 

For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.


A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay Instant Quote
 

We can accurately see the preview of fabricated PCB generated by the company's online Gerber file viewer.

PCBWay also offer PCBA assembly service at reasonable price.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay PCB Assembly Service


Thursday, December 28, 2023

PIC16F887 HC-SR04 Distance Sensor LCD Using XC8

Overview

HC-SR04 is an ultrasonic range finder. It uses sound wave traveling to find detected object distance. Its working range is between 4cm and 4m. To control and get distance data from this sensor we can use a digital circuit, or a microprocessor, or even manually by hand. Its echo output is a logic high level TTL signal with a specific period. A microprocessor's timer is used for calculating the timing signal associated with distance. For more details about using this sensor with a simple PIC16F84A micro-controller you can see this post.

PIC16F887 HC-SR04 Distance Sensor LCD Using XC8
PIC16F887 Prototyping Board

PIC16F887 HC-SR04 Distance Sensor LCD Using XC8
PIC16F887 Prototyping Board
 
PIC16F887 HC-SR04 Distance Sensor LCD Using XC8
HC-SR04 Module Front

PIC16F887 HC-SR04 Distance Sensor LCD Using XC8
HC-SR04 Module Back

MPLABX IDE and XC8 C Programming

In this XC8 programming example, I use a PIC16F887 8-bit micro-controller to read distance data, and displaying its timing value with calculated distance on a 16x4 character LCD. I use Timer0 timer/counter to measure the sensor's TTL high pulse period.

PIC16F887 HC-SR04 Distance Sensor LCD Using XC8
Schematic and Program Simulation

An 8MHz internal RC oscillator is used for the system. Timer0 prescaler is set to 1:2 to get a 1µs timing ticks. Since this timer module is an 8-bit timer/counter I use TIMER0 interrupt to expand its timing period. Hence it becomes a 16-bit timer with the aid of timing interrupt. 

An 8-bit micro-controller is easy to program using C in MPLABX IDE and XC8 embedded C compiler from Microchip Technology. An HD44780 character LCD is very simple to interface and program. This character LCD is common operate in 4-bit data transfer mode.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on December 28, 2023, 9:51 PM
  6.  */
  7.  
  8. #include <xc.h>
  9. #include "config.h"
  10. #include "lcd.h"
  11.  
  12. #include <stdio.h>
  13.  
  14. #define _XTAL_FREQ 8000000UL
  15.  
  16. #define TRIGGER RD6
  17. #define ECHO RD7
  18.  
  19. volatile uint8_t T0OV=0;
  20. volatile uint16_t myCounter=0;
  21. volatile uint16_t getDistance=0, temp=0;
  22.  
  23. void __interrupt() T0_ISR(){
  24. if(T0IF==1){
  25. T0OV+=1;
  26. myCounter++;
  27. T0IF=0;
  28. }
  29. }
  30.  
  31. void readDistance(void){
  32. char msg[8];
  33. TRIGGER=1;
  34. __delay_us(10);
  35. TRIGGER=0;
  36. __delay_us(30);
  37. while(ECHO==0){
  38. /*Waiting For High Pulse Response, Or Sensor Present*/
  39. temp++;
  40. __delay_us(10);
  41. if(temp>=20) break;
  42. }
  43. TMR0=0;
  44. T0OV=0;
  45. while(ECHO==1);
  46. temp=(T0OV<<8)+TMR0+14;
  47. getDistance=temp/58;
  48.  
  49. lcdXY(5,4);
  50. sprintf(msg,"%4d %cS ",temp,228);
  51. lcdString(msg);
  52. lcdXY(5,2);
  53. if(temp>=116&&getDistance<=400){
  54. sprintf(msg,"%3d",getDistance);
  55. lcdString(msg);
  56.  
  57. temp%=58;
  58. temp=(temp*100)/58;
  59. sprintf(msg,".%d%dcm",temp/10,temp%10);
  60. lcdString(msg);
  61. }else lcdString("Invalid");
  62. temp=0;
  63. T0OV=0;
  64. TMR0=0;
  65. }
  66.  
  67. void main(void) {
  68. /*8MHz internal oscillator*/
  69. OSCCONbits.IRCF=7;
  70. lcdInit();
  71. /*RD7 as input*/
  72. PORTD=0;
  73. TRISD=0x80;
  74. /*Select system clock*/
  75. T0CS=0;
  76. /*Timer0 Prescaler*/
  77. PSA=0;
  78. /*Select 1:2 Prescaler*/
  79. OPTION_REGbits.PS=0;
  80. /*Enable Timer0 Interrupt*/
  81. T0IE=1;
  82. GIE=1;
  83. T0IF=0;
  84.  
  85. lcdXY(5,1);
  86. lcdString("PIC16F887 ");
  87. lcdXY(1,2);
  88. lcdString("HC-SR04 Distance");
  89. lcdXY(2,3); lcdString("Sensor Example");
  90. lcdXY(2,4); lcdString("MPLABX IDE XC8");
  91. __delay_ms(2000);
  92. lcdCommand(0x0C);
  93. lcdCommand(0x01);
  94. __delay_ms(5);
  95. lcdXY(5,1);
  96. lcdString("Distance: ");
  97. lcdXY(1,3);
  98. lcdString("TIMER0 Duration:");
  99. TMR0=0;
  100. while(1){
  101. if(myCounter>=5000){
  102. readDistance();
  103. myCounter=0;
  104. }
  105. }
  106. return;
  107. }
  108.  
  109.  
  110.  

The Interrupt Service Routine (ISR) for PIC micro-controller in XC8 programming is differ depends on the version of the compiler. Some time the source codes can generate error in other version. In some case, it can be build but the ISR does not work during run-time. So we will need to read its compiler user manual. 

PIC16F887 HC-SR04 Distance Sensor LCD Using XC8
MPLABX IDE Dashboard

Click here to download its source file.




Search This Blog

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (34) Analog Comparator (1) Arduino (16) ARM (13) AT89C52 (7) ATMega32 (58) ATMega644P (27) AVR (87) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (4) Display (133) Distance Sensor (3) ds1307 (10) DS18B20 (5) ds3231 (1) ds3232 (2) dsPIC (5) dsPIC30F1010 (3) dsPIC30F2010 (5) EEPROM (5) Environment Sensor (5) ESP32 (1) esp8266 (1) Graphical LCD (2) I2C (38) ILI9341 (1) Input/Output (75) Interrupt (22) Keil (5) Keypad (16) KS0108 (2) LCD (75) LM35 (3) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (10) PCD8544 (3) PCF8574 (10) PIC (108) PIC12F (3) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (13) SH1106 (3) Shift Register (13) Shift Registers (10) Software TWI (2) SPI (39) ST7735 (1) STM32 (11) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (5) STM32CubeIDE (13) STM32F103C8T6 (9) STM32F401CCU6 (1) SysTick (4) temperature sensor (13) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (8) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)