Input devices (Mar 27)


Assignment
   Measure something: add a sensor to a microcontroller board that you've made and read it



This week I decided to use a sensor of light level control. The name of this sensor is photoresistor or light dependent resistor (LDR). The LDR is a resistor whose resistance decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. The Attiny can not measure changes in resistance, but it can read analog voltage values​​. I will create a voltage divider between one constant resistance and one LDR. When change the LDR resistance, I will be able to read the voltage variations, following Ohm's law V = R * I.

To do all this, I will programme an analog input of Atinny, the A0. This read value is compared with a constant value and discriminates when is dark or light to turn on or turn of the led.

Video of testing LDR sensor






This is a Arduino code example. 
/*
  Analog Input
 Analog input by reading an analog sensor on analog pin A0 and
 turning on and off a light emitting diode(LED) connected to digital pin 7.r

Resistor 10k connected to VCC and A0
 LDR
connected to A0 and GND  
    Created by Rubén Saguar    */ int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 7; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int const sensorRef = 900; void setup() {   // declare the ledPin as an OUTPUT:   pinMode(ledPin, OUTPUT); } void loop() {   // read the value from the sensor:   sensorValue = analogRead(sensorPin);   if (sensorValue<=sensorRef)   {     // turn the ledPin on     digitalWrite(ledPin, HIGH);   }   else   {     // turn the ledPin off:     digitalWrite(ledPin, LOW);   }    }