Simple Sensors

Simple Sensors


Inspiration #

Variable resistors #

Many sensors are just variable resistors that change their resistance value based on some external input (light, temperature, force etc.). The Arduino is not able to read the change in resistance directly, but we can convert that resistance change into a change in voltage using a voltage divider.

Voltage Divider | Converting resistance to voltage #

If you connect two resistors in series as in the image below, the voltage read from Vout depends on the ratio of the two resistors. Using a variable resistor instead of a fixed-value one as one of the resistors will create a circuit where the voltage read between the resistors will vary depending on the conditions that change the resistance. For example, using an LDR (light dependent resistor) as R1 allows you to read the change of the light level.

Read the Wikipedia article or check this tutorial from Sparkfun, if you want to learn how to calculate the values.

Voltage Divider

Light Dependent Resistor #

See the LDR tutorial to learn how to use the photoresistor (LDR) we have in the kit.

Other variable resistors #

  • Potentiometers
  • Thermistor
  • Stretch Sensor
  • Force Sensitive Resistor (FSR)

Sensors with analog output #

There are also many sensors that have an analog output. Analog in this case meaning that they are directly outputting a varying level of voltage. Some of the analog sensors we have in the Mechatronics workshops:

  • Accelerometer ADXL335
  • Sharp IR Distance Sensors

Sensors with simple digital output #

The most basic sensor with a digital output is just a switch. We covered those last week. Some other sensors that just send out a simple on/off signal:

  • PIR Motion sensors

pulseIn() #

Some sensors send their data as a short pulse that has a specific duration. You can read those using the pulseIn() function in Arduino. Sensors that work like this:

Complex digital output #

There are many other digital communication protocols that are often used with modern sensors. We will cover these in the next class.


Working with analog signals #


Examples done in class #

The project we are going to build in class implements a simple threshold for deciding when an LED should turn on.

Circuit #

This example uses two analog inputs:

  • Light sensor connected to pin A0
  • Potentiometer connected to pin A1

One digital output:

  • LED connected to pin 9

Example Breadboard image

Code #

The code does the following

  • Read the light level using the light sensor and store the value to a variable called lightValue
  • Read the potentiometer value and store the value to a variable called pot
  • If lightValue is less than pot, turn on the LED with the brigthness value b
  • Otherwise, turn the LED off
  • Print all the values using the serial port for feedback. Variable called trigger is used to print out a value of 0 or 1023 depending on the state of the LED.
int lightValue;
int pot;
// b is used to store the brightness of the LED
int b = 255;
// trigger variable is used to visualize the output on the plotter
int trigger = 0;

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  lightValue = analogRead(A0);
  pot = analogRead(A1);
  if (lightValue < pot) {
    analogWrite(9, b);
    trigger = 1023;
  } else {
    analogWrite(9, LOW);
    trigger = 0;
  }

  // 0 and 1023 are printed to make sure the plotter doesn't autoscale
  Serial.println("Min:0, Max:1023");

  Serial.print(lightValue);
  Serial.print(" ");
  Serial.print(pot);
  Serial.print(" ");
  Serial.println(trigger);
  delay(10);
}

References #

There are various sites that list out sensors:

I find it useful to browse some online shops that sell sensor boards to get an understanding what kind of things are possible.