2: Arduino Tutorial- Reading sensor data! (Super Easy)

Enough of blinking LEDs. Now, lets move on to something more interesting, like conditionally turning on an LED. The aim of the tutorial is to familiarise you with the process of using almost any sensor with Arduino.

Let us first get the Arduino to do something, and then understand why we did, what. 

Step 1: Hook up the following circuit. You will need:
  1.  1 x Breadboard
  2.  1 x Arduino (any will do)
  3.  1 x 1kΩ resistor (or any other value between 330Ω  and 1kΩ )
  4.  1x LDR (light-dependent-resistor)
  5.  1x LED (any colour)
And then upload the following code to you Arduino to get it up and running!


int ledPin=6;
int sensorPin=A1;
int sensorVal;
void setup() {
  // put your setup code here, to run once:
  pinMode(sensorPin, INPUT); //initialize ldr as input
  pinMode(ledPin, OUTPUT); // initialize led as output
  Serial.begin(9600); // start serial communication
}

void loop() {
  // put your main code here, to run repeatedly:
sensorVal=analogRead(sensorPin); //read sensorvalue from arduino
Serial.println(sensorVal); // print sensorvalue
delay(100); // wait for a tenth of a second

}



Comments