0% found this document useful (0 votes)
9 views39 pages

LCD Sensor Detection Code

The document provides various Arduino Uno code examples for interfacing with an LCD and different sensors. It includes code for displaying messages based on ultrasonic distance detection, temperature readings, and user input from buttons to generate random numbers and calculate sums. Additionally, it offers wiring guides and explanations of how the code works, along with suggestions for improvements and modifications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views39 pages

LCD Sensor Detection Code

The document provides various Arduino Uno code examples for interfacing with an LCD and different sensors. It includes code for displaying messages based on ultrasonic distance detection, temperature readings, and user input from buttons to generate random numbers and calculate sums. Additionally, it offers wiring guides and explanations of how the code works, along with suggestions for improvements and modifications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lcd with sensor.

Will print "HELLO" if


detected

give me code for arduino uno

Here’s a simple Arduino Uno code where an LCD displays "HELLO" when a sensor detects
something.

I’ll assume you're using:

LCD with I2C (16x2)


Ultrasonic sensor (HC-SR04) (common for detection)

✅ Arduino Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD setup (address 0x27, 16 columns, 2 rows)


LiquidCrystal_I2C lcd(0x27, 16, 2);

// Ultrasonic sensor pins


const int trigPin = 6;
const int echoPin = 7;

long duration;
int distance;

void setup() {
[Link]();
[Link]();

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

[Link](0, 0);
[Link]("System Ready");
delay(2000);
[Link]();
}

void loop() {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;

[Link]();

// If object is detected within 10 cm


if (distance > 0 && distance <= 10) {
[Link](0, 0);
[Link]("HELLO");
} else {
[Link](0, 0);
[Link]("No Object");
}

delay(500);
}

🔌 Wiring Guide
LCD I2C
VCC → 5V
GND → GND
SDA → A4
SCL → A5

Ultrasonic Sensor
VCC → 5V
GND → GND
TRIG → Pin 6
ECHO → Pin 7

⚡ How it works
Sensor measures distance
If object is ≤ 10 cm, LCD shows HELLO
Otherwise, shows No Object

🔧 Want alternatives?
If your sensor is different, I can adjust the code:
IR sensor 👀
LDR (light sensor) 🌞
PIR motion sensor 🚶

Just tell me 👍

why duration

Good question—this is the core of how the ultrasonic sensor works.

🔍 Why do we use duration ?

duration stores how long (in microseconds) the ultrasonic signal takes to travel:

1. From the sensor → to the object


2. Then bounce back → to the sensor

So basically:

duration = travel time of the sound wave (go + return)


⚙️ This line:

duration = pulseIn(echoPin, HIGH);


C++
means:
Wait for the echo pin to go HIGH
Measure how long it stays HIGH
That time = duration

📏 Why is it important?
We use duration to compute distance:

distance = duration * 0.034 / 2;


C++
0.034 cm/µs = speed of sound
Divide by 2 because the signal travels back and forth

🧠 Simple explanation
Think of it like shouting in a cave:

You shout 👄
You hear the echo 👂
The time between shout and echo = duration

The longer the time → the farther the object

🔁 Without duration ?

You cannot calculate distance, so the sensor wouldn’t know if something is near or far.
If you want, I can show you a version without distance ( just detect HIGH/LOW using IR
sensor) which is even simpler 👍

Lcd with temp sensor. Will display the


temp

Here’s a simple Arduino Uno setup where an LCD displays the temperature from a sensor
🌡️

I’ll use a TMP36/LM35 (analog temperature sensor) + I2C LCD (16x2).

✅ Arduino Code (LM35 / TMP36)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Temp sensor pin


const int tempPin = A0;

void setup() {
[Link]();
[Link]();

[Link](0, 0);
[Link]("Temp Monitor");
delay(2000);
[Link]();
}

void loop() {
int sensorValue = analogRead(tempPin);
// Convert to voltage
float voltage = sensorValue * (5.0 / 1023.0);

// For LM35: 10mV per °C


float temperature = voltage * 100;

[Link]();
[Link](0, 0);
[Link]("Temp: ");
[Link](temperature);
[Link](" C");

delay(1000);
}

🔌 Wiring Guide
📟 LCD (I2C)
VCC → 5V
GND → GND
SDA → A4
SCL → A5

🌡️ LM35 / TMP36
VCC → 5V
GND → GND
OUT → A0

⚠️ Important (Sensor Type)


If you are using LM35
✔ Code above is correct

If you are using TMP36, change this line:

float temperature = (voltage - 0.5) * 100;


C++
🧠 How it works
Sensor outputs analog voltage based on temperature
Arduino reads it using analogRead()
Converts voltage → °C
LCD displays the temperature

🔧 Optional Improvements
Show °C and °F
Add "HOT!" warning
Smooth readings (average values)

If you tell me your exact sensor (LM35, TMP36, or DHT11), I can give a perfect version for
your lab 👍

. LCD that displays "i pass micron" and


beeps (no button)

Got it 👍 you want:


LCD shows “I PASS MICRON”
Buzzer beeps automatically (no button)
Arduino Uno

Here’s a simple working setup 👇

✅ Arduino Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Buzzer pin
const int buzzerPin = 8;

void setup() {
[Link]();
[Link]();

pinMode(buzzerPin, OUTPUT);

// Display message
[Link](0, 0);
[Link]("I PASS MICRON");

// Beep 3 times
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(300);
}
}

void loop() {
// No loop needed (runs once)
}

🔌 Wiring Guide
📟 LCD (I2C)
VCC → 5V
GND → GND
SDA → A4
SCL → A5

🔊 Buzzer
Positive (+) → Pin 8
Negative (–) → GND

🔊 Optional: Melody Instead of Beep


If you want a simple melody, replace the beep loop with this:

tone(buzzerPin, 1000); // 1kHz sound


delay(300);
noTone(buzzerPin);

delay(200);

tone(buzzerPin, 1500);
delay(300);
noTone(buzzerPin);

🧠 How it works
LCD displays message once at startup
Buzzer beeps automatically
No button needed (runs immediately)

If you want it to repeat continuously or beep every few seconds, I can modify it 👍

what if i want it repeatedly

If you want it to repeat continuously, just move the display + buzzer code into the
loop() so it keeps running 🔁
✅ Repeating Version (Continuous)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Buzzer pin
const int buzzerPin = 8;

void setup() {
[Link]();
[Link]();
pinMode(buzzerPin, OUTPUT);
}

void loop() {
[Link]();
[Link](0, 0);
[Link]("I PASS MICRON");

// Beep 3 times
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(300);
}

delay(2000); // wait before repeating


}

🔁 What happens now


Shows "I PASS MICRON"
Beeps 3 times
Waits 2 seconds
Repeats forever
🔧 If you want smoother display (no flicker)
Instead of clearing every loop, do this:

void setup() {
[Link]();
[Link]();
pinMode(buzzerPin, OUTPUT);

[Link](0, 0);
[Link]("I PASS MICRON");
}

void loop() {
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
delay(300);
}

delay(2000);
}

✔ LCD stays steady


✔ Only buzzer repeats

If you want next level 🔥


play a song
blink LED with buzzer
scroll text on LCD

just tell me 👍

what if i want the lcd display to blink at the same time with
the buzzer

Nice idea 🔥 — you want the LCD text to blink in sync with the buzzer.

We can do that by:

Turning LCD display ON/OFF


Turning buzzer ON/OFF at the same time

✅ Code (LCD + Buzzer Blink Together)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Buzzer pin
const int buzzerPin = 8;

void setup() {
[Link]();
[Link]();
pinMode(buzzerPin, OUTPUT);

[Link](0, 0);
[Link]("I PASS MICRON");
}

void loop() {
// ON (show text + beep)
[Link](); // turn LCD ON
digitalWrite(buzzerPin, HIGH);
delay(300);

// OFF (hide text + silent)


[Link](); // turn LCD OFF
digitalWrite(buzzerPin, LOW);
delay(300);
}
🔁 What happens
LCD text appears + buzzer ON 🔊
LCD disappears + buzzer OFF 🔇
Repeats continuously = blinking effect

⚡ Alternative (blink only text, not whole screen)


If you don’t want the whole screen to turn off, use cursor blinking instead:

[Link](); // blinking cursor


C++
But ⚠️ this only blinks the cursor, not the message.

🔥 Pro Tip (Cleaner Blink)


If you want smoother effect, adjust timing:

delay(500); // slower blink


C++

If you want next upgrade:


🔊 sync with melody
💡 add LED blinking too
📟 scrolling blinking text
just tell me 👍
user enters x and y using buttons one is for x and one is
for y and they are displayed in the lcd. the third button is
for sum that is also displayed in the lcd. Numbers are
random when buttons are clicked

Nice mini-project 🔥 this is like a random number generator + LCD + buttons + sum

🎯 Goal:
Button 1 → generate random X
Button 2 → generate random Y
Button 3 → display SUM (X + Y)
Show everything on LCD

✅ Arduino Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Buttons
const int btnX = 2;
const int btnY = 3;
const int btnSum = 4;

// Variables
int x = 0;
int y = 0;

void setup() {
[Link]();
[Link]();

pinMode(btnX, INPUT_PULLUP);
pinMode(btnY, INPUT_PULLUP);
pinMode(btnSum, INPUT_PULLUP);
randomSeed(analogRead(0)); // for randomness

[Link](0, 0);
[Link]("Press Buttons");
}

void loop() {
// Button X pressed
if (digitalRead(btnX) == LOW) {
x = random(1, 101); // random 1–100
[Link]();
[Link](0, 0);
[Link]("X = ");
[Link](x);
delay(300); // debounce
}

// Button Y pressed
if (digitalRead(btnY) == LOW) {
y = random(1, 101);
[Link]();
[Link](0, 0);
[Link]("Y = ");
[Link](y);
delay(300);
}

// Button SUM pressed


if (digitalRead(btnSum) == LOW) {
int sum = x + y;
[Link]();
[Link](0, 0);
[Link]("X=");
[Link](x);

[Link](0, 1);
[Link]("Y=");
[Link](y);

delay(1500);

[Link]();
[Link](0, 0);
[Link]("SUM = ");
[Link](sum);

delay(500);
}
}

🔌 Wiring Guide
📟 LCD (I2C)
VCC → 5V
GND → GND
SDA → A4
SCL → A5

🔘 Buttons
(Using INPUT_PULLUP, so simple wiring)
One leg of each button → Arduino pins:
X → Pin 2
Y → Pin 3
SUM → Pin 4
Other leg → GND

🧠 How it works
Press X → generates random number → displays X
Press Y → generates random number → displays Y
Press SUM → shows X, Y, then their sum

🔥 Optional Improvements
Keep X and Y visible always
Add buzzer when SUM is shown 🔊
Limit numbers (e.g., 1–10 for easier testing)
Add LED indicators 💡
If you want, I can make a clean UI version (X and Y always visible + sum updates
instantly)—that’s usually what professors like 👍

add reset

Nice upgrade 👍 — let’s add a RESET button that:


Sets X = 0
Sets Y = 0
Clears the LCD
Shows “Reset Done”

✅ Updated Code (with RESET button)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Buttons
const int btnX = 2;
const int btnY = 3;
const int btnSum = 4;
const int btnReset = 5; // NEW

// Variables
int x = 0;
int y = 0;

void setup() {
[Link]();
[Link]();

pinMode(btnX, INPUT_PULLUP);
pinMode(btnY, INPUT_PULLUP);
pinMode(btnSum, INPUT_PULLUP);
pinMode(btnReset, INPUT_PULLUP); // NEW

randomSeed(analogRead(0));

[Link](0, 0);
[Link]("Press Buttons");
}

void loop() {
// X button
if (digitalRead(btnX) == LOW) {
x = random(1, 101);
[Link]();
[Link](0, 0);
[Link]("X = ");
[Link](x);
delay(300);
}

// Y button
if (digitalRead(btnY) == LOW) {
y = random(1, 101);
[Link]();
[Link](0, 0);
[Link]("Y = ");
[Link](y);
delay(300);
}

// SUM button
if (digitalRead(btnSum) == LOW) {
int sum = x + y;

[Link]();
[Link](0, 0);
[Link]("X=");
[Link](x);

[Link](0, 1);
[Link]("Y=");
[Link](y);

delay(1500);

[Link]();
[Link](0, 0);
[Link]("SUM = ");
[Link](sum);
delay(500);
}

// RESET button 🔄
if (digitalRead(btnReset) == LOW) {
x = 0;
y = 0;

[Link]();
[Link](0, 0);
[Link]("Reset Done");

delay(1000);

[Link]();
[Link](0, 0);
[Link]("X=0 Y=0");
delay(300);
}
}

🔌 Additional Wiring
🔘 RESET Button
One leg → Pin 5
Other leg → GND

(Same as other buttons using INPUT_PULLUP )

🔁 What RESET does


Clears values
Shows Reset Done
Displays X=0 Y=0

🔥 Optional (Better UX)


Instead of clearing every time, you can:
Always show:

X=__ Y=__
SUM=__

Reset just updates values (cleaner UI 👀)

If you want, I can make that final polished version (looks like a calculator) — perfect for
demo 👍

TEMP SENSOR. LED TURNS ON IF HOT

Here’s a simple Arduino Uno setup:

🎯 Goal:

Read temperature from sensor


If it’s HOT → LED ON
If not → LED OFF

✅ Arduino Code (LM35 / TMP36)

const int tempPin = A0;


const int ledPin = 13;

float temperature;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
int sensorValue = analogRead(tempPin);

// Convert to voltage
float voltage = sensorValue * (5.0 / 1023.0);

// For LM35 (10mV per °C)


temperature = voltage * 100;

// Set threshold (e.g., 30°C = HOT)


if (temperature >= 30) {
digitalWrite(ledPin, HIGH); // LED ON
} else {
digitalWrite(ledPin, LOW); // LED OFF
}

delay(500);
}

🔌 Wiring Guide
🌡️ Temperature Sensor (LM35)
VCC → 5V
GND → GND
OUT → A0

💡 LED
Long leg → Pin 13
Short leg → GND (with resistor ~220Ω)

⚠️ If using TMP36 instead


Replace this line:

temperature = (voltage - 0.5) * 100;


C++

🧠 How it works
Sensor reads temperature
Arduino converts it to °C
If ≥ 30°C, LED turns ON
Otherwise OFF

🔥 Optional Improvements
Change threshold (e.g., 35°C for hotter condition)
Add buzzer 🔊
Display temp on LCD 📟
Use multiple LEDs (Normal / Warning / Hot)

If you want, I can combine this with your LCD project (show temp + LED warning) 👍

ULTRASONIC SENSOR. BUZZ IF THERE IS OBSTACLE.

Here’s a simple Arduino Uno project:

🎯 Goal:

Use ultrasonic sensor (HC-SR04)


If an object is near → buzzer ON
If far → buzzer OFF

✅ Arduino Code

const int trigPin = 6;


const int echoPin = 7;
const int buzzerPin = 8;
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}

void loop() {
// Send pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo
duration = pulseIn(echoPin, HIGH);

// Convert to distance (cm)


distance = duration * 0.034 / 2;

// If object is within 10 cm
if (distance > 0 && distance <= 10) {
digitalWrite(buzzerPin, HIGH); // BUZZ
} else {
digitalWrite(buzzerPin, LOW); // SILENT
}

delay(200);
}

🔌 Wiring Guide
📡 Ultrasonic Sensor (HC-SR04)
VCC → 5V
GND → GND
TRIG → Pin 6
ECHO → Pin 7

🔊 Buzzer
→ Pin 8
– → GND

🧠 How it works
Sensor sends sound wave
Measures return time ( duration )
Converts to distance
If ≤ 10 cm → obstacle detected → buzzer ON

🔥 Optional Upgrades
🔊 Faster beeping when closer (cool effect)

if (distance <= 20) {


int delayTime = distance * 20; // closer = faster beep

digitalWrite(buzzerPin, HIGH);
delay(100);
digitalWrite(buzzerPin, LOW);
delay(delayTime);
}

📟 Add LCD display (distance shown)


💡 Add LED indicator

If you want, I can combine this with your previous LCD + buzzer system (distance +
warning display) 👍
6 LEDs that turn on and off and
buzzer at the same time (with button)

the leds light up in patterns and at the same time with the
buzzer

Perfect! You want a button-controlled LED + buzzer light show, where:


6 LEDs turn on/off in patterns
Buzzer sounds in sync
Starts when button is pressed

Here’s a simple Arduino Uno code for that:

✅ Arduino Code

const int ledPins[6] = {2, 3, 4, 5, 6, 7}; // 6 LEDs


const int buzzerPin = 8; // Buzzer
const int buttonPin = 9; // Start button

void setup() {
for (int i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Button uses internal pull-up
}

void loop() {
// Wait for button press
if (digitalRead(buttonPin) == LOW) {
// Example pattern 1: LEDs chase forward
for (int i = 0; i < 6; i++) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);
}

// Example pattern 2: LEDs chase backward


for (int i = 5; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);
}

// Example pattern 3: Blink all together


for (int j = 0; j < 3; j++) {
for (int i = 0; i < 6; i++) digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);
delay(300);
for (int i = 0; i < 6; i++) digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);
delay(300);
}
}
}

🔌 Wiring Guide
LEDs
6 LEDs → Pins 2–7
Short leg → GND
Use 220Ω resistor with each LED

Buzzer
→ Pin 8
– → GND

Button
One leg → Pin 9
Other leg → GND
Using INPUT_PULLUP , so no external resistor needed
🔁 How it works
1. Waits for button press
2. Runs LED pattern 1 with buzzer
3. Runs LED pattern 2 with buzzer
4. Blinks all LEDs 3 times with buzzer

Everything is synchronized with the buzzer

🔥 Optional Improvements
Make speed faster for more excitement
Add random patterns each press
Include more complex buzzer tones like a melody
Use for a mini “light + sound show” project

If you want, I can make a version where the LED patterns loop continuously with buzzer
until you press another button to stop — that looks really cool in demos 🔥

Do you want me to do that version?

the buzzer should also "blink" at the same time with led

Ah! Got it ✅ — you want the buzzer to act like an LED, i.e., turn ON/OFF exactly in sync
with each LED in the pattern.

Here’s a clean version:

✅ Arduino Code (Buzzer Blinks with LEDs)


const int ledPins[6] = {2, 3, 4, 5, 6, 7}; // 6 LEDs
const int buzzerPin = 8; // Buzzer
const int buttonPin = 9; // Start button

void setup() {
for (int i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // button uses internal pull-up
}

void loop() {
// Wait for button press
if (digitalRead(buttonPin) == LOW) {

// --- Pattern 1: LED chase forward with buzzer ---


for (int i = 0; i < 6; i++) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH); // buzzer ON
delay(200);
digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW); // buzzer OFF
delay(50);
}

// --- Pattern 2: LED chase backward with buzzer ---


for (int i = 5; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);
delay(50);
}

// --- Pattern 3: All LEDs blink together with buzzer ---


for (int j = 0; j < 3; j++) {
for (int i = 0; i < 6; i++) digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);
delay(300);
for (int i = 0; i < 6; i++) digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);
delay(300);
}
}
}

🔌 Wiring
LEDs
Pins 2–7 → LEDs (with 220Ω resistor to GND)

Buzzer
→ Pin 8
– → GND

Button
Pin 9 → one leg of button
Other leg → GND

🔁 How it works
When button is pressed, LEDs light in patterns
Buzzer turns ON/OFF exactly with each LED
Buzzer “blinks” with LEDs, synchronized

💡 Optional upgrades:
Faster or slower patterns (adjust delay )
Add more patterns
Play a short melody where each note matches LED blink

If you want, I can make a super polished version where LED patterns + buzzer repeat
continuously until you press the button again to stop, like a mini light show 🎆.

Do you want me to do that version?


6 LED that turns on and off
alternately (LED 1 buzz 1, LED 2 buzz
2, turns off the other)

when the first buzzer and led turns on the second led and
buzzer turns off and vice versa

Ah! Now I understand clearly — you want a “ping-pong” effect”:


6 LEDs in alternating pairs
Each LED turns ON with its own buzzer
The other LEDs and buzzers turn OFF at the same time
Works continuously or with a button trigger

Here’s a clean Arduino example:

✅ Arduino Code (Alternating LEDs + Buzzers)

const int ledPins[6] = {2, 3, 4, 5, 6, 7}; // 6 LEDs


const int buzzerPins[6] = {8, 9, 10, 11, 12, 13}; // 6 buzzers (or single buzzer
const int buttonPin = A0; // start button

void setup() {
for (int i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buzzerPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP); // button
}

void loop() {
if (digitalRead(buttonPin) == LOW) {
// Alternating LEDs with corresponding buzzers
for (int i = 0; i < 6; i++) {
// Turn ON current LED + buzzer
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPins[i], HIGH);

// Turn OFF all other LEDs + buzzers


for (int j = 0; j < 6; j++) {
if (j != i) {
digitalWrite(ledPins[j], LOW);
digitalWrite(buzzerPins[j], LOW);
}
}

delay(300); // time each LED/buzzer stays on


}
}
}

🔌 Wiring
LEDs: Pins 2–7 → LEDs with 220Ω resistor → GND
Buzzers: Pins 8–13 → Buzzers → GND
If you have only one buzzer, connect it to one pin and replace buzzerPins[i]
with that pin, it will “blink” with the active LED.
Button: Pin A0 → Button → GND

⚡ How it works
1. Press button → sequence starts
2. LED 1 + buzzer 1 ON, others OFF
3. LED 2 + buzzer 2 ON, others OFF
4. Continues for all 6 LEDs
5. Creates alternating “ping-pong” effect

💡 Optional Improvements
Make it loop continuously
Use single buzzer instead of 6 (simpler)
Adjust delay for faster/slower effect
Add button to stop/start
If you want, I can make a single-buzzer version, where only one buzzer blinks with the
active LED, which is much simpler and still looks like a ping-pong light show 🔥.

Do you want me to do that?

no this is a new prompt

Ah! Got it — this is a new project prompt. Let’s design it clearly:

Goal:

6 LEDs turn on/off alternately


LED 1 + buzzer 1 turns on → LED 2 + buzzer 2 turns off
Then LED 2 + buzzer 2 turns on → LED 1 + buzzer 1 turns off
Continues in a ping-pong fashion

We can use 1 button to start the sequence, or make it continuous automatically. I’ll make a
simple Arduino Uno code with 1 buzzer shared for simplicity (blinks with active LED).

✅ Arduino Code (6 LEDs Alternating with Buzzer)

const int ledPins[6] = {2, 3, 4, 5, 6, 7}; // 6 LEDs


const int buzzerPin = 8; // single buzzer
const int buttonPin = 9; // start button

void setup() {
for (int i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // button uses internal pull-up
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
// Ping-pong effect: LED 1 & 2, LED 3 & 4, LED 5 & 6
for (int i = 0; i < 6; i++) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);

// Turn OFF all other LEDs


for (int j = 0; j < 6; j++) {
if (j != i) digitalWrite(ledPins[j], LOW);
}

delay(300); // time LED + buzzer stay ON

digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);
delay(100); // short off period
}
}
}

🔌 Wiring
LEDs: Pins 2–7 → LED → 220Ω resistor → GND
Buzzer: Pin 8 → buzzer → GND
Button: Pin 9 → button → GND (using INPUT_PULLUP)

⚡ How It Works
1. Press button → sequence starts
2. LED 1 ON → buzzer ON, others OFF
3. LED 2 ON → buzzer ON, others OFF
4. Continues for all 6 LEDs
5. Creates alternating ping-pong pattern with buzzer

If you want, I can make a more advanced version where:


LED 1 + buzzer 1 alternates with LED 2 + buzzer 2 specifically (pairwise)
All 6 LEDs loop continuously
No button required, fully automated

Do you want me to do that version?

no buttons are used

Ah! Perfect — no buttons this time ✅

Goal (fully automated):

6 LEDs turn on and off alternately in sequence


Buzzer turns ON exactly when its corresponding LED is ON
When one LED + buzzer is ON, all others are OFF
Runs continuously without any button

✅ Arduino Code (Automatic Ping-Pong with Buzzer)

const int ledPins[6] = {2, 3, 4, 5, 6, 7}; // 6 LEDs


const int buzzerPin = 8; // Single buzzer synced with active LE
const int delayTime = 300; // Time LED + buzzer stays ON

void setup() {
for (int i = 0; i < 6; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
}

void loop() {
// Loop forward (LED 1 → LED 6)
for (int i = 0; i < 6; i++) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);

// Turn OFF all other LEDs


for (int j = 0; j < 6; j++) {
if (j != i) digitalWrite(ledPins[j], LOW);
}

delay(delayTime); // ON duration

digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);

delay(100); // short OFF period before next


}

// Loop backward (LED 6 → LED 1)


for (int i = 5; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
digitalWrite(buzzerPin, HIGH);

// Turn OFF all other LEDs


for (int j = 0; j < 6; j++) {
if (j != i) digitalWrite(ledPins[j], LOW);
}

delay(delayTime);
digitalWrite(ledPins[i], LOW);
digitalWrite(buzzerPin, LOW);

delay(100);
}
}

🔌 Wiring
LEDs: Pins 2–7 → LED → 220Ω resistor → GND
Buzzer: Pin 8 → buzzer → GND

⚡ How It Works
1. LEDs turn on one by one in sequence
2. Buzzer blinks exactly with the active LED
3. When LED + buzzer ON → all others OFF
4. After reaching LED 6 → sequence reverses (ping-pong)
5. Runs continuously, fully automated

💡 Optional Enhancements:
Adjust delayTime to make the sequence faster or slower
Use two buzzers for alternate LED pairs
Add different tones for each LED using tone(buzzerPin, frequency)

If you want, I can make a version where each LED has its own unique buzzer tone, fully
automated, which looks like a mini light & sound show 🔥.

Do you want me to do that?

6 LED that turns on and off


alternately (LED 1 buzz 1, LED 2 buzz
2, turns off the other)

when led 1 turns on the first buzzer turns on and when the
led 2 lights up the second buzzer turns on and when led 3
lights up the first buzzer turns on and so on until led 6.
Note that when buzzer one is on the second is off and
when second is on the first one is off

Ah! Now I understand perfectly — you want a 6-LED alternating pattern with 2 buzzers,
where the buzzers toggle in sync with the LEDs:

LED 1 → Buzzer 1 ON, Buzzer 2 OFF


LED 2 → Buzzer 2 ON, Buzzer 1 OFF
LED 3 → Buzzer 1 ON, Buzzer 2 OFF
LED 4 → Buzzer 2 ON, Buzzer 1 OFF
… continues up to LED 6
Only one LED ON at a time
Buzzer 1 and Buzzer 2 alternate accordingly

Here’s a fully automatic Arduino implementation:

✅ Arduino Code (6 LEDs + 2 alternating buzzers)

C++

const int ledPins[6] = {2, 3, 4, 5, 6, 7}; // 6 LEDs


const int buzzer1 = 8; // Buzzer 1
const int buzzer2 = 9; // Buzzer 2
const int delayTime = 300; // Time each LED + buzzer stays ON

void setup() {
for (int i = 0; i < 6; i++) pinMode(ledPins[i], OUTPUT);
pinMode(buzzer1, OUTPUT);
pinMode(buzzer2, OUTPUT);
}

void loop() {
for (int i = 0; i < 6; i++) {
// Turn ON the current LED
digitalWrite(ledPins[i], HIGH);

// Alternate buzzers
if (i % 2 == 0) { // LEDs 1,3,5 → Buzzer 1 ON
digitalWrite(buzzer1, HIGH);
digitalWrite(buzzer2, LOW);
} else { // LEDs 2,4,6 → Buzzer 2 ON
digitalWrite(buzzer1, LOW);
digitalWrite(buzzer2, HIGH);
}

// Turn OFF other LEDs


for (int j = 0; j < 6; j++) {
if (j != i) digitalWrite(ledPins[j], LOW);
}

delay(delayTime); // Keep LED + buzzer ON


digitalWrite(ledPins[i], LOW);
digitalWrite(buzzer1, LOW);
digitalWrite(buzzer2, LOW);

delay(100); // short pause before next LED


}
}

🔌 Wiring
LEDs: Pins 2–7 → LEDs → 220Ω resistors → GND
Buzzer 1: Pin 8 → +, GND → –
Buzzer 2: Pin 9 → +, GND → –

⚡ How it works
1. Only one LED is ON at a time
2. Buzzer 1 and Buzzer 2 alternate with each LED
LED 1,3,5 → Buzzer 1 ON
LED 2,4,6 → Buzzer 2 ON
3. All other LEDs are OFF
4. Fully automatic ping-pong sequence repeats forever

💡 Optional Enhancements:
Use tone() to make different buzzer pitches for each LED
Adjust delayTime for faster or slower sequence
Make it bidirectional ping-pong (LED 6 → LED 1) for smoother effect

If you want, I can make a version with different tones for buzzer 1 and buzzer 2, so each
LED sounds a different note 🎵, which is super cool for demos.

Do you want me to do that?

You might also like