Analog Input with “trimmer” threshold calibration

trimmer-breadboard trimmer-schematic

This circuit allows for easy adjustment of a threshold you define for some sensor input. For example, you use a photocell to detect ambient light; if it gets dark enough, play a sound. You define the threshold in code as some number, but when you install the piece in a different space with different ambient light, the threshold needs to be changed. Rather than need to open up your piece, connect a computer, and reprogram, you can just turn a knob to “trim” the threshold value up or down.

You can test the above circuit using a potentiometer instead of the force sensor shown, remember it can be a simple voltage divider like the trimmer is, or you can use 2 legs and a fixed resistor.

For the fixed resistor, try between 1/10 and 1/2 of the highest value of your variable resistor. E.g. if you’re using a 5k variable resistor (or part of a potentiometer), which goes between 0 and 5000 ohms, try a 500-2500 ohm fixed resistor with it (a 1K prob easiest to find).

The optimal value for you math people is
SQRT (minimum * maximum)
https://ericjformanteaching.wordpress.com/2013/02/04/voltage-divider-circuit/

In practice the true minimum might be zero which messes up the equation, and the true maximum might be in the millions, with a very steep curve. So it’s important to test the real-world boundaries. It usually doesn’t matter too much if you are fairly close to optimal. The only way to really tell what’s going on is to graph the “response curve” – i.e. the resulting voltage output as you interact with the sensor – either with an oscilloscope or by sending arduino data to processing. We’ll do the latter in class soon.

/*
 Analog input with "trimmer" calibration; serial output

 The circuit:
   - analog input connected to analog pin 0.
     - If potentiometer, center pin to analog 0, side pins to +5V and ground
     - If variable resistor, one pin to +5V, other pin to both analog 0 AND
       to a fixed resistor to ground
   - trim pot to analog pin 1 (as voltage divider, see above)
 */

 int threshold = 250;

 void setup() {
  Serial.begin(9600);
 }

 void loop() {
   // read the analog input into a variable:
   int analogValue = analogRead(0);

   // read the trimpot analog input:
   int trimmer = analogRead(1);

   // convert trimmer # into a range which will be added to analogValue:
   trimmer = map(trimmer,0,1023,-50,50);

   // print the result:
   Serial.print(analogValue);
   Serial.print(" + ");
   Serial.println(trimmer);

   // if analogValue is over the "trimmed" threshold, do something:
   if(analogValue > (threshold + trimmer)) {
     Serial.println("HEY!!!!!!!!!!!!!!!!!!!");
   }
 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: