Sensors

A sensor is a device that measures a physical quantity and converts it into an electric signal. They can be analog (working like potentiometers) or digital (working like switches). Some examples of analog sensors.

Testing a sensor
To see if a sensor works we can read its analog value to control an analog output: an LED in Pulse Width Modulation (PWM) variable brightness.
We send the value to serial as well (open the serial monitor).
You can connect a piezo speaker to have an audio feedback (+pin to ~9 on the Arduino, -pin to ground).

int sensorPin = A0;   //sensor on the Arduino analog 0
int speakerPin = 9;   //speaker on Arduino pin 9
int ledPin = 13;      //led on Arduino pin ~13
 
//minimum and maximum values detected by the sensor
//it may depend on the sensor and circuit
//you can test them empirically
int minValue = 0;      
int maxValue = 1023;
 
void setup()
{
  //setting the digital 
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop()
{
    int sensorValue = analogRead(sensorPin);    
 
    //for the LED in PWM (light intensity) the value I read needs to be mapped to 0-255
    int output = map(sensorValue, minValue, maxValue, 0, 255);
 
    //send it to the pin
    analogWrite(ledPin, output);
 
    //for the sound we need to map it between 100 and 5000 to be audible
    int value = map(sensorValue, minValue, maxValue, 100, 5000);
    tone(speakerPin, value);
    delay(10);
    noTone(speakerPin);
    delay(10);
 
    //print it to the console
    Serial.print("\t Analog input = ");      
    Serial.println(sensorValue);
 
    Serial.print("\t Analog output = ");      
    Serial.println(output);
 
}

Force sensitive resistor

force_sensitive_res

Flex sensor
This works in a similar way. The code is calibrated for a 2.2 inches Flex sensor and a 10K ohm resistor.
If you have different sensors and resistors just read the value at rest and the value at maximum stress (which probably won’t be 0 and 1024) and map accordingly.

flex_sensor

// Sensor pin - GND
// Sensor pin - Analog In 0, with 10K resistor to +5V
int LedPin = 13;    // LED connected to analog pin 13
int SensorPin = A0;    // Sensor connected to analog pin A0
 
void setup()
{
    // initialize serial communications
    Serial.begin(9600); 
}
 
void loop()
{
    // read the voltage from the voltage divider (sensor plus resistor)
    int sensor = analogRead(SensorPin);
 
    int output = map(sensor, 730, 900, 0, 255);
 
    // print out the result
    Serial.print("analog input: ");
    Serial.print(sensor,DEC);
    Serial.print(" output: ");
    Serial.println(output,DEC);
 
    analogWrite(LedPin, output);      
 
    // pause before taking the next reading
    delay(100); 
}

Photoresistor
photoresistor

Photocell are basically resistors that change its resistive value (in ohms Ω) depending on how much light is shining onto the squiggly face.
You can use in a laser tripwire.

Stretch Sensor
A piece of conductive rubber cord whose resistance chances according to the stretch ratio.

stretchsensor

It actually looks more like this
conductivecord2mm_MED

This code is calibrated for the circuit above, a piece of cord of about 3 inches + a 10Kohm resistor. If you need a longer cord you can read the analog value and map accordingly.

// Sensor pin - GND
// Sensor pin - Analog In 0, with 10K resistor to +5V
int LedPin = 13;    // LED connected to analog pin 13
int SensorPin = A0;    // Sensor connected to analog pin A0
 
 
void setup()
{
    // initialize serial communications
    Serial.begin(9600); 
}
 
void loop()
{
    // read the voltage from the voltage divider (sensor plus resistor)
    int sensor = analogRead(SensorPin);
 
    int output = map(sensor, 30, 70, 0, 255);
 
    // print out the result
    Serial.print("analog input: ");
    Serial.print(sensor,DEC);
    Serial.print(" output: ");
    Serial.println(output,DEC);
 
    analogWrite(LedPin, output);      
 
    // pause before taking the next reading
    delay(100); 
}

PIR Motion sensor
PIR-Arduino_hookup
PIR or Passive Infrared is a common method of motion detection that measure changes in heat to signal the change. The basic model is that they take IR (heat) images on 2 sensors at different times, when they differ, they know something has changed. The one we have is this one which signals LOW when the motion is detected. Since it’s an ON/OFF kind of signal you have to use a digital input.

The colors of the wires can be confusing so ignore them. Looking at the unit from the top, the middle wire is ground, the left-most wire is the alarm, and the right-most is the power.
Full tutorial here.

This the code that reads from the sensor and plays a sound on a piezo speaker.

int speakerPin = 9; //connect the + pin of the speaker to arduino output 9 and the - to ground
int sensorPin = 2;
int duration = 500; //speaker frequency
int frequency = 1000; //time between reads
 
void setup(){
 Serial.begin(9600); 
 pinMode(sensorPin, INPUT);
 pinMode(speakerPin, OUTPUT);
}
 
void loop(){
  int motion = digitalRead(sensorPin);
 
  if(motion == LOW){ //was motion detected
 
    Serial.println("Motion Detected"); 
    tone(speakerPin, frequency);
    delay(duration);
    noTone(speakerPin);
 
  }
  delay(duration);
}

IR Distance sensor
IR distance sensor
Wiring is straightforward but check the datasheet to see which pin goes to ground, +5 and which one is carrying the sensed value.

You’ll have to figure out what is the minimum and maximum distance that the sensor can read and map it to the minValue and maxValue you can detect empirically.

int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
int analogOutPin = 13; // Analog output pin that the LED is attached to
 
int sensorValue = 0;        // value read from the sensor
int outputValue = 0;        // value output to the PWM (analog out)
 
int minValue =   50;         //minimum value (depends on the sensor try and see)
int maxValue =   600;        //max value
 
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}
 
void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, minValue, maxValue, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           
 
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   
 
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(20);                     
}

Triple Axis Accelerometer Breakout
Similar to what you have on the wii or iPhone. Senses acceleration in 3 axis, if calibrated detecting the inclination on a plane.
Follow the instructions here

Circuit:

Check the table for the Arduino connections (called UNO/Duemilanove which are similar models):

Download the library AcceleroMMA7361 from here

Example code:

#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int x;
int y;
int z;
void setup()
{
 Serial.begin(9600);
 accelero.begin(13, 12, 11, 10, A0, A1, A2);
 accelero.setARefVoltage(5); //sets the AREF voltage to 3.3V
 accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
 accelero.calibrate();
}
void loop()
{
 x = accelero.getXAccel();
 y = accelero.getYAccel();
 z = accelero.getZAccel();
 Serial.print("\nx: ");
 Serial.print(x);
 Serial.print(" \ty: ");
 Serial.print(y);
 Serial.print(" \tz: ");
 Serial.print(z);
 Serial.print("\tG*10^-2");
 delay(500); //make it readable
}

Microphone
mic
These microphones come with a breakout board for amplification and they work better when the VCC pin is wired to the 3.3V pin on your Arduino.
This is a step by step tutorial, very straightforward.
The code to obtain a clean reading of the mic activity is here:

    /****************************************
    Example Sound Level Sketch for the
    Adafruit Microphone Amplifier
    ****************************************/
 
    const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
    unsigned int sample;
 
    void setup()
    {
    Serial.begin(9600);
    }
 
 
    void loop()
    {
    unsigned long startMillis= millis(); // Start of sample window
    unsigned int peakToPeak = 0; // peak-to-peak level
 
    unsigned int signalMax = 0;
    unsigned int signalMin = 1024;
 
    // collect data for 50 mS
    while (millis() - startMillis < sampleWindow)
    {
    sample = analogRead(0);
    if (sample < 1024) // toss out spurious readings
    {
    if (sample > signalMax)
    {
    signalMax = sample; // save just the max levels
    }
    else if (sample < signalMin)
    {
    signalMin = sample; // save just the min levels
    }
    }
    }
    peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
    double volts = (peakToPeak * 3.3) / 1024; // convert to volts
 
    Serial.println(volts);
    }
Posted in

Post a comment