0% found this document useful (0 votes)
11 views

All Simplified Arduino IOT Codes

The document contains various simplified Arduino sketches for controlling LEDs, including blinking, responding to serial commands, and implementing logic gates with buttons. It also includes sketches for mathematical operations like squaring and finding square roots, as well as controlling an RGB LED with potentiometers and using a PIR motion sensor. Each sketch demonstrates basic programming concepts and hardware interactions in Arduino.
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)
11 views

All Simplified Arduino IOT Codes

The document contains various simplified Arduino sketches for controlling LEDs, including blinking, responding to serial commands, and implementing logic gates with buttons. It also includes sketches for mathematical operations like squaring and finding square roots, as well as controlling an RGB LED with potentiometers and using a PIR motion sensor. Each sketch demonstrates basic programming concepts and hardware interactions in Arduino.
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/ 12

1.

LED Blinking (Simplified)

int led = 13;

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

void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
2. LED Based on Counter (Simplified)

int green = 13;


int yellow = 12;
int red = 11;
int count = 0;

void setup() {
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
Serial.begin(9600);
}

void loop() {
count++;
Serial.println(count);

if (count <= 100) {


digitalWrite(green, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
} else if (count <= 200) {
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
} else {
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
}

if (count > 250) count = 0;


delay(1000);
}
3. Serial Command for LEDs (Simplified)

const int green = 8;


const int yellow = 9;
const int red = 10;

void setup() {
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (Serial.available()) {
char cmd = Serial.read();
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);

if (cmd == 'b') {
for (int i = 0; i < 5; i++) {
digitalWrite(green, HIGH);
delay(500);
digitalWrite(green, LOW);
delay(500);
}
} else if (cmd == 'g') digitalWrite(green, HIGH);
else if (cmd == 'y') digitalWrite(yellow, HIGH);
else if (cmd == 'r') digitalWrite(red, HIGH);
}
}
4.1 AND Gate with Buttons

int btn1 = 2;
int btn2 = 3;
int led = 13;

void setup() {
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(led, OUTPUT);
}

void loop() {
if (digitalRead(btn1) && digitalRead(btn2))
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
}
4.2 OR Gate with Buttons

int btn1 = 2;
int btn2 = 3;
int led = 13;

void setup() {
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(led, OUTPUT);
}

void loop() {
if (digitalRead(btn1) || digitalRead(btn2))
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
}
4.3 XOR Gate with Buttons

int btn1 = 2;
int btn2 = 3;
int led = 13;

void setup() {
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(led, OUTPUT);
}

void loop() {
int a = digitalRead(btn1);
int b = digitalRead(btn2);
if (a != b)
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
}
5. Square of a Number (Simplified)

void setup() {
Serial.begin(9600);
Serial.println("Enter a number to square:");
}

void loop() {
if (Serial.available()) {
int n = Serial.parseInt();
Serial.print("Square of ");
Serial.print(n);
Serial.print(" is ");
Serial.println(n * n);
delay(100);
}
}
6. Square Root (Simplified)

void setup() {
Serial.begin(9600);
Serial.println("Enter a number to find square root:");
}

void loop() {
if (Serial.available()) {
float n = Serial.parseFloat();
if (n >= 0) {
Serial.print("Square root of ");
Serial.print(n);
Serial.print(" is ");
Serial.println(sqrt(n));
} else {
Serial.println("Enter non-negative number.");
}
delay(100);
}
}
7. Cube of a Number (Simplified)

void setup() {
Serial.begin(9600);
Serial.println("Enter a number to find cube:");
}

void loop() {
if (Serial.available()) {
float n = Serial.parseFloat();
Serial.print("Cube of ");
Serial.print(n);
Serial.print(" is ");
Serial.println(n * n * n);
delay(100);
}
}
8. Cube Root (Simplified)

void setup() {
Serial.begin(9600);
Serial.println("Enter a number to find cube root:");
}

void loop() {
if (Serial.available()) {
float n = Serial.parseFloat();
if (n >= 0) {
Serial.print("Cube root of ");
Serial.print(n);
Serial.print(" is ");
Serial.println(pow(n, 1.0/3.0));
} else {
Serial.println("Enter non-negative number.");
}
delay(100);
}
}
9. RGB LED with 3 Potentiometers (Simplified)

const int redPin = 9;


const int greenPin = 10;
const int bluePin = 11;
const int potR = A0;
const int potG = A1;
const int potB = A2;

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop() {
analogWrite(redPin, map(analogRead(potR), 0, 1023, 0, 255));
analogWrite(greenPin, map(analogRead(potG), 0, 1023, 0, 255));
analogWrite(bluePin, map(analogRead(potB), 0, 1023, 0, 255));
delay(50);
}
10. PIR Motion Sensor (Simplified)

const int pirPin = A0;


const int led = 13;

void setup() {
pinMode(pirPin, INPUT);
pinMode(led, OUTPUT);
}

void loop() {
if (digitalRead(pirPin)) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
delay(100);
}

You might also like