1 of 34

INPUT DEVICES RECITATION

HTMaA 2021 - Week 7

2 of 34

What we will talk about

  • What sort of things would you like to measure?
  • How to read a sensor
    • Reading analog inputs with an ADC
      • Example
    • Serial communication - more options, configurations
      • How to read and approach a sensor's datasheet
      • Example
  • Post processing
    • Noise
    • Filtering
  • Some basics of digital signal processing

3 of 34

What we will talk about

  • What sort of things would you like to measure?
  • How to read a sensor
    • Reading analog inputs with an ADC
      • Example
    • Serial communication - more options, configurations
      • How to read and approach a sensor's datasheet
      • Example
  • Post processing
    • Noise
    • Filtering
  • Some basics of digital signal processing

4 of 34

What sort of things would you like to measure?

Analog to Digital Converter (ADC) - Voltage* (an ADC is in the loop for most digital measurement). Here's how an ADC works.

Remember that you can mix and match! A light sensor can measure proximity and a hall effect sensor can measure displacement, etc.

5 of 34

What sort of things would you like to measure?

Cedric made this excellent HTMSTMaA writeup on sensors

6 of 34

What sort of things would you like to measure?

  • Analog vs. Digital (binary) inputs

7 of 34

What sort of things would you like to measure?

  • This recitation will use a hall effect sensor as an example, but everything here can be applied to inputs in general!

  • We are measuring magnetic flux (strength of a magnetic field)
  • Commonly used as displacement sensors

8 of 34

Measuring an Analog Input

  • Let's look at some raw hall effect sensor data from the DRV5055

9 of 34

Measuring an Analog Input

  • An Arduino Uno has a 10 bit ADC. This means that we can resolve 2^10 = 1024 unique values for our measurement. Or 1/1024*100 =
  • Imagine you are measuring angle of a volume knob on a stereo:
    • 2^8 = 256 possible values, so 360/1024 => 1.40 deg
    • 2^10 = 1024 possible values, so 360/1024 => 0.35 deg
    • 2^16 = 65536 possible values, so 360/65536 => 0.0055 deg

  • To measure at higher resolution, you typically need to take measurements slower
  • Some resolution will always be lost to noise
  • You probably won't be using the full range of your signal

So in practice, for a 10 bit resolution measurement, half might be lost to your signal range, and 10 counts to noise, leaving 1024*(0.5)/(10) = 51.2 true resolution.

10 of 34

Measuring an Analog Input

  • We are measuring a physical parameter represented as a voltage, then reading a value in bits, then back to some physical parameter
    • Do we need to calibrate with another sensor? Or will the datasheet just tell us?
    • Where is our zero point? Is our signal signed? If so where are we centered?

11 of 34

Measuring an Analog Input

  • Let's start by taring our measurement
  • We can take a measurement while it's in some known configuration
  • For a linear signal you technically only need to do this in two places

12 of 34

Measuring an Analog Input

  • Hysteresis is the dependance of our measurement on past measurements (usually from the immediate past)
  • Let's see if we can see some in real time

13 of 34

Filtering

  • Noise (unwanted disturbances) is easiest to see at steady state.
  • To address this, let's start by just averaging a bunch of measurements together
    • Moving Average Filter
    • Arduino Example

14 of 34

Filtering

  • We were able to reduce noise from 4 counts to 1 count!
  • Also oversampling is effectively getting us more resolution! (at the cost of bandwidth)

15 of 34

Filtering

  • What's the price of filtering?
  • We introduce a phase shift in our measurement

16 of 34

Filtering

  • We have to start to be careful if we are measuring a signal that contains a frequency close to (or above) our sampling frequency!
  • Nyquist limit is ½ the sampling rate

17 of 34

Filtering

  • We can actually engineer more sophisticated filters that target specific ranges of frequencies
  • If we overfilter we introduce a phase shift, so we have an incentive to design our filters carefully

18 of 34

Low Pass Filter

  • Step 1 - Determine your cutoff frequency and sampling frequency
    • Cuttoff frequency we want is 100Hz
    • We will sample at 1kHz using a delay(1)
  • Step 2 - Calculate your two coefficients
    • RC = 1 / (2pi * 100) = 0.00159
    • deltaT = 1/1000Hz = 0.001
  • Step 3 - Plug it into your loop!

19 of 34

High Pass + Band Pass Filters

  • What if we want to get rid of low frequency noise (60Hz is a common example)? Or only pass data at a narrow range of frequencies?

High Pass

Band Pass

20 of 34

Hardware Filtering

  • Remember that filters can (and often are) be implemented in hardware!
  • Useful for DAC approximation with PWM
  • Our capacitor is 0.1uF
  • Our resistor is 15kΩ
  • RC is 0.0015 (same as last time)
  • F_cutoff ~= 1kHz

Teensy PWMs at either 3.6kHz or 4.5kHz

What do we see on the scope?

21 of 34

Linearity

  • Why might a hall effect sensor be a poor distance sensor?
  • If the magnet moves 1mm 10mm away from the sensor vs 2mm from the sensor

22 of 34

Linearity

  • Hall-effect example
  • Zero-point is at roughly 2000 ADC counts - note that our derivative is zero here!
    • If we move 1mm at point B, our signal will change only slightly
    • If we move 1mm at point A our signal will change a lot!

23 of 34

Linearity

  • To deal with nonlinearities you can:
    • Ignore and accept some amount of error (see straight line in last slide)
    • Fit an analytical model to your data and solve it every measurement (slow but memory efficient)
    • Make a lookup table (fast but memory demanding)

24 of 34

Configuring Advanced Input Devices

Let's change gears and talk about configuring a more sophisticated sensor with its own logic and communication protocol.

We might want to do this because:

  • Analog signals are susceptible to noise
    • If our sensor is far from the ADC or there's noisy stuff near it...
  • Limited number of ADC channels to measure lots of sensors
  • Maybe our sensor allows us to configure/optimize it for our application?

25 of 34

Configuring Advanced Input Devices

Let's use TI's TMAG5170-Q1 3-Axis Linear Hall Effect Sensor With SPI Interface as a case study for the rest of recitation.

  • It measures flux in 3 different axes!
  • It can perform temperature compensation!
  • It can be configured to measure smaller flux with higher accuracy or higher flux with lower accuracy!

26 of 34

Configuring Advanced Input Devices

27 of 34

Configuring Advanced Input Devices

  • Here's how we communicate with this device, it happens to use SPI, but more on that in networking and communications week...

28 of 34

Configuring Advanced Input Devices

  • Look for a register map in the datasheet

29 of 34

Configuring Advanced Input Devices

  • Think of configuring a register as flipping groups of switches
  • Each register is composed of 16 switches
  • One switch could give us two states for one parameter (2^1 = 1)
  • Or 3 switches could give us 8 states for one parameter (2^3 = 8)

30 of 34

Configuring Advanced Input Devices

  • When we have everything configured, we will just ask the device what's in a result register

31 of 34

Configuring Advanced Input Devices

  • Let's look at some code and an example

32 of 34

Configuring Advanced Input Devices

  • If this feels overwhelming you can generally find an input device that fits your requirements sold by Sparkfun or Adafruit with a breakout board and a library already written for it.
  • For example if you want to measure force, the HX711 Load Cell Amplifier is a great place to start, and it has a very simple arduino library.
  • Let's look at their guide briefly

33 of 34

Configuring Advanced Input Devices

#include "HX711.h"

#define calibration_factor -7050.0 // This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT 3

#define CLK 2

HX711 scale;

void setup() {

Serial.begin(9600);

Serial.println("HX711 scale demo");

scale.begin(DOUT, CLK);

scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch

scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

Serial.println("Readings:");

}

void loop() {

Serial.print("Reading: ");

Serial.print(scale.get_units(), 1); //scale.get_units() returns a float

Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor

Serial.println();

}

34 of 34

Questions