{"id":1373,"date":"2022-12-04T05:23:59","date_gmt":"2022-12-04T05:23:59","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1373"},"modified":"2022-12-05T04:17:15","modified_gmt":"2022-12-05T04:17:15","slug":"working-with-stm32-and-pressure-sensors-hk1100c","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1373","title":{"rendered":"Working with STM32 and pressure sensors: HK1100C"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1024x812.jpg\" alt=\"\" class=\"wp-image-1374\" width=\"512\" height=\"406\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1024x812.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-300x238.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-768x609.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1536x1219.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-2048x1625.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1150x912.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-750x595.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-400x317.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-250x198.jpg 250w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall interface HK1100C pressure sensor with STM32 and able to measure the pressure from 0 up to 1.2MPa.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>HK1100C pressure sensor.<\/li><li>Connection with STM32F4.<\/li><li>Getting the relative equation.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. HK1100C Pressure Sensor:<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1024x812.jpg\" alt=\"\" class=\"wp-image-1374\" width=\"512\" height=\"406\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1024x812.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-300x238.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-768x609.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1536x1219.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-2048x1625.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-1150x912.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-750x595.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-400x317.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/water-pressure-sensor-g14-2440x1936-1-250x198.jpg 250w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure><\/div>\n\n\n\n<p>Low cost pressure sensor, stainless steel package, socket type connection, convenient replacement and installation, 1\/2 inch pressure connector, quick connect pressure interface,<br>prominent in the engineering<\/p>\n\n\n\n<p>application, the test atmospheric pressure, water pressure and oil pressure ,customers have good feedback. Pressure range includes 0-1.2MPa, can meet most applications.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Sensor type:<\/strong>&nbsp;HK1100C<\/li><li><strong>Size:<\/strong>&nbsp;\u00bc Inch<\/li><li><strong>Working Voltage:<\/strong>&nbsp;5.0&nbsp;VDC<\/li><li><strong>Output Voltage:&nbsp;<\/strong>0.5-4.5 VDC<\/li><li><strong>Working Current:<\/strong>&nbsp;&lt;=10mA<\/li><li><strong>Working Pressure Range:<\/strong>&nbsp;0-1.2MPa<\/li><li><strong>The Biggest Pressure:<\/strong>&nbsp;2.4MPa<\/li><li><strong>Destroy Pressure:<\/strong>&nbsp;3.0MPa<\/li><li><strong>Working TEMP. Range:<\/strong>&nbsp;0-85 Celsius Degree<\/li><li><strong>Storage Temperature Range:<\/strong>&nbsp;0-100&nbsp;Celsius Degree<\/li><li><strong>Measuring Error:<\/strong>&nbsp;1.5%FSO<\/li><li><strong>Temperature Range Error:<\/strong>&nbsp;+3.5%FSO<\/li><li><strong>Response Time:<\/strong>&nbsp;&nbsp;&lt;=2.0ms<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Connecting the Sensor with STM32F4:<\/h2>\n\n\n\n<p>Since the sensor has three wires, which they are Red, Black and Yellow. The connection as following:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>STM32F411 Nucleo-64<\/td><td>HK1100C Sensor<\/td><\/tr><tr><td>5V<\/td><td>Red wire<\/td><\/tr><tr><td>GND<\/td><td>Black wire<\/td><\/tr><tr><td>PA1<\/td><td>Yellow wire.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Since the output of the sensor between 0 and 5V and STM32F4 can&#8217;t measure beyond the 3.3V on any adc pin, we need to convert the 5 down to 3.3V using simple three resistors as following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-586x1024.png\" alt=\"\" class=\"wp-image-1385\" width=\"NaN\" height=\"NaN\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-586x1024.png 586w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-172x300.png 172w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-768x1341.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-880x1536.png 880w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-750x1310.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-400x698.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM-250x437.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-05-at-7.14.51-AM.png 1016w\" sizes=\"(max-width: 586px) 100vw, 586px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Getting the relative equation:<\/h2>\n\n\n\n<p>The mentioned equation in the datasheet as following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"100\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.25.05-AM.png\" alt=\"\" class=\"wp-image-1382\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.25.05-AM.png 510w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.25.05-AM-300x59.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.25.05-AM-400x78.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.25.05-AM-250x49.png 250w\" sizes=\"(max-width: 510px) 100vw, 510px\" \/><\/figure><\/div>\n\n\n\n<p>By rearranging the equation to get the P which is the pressure value, we can get the following equation:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"374\" height=\"148\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.14.50-AM.png\" alt=\"\" class=\"wp-image-1376\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.14.50-AM.png 374w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.14.50-AM-300x119.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.14.50-AM-250x99.png 250w\" sizes=\"(max-width: 374px) 100vw, 374px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Since STM32 is working with 3.3V, we shall insert 3.3 to the Vcc and the final equation as follows:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"374\" height=\"148\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.18.17-AM.png\" alt=\"\" class=\"wp-image-1377\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.18.17-AM.png 374w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.18.17-AM-300x119.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.18.17-AM-250x99.png 250w\" sizes=\"(max-width: 374px) 100vw, 374px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>We shall use continuous conversion mode for the ADC. For how to configure the ADC, check this guide <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=224\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=224\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The code as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;delay.h&quot;\n#include &quot;stdio.h&quot;\n\nextern void uart2_rxtx_init(void);\n\nfloat get_voltage()\n{\n\twhile(!((ADC1-&gt;SR)&amp;ADC_SR_EOC)){;}\n\tuint16_t adc_value= ADC1-&gt;DR;\n\n\treturn (float)((adc_value)*3300\/4095)\/1000;\n\n}\n\n\nfloat pressure (float voltage)\n{\nreturn (float)(voltage - 0.33)\/(2.475);\n\n}\n\n\n\nint main(void)\n{\n\tuart2_rxtx_init();\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN; \/\/enable gpio a clock\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN; \/\/enable adc clock\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODER1;  \/\/set the PA1 to analog mode\n\tADC1-&gt;CR2=0; \/\/disable the adc\n\tADC1-&gt;CR2|=ADC_CR2_CONT;\n\tADC1-&gt;SQR3|=1; \/\/we are converting only one channel\n\tADC1-&gt;CR2|=1; \/\/enable the adc\n\tADC1-&gt;CR2|=ADC_CR2_SWSTART; \/\/start adc conversion\n\n\twhile(1)\n\t\t{\n\t\t\tprintf(&quot;pressure is = %f MPa\\r\\n&quot;,pressure(get_voltage()));\n\t\t\tdelay(500);\n\t\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/HK1100C.zip\">HK1100C<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/HK1100C.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>After compiling and program your STM32F411 Nucleo64, open serial terminal program and set the baudrate to be 115200 and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"736\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-1024x736.png\" alt=\"\" class=\"wp-image-1378\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-1024x736.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-300x216.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-768x552.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-1150x827.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-750x539.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-400x288.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM-250x180.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-04-at-8.21.36-AM.png 1168w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Note: Sensor is not calibrated, hence the results won&#8217;t be accurate.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall interface HK1100C pressure sensor with STM32 and able to measure the pressure from 0 up to 1.2MPa. In this guide, we shall cover the following: HK1100C pressure sensor. Connection with STM32F4. Getting the relative equation. Code. Results. 1. HK1100C Pressure Sensor: Low cost pressure sensor, stainless steel package, socket type [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-1373","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1373"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1373"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1373\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1373\/revisions\/1386"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}