Interactive Sensor Hub: An Arduino Project with 4 Smart Sensor Modes
This is a fun Arduino project that combines four different sensor modes into one system. You can switch between modes using an IR remote control, and each mode shows helpful feedback on an LCD screen. Whether you’re learning Arduino, showing off sensors, or just having fun, this project is a great hands-on experience with simple components.
Components and supplies
1
16x2 LCD display with I²C interface
1
Photoresistor
1
Ultrasonic Sensor - HC-SR04
1
Active Buzzer
3
5mm LED
1
Potentiometer (10K)
1
Breadboard - 830 contacts
1
IR Remote
5
Resistors
1
IR Receiver
1
Arduino UNO
1
Sound sensor KY-038
Apps and platforms
1
Arduino IDE
Project description
Code
Sensor Project
js
1#include <IRremote.hpp> // Use IRremote.hpp in new versions 2#include <LiquidCrystal.h> 3 4 5#define PHOTORESISTOR_PIN A0 6 7#define SOUND_SENSOR_PIN A5 8 9#define IR_RECEIVE_PIN 5 10 11 12#define ECHO_PIN 3 13#define TRIGGER_PIN 4 14 15#define IR_BUTTON_1 12 16#define IR_BUTTON_2 24 17#define IR_BUTTON_3 94 18#define IR_BUTTON_4 8 19#define IR_BUTTON_PLAY 64 20 21#define LED_RED 12 22#define LED_YELLOW 11 23#define LED_GREEN 10 24 25#define LCD_RS_PIN A1 26#define LCD_E_PIN A2 27#define LCD_D4_PIN 6 28#define LCD_D5_PIN 7 29#define LCD_D6_PIN 8 30#define LCD_D7_PIN 9 31 32#define BUZZER 13 33#define BUTTON 2 34 35 36LiquidCrystal lcd(LCD_RS_PIN, LCD_E_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN); // Initializes LCD with given pins 37 38unsigned long last_time_ultrasonic_trigger = millis(); 39unsigned long ultrasonic_trigger_delay = 500; 40volatile unsigned long pulse_in_begin; 41volatile unsigned long pulse_in_end; 42volatile bool new_distance_available = false; 43 44unsigned long previous_time_buzz = millis(); 45 46int current_mode = 0; // 0 = idle, 1 = Button 1, 2 = Button 2, etc. 47 48int new_distance = 0; 49int old_distance = 0; 50 51float km = 0; 52 53 54void trigger_ultrasonic_sensor(){ 55 digitalWrite(TRIGGER_PIN, LOW); 56 delayMicroseconds(2); 57 digitalWrite(TRIGGER_PIN, HIGH); 58 delayMicroseconds(10); 59 digitalWrite(TRIGGER_PIN, LOW); 60} 61 62// Function to calculate the distance measured by the ultrasonic sensor 63int get_distance() 64{ 65 double duration_micros = pulse_in_end - pulse_in_begin; 66 double distance = duration_micros / 58.0; 67 return distance; 68} 69 70// Interrupt service routine (ISR) for detecting the echo pulse 71void echo_pin_interrupt() 72{ 73 if (digitalRead(ECHO_PIN) == HIGH) { 74 pulse_in_begin = micros(); 75 } 76 else { 77 pulse_in_end = micros(); 78 new_distance_available = true; 79 } 80} 81 82int buzzer_state = LOW; 83 84 85void setup() { 86 Serial.begin(115200); 87 IrReceiver.begin(IR_RECEIVE_PIN); // Start the IR receiver 88 89 pinMode(LED_RED, OUTPUT); 90 pinMode(LED_YELLOW, OUTPUT); 91 pinMode(LED_GREEN, OUTPUT); 92 93 pinMode(LED_RED, OUTPUT); 94 pinMode(LED_YELLOW, OUTPUT); 95 pinMode(LED_GREEN, OUTPUT); 96 pinMode(ECHO_PIN, INPUT); 97 98 pinMode(TRIGGER_PIN, OUTPUT); 99 pinMode(BUZZER, OUTPUT); 100 101 lcd.begin(16, 2); // For a 16x2 LCD 102 103 attachInterrupt(digitalPinToInterrupt(ECHO_PIN), echo_pin_interrupt, CHANGE); 104 105 pinMode(SOUND_SENSOR_PIN, INPUT); 106 107 108} 109 110void loop() { 111 unsigned long time_now = millis(); 112 113 114 115 if (IrReceiver.decode()) { 116 unsigned long value = IrReceiver.decodedIRData.command; 117 118 if (value == IR_BUTTON_1) { 119 current_mode = 1; 120 } 121 else if (value == IR_BUTTON_2) { 122 current_mode = 2; 123 } 124 else if (value == IR_BUTTON_3) { 125 current_mode = 3; 126 } 127 else if (value == IR_BUTTON_4) { 128 current_mode = 4; 129 } 130 else if (value == IR_BUTTON_PLAY) { 131 current_mode = 5; 132 } 133 134 IrReceiver.resume(); // Ready for next signal 135 } 136 137 // Button 1 logic runs continuously if current_mode is 1 138 if (current_mode == 1) { 139 140 if (time_now - last_time_ultrasonic_trigger > ultrasonic_trigger_delay) { 141 last_time_ultrasonic_trigger += ultrasonic_trigger_delay; 142 trigger_ultrasonic_sensor(); 143 } 144 145 if (new_distance_available) { 146 new_distance_available = false; 147 int distance = get_distance(); 148 149 old_distance = new_distance; 150 new_distance = distance; 151 152 if (old_distance > new_distance){ 153 km = (old_distance - new_distance) * 2.0 / 100000 * 3600; 154 Serial.println(km); 155 } 156 else { 157 km = (new_distance - old_distance) * 2.0 / 100000 * 3600; 158 Serial.println(km); 159 } 160 161 Serial.println(distance); 162 lcd.setCursor(0, 0); 163 lcd.print(" "); // Clear line 164 lcd.setCursor(0, 0); 165 lcd.print("distance: "); 166 lcd.print(distance); 167 lcd.print("CM"); 168 169 if (old_distance - new_distance > 5){ 170 lcd.setCursor(0, 1); 171 lcd.print(" "); // Clear line 172 lcd.setCursor(0, 1); 173 lcd.print("coming "); lcd.print(km); lcd.print("Km/h"); 174 } 175 else if(new_distance - old_distance > 5){ 176 lcd.setCursor(0, 1); 177 lcd.print(" "); // Clear line 178 lcd.setCursor(0, 1); 179 lcd.print("going "); lcd.print(km); lcd.print("Km/h"); 180 181 }else{ 182 lcd.setCursor(0, 1); 183 lcd.print(" "); // Clear line 184 lcd.setCursor(0, 1); 185 lcd.print("stationary"); 186 } 187 } 188 } 189 190 // Button 2 placeholder 191else if (current_mode == 2) { 192 if (time_now - last_time_ultrasonic_trigger > ultrasonic_trigger_delay) { 193 last_time_ultrasonic_trigger += ultrasonic_trigger_delay; 194 trigger_ultrasonic_sensor(); 195 } 196 197 if (new_distance_available) { 198 new_distance_available = false; 199 int distance = get_distance(); 200 Serial.println(distance); 201 202 if (distance > 0 && distance <= 50) { 203 // Red light + fast buzzer (every 250 ms) 204 if (time_now - previous_time_buzz > 250) { 205 digitalWrite(LED_RED, HIGH); 206 digitalWrite(LED_YELLOW, LOW); 207 digitalWrite(LED_GREEN, LOW); 208 209 buzzer_state = !buzzer_state; 210 digitalWrite(BUZZER, buzzer_state); 211 212 previous_time_buzz = time_now; 213 214 lcd.clear(); 215 lcd.print("close"); 216 } 217 } 218 else if (distance > 50 && distance <= 150) { 219 // Yellow light + medium buzzer (every 500 ms) 220 if (time_now - previous_time_buzz > 500) { 221 digitalWrite(LED_RED, LOW); 222 digitalWrite(LED_YELLOW, HIGH); 223 digitalWrite(LED_GREEN, LOW); 224 225 buzzer_state = !buzzer_state; 226 digitalWrite(BUZZER, buzzer_state); 227 228 previous_time_buzz = time_now; 229 230 lcd.clear(); 231 lcd.print("medium"); 232 } 233 } 234 else if (distance > 150) { 235 // Green light, no buzzer 236 digitalWrite(LED_RED, LOW); 237 digitalWrite(LED_YELLOW, LOW); 238 digitalWrite(LED_GREEN, HIGH); 239 240 digitalWrite(BUZZER, LOW); // Turn buzzer off 241 buzzer_state = LOW; 242 243 lcd.clear(); 244 lcd.print("far"); 245 } 246 } 247} 248 // Button 3 placeholder 249else if (current_mode == 3) { 250 Serial.println("Mode 3 active"); 251 252 int light = analogRead(PHOTORESISTOR_PIN); 253 Serial.print("Light: "); 254 Serial.println(light); 255 256 if (light < 300) { 257 digitalWrite(LED_RED, HIGH); // Turn on RED LED if dark 258 } else { 259 digitalWrite(LED_RED, LOW); 260 } 261 262 int brightness = 255 - light / 4; 263 analogWrite(LED_GREEN, brightness); // PWM works on pin 10 (Uno) 264 265 lcd.setCursor(0, 0); 266 lcd.print("Light:"); 267 lcd.print(light); 268 lcd.print(" "); 269 270 lcd.setCursor(0, 1); 271 lcd.print("Green PWM:"); 272 lcd.print(brightness); 273 lcd.print(" "); 274} 275 276 277 // Button 4 placeholder 278else if (current_mode == 4) { 279 static bool leds_on = false; 280 static unsigned long last_clap_time = 0; 281 static bool last_state = LOW; 282 283 int sound = digitalRead(A5); // digital pin connected to DO pin 284 Serial.println(sound); // debug - watch it go HIGH when clapping 285 286 unsigned long now = millis(); 287 288 if (sound == HIGH && last_state == LOW && (now - last_clap_time > 400)) { 289 leds_on = !leds_on; 290 last_clap_time = now; 291 292 digitalWrite(LED_RED, leds_on ? HIGH : LOW); 293 digitalWrite(LED_YELLOW, leds_on ? HIGH : LOW); 294 digitalWrite(LED_GREEN, leds_on ? HIGH : LOW); 295 } 296 297 last_state = sound; // remember the last value 298} 299 300}
Documentation
Sensor Project
s
https://wokwi.com/projects/433398774672126977
Comments
Only logged in users can leave comments