{"id":3557,"date":"2011-09-09T19:57:19","date_gmt":"2011-09-09T23:57:19","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=3557"},"modified":"2012-07-30T09:29:15","modified_gmt":"2012-07-30T13:29:15","slug":"wireless-data-transmission-between-two-pic-microcontrollers-using-low-cost-rf-modules","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/wireless-data-transmission-between-two-pic-microcontrollers-using-low-cost-rf-modules\/","title":{"rendered":"Wireless data transmission between two PIC microcontrollers using low-cost RF modules"},"content":{"rendered":"<p><!-- @font-face {   font-family: \"Cambria\"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0in 0in 10pt; line-height: 115%; font-size: 11pt; font-family: \"Times New Roman\"; }div.Section1 { page: Section1; } --> A lot of times we need to keep track of data from a device or a sensor located in a remote location from the point where it is processed. In other situations we desire wireless solutions for ease.\u00a0 Using long cables, infrared (IR) or other means are often tedious and not loss-less. Imagine collecting pH level data from a chemically lethal or toxic treatment plant where human presence is highly health hazardous. Running long cables from the pH sensor to the control or monitor station will surely introduce noisy signals and signal-to-noise ratio will thus drastically decrease. The result is erroneous data acquisition and thereby false decisions may be generated. If infrared signals or other optical means including lasers are used, they will need good obstacle-free line of sight or expensive and delicate optical fibers. Thus the solution stays in the radio frequency (RF) domain. This article talks about interfacing low cost RF modules (<a href=\"http:\/\/embedded-lab.com\/uploads\/datasheets\/Tx_KST-TX01.pdf\">KST-TX01<\/a> and <a href=\"http:\/\/embedded-lab.com\/uploads\/datasheets\/Rx_KST-RX806.pdf\">KST-RX806<\/a>) for transmitting data between two remotely located PIC microcontrollers.<\/p>\n<div id=\"attachment_3558\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_DataTitle.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3558\" class=\"size-full wp-image-3558\" title=\"RF_DataTitle\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_DataTitle.jpg\" alt=\"\" width=\"560\" height=\"356\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_DataTitle.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_DataTitle-550x349.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3558\" class=\"wp-caption-text\">Data broadcast using RF modules<\/p><\/div>\n<p><!--more--><!-- @font-face {   font-family: \"Calibri\"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0in 0in 10pt; line-height: 115%; font-size: 11pt; font-family: \"Times New Roman\"; }div.Section1 { page: Section1; } --> In this demo, we will see how to achieve an easy data transaction between two PIC16F877A microcontrollers using inexpensive RF modules. The theory is pretty simple and straight. It uses\u00a0<a href=\"http:\/\/embedded-lab.com\/uploads\/datasheets\/Tx_KST-TX01.pdf\">KST-TX01<\/a> and\u00a0<a href=\"http:\/\/embedded-lab.com\/uploads\/datasheets\/Rx_KST-RX806.pdf\">KST-RX806<\/a> RF modules which is a 433 MHz serial data transmitter\/receiver pair. One PIC16F877A is programmed to transmit its ADC data (RA0\/AN0 channel) serially using its built-in USART hardware at 1200 baud with no parity and 8-bit data stream. The PIC\u2019s USART transmitter (TX) pin feeds the data into the data pin of the KST-TX01 which transmits it using 433 MHz ASK RF signal. On the receiving end the KST-RX806 module receives the data and its output is connected to the another PIC&#8217;s USART input pin. The second PIC is programmed to read its USART receiver (RX) pin. On both ends, two LCD displays are also connected which show the transmitted and received bytes. Since RS232 communications typically allow 8-bit data, the 8-bit A\/D conversion is used here for simplicity, instead of the more common 10-bit ADC.<\/p>\n<div id=\"attachment_3583\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Tx_Unit.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3583\" class=\"size-medium wp-image-3583\" title=\"Tx_Unit\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Tx_Unit-550x412.jpg\" alt=\"\" width=\"550\" height=\"412\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Tx_Unit-550x412.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Tx_Unit.jpg 800w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-3583\" class=\"wp-caption-text\">Transmitting unit<\/p><\/div>\n<div id=\"attachment_3575\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Rx_Unit.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3575\" class=\"size-medium wp-image-3575\" title=\"Rx_Unit\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Rx_Unit-550x423.png\" alt=\"\" width=\"550\" height=\"423\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Rx_Unit-550x423.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/Rx_Unit.png 650w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-3575\" class=\"wp-caption-text\">Receiving unit<\/p><\/div>\n<div id=\"attachment_3560\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_Setup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3560\" class=\"size-full wp-image-3560\" title=\"RF_Setup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_Setup.jpg\" alt=\"\" width=\"560\" height=\"358\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_Setup.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/09\/RF_Setup-550x351.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3560\" class=\"wp-caption-text\">Wireless data transmission between two PIC micros<\/p><\/div>\n<p><strong>Software<\/strong><\/p>\n<p>The demo codes given for the receiver and the transmitter PICs are written using CCS PCWHD compiler. The firmwares are easy to understand and needs no explanation. In CCS PCWHD, the LCD pins are defined in the <strong>lcd.c <\/strong>file. By default, it uses the PORTD pins (as shown in the circuit diagram above) as connections to the LCD. If you are using a different port, you need to edit the <strong>lcd.c<\/strong> file accordingly.<\/p>\n<p><span style=\"color: #003366;\"><strong>Receiving side code<\/strong><\/span><\/p>\n<pre>#include &lt;16F877A.h&gt;\r\n#device *= 16\r\n#fuses HS, NOWDT, NOPROTECT, NOLVP, PUT\r\n#fuses NOBROWNOUT, CPD, NODEBUG, NOWRT\r\n#use delay(clock=10MHz)\r\n#use rs232(baud=1200, rcv=PIN_C7, bits=8, parity=N) \r\n\r\n#include &lt;lcd.c&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\r\nvoid main()\r\n{\r\n byte c;\r\n lcd_init();\r\n lcd_putc(\"\\f\");\r\n while(true)\r\n {\r\n c=getc();\r\n printf(lcd_putc,\"\\fRx Data = %u\",c);\r\n delay_ms(100);\r\n }\r\n}<\/pre>\n<p><span style=\"color: #003366;\"><strong>Transmitting side code<br \/>\n<\/strong><\/span><\/p>\n<pre>#include &lt;16F877A.h&gt;\r\n#device *= 16\r\n#device adc=8\r\n#fuses HS, NOWDT, NOPROTECT, NOLVP, PUT\r\n#fuses NOBROWNOUT, CPD, NODEBUG, NOWRT\r\n#use delay(clock=10MHz)\r\n#use rs232(baud=1200, xmit=PIN_C6, bits=8, parity=N)\u00a0\u00a0\u00a0 \u00a0\r\n\r\n#include &lt;lcd.c&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\r\nvoid main()\r\n{\r\n byte s=0;\r\n lcd_init();\r\n lcd_putc(\"\\f\");\r\n setup_adc_ports(adc_clock_internal);\r\n setup_adc(AN0);\r\n set_adc_channel(0);\r\n while(true)\r\n {\r\n read_adc(adc_start_only);\r\n while(!adc_done());\r\n s = read_adc(adc_read_only);\r\n lcd_gotoxy(1,1);\r\n printf(lcd_putc, \"\\fTx Data = %u\" s);\r\n putc(s);\r\n delay_ms(100);\r\n }\r\n}<\/pre>\n<p><strong>Summary<\/strong><\/p>\n<p>Thus we saw that <a href=\"..\/..\/uploads\/datasheets\/Tx_KST-TX01.pdf\">KST-TX01<\/a> and\u00a0<a href=\"..\/..\/uploads\/datasheets\/Rx_KST-RX806.pdf\">KST-RX806<\/a> RF modules are very easy to use for RF transmission and reception of data as they can be directly interfaced to the UART port of microcontrollers. However, there are few things that I noted while experimenting with them. One is, when the ADC value (8-bit in this demo) is around 250 or higher the receiver starts getting garbage data. It seems like when the data contains too many 1s, it is not recognized by the receiver which literally \u201cbelieves\u201d that the data is static. One solution could be dividing the ADC byte into two nibbles and send them separately. It will slow down the net data transmission rate but the received data will be less likely to be erroneous. It also adds an extra overhead of reconstructing the bytes from the nibbles. Similarly, if you want to use 10-bit (or more) resolution for A\/D conversion, the ADC result can also be broken into two 5-bit fragments (MSB 5-bit and LSB 5-bit) before transmission. Adding three extra 0s on the MSB end of each fragment completes the transmission byte. By doing so, the numerical value of each data byte will be less than 32.<\/p>\n<p>Another important fact to note is the range of the modules. Many manufactures claim ranges of up to 10m or so. In reality this may not be what they claim because of the antenna length and the quality of the modules themselves. A wire of 12 inches or so is adequate for a reasonably good wireless communication.<\/p>\n<p>Lastly, I found the two RF modules perform best at 1200 baud. While data transmission at 2400 baud is also possible, it will not be error free. Baud rates more than that are not useful at all. Several improvements can be done to add more features but I have kept it for those who are willing to explore it more and use it in various purposes.<\/p>\n<p>The lesson from this demo can be expanded to a wide range of applications. Here are some common applications:<\/p>\n<ul>\n<li>Remote appliance controllers,<\/li>\n<li>Wireless headsets,<\/li>\n<li>Remote data acquisition systems,<\/li>\n<li>Surveillance and security systems,<\/li>\n<li>Robotics,<\/li>\n<li>Hands free devices,<\/li>\n<li>Digital radios,<\/li>\n<li>X-bee and Bluetooth,<\/li>\n<li>Navigation systems,<\/li>\n<li>Toys and so on.<\/li>\n<\/ul>\n<table style=\"width: 560px;\" border=\"0\" cellspacing=\"5\" cellpadding=\"2\" align=\"center\">\n<tbody>\n<tr>\n<td style=\"border-color: #cccccc; background-color: #fdfdfd;\"><span style=\"color: #404040;\"><em>Many thanks to Shawon Shahryiar for contributing this article to Embedded Lab. He works as an Engineer in the Research and Development Cell at ELECTRO Group in Dhaka, Bangladesh. You can contact him at:<\/em><\/span><em><br \/>\n<\/em><em><strong> <\/strong> <\/em><img decoding=\"async\" src=\"http:\/\/embedded-lab.com\/uploads\/images\/ShawonEmail.png\" alt=\"\" \/><\/td>\n<td style=\"border-color: #cccccc; background-color: #fdfdfd;\"><img decoding=\"async\" src=\"http:\/\/embedded-lab.com\/uploads\/images\/Shawon.jpg\" alt=\"\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>A lot of times we need to keep track of data from a device or a sensor located in a remote location from the point where it is processed. In other situations we desire wireless solutions for ease.\u00a0 Using long cables, infrared (IR) or other means are often tedious and not loss-less. Imagine collecting pH level data from a chemically lethal or toxic treatment plant where human presence is highly health hazardous. Running long cables from the pH sensor to the control or monitor station will surely introduce noisy signals and signal-to-noise ratio will thus drastically decrease. The result is<\/p>\n","protected":false},"author":1,"featured_media":3558,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,183],"tags":[342,341,337,335,336],"class_list":["post-3557","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pic-tutorials","category-tips-and-tricks","tag-kst-rx806","tag-kst-tx01","tag-pic16f877a","tag-rf-transceiver","tag-wireless-data"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=3557"}],"version-history":[{"count":28,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3557\/revisions"}],"predecessor-version":[{"id":5727,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3557\/revisions\/5727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/3558"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=3557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=3557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=3557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}