Antarmuka Sensor Pada Arduino
1. LDR Light Sensor
Source Code:
/*
LDR blinking rate sketch
Blink a LED with a rate based on light intensity upon an LDR
*/
const int ledPin = 2; // LED connected to digital pin 2
const int ldrPin = 0; // connect LDR to analog input 0
void setup()
{
pinMode(ledPin, OUTPUT); // enable output on the led pin
Serial.begin(9600);
}
void loop()
{
int rate = analogRead(ldrPin); // read the analog input
Serial.println(rate);
digitalWrite(ledPin, HIGH); // set the LED on
delay(rate); // wait duration dependent on light level
digitalWrite(ledPin, LOW); // set the LED off
delay(rate);
}
2. LM35 Temperature Sensor
Source Code:
/*
LM35 sketch
Prints the temperature to the Serial Monitor
*/
const int lm35Pin = 0; // LM35 connected to analog input pin 0
void setup()
{
Serial.begin(9600);
}
void loop()
{
int tempVal = analogRead(lm35Pin);
Serial.print(tempVal); Serial.print(" > ");
float millivolts = (tempVal / 1024.0) * 5000;
float celsius = millivolts / 10; // sensor output is 10mV / Celsius
Serial.print(celsius);
Serial.print(" degrees Celsius, ");
Serial.print( (celsius * 9)/ 5 + 32 ); // converts to fahrenheit
Serial.println(" degrees Fahrenheit");
delay(1000); // wait for one second
}
3. Dimmer
Source Code:
/*
LDR dimmer sketch
Dim a LED based on light intensity upon an LDR
*/
const int ledPin = 2; // LED connected to digital pin 2
const int ldrPin = 0; // connect LDR to analog input 0
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int ldrVal = analogRead(ldrPin);
Serial.println(ldrVal);
delay(200);
ldrVal = constrain(ldrVal, 20,150);
int ledLevel = map(ldrVal,20,150, 255,0);
analogWrite (ledPin, ledLevel);
}
4. Temperature Indicator
Source Code:
/*
LM35 sketch
Turn on a LED if temperature is greater than a threshold
*/
const int lm35Pin = 0; // sensor connected to this analog pin
const int ledPin = 2; // digital output pin for LED
const int threshold = 25; // the degree that will turn on the LED
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int tempVal = analogRead(lm35Pin);
long celsius = (tempVal * 500L) /1024; // 10 mV per degree celcius
Serial.print(celsius);
Serial.print(" degrees Celsius: ");
if(celsius > threshold)
{
digitalWrite(ledPin, HIGH); // LED on
Serial.println("LED is ON");
}
else
{
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF"); // LED off
}
delay(1000); // wait for one second
}
5. PIR Motion Detector
Source Code:
/*
PIR sketch
Turn on a LED if motion is detected
*/
const int ledPin = 9; // choose the pin for the LED
const int pirPin = 2; // choose the input pin for the PIR sensor
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare PIR as input
}
void loop()
{
int pirVal = digitalRead(pirPin); // read input value
if (pirVal == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if motion is detected
delay(500);
digitalWrite(ledPin, LOW); // turn LED off
}
}
6. IR Distance Sensor
Source Code:
/*
IR-distance sketch
Display distance of object from Sharp Distance Sensor 2Y0A02
(on Serial Monitor)
*/
const int irPin = 0; // choose the pin for the IR distance sensor
float irVal, cm; //Must be of type float for pow()
void setup()
{
Serial.begin(9600);
}
void loop()
{
irVal = analogRead(irPin);
//inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
cm = 10650.08 * pow(irVal,-0.935) - 10;
delay(100);
Serial.print("Distance (cm) : ");
Serial.println(cm);
}






0 Comment:
Posting Komentar