{"id":277,"date":"2013-03-14T14:25:40","date_gmt":"2013-03-14T21:25:40","guid":{"rendered":"https:\/\/www.uniteng.com\/?p=277"},"modified":"2021-01-05T14:45:46","modified_gmt":"2021-01-05T06:45:46","slug":"raspberry-pi-with-relay-io-board","status":"publish","type":"post","link":"https:\/\/www.uniteng.com\/index.php\/2013\/03\/14\/raspberry-pi-with-relay-io-board\/","title":{"rendered":"Raspberry Pi with Relay I\/O Board"},"content":{"rendered":"<p>\n\tRelay I\/O Board is widely used in applications of smart home project. In this article, I will detail how to control the Relay I\/O board using Raspberry Pi with <a href=\"http:\/\/projects.archive.uniteng.com\/digital-system-projects\/raspberrypiuniversalexpansionboard\">Raspberry Pi Universal Expansion Board<\/a> via internet. The codes and schematic diagrams used in this article was listed at the end of this article.\n<\/p>\n<p>\n\t<strong>Part I: Hardware<\/strong>\n<\/p>\n<p style=\"margin-left: 40px;\">\n\t<strong>1. Pin Mapping<\/strong>\n<\/p>\n<p style=\"margin-left: 40px;\">\n\tThe CPLD on the Raspberry Pi Universal Expansion Board can be used to map pins between modules. For this project, we want to map pins as following diagram Fig.1. <a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/ConnectionMap_Temp.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"ConnectionMap_Temp\" class=\"alignnone size-full wp-image-325\" height=\"351\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/ConnectionMap_Temp.png\" width=\"944\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/ConnectionMap_Temp.png 944w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/ConnectionMap_Temp-300x111.png 300w\" sizes=\"auto, (max-width: 944px) 100vw, 944px\" \/><\/a>\n<\/p>\n<div align=\"center\" style=\"margin-left: 40px;\">\n\tFig. 1 Pin Mapping\n<\/div>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tThis part is pretty easy since we only need to map the UART1 of the STM32 to the Raspberry Pi and PC10, PC11 of the STM32 to the Relay I\/O board using the CPLD. The following Verilog code can implement the idea:\n<\/p>\n<pre class=\"brush:cpp;\" style=\"margin-left: 40px;\">\r\nmodule RS232(RXD_in,TXD_in,RXD_out,TXD_out,\r\nK1_in,K1_out,K2_in,K2_out);\r\n\r\ninput   RXD_in, TXD_in,K1_in,K2_in;\r\noutput  RXD_out,TXD_out,K1_out,K2_out;\r\n\r\nassign RXD_out = RXD_in;\r\nassign TXD_out = TXD_in;\r\n\r\n\/\/assign pins for relays K1 and K2\r\nassign K1_out = K1_in;\r\nassign K2_out = K2_in;\r\n\r\nendmodule\r\n<\/pre>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tNavigate to Quartus -&gt; Assignments -&gt; Pin Planner. The IO need to be configured as following picture.\n<\/p>\n<p style=\"margin-left: 40px;\">\n\t<a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Pins.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"UEB_Relay_Pins\" class=\"alignnone size-full wp-image-295\" height=\"150\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Pins.png\" width=\"851\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Pins.png 851w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Pins-300x52.png 300w\" sizes=\"auto, (max-width: 851px) 100vw, 851px\" \/><\/a>\n<\/p>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tWe can flash the design into the CPLD right now.\n<\/p>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\t<strong>2. STM32 Configuration<\/strong>\n<\/p>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tCreating a new .c source code file RelayIOBoard.c as the driver of the Relay I\/O board:\n<\/p>\n<pre class=\"brush:cpp;\" style=\"margin-left: 40px;\">\r\n\/*Driver for Relay IO board*\/\r\n#include &lt; stm32f10x.h&gt;\r\n#include &lt; stdio.h&gt;\r\n\r\n#define on  1\r\n#define off 0\r\n\r\nint k1_state;\r\nint k2_state;\r\n\r\nvoid Relay(int relay_num, int on_off){\r\n  switch( relay_num ) \r\n  {\r\n    case 1:\r\n\t\t\t\/\/For K1 which is controlled by the PC10\r\n        if(on_off == on)\r\n\t\t\t\t{\r\n         GPIO_SetBits(GPIOC, GPIO_Pin_10);\r\n\t\t\t\t k1_state = on;\r\n        }\r\n        if(on_off == off)\r\n\t\t\t\t{\r\n         GPIO_ResetBits(GPIOC, GPIO_Pin_10);\r\n\t\t\t\t k1_state = off;\r\n        }\t\t\t\t\r\n        break;\r\n    case 2 :\r\n\t\t\t\/\/For K2 which is controlled by the PC11\r\n        if(on_off == on)\r\n\t\t\t\t{\r\n         GPIO_SetBits(GPIOC, GPIO_Pin_11);\r\n\t\t\t\t k2_state = on;\r\n        }\r\n        if(on_off == off)\r\n\t\t\t\t{\r\n         GPIO_ResetBits(GPIOC, GPIO_Pin_11);\r\n\t\t\t\t k2_state = off;\r\n        }\t\t\r\n        break;\r\n  }\r\n}\r\n\r\nvoid Get_Relay_States(void){\r\n   if(k1_state == on){\r\n      printf(&quot;k1on\\r\\n&quot;);\r\n   }\r\n\t else{\r\n      printf(&quot;k1off\\r\\n&quot;);\r\n   }\r\n   if(k2_state == on){\r\n      printf(&quot;k2on\\r\\n&quot;);\r\n   }\r\n\t else{\r\n      printf(&quot;k2off\\r\\n&quot;);\r\n   }\r\n}\r\n\r\nvoid Relay_init(void )\r\n{\r\n  Relay(1,off);\r\n\tRelay(2,off);\r\n\tk1_state =off;\r\n\tk2_state =off;\r\n}\r\n\r\n<\/pre>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tWe need to add following commands to the Shell() in CommandShell.c:\n<\/p>\n<pre class=\"brush:cpp;\" style=\"margin-left: 40px;\">\r\n\t \/\/Command K1 on\r\n\t if(IsStringMatch(Command,&quot;k1on&quot;,Counter)){\r\n\t\t  Relay(1, 1);\r\n\t\t printf(&quot;k1on\\r\\n&quot;);\r\n\t   }\r\n\t \/\/Command K1 off\r\n\t if(IsStringMatch(Command,&quot;k1off&quot;,Counter)){\r\n\t\t  Relay(1, 0);\r\n\t\t printf(&quot;k1off\\r\\n&quot;);\r\n\t   }\r\n\t \/\/Command K2 on\r\n\t if(IsStringMatch(Command,&quot;k2on&quot;,Counter)){\r\n\t\t  Relay(2, 1);\r\n\t\t printf(&quot;k2on\\r\\n&quot;);\r\n\t   }\r\n\t \/\/Command K2 off\r\n\t if(IsStringMatch(Command,&quot;k2off&quot;,Counter)){\r\n\t\t  Relay(2, 0);\r\n\t\t printf(&quot;k2off\\r\\n&quot;);\r\n\t   }\r\n\t \/\/Command krs, get relay states\r\n\t if(IsStringMatch(Command,&quot;krs&quot;,Counter)){\r\n\t\t  Get_Relay_States();\r\n\t   }\r\n<\/pre>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tFor convenience, the Firmware <a href=\"https:\/\/sites.google.com\/site\/eecsprojects\/home\/digital-system-projects\/raspberrypiuniversalexpansionboard\/STM32_Relay_1.0.hex?attredirects=0&amp;d=1\">STM32_Relay_1.0.hex<\/a> needs to be flashed into the MCU. The Firmware will host a Command Shell on UART1. It also includes the driver of the Relay I\/O board.\n<\/p>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\t<strong>3. Testing<\/strong>\n<\/p>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tConnecting Relay I\/O Board to the <a href=\"https:\/\/sites.google.com\/site\/eecsprojects\/home\/digital-system-projects\/raspberrypiuniversalexpansionboard\/Digilent-Pmod_%20Interface_Specification.pdf?attredirects=0&amp;d=1\">Pmod1 socket<\/a> on the Raspberry Pi Universal Expansion Board. A cooling fan may be controlled by the relay as following figure. <a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Testing.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"UEB_Relay_Testing\" class=\"alignnone size-full wp-image-313\" height=\"703\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Testing.png\" width=\"1175\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Testing.png 1175w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Testing-300x179.png 300w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/UEB_Relay_Testing-1024x612.png 1024w\" sizes=\"auto, (max-width: 1175px) 100vw, 1175px\" \/><\/a>\n<\/p>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tEntering following command in the Linux shell of the Raspberry Pi:\n<\/p>\n<pre class=\"brush:cpp;\" style=\"margin-left: 40px;\">\r\nminicom -b 9600 -o -D \/dev\/ttyAMA0\r\n<\/pre>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\tTry k1on, k1off, k2on and k2off commands to control the relays.\n<\/p>\n<p>\n\t<strong>Part II: Software<\/strong>\n<\/p>\n<ul>\n<li style=\"margin-left: 40px;\">\n\t\t<strong>Prerequisites:<\/strong> <a href=\"https:\/\/www.uniteng.com\/index.php\/2013\/03\/11\/accessing-serial-port-via-cgi-script\/\">Accessing serial port via CGI script<\/a>\n\t<\/li>\n<\/ul>\n<p style=\"margin-left: 40px; text-align: justify;\">\n\t<strong>1. Web API Design<\/strong> In above section, a batch of commands was implemented which could control the relay I\/O board via UART port. Typically, the UART port is only reachable locally, but our plan is controlling the relays through internet, thus we need to design a Web API using CGI and python, this Web API will act as a bridge between internet and local UART port.\n<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>\n\t\t\t\tAPI\n\t\t\t<\/th>\n<th>\n\t\t\t\tParameters\n\t\t\t<\/th>\n<th>\n\t\t\t\tReturns\n\t\t\t<\/th>\n<th>\n\t\t\t\tDescription\n\t\t\t<\/th>\n<\/tr>\n<tr>\n<th>\n\t\t\t\tcgi\/relaycfg.py\n\t\t\t<\/th>\n<th>\n\t\t\t\tk state\n\t\t\t<\/th>\n<th>\n\t\t\t\tkXoff kXon Note: X is the number of the corresponding relay.\n\t\t\t<\/th>\n<th>\n\t\t\t\tThis API accept two parameters k and state. k is the number of the relay, it can be assigned to k1 or k2. state can be assigned to on, off or query. When state is assigned to on or off, the corresponding relay will be turned on or turned off. When state is assigned to query, the current state of the corresponding relay will be returned.\n\t\t\t<\/th>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin-left: 40px;\">\n\te.g.: cgi\/relaycfg.py?k=k2&amp;state=off <a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_off.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"state_off\" class=\"alignnone size-full wp-image-346\" height=\"109\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_off.png\" width=\"633\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_off.png 633w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_off-300x51.png 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/a> cgi\/relaycfg.py?k=k2&amp;state=on <a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_on.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"state_on\" class=\"alignnone size-full wp-image-347\" height=\"115\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_on.png\" width=\"630\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_on.png 630w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_on-300x54.png 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a> cgi\/relaycfg.py?k=k2&amp;state=query <a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query1.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"state_query1\" class=\"alignnone size-full wp-image-348\" height=\"109\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query1.png\" width=\"631\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query1.png 631w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query1-300x51.png 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/a> Or <a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query2.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"state_query2\" class=\"alignnone size-full wp-image-349\" height=\"108\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query2.png\" width=\"631\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query2.png 631w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/state_query2-300x51.png 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/a> The source code of this API was provided in Part III.\n<\/p>\n<p style=\"margin-left: 40px;\">\n\t<strong>2. UI Design<\/strong>\n<\/p>\n<p style=\"margin-left: 40px;\">\n\tThe UI was implemented using jQuery mobile and AJAX technology.\n<\/p>\n<p style=\"margin-left: 40px;\">\n\tScreenshots\n<\/p>\n<p style=\"margin-left: 40px;\">\n\t<a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/Relay_UI.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"Relay_UI\" class=\"alignnone size-full wp-image-350\" height=\"557\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/Relay_UI.png\" width=\"647\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/Relay_UI.png 647w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/Relay_UI-300x258.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/a>\n<\/p>\n<p style=\"margin-left: 40px;\">\n\tImportant features:\n<\/p>\n<ul style=\"margin-left: 40px;\">\n<li>\n\t\tcgi\/relaycfg.py?k=k2&amp;state=query will be executed just after the DOM is fully loaded. This operation will return the current status of relay k2 (Cooling Fan). The buttons will be enabled or disabled and the State will also be updated according to the current status of k2.\n\t<\/li>\n<li>\n\t\tThe buttons will be enabled or disabled and the State will also be updated during the operation. The source code of this UI design was provided in Part III.\n\t<\/li>\n<\/ul>\n<p>\n\t<strong>Part III: Resource<\/strong>\n<\/p>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/sites.google.com\/site\/eecsprojects\/home\/digital-system-projects\/raspberrypiuniversalexpansionboard\/Relay.pdf?attredirects=0&amp;d=1\">Schematic diagram of the Relay I\/O Board<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/sites.google.com\/site\/eecsprojects\/home\/digital-system-projects\/raspberrypiuniversalexpansionboard\/Raspberry%20Pi%20Universal%20Expansion%20Board%20-%20Special%20Edition.pdf?attredirects=0&amp;d=1\">Schematic diagram of the Raspberry Pi Universal Expansion Board<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/sites.google.com\/site\/eecsprojects\/home\/digital-system-projects\/raspberrypiuniversalexpansionboard\/relay_sourcecode.zip?attredirects=0&amp;d=1\">Souce Code for Part II<\/a>\n\t<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Relay I\/O Board is widely used in applications of smart home project. In this article, I will detail how to control the Relay I\/O board using Raspberry Pi with Raspberry Pi Universal Expansion Board via internet. The codes and schematic diagrams used in this article was listed at the end of this article. Part I: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ngg_post_thumbnail":0,"footnotes":""},"categories":[5,11],"tags":[6,9,8,12,7],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-hardware-notes","category-software-notes","tag-raspberrypi","tag-rs232","tag-serial-port","tag-smart-home-portal","tag-universal-expansion-board"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"neilhao","author_link":"https:\/\/www.uniteng.com\/index.php\/author\/neilhao\/"},"uagb_comment_info":0,"uagb_excerpt":"Relay I\/O Board is widely used in applications of smart home project. In this article, I will detail how to control the Relay I\/O board using Raspberry Pi with Raspberry Pi Universal Expansion Board via internet. The codes and schematic diagrams used in this article was listed at the end of this article. Part I:&hellip;","_links":{"self":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":0,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}