mirror of
https://github.com/ImplFerris/esp32-book.git
synced 2025-09-24 14:31:13 +00:00
pir sensor module - esp-hal 1.0.0-beta
This commit is contained in:
parent
f7ba860f5b
commit
b5de2cd550
@ -54,8 +54,8 @@ The PIR sensor connection is the same as before (see [circuit](./circuit.md)). W
|
||||
We'll set up GPIO 18 as an Output pin with an initial Low state for the active buzzer. The onboard LED, connected to GPIO 2, will also be configured as an Output pin with an initial Low state.
|
||||
|
||||
```rust
|
||||
let mut buzzer_pin = Output::new(peripherals.GPIO18, Level::Low);
|
||||
let mut led = Output::new(peripherals.GPIO2, Level::Low);
|
||||
let mut buzzer_pin = Output::new(peripherals.GPIO18, Level::Low, OutputConfig::default());
|
||||
let mut led = Output::new(peripherals.GPIO2, Level::Low, OutputConfig::default());
|
||||
```
|
||||
|
||||
## The Logic
|
||||
@ -67,11 +67,11 @@ loop {
|
||||
if sensor_pin.is_high() {
|
||||
buzzer_pin.set_high();
|
||||
led.set_high();
|
||||
delay.delay(100.millis());
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
buzzer_pin.set_low();
|
||||
led.set_low();
|
||||
}
|
||||
delay.delay(100.millis());
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
}
|
||||
```
|
||||
|
||||
@ -91,36 +91,45 @@ cd esp32-projects/burglar-alarm
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Input, Level, Output, Pull};
|
||||
use esp_hal::prelude::*;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull};
|
||||
use esp_hal::main;
|
||||
use esp_hal::time::{Duration, Instant};
|
||||
|
||||
#[entry]
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[main]
|
||||
fn main() -> ! {
|
||||
let peripherals = esp_hal::init({
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
config
|
||||
});
|
||||
// generator version: 0.3.1
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let sensor_pin = Input::new(peripherals.GPIO33, Pull::Down);
|
||||
let sensor_pin = Input::new(
|
||||
peripherals.GPIO33,
|
||||
InputConfig::default().with_pull(Pull::Down),
|
||||
);
|
||||
|
||||
let mut buzzer_pin = Output::new(peripherals.GPIO18, Level::Low);
|
||||
let mut led = Output::new(peripherals.GPIO2, Level::Low);
|
||||
let mut buzzer_pin = Output::new(peripherals.GPIO18, Level::Low, OutputConfig::default());
|
||||
let mut led = Output::new(peripherals.GPIO2, Level::Low, OutputConfig::default());
|
||||
|
||||
let delay = Delay::new();
|
||||
loop {
|
||||
if sensor_pin.is_high() {
|
||||
buzzer_pin.set_high();
|
||||
led.set_high();
|
||||
delay.delay(100.millis());
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
buzzer_pin.set_low();
|
||||
led.set_low();
|
||||
}
|
||||
delay.delay(100.millis());
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
|
||||
fn blocking_delay(duration: Duration) {
|
||||
let delay_start = Instant::now();
|
||||
while delay_start.elapsed() < duration {}
|
||||
}
|
||||
```
|
||||
|
@ -11,14 +11,26 @@ To create the project, use the `esp-generate` command. Run the following:
|
||||
esp-generate --chip esp32 pir-sensor
|
||||
```
|
||||
|
||||
This will open a screen asking you to select options. We dont need to select any options. Just save it by pressing "s" in the keyboard.
|
||||
This will bring up a screen asking you to select options.
|
||||
|
||||
In this exercise, we will be printing messages to the system console. In order to do that, we will enable the logging feature.
|
||||
|
||||
So, scroll to "Flashing, logging and debugging (espflash)" and hit Enter.
|
||||
|
||||
Select "Use defmt to print messages".
|
||||
|
||||
Just save it by pressing "s" on the keyboard.
|
||||
|
||||
|
||||
## Sensor Output Pin to ESP32's Input
|
||||
|
||||
We'll configure GPIO 33 as an input pin with an initial pull-down state. This pin is connected to the PIR sensor's output, which goes HIGH whenever motion is detected.
|
||||
|
||||
```rust
|
||||
let sensor_pin = Input::new(peripherals.GPIO33, Pull::Down);
|
||||
let sensor_pin = Input::new(
|
||||
peripherals.GPIO33,
|
||||
InputConfig::default().with_pull(Pull::Down),
|
||||
);
|
||||
```
|
||||
|
||||
## The logic
|
||||
@ -27,14 +39,13 @@ The idea is simple: we continuously check the sensor's output in a loop. When th
|
||||
```rust
|
||||
loop {
|
||||
if sensor_pin.is_high() {
|
||||
println!("Motion detected");
|
||||
delay.delay(100.millis());
|
||||
info!("Motion detected");
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
}
|
||||
delay.delay(100.millis());
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Clone the existing project
|
||||
You can clone (or refer) project I created and navigate to the `pir-sensor` folder.
|
||||
|
||||
@ -49,31 +60,41 @@ cd esp32-projects/pir-sensor
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Input, Pull};
|
||||
use esp_hal::prelude::*;
|
||||
use esp_println::println;
|
||||
use defmt::info;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::gpio::{Input, InputConfig, Pull};
|
||||
use esp_hal::main;
|
||||
use esp_hal::time::{Duration, Instant};
|
||||
use esp_println as _;
|
||||
|
||||
#[entry]
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[main]
|
||||
fn main() -> ! {
|
||||
let peripherals = esp_hal::init({
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
config
|
||||
});
|
||||
// generator version: 0.3.1
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let sensor_pin = Input::new(peripherals.GPIO33, Pull::Down);
|
||||
let sensor_pin = Input::new(
|
||||
peripherals.GPIO33,
|
||||
InputConfig::default().with_pull(Pull::Down),
|
||||
);
|
||||
|
||||
let delay = Delay::new();
|
||||
loop {
|
||||
if sensor_pin.is_high() {
|
||||
println!("Motion detected");
|
||||
delay.delay(100.millis());
|
||||
info!("Motion detected");
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
}
|
||||
delay.delay(100.millis());
|
||||
blocking_delay(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
|
||||
fn blocking_delay(duration: Duration) {
|
||||
let delay_start = Instant::now();
|
||||
while delay_start.elapsed() < duration {}
|
||||
}
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user