0% found this document useful (0 votes)
0 views9 pages

Arduino Project

The document contains various Arduino code snippets for controlling LEDs, buttons, RGB lights, buzzers, gas sensors, servo motors, and LDR sensors. Each section provides a specific functionality, such as turning an LED on/off with a button press or changing the color of an RGB LED. The examples demonstrate basic input/output operations and sensor integrations with Arduino programming.

Uploaded by

cedod37508
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)
0 views9 pages

Arduino Project

The document contains various Arduino code snippets for controlling LEDs, buttons, RGB lights, buzzers, gas sensors, servo motors, and LDR sensors. Each section provides a specific functionality, such as turning an LED on/off with a button press or changing the color of an RGB LED. The examples demonstrate basic input/output operations and sensor integrations with Arduino programming.

Uploaded by

cedod37508
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
You are on page 1/ 9

Write a program to led with button.

1. Push button – Led on


2. Push button – led off

Code:
int led=11;
int btn=10;
int BUTTONstate=0;
void setup() {
pinMode(btn, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
BUTTONstate=digitalRead(btn);
if(BUTTONstate==HIGH)
{
digitalWrite(led, HIGH);

}
BUTTONstate=digitalRead(btn);
if(BUTTONstate==HIGH)
{
digitalWrite(led, LOW);
}
}
Just a simple slide-switch demo
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(5, INPUT_PULLUP);
}
void loop() {
digitalWrite(LED_BUILTIN, digitalRead((5)));
}
Buzzer with led light
int b=13;

int led=8;

void setup() {

pinMode(b,OUTPUT);

pinMode(led,OUTPUT);

void loop() {

tone(b,1000);

digitalWrite(led , HIGH);

delay(1000);

noTone(b);

digitalWrite(led, LOW);

delay(1000);

}
Simple button with led
int led=9;
int btn=8;
void setup() {
pinMode(9, OUTPUT);
pinMode(8, INPUT);
}
void loop() {
if (digitalRead(8)==1){
digitalWrite(9, HIGH);
}
else{
digitalWrite(9, LOW);
}
}
RGB LED LIGHT
int Rled=13;
int Gled=12;
Common Pin= Cathode
int Bled=11;
void setup() {
pinMode (Rled,OUTPUT);
pinMode (Gled,OUTPUT);
pinMode (Bled,OUTPUT);
}
void loop() {
digitalWrite (Rled,HIGH);
delay (1000);
digitalWrite (Rled,LOW);
digitalWrite (Gled,HIGH);
delay(1000);
digitalWrite (Gled,LOW);
digitalWrite (Bled,HIGH);
delay(1000);
digitalWrite (Bled,LOW);
delay (1000);
}
Interfacing RGB Led with Arduino
int redPin= 5; Common Pin= Cathode
int greenPin = 6;
int bluePin = 7;

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
setColor(255, 0, 0); // Red Color
delay(1000);
setColor(0, 255, 0); // Green Color
delay(1000);
setColor(0, 0, 255); // Blue Color
delay(1000);
setColor(255, 255, 255); // White Color
delay(1000);
setColor(170, 0, 255); // Purple Color
delay(1000);
setColor(127, 127, 127); // Light Blue
delay(1000);
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
MQ135 GAS SENSOR
int sensorValue;
int digitalValue;
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
pinMode(13, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
sensorValue = analogRead(0); // read analog input pin 0
digitalValue = digitalRead(2);
if (sensorValue > 400)
{
digitalWrite(13, HIGH);
}
else
digitalWrite(13, LOW);
Serial.println(sensorValue, DEC); // prints the value read
Serial.println(digitalValue, DEC);
delay(1000); // wait 100ms for next reading
}
SERVO MOTOR
#include <Servo.h>

Servo myServo; // Create servo object

void setup() {

myServo.attach(11); // Attach to pin D9

void loop() {

// Sweep from 0 to 180 degrees

for (int pos = 0; pos <= 180; pos++) {

myServo.write(pos); // Move to position

delay(15); // Wait for servo to reach position

// Sweep back from 180 to 0 degrees

for (int pos = 180; pos >= 0; pos--) {

myServo.write(pos);

delay(15);

}
LDR SENSOR WITH A LED LIGHT
const int ldrPin = A0; // LDR connected to analog pin A0

const int ledPin = 13; // LED connected to digital pin 13

int ldrValue = 0; // Store LDR reading

void setup() {

pinMode(ledPin, OUTPUT);

Serial.begin(9600); // For debugging

void loop() {

ldrValue = analogRead(ldrPin); // Read light level

Serial.println(ldrValue); // Print to Serial Monitor

if (ldrValue < 400) { // Adjust this threshold based on your lighting

digitalWrite(ledPin, HIGH); // Turn on LED when it's dark

} else {

digitalWrite(ledPin, LOW); // Turn off LED when it's bright

delay(500); // Wait a bit before next reading

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy