This circuit involves multiple Arduino Nano microcontrollers, a Bluetooth module, an LCD screen, a buzzer, an ultrasonic sensor, a motor driver, and motors with reducers. The circuit is designed to control a motor based on commands received via Bluetooth and display status messages on an LCD screen. Additionally, it uses an ultrasonic sensor to detect obstacles and stop the motor if an object is too close.
Arduino Nano
Bluetooth HC-06
LCD screen 16x2 I2C
Buzzer
HC-SR04 Ultrasonic Sensor
L293D Motor Driver
Motor with Reducer
Electrolytic Capacitor
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with the I2C address 0x27 and 16x2 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin definitions
const int buzzerPin = 12; // Buzzer connected to D12/MISO
const int motorInput1 = 3; // Motor driver input 1 connected to D3
const int motorInput2 = 4; // Motor driver input 2 connected to D4
const int trigPin = 11; // Ultrasonic sensor TRIG connected to D11/MOSI
const int echoPin = 10; // Ultrasonic sensor ECHO connected to D10
// Distance threshold in centimeters
const int distanceThreshold = 20;
void setup() {
// Initialize serial communication for Bluetooth
Serial.begin(9600);
// Initialize the LCD
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Waiting for cmd");
// Initialize pins
pinMode(buzzerPin, OUTPUT);
pinMode(motorInput1, OUTPUT);
pinMode(motorInput2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Check if data is available from Bluetooth
if (Serial.available()) {
char command = Serial.read();
delay(10); // Debounce input
// Process the received command
switch (command) {
case '1':
// Turn on the buzzer
digitalWrite(buzzerPin, HIGH);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Buzzer ON ");
break;
case '0':
// Turn off the buzzer
digitalWrite(buzzerPin, LOW);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Buzzer OFF");
break;
case 'F':
// Move motor forward
moveMotorForward();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Motor FWD ");
break;
case 'B':
// Move motor backward
moveMotorBackward();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Motor BWD ");
break;
case 'S':
// Stop motor
stopMotor();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Motor STOP");
break;
default:
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Invalid cmd");
break;
}
}
// Check the distance to the nearest object
int distance = measureDistance();
if (distance < distanceThreshold) {
// Stop the motor if the object is too close
stopMotor();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Object Detected");
}
}
int measureDistance() {
// Send a 10us pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);