// WEEK 5: DHT22 + PIR + SERVO
# include <DHT.h>
# include <Servo.h>
// ---- DHT22 ----
# define DHTPIN PB9
# define DHTTYPE DHT22
DHT dht ( DHTPIN , DHTTYPE ) ;
// ---- PIR ----
# define PIR_PIN PB7
// ---- Servo ----
Servo myServo ;
# define SERVO_PIN PB6
// ---- LED + Button ----
# define LED_PIN PB11
# define BTN_PIN PB10
void setup () {
Serial . begin (9600) ;
pinMode ( LED_PIN , OUTPUT ) ;
pinMode ( BTN_PIN , INPUT_PULLUP ) ;
pinMode ( PIR_PIN , INPUT ) ;
dht . begin () ; // start DHT22
STM32 + DHT22 + PIR + Servo on Wokwi Week 5: Sensor Lab
myServo . attach ( SERVO_PIN ) ; // start Servo
Serial . println (" ALL COMPONENTS READY !") ;
}
void loop () {
digitalWrite ( LED_PIN , HIGH ) ; // LED ON
delay (100) ;
// ----- DHT22 -----
Serial . println ("\ n - - - DHT22 ( Room ) - - -") ;
float h = dht . readHumidity () ;
float t = dht . readTemperature () ;
if ( isnan ( h ) || isnan ( t ) ) {
Serial . println (" DHT22 failed ! Check wiring .") ;
} else {
Serial . print (" Temp : ") ; Serial . print ( t ) ; Serial . println (" C ") ;
Serial . print (" Hum : ") ; Serial . print ( h ) ; Serial . println (" %") ;
}
// ----- PIR -----
Serial . println (" - - - PIR ( Motion ) - - -") ;
int motion = digitalRead ( PIR_PIN ) ;
Serial . println ( motion ? " Motion DETECTED " : " No motion ") ;
// ----- Servo -----
Serial . println (" - - - Servo - - -") ;
if ( motion ) {
myServo . write (90) ; // turn to 90 degrees when motion
Serial . println (" Servo -> 90 degrees ") ;
} else {
myServo . write (0) ; // back to 0 degrees
Serial . println (" Servo -> 0 degrees ") ;
}
// ----- Button ( fake data ) -----
if ( digitalRead ( BTN_PIN ) == LOW ) {
Serial . println (" BUTTON PRESSED FAKE DATA !") ;
Serial . println (" Temp : 25.0 C | Hum : 50% | Motion : YES | Servo : 90 degrees ") ;
}
Serial . println (" ====================\ n ") ;
digitalWrite ( LED_PIN , LOW ) ;
delay (1900) ; // total 2 s cycle
}Loading
st-nucleo-c031c6
st-nucleo-c031c6