You are on page 1of 6

Antarmuka Sensor Pada Arduino

1. LDR Light Sensor

Source Code:
1
2
3 /*
LDR blinking rate sketch
4 Blink a LED with a rate based on light intensity upon an LDR
5 */
6 const int ledPin = 2; // LED connected to digital pin 2
7 const int ldrPin = 0; // connect LDR to analog input 0
void setup()
8 {
9 pinMode(ledPin, OUTPUT); // enable output on the led pin
10 Serial.begin(9600);
11}
12void loop()
{
13 int rate = analogRead(ldrPin); // read the analog input
14 Serial.println(rate);
15 digitalWrite(ledPin, HIGH); // set the LED on
16 delay(rate); // wait duration dependent on light level
17 digitalWrite(ledPin,
delay(rate);
LOW); // set the LED off
18}
19
20

2. LM35 Temperature Sensor


Source Code:
1
2
3 /*
LM35 sketch
4 Prints the temperature to the Serial Monitor
5 */
6 const int lm35Pin = 0; // LM35 connected to analog input pin 0
7 void setup()
8 {
Serial.begin(9600);
9 }
10void loop()
11{
12 int tempVal = analogRead(lm35Pin);
13 Serial.print(tempVal); Serial.print(" > ");
float millivolts = (tempVal / 1024.0) * 5000;
14 float celsius = millivolts / 10; // sensor output is 10mV / Celsius
15 Serial.print(celsius);
16 Serial.print(" degrees Celsius, ");
17 Serial.print( (celsius * 9)/ 5 + 32 ); // converts to fahrenheit
Serial.println(" degrees Fahrenheit");
18 delay(1000); // wait for one second
19}
20
21

3. Dimmer
Source Code:
1
2
3 /*
LDR dimmer sketch
4 Dim a LED based on light intensity upon an LDR
5 */
6 const int ledPin = 2; // LED connected to digital pin 2
7 const int ldrPin = 0; // connect LDR to analog input 0
void setup()
8 {
9 pinMode(ledPin, OUTPUT);
10 Serial.begin(9600);
11}
12void loop()
{
13 int ldrVal = analogRead(ldrPin);
14 Serial.println(ldrVal);
15 delay(200);
16 ldrVal = constrain(ldrVal, 20,150);
17 int ledLevel = map(ldrVal,20,150, 255,0);
analogWrite (ledPin, ledLevel);
18}
19
20

4. Temperature Indicator
Source Code:
1
2
3 /*
4 LM35 sketch
5 Turn on a LED if temperature is greater than a threshold
*/
6 const int lm35Pin = 0; // sensor connected to this analog pin
7 const int ledPin = 2; // digital output pin for LED
8 const int threshold = 25; // the degree that will turn on the LED
9 void setup()
10{ Serial.begin(9600);
11 pinMode(ledPin, OUTPUT);
12}
13void loop()
14{
int tempVal = analogRead(lm35Pin);
15 long celsius = (tempVal * 500L) /1024; // 10 mV per degree celcius
16 Serial.print(celsius);
17 Serial.print(" degrees Celsius: ");
18 if(celsius > threshold)
19 {
digitalWrite(ledPin, HIGH); // LED on
20 Serial.println("LED is ON");
21 }
22 else
23 {
digitalWrite(ledPin, LOW);
24 Serial.println("LED is OFF"); // LED off
25 }
26 delay(1000); // wait for one second
27}
28
29
30

5. PIR Motion Detector

Source Code:
1
2
3 /*
PIR sketch
4 Turn on a LED if motion is detected
5 */
6 const int ledPin = 9; // choose the pin for the LED
7 const int pirPin = 2; // choose the input pin for the PIR sensor
8 void setup()
{
9 pinMode(ledPin, OUTPUT); // declare LED as output
10 pinMode(pirPin, INPUT); // declare PIR as input
11}
12void loop()
13{ int pirVal = digitalRead(pirPin); // read input value
14 if (pirVal == HIGH) // check if the input is HIGH
15 {
16 digitalWrite(ledPin, HIGH); // turn LED on if motion is detected
17 delay(500);
digitalWrite(ledPin, LOW); // turn LED off
18 }
19}
20
21

6. IR Distance Sensor
Source Code:
1
2
3 /*
IR-distance sketch
4 Display distance of object from Sharp Distance Sensor 2Y0A02
5 (on Serial Monitor)
6 */
7 const int irPin = 0; // choose the pin for the IR distance sensor
float irVal, cm; //Must be of type float for pow()
8 void setup()
9 {
10 Serial.begin(9600);
11}
12void loop()
{
13 irVal = analogRead(irPin);
14 //inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
15 cm = 10650.08 * pow(irVal,-0.935) - 10;
16 delay(100);
17 Serial.print("Distance
Serial.println(cm);
(cm) : ");
18}
19
20
Kirimkan Ini lewat EmailBlogThis!Berbagi ke TwitterBerbagi ke Facebook

You might also like