---
title: "Discussion on \"ESP32 Standard Library Embedded Rust: GPIO Control\" | Hashnode"
description: "Discussion on \"ESP32 Standard Library Embedded Rust: GPIO Control\". Introduction\nEmbedded programming can generally be classified into two areas; hosted and bare-metal. Bare-metal programming assumes a clean slate, meaning that the target hardware or environment inclu"
url: "https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [ESP32 Standard Library Embedded Rust: GPIO Control](https://blog.theembeddedrustacean.com/esp32-standard-library-embedded-rust-gpio-control?utm_source=hashnode&utm_medium=feed)

Introduction Embedded programming can generally be classified into two areas; hosted and bare-metal. Bare-metal programming assumes a clean slate, meaning that the target hardware or environment inclu

Jul 13, 2023 · 17 min read · blog.theembeddedrustacean.com  
Updated Jul 1  
Read the article

-   [#rust](https://hashnode.com/tag/rust)
-   [#esp32](https://hashnode.com/tag/esp32)
-   [#embedded](https://hashnode.com/tag/embedded)
-   [#embedded-systems](https://hashnode.com/tag/embedded-systems)
-   [#tutorial](https://hashnode.com/tag/tutorial)

## Written by

[Omar Hiari](https://hashnode.com/@omarhiari)

@omarhiari

  
[View profile](https://hashnode.com/@omarhiari)

I am an 📟 Embedded Engineer with years of experience in both industry 🏭 and academia 🏫. Passionate Mentor 👨‍💼 and Instructor 👨‍🏫. Rustacean 🦀

## Responses 3

Markdown supported · ⌘↵ to post

[Igor Cavalcante](https://hashnode.com/@igorcavalcante)

[Jul 28, 2023](https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2/comment/64c41058917e545aeed98bb5)

Great article. I love rust and electronic, but I never found a way to use rust with microcontrolers without a lot of boilerplate. I will try this approach. ty

[Omar Hiari](https://hashnode.com/@omarhiari)

[Jul 29, 2023](https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2/comment/64c41058917e545aeed98bb5#reply-64c49f05cbf7a460bdc58b05)

Thank you! I hope that you’ll find it better. Rust has a steeper curve for sure, especially in embedded, though I feel it’s getting better as the ecosystem, including educational material, is growing.

[Christian Foucher](https://hashnode.com/@Valda60)

[Jul 21, 2023](https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2/comment/64ba889a2e3477ebf613499c)

Another suggestion, you can remove all the "return" key words of the button\_pressed function (don't forget to remove also semicolon). It is more idiomatic Rust, but also less readable for the new comer.

fn button_pressed(but: &PinDriver<'_, Gpio3, Input>, del: &u32) -> u32 { // Check if Button has been pressed // If not pressed, return the delay value unchanged if but.is\_low() { // if the value of the delay passed is less of equal to 50 then reset it to initial value // else subtract 50 from the passed delay println!("Button Pressed!"); if del <= &50\_u32 { 200\_u32 } else { 50\_u32 } } else { \*del } }

[Christian Foucher](https://hashnode.com/@Valda60)

[Jul 20, 2023](https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2/comment/64b94706a884f3de56a26afd)

I think you should use an array instead of declaring 10 variables (one variable by pin!). Then you will be able to iterate the array with a for loop.

// Configure all LED pins to digital outputs let mut leds = \[ PinDriver::output(dp.pins.gpio1.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio10.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio19.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio18.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio4.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio5.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio6.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio7.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio8.downgrade\_output()).unwrap(), PinDriver::output(dp.pins.gpio9.downgrade\_output()).unwrap(), \];

// Put the following code in the loop for mut led in &mut leds { // LED i led.set\_high().unwrap(); blinkdelay = button\_pressed(&button, &blinkdelay); FreeRtos::delay\_ms(blinkdelay); led.set\_low().unwrap(); FreeRtos::delay\_ms(100\_u32); }

// I have tested the result on wokwi

[Omar Hiari](https://hashnode.com/@omarhiari)

[Jul 20, 2023](https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2/comment/64b94706a884f3de56a26afd#reply-64b9a4c1af9957de874117ab)

Thanks for the tip [Christian Foucher](https://hashnode.com/@Valda60)! You are absolutely correct. I actually had the same thought when creating the application, however, my issue was with the different types. When creating the pins, each one had a different type and I could not place them in a single array. However, at the time I wasn't aware there was the `downgrade_output` method that would change the pins to a generic type thus enabling the placing of all pins in a single array. After your suggestion, when I went back to check the hal documentation, it seems like I was looking in the wrong place at the time. I will modify the post sometime soon to include this as an optimized alternative.

[Christian Foucher](https://hashnode.com/@Valda60)

[Jul 21, 2023](https://hashnode.com/posts/esp32-standard-library-embedded-rust-gpio-control/64afe421c3220ca4300ee7a2/comment/64b94706a884f3de56a26afd#reply-64ba7c8d2e3477ebf613485f)

[Omar Hiari](https://hashnode.com/@omarhiari) I have created a suggestion issue in your gitlab repository (because code formatting is ugly in my comment).
