{"id":2311,"date":"2024-01-27T13:48:09","date_gmt":"2024-01-27T13:48:09","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2311"},"modified":"2024-01-27T13:48:13","modified_gmt":"2024-01-27T13:48:13","slug":"getting-started-with-stm32g0-and-stm32cubeide-gpio-input","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2311","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE : GPIO Input"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-1.webp\" alt=\"\" class=\"wp-image-2312\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-1.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-1-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" target=\"_blank\">here<\/a>), we took a look at the STM32G070 features and we were able to blink and LED using HAL. In this guide, we shall toggle the LED using pushbutton connected to out STM32G070.<\/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>Input modes.<\/li><li>Configure the GPIO as input with internal pull up resistor.<\/li><li>Hardware Setup.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Input Modes:<\/h2>\n\n\n\n<p>GPIO input modes include<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>high impedance<\/li><li>pull-up<\/li><li>pull-down<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"floating-high-impedance-tri-stated\">Floating, High Impedance, Tri-Stated<\/h4>\n\n\n\n<p>Floating, high impedance, and tri-stated are three terms that mean the same thing: the pin is just flopping in the breeze. Its state is indeterminate unless it is driven high or low externally. You only want to configure a pin as floating if you know it will be driven externally. Otherwise, configure the input using pulling resistors.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"pull-updown\">Pull Up\/Down<\/h4>\n\n\n\n<p>If an input is configured with an internal pull-up, it will be high unless it is externally driven low. Pull-down inputs do the opposite (&nbsp;they\u2019re low unless driven high).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1024x768.png\" alt=\"\" class=\"wp-image-866\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1024x768.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-300x225.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-768x576.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1536x1152.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1150x862.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-750x562.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-400x300.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-250x187.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM.png 1566w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure the GPIO as Input with Internal Pull up resistor:<\/h2>\n\n\n\n<p>We start with the same process as in the previous guide we create a new project with name of GPIO_Input.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enable PA0 as Output.<\/li><\/ul>\n\n\n\n<p>Now Enable PA0 as input:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-1024x572.jpg\" alt=\"\" class=\"wp-image-2313\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-1024x572.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-768x429.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-1536x857.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-2048x1143.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-1150x642.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-750x419.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-400x223.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/2024-01-27_16-13-55-250x141.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After setting PA1 as input, from System Core, open GPIO, select PA1 and set the Pull-up mode as shown in the figure above.<\/p>\n\n\n\n<p>Now save the project to generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Hardware Setup:<\/h2>\n\n\n\n<p>Since we are using the internal pull up resistor, we can connect the push button directly to out MCU without using external components as shown in the figure below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-860x1024.png\" alt=\"\" class=\"wp-image-2314\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-860x1024.png 860w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-252x300.png 252w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-768x914.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-1290x1536.png 1290w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-1150x1369.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-750x893.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-400x476.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM-250x298.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-27-at-4.26.08\u202fPM.png 1396w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note:<\/p>\n\n\n\n<p>The internal pull up pull down resistors are relatively high values which is not recommended to be used in case there are interference from the environment.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>At use code begin 3, preform the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Check if the button is pressed, if yer:<\/li><li>Toggle the LED.<\/li><li>Wait for 10ms for debounce effect.<\/li><li>wait until the button is released.<\/li><\/ul>\n\n\n\n<p>Hence, 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;}\">\t  if((HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1))==RESET)\n\t  {\n\t\t  \/*Toggle the pin state*\/\n\t\t  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);\n\t\t  \/*Delay by 10ms to ensure stability*\/\n\t\t  HAL_Delay(10);\n\t\t  \/*Wait until the button is released*\/\n\t\t  while((HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1))==RESET)\n\t\t  {\n\t\t\t  \/*Do nothing wasting CPU cycles*\/\n\t\t  }\n\t  }<\/pre><\/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>Upload the code to your board and press the button, you should see the LED toggle with each press.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"GPIO input Mode on STM32G070\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/ilPGycsoyCM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at the STM32G070 features and we were able to blink and LED using HAL. In this guide, we shall toggle the LED using pushbutton connected to out STM32G070. In this guide, we shall cover the following : Input modes. Configure the GPIO as input with internal [&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-2311","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\/2311"}],"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=2311"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2311\/revisions"}],"predecessor-version":[{"id":2315,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2311\/revisions\/2315"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}