Input devices

The assignment for this week was add a sensor to a microcontroller board that had made and we had to read it. I have used my board HelloWorld on Electronics design (Feb 27). When I designed this board I thought to add more devices. For this reason, I've left a connector with 3 pins: +5 V, a ground and a free entry (PA2).

The sensor that I have used is an Analog Distance Sensor IR.

The model is SHARP 2Y0A21

First I've checked the sensor with the Arduino board. I've made a simple connector to connect the power and the analog input (A0) of Arduino.



I've used the example program that offers Arduino for analog inputs. I've successfully programed and verified that the sensor is right!



After that, I've connected the sensor in my HelloWorld board

My "HelloWorld" board with a distance sensor:



I've programmed my board using the analog input pin 2 (sensor) and pin 7 (led).

This is the program using Arduino IDE:

/*
 Analog Input - HelloWorld board made by Juan Ranera in FabLab 2013
 Demonstrates analog input by reading an analog sensor on analog pin 2 and
 turning on and off a light emitting diode(LED)  connected to digital pin 7. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 
 Created by Juan Ranera
 April 2013
  
 */

int sensorPin = 2;    // 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

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  // declare the sensorPin as an INPUT:
  pinMode(sensorPin, INPUT);    
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin)/10;    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                  
}


After compiling it, I've programmed my board using my FabISP