Sensors


By Prof. Seungchul Lee
http://iai.postech.ac.kr/
Industrial AI Lab at POSTECH

Table of Contents

1. SensorsĀ¶

  • Allow the microcontroller to receive information about the environment
    • How bright is it? (analog)
    • How loud is it? (analog)
    • How humid is it? (analog)
    • Is the button being pressed? (digital)
  • Perform operations based on the state of the environment
    • Trun on a light if it's dark out
    • Voice-controlled operation

Sensing the Environment

  • Microcontrollers sense voltage
    • digitalRead(pin) returns state of a digital pin
    • ananlogRead(pin) returns the analog voltage on a pin
  • Sensor logic must convert an environmental effect into voltage

Types 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

InĀ [5]:
%%html
<iframe src="https://www.youtube.com/embed/W0qupcTo6GU" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>

2. Types of SensorsĀ¶

2.1. Reading a PushbuttonĀ¶

  • Make a pin high when the button is pressed, and low when it is not pressed

  • pull-up circuit

  • pull-down circuit



Lab 01: Detecting Motion (Passive Infrared Detectors)

- Some sensors control voltage directly
- Signal is pulled high when motion is detected
- Open-collector: signal floats without motion

InĀ [6]:
%%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);
  }
}

2.2. Resistive SensorsĀ¶

- Many sensors change resistance
$\quad$- Photoresistor, thermistor, flex resistor, etc.
- Connect sensor in a voltage divider

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.

- As brightness increases, resistance decreases
- Resistance = 10K Ohms, voltage = 2.5 Volts
- Resistance = 5K Ohms, voltage = 3.3 Volts



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.
}
InĀ [7]:
%%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.

InĀ [8]:
%%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

  • Thermistors are simple, inexpensive, and accurate components that make it easy to get temperature readings
InĀ [9]:
%%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);
}
InĀ [10]:
%%html
<iframe src="https://www.youtube.com/embed/-_XkGju35MI" 
width="560" height="315" frameborder="0" allowfullscreen></iframe>

Lab 05: Use TMP36 Temp Sensor

InĀ [11]:
%%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
}

2.3. Measure DistanceĀ¶


Lab 06: Measuring Distance

You want to measure the distance to something, such as a wall or someone walking toward the Arduino.

  • Ultrasonic sensor

#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);
}

2.4. Analog AccelerometerĀ¶

Lab 07: Acceleration from Motion, Shock, or Vibration

InĀ [14]:
%%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);
}

2.5. Other Voltage-Controlling SensorsĀ¶

- Accelerometer reports acceleration in 3 dimensions
- Gyroscope reports angular velocity in 2 dimensions
- Will be used later in a class
InĀ [15]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')