Arduino Codes PDF

Summary

This document provides code examples for various Arduino projects, encompassing LED blinking, RGB LED control, servo motor operation, and sensor interactions such as PIR motion detection, ultrasonic range measurement, and temperature sensing. The code snippets illustrate practical applications of Arduino microcontrollers for controlling and interacting with electronic components and devices.

Full Transcript

LED blinking code using an Arduino Code:- int ledPin = 10; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } LED blinking code using an Arduino int ledPin1 = 9; int ledPin2 = 10; int ledPin3 = 11; v...

LED blinking code using an Arduino Code:- int ledPin = 10; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } LED blinking code using an Arduino int ledPin1 = 9; int ledPin2 = 10; int ledPin3 = 11; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } void loop() { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); delay(1000); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); delay(1000); } Series and Parallel Combinations RGB LED void setup() { pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); } void loop() { analogWrite(9, 255); analogWrite(10, 0); analogWrite(11, 0); delay(1000); analogWrite(9, 0); analogWrite(10, 255); analogWrite(11, 0); delay(1000); analogWrite(9, 0); analogWrite(10, 0); analogWrite(11, 255); delay(1000); analogWrite(9, 255); analogWrite(10, 255); analogWrite(11, 0); delay(1000); analogWrite(9, 0); analogWrite(10, 255); analogWrite(11, 255); delay(1000); analogWrite(9, 255); analogWrite(10, 0); analogWrite(11, 255); delay(1000); analogWrite(9, 252); analogWrite(10, 186); analogWrite(11, 3); delay(1000); analogWrite(9, 3); analogWrite(10, 244); analogWrite(11, 25); delay(1000); } Servo Motor #include Servo myServo; int servoPin = 9; void setup() { myServo.attach(servoPin); // Attach the servo to pin 9 } void loop() { // Move the servo to 0 degrees myServo.write(0); delay(1000); // Move the servo to 90 degrees myServo.write(90); delay(1000); // Move the servo to 180 degrees myServo.write(180); delay(1000); } PIR (Passive Infrared Sensor) int pirSensorPin = 2; int ledPin = 13; void setup() { Serial.begin(9600); pinMode(pirSensorPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); } void loop() { int motionDetected = digitalRead(pirSensorPin); if (motionDetected == HIGH) { digitalWrite(ledPin, HIGH); Serial.println("Motion detected!"); } else { digitalWrite(ledPin, LOW); Serial.println("No motion."); } delay(500); } UltraSonic Sensor // Define pins for HC-SR04 const int trigPin = 9; // Trigger Pin const int echoPin = 10; // Echo Pin void setup() { // Initialize serial communication for displaying the result Serial.begin(9600); // Set trigPin as OUTPUT and echoPin as INPUT pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // Ensure that the trigger pin is low digitalWrite(trigPin, LOW); delayMicroseconds(2); // Send a 10µs high pulse to trigger the sensor digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the time it takes for the echo to be received (in microseconds) long duration = pulseIn(echoPin, HIGH); // Calculate distance in cm (Speed of sound = 343 m/s or 0.0343 cm/µs) long distance = duration * 0.0343 / 2; // Print the distance on the serial monitor Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); // Wait before taking another reading delay(500); } Temperature Sensor Code // Define the pin for the temperature sensor int sensorPin = A0; void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate } void loop() { int sensorValue = analogRead(sensorPin); // Read the analog value from LM35 float temperature = (sensorValue * 5.0 * 100.0) / 1024.0; // Convert to Celsius Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" °C"); delay(1000); // Update every 1 second }

Use Quizgecko on...
Browser
Browser