0% found this document useful (0 votes)
6 views10 pages

Arduino LED and Sensor Projects Guide

The document contains multiple Arduino programming examples demonstrating various functionalities such as controlling LEDs with switches, reading temperature and humidity from a DHT11 sensor, controlling a servo motor with a potentiometer, detecting objects with an IR sensor, and gradually controlling motor speed using PWM. Each example includes setup and loop functions with specific pin configurations and serial communication for monitoring. Additionally, there are diagrams referenced but not included in the text.

Uploaded by

habibganesh26
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)
6 views10 pages

Arduino LED and Sensor Projects Guide

The document contains multiple Arduino programming examples demonstrating various functionalities such as controlling LEDs with switches, reading temperature and humidity from a DHT11 sensor, controlling a servo motor with a potentiometer, detecting objects with an IR sensor, and gradually controlling motor speed using PWM. Each example includes setup and loop functions with specific pin configurations and serial communication for monitoring. Additionally, there are diagrams referenced but not included in the text.

Uploaded by

habibganesh26
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

Arduino Programming

/*simple program to glow led when switch is pressed*/


/*demonstrates the use of digital read and write */
/* for pinout refer to the manual*/
/*LEDS - pins 0, 1, 2, 4*/
/*LED will be ON when pin is LOW - inverse logic*/
/*SWITCH -10,11,12,13 */

void setup() {
// put your setup code here, to run once:
pinMode(0,OUTPUT); /*set LED pin as OUTPUT*/
pinMode(1,OUTPUT); /*set LED pin as OUTPUT*/
pinMode(2,OUTPUT); /*set LED pin as OUTPUT*/
pinMode(4,OUTPUT); /*set LED pin as OUTPUT*/

pinMode(10,INPUT); /*set switch pin as INPUT*/


pinMode(11,INPUT); /*set switch pin as INPUT*/
pinMode(12,INPUT); /*set switch pin as INPUT*/
pinMode(13,INPUT); /*set switch pin as INPUT*/
[Link](9600);
[Link]("LED ON");
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(4,HIGH);

/*check for key press and then make the LED ON*/
if(digitalRead(10)== 0)
{
digitalWrite(0,LOW);
[Link]("LED ON 1");
delay(500);
}

if(digitalRead(11)== 0)
{
digitalWrite(1,LOW);
[Link]("LED ON 2");
delay(1000);
}
if(digitalRead(12)== 0)
{
digitalWrite(2,LOW);
[Link]("LED ON 3");
delay(1000);
}

if(digitalRead(13)== 0)
{
digitalWrite(4,LOW);
[Link]("LED ON 4");
delay(1000);
}
}
Arduino Programming

#include <dht.h>

dht DHT;

#define DHT11_PIN A1

void setup()
{
[Link](9600);
[Link]("Humidity (%),\tTemperature (C)");
}

void loop()
{
// READ DATA
int chk = DHT.read11(DHT11_PIN);
// DISPLAY DATA
[Link]([Link], 1);
[Link]("\t");
[Link]([Link], 1);

delay(2000);
}
//
// END OF FILE
//

//
// END OF FILE
//
Arduino Programming

#include <Servo.h> // Include Servo library

Servo myServo; // Create servo object

const int potPin = A0; // Potentiometer connected to analog pin A0

int potValue = 0; // Variable to store analog value

int angle = 0; // Variable to store mapped servo angle

void setup() {

[Link](9); // Attach servo to digital pin 9 (PWM)

[Link](9600); // Start serial communication

void loop() {

potValue = analogRead(potPin); // Read analog value (0–1023)

angle = map(potValue, 0, 1023, 0, 180); // Map analog value to 0–180°

[Link](angle); // Rotate servo to the mapped angle

// Print values to Serial Monitor

[Link]("Potentiometer: ");

[Link](potValue);

[Link](" -> Servo Angle: ");

[Link](angle);

delay(15); // Small delay for smooth movement

}
Arduino Programming

void setup() {

// put your setup code here, to run once:

pinMode(A0, INPUT);//IR sensor to CN11

pinMode(0, OUTPUT);

pinMode(1, OUTPUT);

pinMode(2, OUTPUT);

pinMode(4, OUTPUT);

[Link](9600);

void loop() {

// put your main code here, to run repeatedly:

if(digitalRead(A0)==1)

[Link]("object not detected");

digitalWrite(0,HIGH);

digitalWrite(1,HIGH);

digitalWrite(2,HIGH);

digitalWrite(4,HIGH);

if(digitalRead(A0)==0)

[Link]("object detected");

digitalWrite(0,LOW);

digitalWrite(1,LOW);

digitalWrite(2,LOW);

digitalWrite(4,LOW);

} delay(500);
Arduino Programming

(A) Using PWM Ramp-Up (automatic speed increase):

const int motorPin = 9; // Motor connected via transistor to PWM pin D9

void setup() {

pinMode(motorPin, OUTPUT); // Set motorPin as output

void loop() {

// Gradually increase motor speed from 0 to 255

for (int speed = 0; speed <= 255; speed++) {

analogWrite(motorPin, speed); // Write PWM signal

delay(20); // Small delay for gradual speed up

delay(1000); // Keep motor at full speed for a while

// Gradually decrease motor speed from 255 to 0

for (int speed = 255; speed >= 0; speed--) {

analogWrite(motorPin, speed);

delay(20);

delay(1000); // Wait before restarting loop

}
Arduino Programming

#include <dht.h>

dht DHT;

#define DHT11_PIN A1

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

// Initialize PLX-DAQ
[Link]("CLEARDATA");
[Link]("LABEL,Date,Time,Humidity (%),Temperature (C)");
[Link]("RESETTIMER");
}

void loop()
{
// Read DHT11 sensor
int chk = DHT.read11(DHT11_PIN);

// Send data to PLX-DAQ


[Link]("DATA,DATE,TIME,");
[Link]([Link], 1);
[Link](",");
[Link]([Link], 1);

delay(2000); // log every 2 seconds


}
//
// END OF FILE
//
DIAGRAM 1

DIAGRAM 2
DIAGRAM 3

DIAGRAM 4
DIAGRAM 6

You might also like