Simple Sensors

Simple Sensors


Inspiration #


Setup #

Firstly, let’s do some inventory. Check that your kit has all of the items shown here.

You should have one of these Pico Under Plates in your Kit.

Pico Under Plate

Plug your Pico on top of the Under Plate. Make sure that you connect it in the correct orientation. USB connector side is marked in the bottomo of the board. This board allows us an easier access to the pins and it has labels for most of them, so you don’t have to go look at the diagram all the time.

Pico Under Plate Connected

One side of the board has a reset button, you can press it to have the board reset and run the program from the beginning.

Pico Under Plate Reset Button

The other side has a connector that we will use a lot for easily connecting digital sensors (starting from tomorrow). This connector is used by Adafruit for their sensors and they call it STEMMA QT. Another company called Sparkfun calls it Qwiic connector. I will generally refer to it as the Qwiic connector in this course. More and more components have started to include these connectors, so this Under Plate we use is a very handy addition to our Pico boards.

Pico Under Plate Qwiic


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)

DIY Textile Sensors #

You can also make your own sensors using conductive textiles, yarns and other materials.

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 #


Example 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 ADC0 (GP26)
  • Potentiometer connected to pin ADC1 (GP27)

One digital output:

  • LED connected to pin GP15

Example Schematic image

Example Breadboard image

Codes #

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);
  // make the pin where LED is connected to an output
  pinMode(15, OUTPUT);
}

void loop() {
  lightValue = analogRead(26);
  pot = analogRead(27);
  if (lightValue < pot) {
    analogWrite(15, b);
    trigger = 1023;
  } else {
    analogWrite(15, 0);
    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);
}

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.

This code is for the Arduino Uno boards.

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);
  // make the pin where LED is connected to an output
  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.