{"id":31104,"date":"2025-02-17T07:44:39","date_gmt":"2025-02-17T15:44:39","guid":{"rendered":"https:\/\/digilent.com\/blog\/?p=31104"},"modified":"2025-02-17T07:45:21","modified_gmt":"2025-02-17T15:45:21","slug":"setting-individual-gpio-pins-in-digilent-waveforms-python-from-the-forum","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/setting-individual-gpio-pins-in-digilent-waveforms-python-from-the-forum\/","title":{"rendered":"Setting Individual GPIO Pins in Digilent WaveForms Python: From the Forum"},"content":{"rendered":"<h2><b><span data-contrast=\"auto\">The Problem:<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">A forum member shared their <\/span><span data-contrast=\"auto\">set_gpio()<\/span><span data-contrast=\"auto\"> function, which sets a single pin to a specific state (high or low). However, the function had a limitation: it unintentionally cleared all other DIO pins when setting the target pin. This caused issues when the user wanted to turn on an LED while manipulating other outputs, like relays.<\/span><span data-contrast=\"none\">\u00a0<\/span><\/p>\n<h2><b><span data-contrast=\"auto\">The Solution:<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">Digilent Engineer Arthur responded with a helpful approach: reading the current state of all DIO pins, modifying the desired bit for the target LED, and then writing the entire modified state back out. This ensures only the target pin is affected.<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"auto\">Read Current State:<\/span><\/b><span data-contrast=\"auto\"> Before modifying anything, the code reads the current state of all DIO pins. This provides a &#8220;snapshot&#8221; of the current configuration.<\/span><span data-ccp-props=\"{&quot;335559739&quot;:0}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"auto\">Modify LED Bit:<\/span><\/b><span data-contrast=\"auto\"> The specific bit corresponding to the target LED (e.g., <\/span><span data-contrast=\"auto\">READY_LED<\/span><span data-contrast=\"auto\">) is identified. The code then modifies only this bit within the read state data, setting it to the desired value (high or low) for the LED.<\/span><span data-ccp-props=\"{&quot;335559739&quot;:0}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><b><span data-contrast=\"auto\">Write Back Modified State:<\/span><\/b><span data-contrast=\"auto\"> Finally, the entire modified state, including unchanged bits for other pins, is written back to the DIO controller. This ensures only the target LED is affected while preserving the state of other pins.<\/span><span data-ccp-props=\"{&quot;335559739&quot;:0}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-10-103914-1024x247.png\" alt=\"\" width=\"735\" height=\"177\" class=\"alignnone size-large wp-image-31333\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-10-103914-1024x247.png 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-10-103914-600x145.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-10-103914-1536x371.png 1536w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-10-103914-2048x495.png 2048w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/p>\n<h2><b><span data-contrast=\"auto\">Benefits:<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">This method offers several advantages:<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"auto\">Clarity:<\/span><\/b><span data-contrast=\"auto\"> It&#8217;s a clear and well-understood approach for manipulating individual bits.<\/span><span data-ccp-props=\"{&quot;335559739&quot;:0}\">\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"auto\">Efficiency:<\/span><\/b><span data-contrast=\"auto\"> While potentially requiring more code than a single-line write function, it efficiently updates only the necessary bit.<\/span><span data-ccp-props=\"{&quot;335559739&quot;:0}\">\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><b><span data-contrast=\"auto\">Flexibility:<\/span><\/b><span data-contrast=\"auto\"> This approach is adaptable to various scenarios involving multiple digital outputs.<\/span><span data-ccp-props=\"{&quot;335559739&quot;:0}\">\u00a0<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><b><span data-contrast=\"auto\">Conclusion:<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">This forum exchange highlights a common challenge in embedded systems programming and provides a practical solution using the WaveForms Python SDK. The approach of reading, modifying, and writing the entire DIO state ensures precise control over individual pins while maintaining the state of others. By understanding this technique, users can write more flexible and efficient code for their Digilent projects.<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For a more detailed discussion and to see the full code examples, visit the thread on the Digilent forum: <\/span><a href=\"https:\/\/forum.digilent.com\/topic\/30507-python-to-control-dio-on-analog-discovery-3\/#comment-93048\"><span data-contrast=\"none\">here<\/span><\/a><span data-contrast=\"auto\">. Feel free to ask any follow-up questions you may have in the forum thread, where our community of engineers and users can assist you!<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-31104 jlk' data-task='like' data-post_id='31104' data-nonce='e853fab138' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-31104 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-31104 jlk' data-task='unlike' data-post_id='31104' data-nonce='e853fab138' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-31104 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-31104 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>The Problem:\u00a0 A forum member shared their set_gpio() function, which sets a single pin to a specific state (high or low). However, the function had a limitation: it unintentionally cleared &hellip; <\/p>\n","protected":false},"author":47,"featured_media":31331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[20,1563],"tags":[5108,5111,5109,5093,5110,4626,4381,452],"ppma_author":[4587],"class_list":["post-31104","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-products","category-guide","tag-ask-an-engineer","tag-dio","tag-dio-pins","tag-forum","tag-led","tag-python","tag-sdk","tag-waveforms"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2025\/02\/Setting-Individual-GPIO-Pins-735x400-1.png","authors":[{"term_id":4587,"user_id":0,"is_guest":1,"slug":"digilent","display_name":"Digilent","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/31104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=31104"}],"version-history":[{"count":2,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/31104\/revisions"}],"predecessor-version":[{"id":31336,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/31104\/revisions\/31336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/31331"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=31104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=31104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=31104"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=31104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}