PIR LIGHT
PIR Sensor
THINK CODE
#include <Adafruit_LiquidCrystal.h>
int motion = 0;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
pinMode(6, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
abi = analogRead(A0);
Serial.println(motion);
lcd_1.setCursor(0, 0);
lcd_1.print("motion detection");
lcd_1.setCursor(0, 1);
lcd_1.print(motion);
delay(1000); // Wait for 1000 millisecond(s)
lcd_1.clear();
if (motion > 0) {
digitalWrite(6, HIGH);
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
digitalWrite(6, LOW);
}
}
This code reads analog input from pin A0 and displays the value on an LCD, while also controlling two output pins (6 and 11) based on the reading. Here’s a summary of the main parts:
### Code Summary:
1. **Initialization**:
- `int motion = 0;` declares a variable to store the analog input value.
- `Adafruit_LiquidCrystal lcd_1(0);` initializes an LCD object.
2. **Setup Function (`setup()`)**:
- Sets pin A0 as an input.
- Initializes serial communication (`Serial.begin(9600)`) for debugging.
- Configures the LCD (`lcd_1.begin(16, 2)`) to have 16 columns and 2 rows.
- Sets pins 6 and 11 as outputs.
3. **Main Loop (`loop()`)**:
- Reads the value from pin A0 using `analogRead(A0)` and assigns it to `abi`.
- Displays the text "motion detection" on the LCD's first line and the value of `abi` on the second line.
- Waits for 1 second (`delay(1000)`) before clearing the LCD for the next update.
- Controls the output pins 6 and 11:
- If the analog value (`abi`) is greater than 0, both pins are set HIGH (e.g., turn on connected LEDs).
- Otherwise, both pins are set LOW.
### Notes and Considerations:
1. **LCD Initialization**:
- The argument `0` used in `Adafruit_LiquidCrystal lcd_1(0);` may depend on your I2C address or wiring configuration. Make sure the correct address or configuration is being used.
2. **Delay and Display**:
- Using `delay(1000)` will cause a delay of 1 second in each iteration of the loop. Consider using a non-blocking approach if responsiveness is required for other tasks.
3. **Motion Detection Logic**:
- This example assumes the analog reading will change based on some motion detection sensor. If you have a digital motion sensor, you may want to use a digital pin instead (`digitalRead`).
4. **Pin Outputs**:
- Pins 6 and 11 will turn on if the analog reading from A0 is greater than 0. If you need to turn on only one pin or use different thresholds, adjust the conditional logic accordingly.
C++
#include <Adafruit_LiquidCrystal.h>
int motion = 0;
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
pinMode(6, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
motion = analogRead(A0);
Serial.println(motion);
lcd_1.setCursor(0, 0);
lcd_1.print("motion detection");
lcd_1.setCursor(0, 1);
lcd_1.print(motion);
delay(1000); // Wait for 1000 millisecond(s)
lcd_1.clear();
if (motion > 0) {
digitalWrite(6, HIGH);
digitalWrite(11, HIGH);
} else {
digitalWrite(6, LOW);
digitalWrite(11, LOW);
}
}
ARDUINO
#include <Adafruit_LiquidCrystal.h>
int motion = 0; // Variable to store motion sensor readings
int threshold = 10; // Threshold for detecting motion
// Initialize the LCD with the I2C address (0 in this case)
Adafruit_LiquidCrystal lcd_1(0);
void setup() {
pinMode(A0, INPUT); // Set A0 as input for the motion sensor
Serial.begin(9600); // Start serial communication at 9600 baud
lcd_1.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
pinMode(6, OUTPUT); // Set pin 6 as output
pinMode(11, OUTPUT); // Set pin 11 as output
}
void loop() {
motion = analogRead(A0); // Read the analog value from the motion sensor
Serial.println(motion); // Print the motion value to the Serial Monitor
// Display motion detection message on the LCD
lcd_1.setCursor(0, 0);
lcd_1.print("motion detection");
// Clear the second line and display the motion value
lcd_1.setCursor(0, 1);
lcd_1.print(" "); // Clear the line
lcd_1.setCursor(0, 1);
lcd_1.print(motion);
delay(1000); // Wait for 1000 milliseconds
// Check if motion is detected based on the threshold
if (motion > threshold) {
digitalWrite(6, HIGH); // Turn on device connected to pin 6
digitalWrite(11, HIGH); // Turn on device connected to pin 11
} else {
digitalWrite(6, LOW); // Turn off device connected to pin 6
digitalWrite(11, LOW); // Turn off device connected to pin 11
}
}

Comments
Post a Comment