Sensors
Table of Contents
Sensing the Environment
digitalRead(pin)
returns state of a digital pinananlogRead(pin)
returns the analog voltage on a pinTypes of Sensors
Acoustics, sound, vibration
Automotive, transportation
Chemical
Electric current, electric potential, magnetic, radio
Flow, fluid velocity
Lonizing radiation, subatomic particles
Navigation instruments
Position, angle, displacement, distance, speed, acceleration
Optical, light, imaging, photon
Pressure
force, density, level
Thermal, head, temperature
Proximity, presence
%%html
<iframe src="https://www.youtube.com/embed/W0qupcTo6GU"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
Lab 01: Detecting Motion (Passive Infrared Detectors)
%%html
<iframe src="https://www.youtube.com/embed/CzgA8UfLD-Q"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
/*
PIR sketch
a Passive Infrared motion sensor connected to pin 2
lights the LED on pin 13
*/
const int ledPin = 8; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for the PIR sensor)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop() {
int val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED on if motion detected
delay(500);
}
else if (val = LOW) {
digitalWrite(ledPin, LOW); // turn LED
delay(500);
}
}
Lab 02: Photoresistor Light Dependent Resistor (LDR)
Sense light levels, measure those levels with the Arduino and print the measurements to the Serial port
LDR is a variable resistor (varying with light).
So we need to convert the varying resistance to a voltage that the Arduino can measure.
We do that by using the LDR and a resistor in a voltate divider circuit.
int lightPin = A0; // define a pin for Photo resistor
void setup() {
Serial.begin(9600); // Begin serial communcation
}
void loop() {
Serial.println(analogRead(lightPin)); // Write the value of the photoresistor to the serial monitor.
delay(10); // short delay for faster response to light.
}
%%html
<iframe src="https://www.youtube.com/embed/CZk0A6zHr2M"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
Lab 03: Flex Sensor
Flex sensor to measure flex !
A flex sensor uses carbon on a strip of plastic to act like a variable resistor
A āvoltage dividerā again to detect this change in resistance.
The sensor bends in one direction
he more it bends, the higher the resistance gets
It has a range from about 10K ohm to 35K ohm.
%%html
<iframe src="https://www.youtube.com/embed/AvFJ3HMoJf4"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
int flexPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int flexRead = analogRead(flexPin);
Serial.println(flexRead);
}
Lab 04: Measuring Temperature
%%html
<iframe src="https://www.youtube.com/embed/34sRdI23AYE"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
#include <math.h>
double ThermistorF(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0/RawADC - 1)));
Temp = 1/(0.001129148 + (0.000234125 + (0.0000000876741*Temp*Temp))*Temp);
Temp = Temp - 273.15;
Temp = (Temp*9.0)/5.0 + 32.0;
return Temp;
}
double ThermistorC(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0/RawADC - 1)));
Temp = 1/(0.001129148 + (0.000234125 + (0.0000000876741*Temp*Temp))*Temp);
Temp = Temp - 273.15;
return Temp;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int valF, valC;
double tempF, tempC, val;
val = analogRead(0);
tempF = ThermistorF(val);
tempC = ThermistorC(val);
Serial.print("Temperature = ");
Serial.print(tempF);
Serial.print(" F; ");
Serial.print(tempC);
Serial.println(" C");
delay(1000);
}
%%html
<iframe src="https://www.youtube.com/embed/-_XkGju35MI"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
Lab 05: Use TMP36 Temp Sensor
Another way to measure temperature
lecture material from: https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor
%%html
<iframe src="https://www.youtube.com/embed/hWdoF6ETxvI"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup() {
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
}
void loop() { // run over and over again
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
// print out the voltage
Serial.print(voltage);
Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(temperatureC);
Serial.println(" degrees C");
// now convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.println(" degrees F");
delay(1000); //waiting a second
}
Lab 06: Measuring Distance
You want to measure the distance to something, such as a wall or someone walking toward the Arduino.
#define trigPin 2
#define echoPin 4
long distance;
long duration;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(){
// Give a short LOW pulse beforehand
// to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
Lab 07: Acceleration from Motion, Shock, or Vibration
%%html
<iframe src="https://www.youtube.com/embed/V-9KvUVBXSc"
width="560" height="315" frameborder="0" allowfullscreen></iframe>
const int xpin = A0;
const int ypin = A1;
const int zpin = A2;
int x = 0;
int y = 0;
int z = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
analogReference(EXTERNAL);
Serial.print("X = " );
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print("Y = " );
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print("Z = ");
Serial.println(analogRead(zpin));
delay(500);
}
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')