Sunteți pe pagina 1din 37

Universidad Nacional Mayor de San Marcos

Facultad de Ingeniera de Sistemas e Informtica


Arquitectura de Computadoras
Laboratorio 9 Sensores

I. Sensor de sonido analgico


This small breakout board couples a small electret microphone with a 100x opamp to
amplify the sounds of voice, door knocks, etc loud enough to be picked up by a
microcontroller's Analog to Digital converter.
Pin Definition
The definition of sound sensor pin is
1. Signal Output
2. GND
3. Power

El cdigo es:
void setup()
{
Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop()
{
int val;
val=analogRead(0); //connect mic sensor to Analog 0
Serial.println(val,DEC);//print the sound value to serial
delay(300);
}

II. Sensor de movimiento infrarrojo digital


This is a simple to use motion sensor. Power it up and wait 1-2 seconds for the sensor to
get a snapshot of the still room. If anything moves after that period, the 'alarm' pin will go
low.

Specification
Type: Digital
Supply Voltage:35V
Current:50A
Working temperature:0 70
Output level(HIGH):4V
Output level(LOW):0.4V
Detect angle:110 Degree
Detect distance:7 meters
Size:28mm36mm
Weight:25g
Pin Definition

+:Power 5V
-:GND
OUT:Digital Signal Out

For DFRobot Version

1:Digital Signal Out


2:VCC (5V)
3:GND

El cdigo de uso es:


/*
plug infrared motion sensor on digital port 3. If signal is detected, digitalRead
will be high. Switch the resistor backward to adjust the sensitivity as you want.
*/
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(3));
delay(100);
}

III. Distance Measuring Sensor Unit


The Sharp distance sensors are a popular choice for many projects that require accurate
distance measurements. This IR sensor is more economical than sonar rangefinders, yet it
provides much better performance than other IR alternatives. Interfacing to most
microcontrollers is straightforward: the single analog output can be connected to an
analog-to-digital converter for taking distance measurements, or the output can be
connected to a comparator for threshold detection. The detection range of this version is
approximately 10 cm to 80 cm (4" to 32"); a plot of distance versus output voltage is
shown below.
The GP2Y0A21 uses a 3-pin JST connector that connects to our 3-pin JST cable for Sharp
distance sensors (not included) as shown in the upper picture on the right. It is also simple
to solder three wires to the sensor where the connector pins are mounted (see the lower
picture to the right). When looking at the back, the three connections from left to right
are power, ground, and the output signal.
NOTE: All Sharp sensors from DFRobot are shipped with JST cable.

Specification

operating voltage: 4.5 V to 5.5 V


average current consumption: 30 mA (typical)
distance measuring range: 10 cm to 80 cm (4" to 32")
output type: analog voltage
output voltage differential over distance range: 1.9 V (typical)
response time: 38 10 ms
package size: 29.513.013.5 mm (1.160.50.53")
weight: 3.5 g (0.12 oz)

El cdigo seria :

int IRpin = 1;
void setup() {
Serial.begin(9600);
}

// analog pin for reading the IR sensor


// start the serial port

void loop() {
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if
running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10);
// worked out from graph 65 = theretical
distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance);
// print the distance
delay(100);
// arbitary wait time.
}
Looking at our graph above from the datasheet, this converted value is of no use to us so
we need to convert this value back to the true analog value. How? We take the voltage
rating of the power supply and divide by 1024 to give us a value per step. So for instance:
5v/1024 = 0.0048828125
We take this value and multiply by what the sensor sends back to get our voltage reading.
The next stage is to work out an equation from the graph on the data sheet to get the
theoretical distance from the voltage reading, if we look at the graph between 20 and
150cm you can see that its exponential.
To get our distance on the graph I came up with:
1/Volts * 65
I got 65 by taking the distance on the graph and dividing that by 1/Volts. So now we know
what to multiply our voltage results by to get the distance. This is fine if we just had one
value to read or if this was a linear graph, in that the change in voltage was always the
same amount of distance.
For the exponential change we have to turn our value now into an exponent, a fancy way
of saying x to the power of y. So our formula for distance from voltage reading is now
something like:

distance = (Volts x)*65.


Since our graph is a decaying exponential the value of x will be a fraction to write this as
a exponent we have to use a minus number e.g. -1. The exponent -1 is the equivalent of
the fraction 1/2 which is 0.5 as a decimal.
Now through trial and error I programmed this into my Arduino and changed the
exponent until my readings became accurate(ish) I started at -1 and by co-incidence the
next value I tried was -1.1 which is 11/10 as a fraction, 0.65 as a decimal and oddly enough
1/100th of my distance ration, 65!

IV. Termistores
The DFRobot LM35 Linear Temperature Sensor is based on the semiconductor LM35
temperature sensor. The DFRobot LM35 Linear Temperature Sensor can be used to detect
ambient air temperature. This sensor is produced by National Semiconductor Corporation
and offers a functional range between -40 degree Celsius to 150 degree Celsius. Sensitivity
is 10mV per degree Celsius. The output voltage is proportional to the temperature.
Pin Definition
The definition of gray-scale sensor pin is
1. Signal Output
2. GND
3. Power

Codigo utilizado para la prueba:


void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
}
void loop()
{
int val;
int dat;
val=analogRead(0);//Connect LM35 on Analog 0
dat=(500*val)>>10;
Serial.print("Tep:"); //Display the temperature on Serial monitor

Serial.print(dat);
Serial.println("C");
delay(500);
}
La salida generada es:

Control de Motor por temperatura

En el caso del motor elctrico, este se conecta en GND y el otro pin a la lnea 10 digital.

El cdigo para su funcionamiento es:

int led=5;
int ntc=0;
int motor=10;
int medida=0;
int nivel=59; //variable que guarda el lmite de temperatura al que se activa el ventilador
valor anterior 700
void setup(){
pinMode(led,OUTPUT);
pinMode(motor,OUTPUT);

Serial.begin(9600);
}
void monitoriza(){ //procedimiento que enva al puerto serie, para ser ledo en el monitor,
Serial.print("La medida es ...");
Serial.println(medida);
//Serial.print();
delay(1000); //para evitar saturar el puerto serie
}
void loop(){
medida=analogRead(ntc);
monitoriza();
if(medida>nivel){ //si la seal del sensor supera el nivel marcado:
digitalWrite(led,HIGH); //se enciende un aviso luminoso
digitalWrite(motor,HIGH); //arranca el motor
}
else{ // si la seal est por debajo del nivel marcado
digitalWrite(led,LOW);
digitalWrite(motor,LOW); // el motor se para
}
}

V. Uso de Relays

El Rel es un dispositivo, que funciona como un interruptor controlado por un circuito elctrico en
el que, por medio de un electroimn, se acciona un juego de uno o varios contactos que permiten
abrir o cerrar otros circuitos elctricos independientes.

De esta forma, podremos separar dos circuitos que funcionen con voltajes diferentes. Uno
a 5V (Arduino) y otro a 220V (la bombilla). Para este ejemplo, se utilizara un circuito de
220V con un mximo de 10A.

Conmutacin entre leds


Diagrama esquemtico

Codigo:
int ledPin = 2; // *********** CHANGE TO PIN 2 ************
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

Encendido de foco de luz


El diagrama esquemtico para este circuito seria:

El cdigo para este circuito seria:


int relayPin = 8; // PIN al que va conectado el rel
void setup(){
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH); // ENCENDIDO
delay(2000);
digitalWrite(relayPin, LOW); // APAGADO
delay(2000);
}

Encendido de foco por LDR y Relay


Se utilizara el siguiente circuito:

Para este control de foco por LDR se esta considerando lo siguiente:


1. Un pin del LDR va a ingresar a la entrada A0.
2. La salida del transistor que es parte del control del relay ira al pin D8.

El cdigo correspondiente ser:


int relayPin = 8; // PIN al que va conectado el rel
int lightPin = 0; //the analog pin the LDR is connected to
void setup(){
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int threshold = 300;
int valor= analogRead(lightPin);

Serial.println(valor);
if(valor < threshold){
digitalWrite(relayPin, HIGH); // ENCENDIDO
}else{
digitalWrite(relayPin, LOW); // APAGADO
}
}

Sensor de Temperatura y de Distancia

Codigo para el TMP36 solo:


const unsigned int BAUD_RATE = 9600;
const unsigned int TEMP_SENSOR_PIN = 0;
const float SUPPLY_VOLTAGE = 5.0;

void setup(){
Serial.begin(BAUD_RATE);
}

void loop(){
Serial.print(get_temperature());
Serial.println(" C");
delay(1000);
}

const float get_temperature(){


const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);
const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;
return (voltage * 1000 - 500) / 10;

b. Los dos sensores juntos:


const unsigned int TEMP_SENSOR_PIN = 0;
const float SUPPLY_VOLTAGE = 5.0;
const unsigned int PING_SENSOR_IO_PIN = 7;
const float SENSOR_GAP = 0.2;
const unsigned int BAUD_RATE = 9600;

float current_temperature = 0.0;


unsigned long last_measurement = millis();
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_RATE);
}

void loop() {

// put your main code here, to run repeatedly:


unsigned long current_millis = millis();
if (abs(current_millis - last_measurement) >= 1000){
current_temperature = get_temperature();
last_measurement = current_millis;
}
Serial.print(scaled_value(current_temperature));
Serial.print(",");
const unsigned long duration = measure_distance();
Serial.println(scaled_value(microseconds_to_cm(duration)));
delay(100);
}

long scaled_value(const float value){


float round_offset = value < 0 ? -0.5 : 0.5;
return (long)(value * 100 + round_offset);
}

const float get_temperature(){


const int sensor_voltage = analogRead(TEMP_SENSOR_PIN);
const float voltage = sensor_voltage * SUPPLY_VOLTAGE / 1024;
return (voltage * 1000 - 500) / 10;
}

const float microseconds_per_cm(){


return 1 / ((331.5 + (0.6 * current_temperature)) / 10000);

const float sensor_offset(){


return SENSOR_GAP * microseconds_per_cm() * 2;
}

const float microseconds_to_cm(const unsigned long microseconds){


const float net_distance = max(0, microseconds - sensor_offset());
return net_distance / microseconds_per_cm() / 2;
}

const unsigned long measure_distance(){


pinMode(PING_SENSOR_IO_PIN, OUTPUT);
digitalWrite(PING_SENSOR_IO_PIN, LOW);
delayMicroseconds(2);
digitalWrite(PING_SENSOR_IO_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(PING_SENSOR_IO_PIN, LOW);
pinMode(PING_SENSOR_IO_PIN, INPUT);
return pulseIn(PING_SENSOR_IO_PIN, HIGH);
}

Acelerometro

Se usara el acelermetro del Kit, DFRobot Triple Axis Accelerometer MMA7361

Introduction
The MMA7361 from Freescale is a very nice sensor with easy analog interface. The
MMA7361 is a 3.3V part and outputs an analog voltage for each of the three outputs. This
voltage is in ratio to the measured acceleration and to the supply voltage (ratiometric). It
has selectable sensitivity by dip switch. You will need some extra hardware to convert this
analog signal to a usable digital one. The Arduino is really good option for it. This break
board is especially designed for Arduino which has 3 JST connector that can be easily plug
into our IO/Sensor expansion board.
Specification

Voltage:3.3-8V
Selectable sensitivity:15g/6g
Low power:500A @ measurement mode3A @standby
High sensivity: 800 mV/g @ 1.5g
Interface:Analog Output
Low pass filter
Size:23x26mm
Weight: 5 gram
Programa de prueba:
const unsigned int X_AXIS_PIN = 2;
const unsigned int Y_AXIS_PIN = 1;

const unsigned int Z_AXIS_PIN = 0;


const unsigned int BAUD_RATE = 9600;
int min_x, min_y, min_z;
int max_x, max_y, max_z;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_RATE);
min_x = min_y = min_z = 1000;
max_x = max_y = max_z = -1000;
}
void loop() {
// put your main code here, to run repeatedly:
const int x = analogRead(X_AXIS_PIN);
const int y = analogRead(Y_AXIS_PIN);
const int z = analogRead(Z_AXIS_PIN);
min_x = min(x, min_x); max_x = max(x, max_x);
min_y = min(y, min_y); max_y = max(y, max_y);
min_z = min(z, min_z); max_z = max(z, max_z);
Serial.print("x(");
Serial.print(min_x);
Serial.print("/");
Serial.print(max_x);
Serial.print("), y(");
Serial.print(min_y);
Serial.print("/");
Serial.print(max_y);
Serial.print("), z(");
Serial.print(min_z);
Serial.print("/");
Serial.print(max_z);
Serial.println(")");
}

Otro ejemplo de uso seria:

const unsigned int X_AXIS_PIN = 2;


const unsigned int Y_AXIS_PIN = 1;
const unsigned int Z_AXIS_PIN = 0;
const unsigned int NUM_AXES = 3;
const unsigned int PINS[NUM_AXES] ={
X_AXIS_PIN, Y_AXIS_PIN, Z_AXIS_PIN };
const unsigned int BUFFER_SIZE = 16;
const unsigned int BAUD_RATE = 9600;
int buffer[NUM_AXES][BUFFER_SIZE];
int buffer_pos[NUM_AXES] = { 0 };
int get_axis(const int axis){

delay(1);
buffer[axis][buffer_pos[axis]] = analogRead(PINS[axis]);
buffer_pos[axis] = (buffer_pos[axis] + 1) % BUFFER_SIZE;
long sum = 0;
for (int i = 0; i < BUFFER_SIZE; i++)
sum += buffer[axis][i];
return round(sum / BUFFER_SIZE);
}
int get_x() { return get_axis(0);}
int get_y() { return get_axis(1);}
int get_z() { return get_axis(2);}
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_RATE);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(get_x());
Serial.print(" ");
Serial.print(get_y());
Serial.print(" ");
Serial.println(get_z());
}

Implementando para control de juego:

Sensor de Humedad (Moisture Sensor)


This moisture sensor can read the amount of moisture present in the soil
surrounding it. It's a low tech sensor, but ideal for monitoring an urban garden,
or your pet plant's water level. This is a must have tool for a connected garden!
This sensor uses the two probes to pass current through the soil, and then it
reads that resistance to get the moisture level. More water makes the soil
conduct electricity more easily (less resistance), while dry soil conducts
electricity poorly (more resistance).
It will be helpful to remind you to water your indoor plants or to monitor the
soil moisture in your garden.

Specification
Power supply: 3.3v or 5v
Output voltage signal: 0~4.2v
Current: 35mA
Pin definition:
1-Analog output(Blue wire)
2-GND(Black wire)
3-Power(Red wire)
Size: 60x20x5mm
Value range:
1. 0 ~300 : dry soil
2. 300~700 : humid soil
3. 700~950 : in water

Shipping List
Moisture sensor(1 unit)
Analog Sensor Cable (1 unit)

Connection Diagram

Ejemplo
/*
#
#
#
#
#

Example code for the moisture sensor


Editor
: Lauren
Date
: 13.01.2012
Version
: 1.0
Connect the sensor to the A0(Analog 0) pin on the Arduino board

# the sensor value description

# 0 ~300
# 300~700
# 700~950
*/

dry soil
humid soil
in water

void setup(){
Serial.begin(57600);
}
void loop(){
Serial.print("Moisture Sensor Value:");
Serial.println(analogRead(0));
delay(100);
}

Sensor de gas analgico (Analog Alcohol Sensor)

The analog gas sensor - MQ3 is suitable for detecting alcohol, this sensor can be used in a
Breathalyser.It has a high sensitivity to alcohol and small sensitivity to Benzine. The sensitivity can
be adjusted by the potentiometer.

Specification

Power supply needs: 5V


Interface type: Analog
Pin Definition: 1-Output 2-GND 3-VCC
High sensitivity to alcohol and small sensitivity to Benzine
Fast response and High sensitivity
Stable and long life
Simple drive circuit
Size: 40x20mm

Pin Definition
1. Signal Output
2. GND
3. Power

Circuito implementado

Sample Code

///Arduino Sample Code


void setup()
{
Serial.begin(9600); //Set serial baud rate to 9600 bps
}
void loop()
{
int val;

val=analogRead(0);Read Gas value from analog 0


Serial.println(val,DEC);//Print the value to serial port
delay(100);
}

S-ar putea să vă placă și