0% found this document useful (0 votes)
54 views5 pages

ESP32C3 LED Blink with Button Code

The document includes code and configuration files for a Rust project that blinks an LED connected to an ESP32-C3 when a button is pressed. It uses the ESP32-C3 HAL and includes a main.rs file that defines a panic handler and blinks the LED in a loop if the button is pressed.

Uploaded by

Bonnie McOwidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views5 pages

ESP32C3 LED Blink with Button Code

The document includes code and configuration files for a Rust project that blinks an LED connected to an ESP32-C3 when a button is pressed. It uses the ESP32-C3 HAL and includes a main.rs file that defines a panic handler and blinks the LED in a loop if the button is pressed.

Uploaded by

Bonnie McOwidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q) Blinky and button std

[Link]

[package] name =
"project" version =
"0.1.0" authors =
["OWIDI"] edition
= "2023"
license = "MIT OR Apache-2.0"
[dependencies] hal =
{ package = "esp32c3-
hal", version = "0.9.0"
}
esp-backtrace =
{ version = "0.7.0",
features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] }
esp-println = { version = "0.5.0", features = ["esp32c3"] }

[Link]

use core::panic::PanicInfo;
use esp32_hal::delay::Delay;
use esp32_hal::gpio::*; use
esp32_hal::target; use
esp32_hal_log::*;

#[panic_handler] fn panic(_info:
&PanicInfo) -> ! {
loop {}
}

fn button_pressed() -> bool {


let gpio_input = unsafe { &(*target::GPIO::ptr()).input };
gpio_input.read().bits() & (1 << 0) == 0
}
fn main() { // Configure logging let peripherals =
target::Peripherals::take().unwrap(); let gpio =
[Link](); let mut serial = peripherals
.UART0
.serial( gpio.gpio1.into_uart(
), gpio.gpio3.into_uart(),
115200.into_baudrate(),
)
.unwrap(); serial.change_mode(|
_| {}).ok(); esp_log::init().unwrap();

let mut led = gpio.gpio2.into_push_pull_output();

let button = gpio.gpio0.into_pullup_input();

log::info!("LED Blink Program Started"); let

delay = Delay::new();

loop {
if button_pressed() {
led.set_high().unwrap();
delay.delay_ms(500);
led.set_low().unwrap();
delay.delay_ms(500);
} else
{ led.set_low().unwrap();
}
}
}

[Link]

[wokwi] version = 1 gdbServerPort = 3333 elf =


"target/riscv32imc-unknown-none-elf/debug/project1" firmware =
"target/riscv32imc-unknown-none-elf/debug/project1"
[Link]

{
"version": 1,
"author": "OWIDI ",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-c3-devkitm-1",
"id": "esp",
"top": -113.29,
"left": -15.02,
"attrs": { "builder": "rust-nostd-esp" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 99.68,
"left": 138.81,
"rotate": 90,
"attrs": { "color": "green", "bounce": "0" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 59.95,
"left": 210.71,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 16.13,
"left": 275.33,
"rotate": 90,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": -138.37,
"left": 184.77,
"attrs": { "color": "red", "flip": "1" }
},
{ "type": "wokwi-gnd", "id": "gnd1", "top": 193.84, "left": 294.01, "attrs": {} }, {
"type": "wokwi-vcc",
"id": "vcc1",
"top": 201.51,
"left": 174.48,
"rotate": 180,
"attrs": {}
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "esp:8", "btn1:2.l", "green", [ "h93.95", "v232.65" ] ],
[ "r2:1", "led1:C", "green", [ "h-98.89", "v-84.19" ] ],
[ "esp:7", "led1:A", "green", [ "h121.63", "v-68.47" ] ],
[ "gnd1:GND", "r2:2", "black", [ "v0" ] ],
[ "gnd1:GND", "r1:2", "black", [ "v-126", "h-35.63" ] ],
[ "esp:8", "r1:1", "green", [ "h93.8", "v107.7" ] ],
[ "vcc1:VCC", "btn1:1.r", "red", [ "v-54.28", "h-5.39" ] ]
],
"serialMonitor": { "display": "terminal" },
"dependencies": {}
}

You might also like