{"id":142,"date":"2013-07-08T17:30:36","date_gmt":"2013-07-08T21:30:36","guid":{"rendered":"http:\/\/julianvidal.com\/blog\/?p=142"},"modified":"2013-07-08T17:30:36","modified_gmt":"2013-07-08T21:30:36","slug":"calibrator-an-arduino-library-to-calibrate-sensors-hooked-to-analog-inputs","status":"publish","type":"post","link":"https:\/\/julianvidal.com\/blog\/calibrator-an-arduino-library-to-calibrate-sensors-hooked-to-analog-inputs\/","title":{"rendered":"Calibrator: An Arduino library to calibrate sensors hooked to analog inputs"},"content":{"rendered":"<p>Once you get past your first few projects with the Arduino, you soon realize that the <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/Calibration\">calibration method<\/a> they show on their webpage is just a sample and cannot be used with many sensors without polluting your code with a ton of variables.<\/p>\n<p>So, here it is. My own take on sensor calibration library. You can <a href=\"https:\/\/github.com\/poisa\/Calibrator\">download<\/a> the source code and a more detailed explanation on the github <a href=\"https:\/\/github.com\/poisa\/Calibrator\">Calibrator<\/a> page.<\/p>\n<p>This is how you use it:<\/p>\n<pre class=\"lang:arduino decode:true \">#include &lt;Calibrator.h&gt;\r\n\r\nCalibrator calibrator;\r\n\r\nint sensorPin = A0; \/\/ The sensor we want to calibrate\r\nint ledPin = 13;    \/\/ Will indicate when calibration is going on\r\n\r\nvoid setup()\r\n{\r\n    pinMode(ledPin, OUTPUT);\r\n    digitalWrite(ledPin, HIGH); \/\/ Turn LED on\r\n    Serial.begin(9600);\r\n\r\n    \/\/ Reset the calibrator. You only need to call reset() if you restart calibration again but\r\n    \/\/ if calibration is only run once like in this example is not needed. It doesn't hurt to \r\n    \/\/ call it here though. \r\n    calibrator.reset();\r\n\r\n    \/\/ Run calibration automatically during the first \r\n    \/\/ five seconds (or 5000 milliseconds) your code runs.\r\n    \/\/ It is important that during calibration you \"exercise\" your sensors to measure both \r\n    \/\/ ends of their range.\r\n    while (millis() &lt; 5000) {\r\n        calibrator.setValue(analogRead(sensorPin));\r\n    }\r\n    digitalWrite(ledPin, LOW); \/\/ Turn LED off\r\n}\r\n\r\nvoid loop()\r\n{\r\n    \/\/ In your code analogRead(sensorPin) will give you the uncalibrated value\r\n    \/\/ and calibrator.getValue(analogRead(sensorPin)) will give you the calibrated value \r\n\r\n    Serial.print(\"Sensor value: \");\r\n    Serial.print(analogRead(sensorPin));\r\n    Serial.print(\" , Calibrator value: \");\r\n    Serial.println(calibrator.getValue(analogRead(sensorPin)));\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once you get past your first few projects with the Arduino, you soon realize that the calibration method they show on their webpage is just a sample and cannot be used with many sensors without polluting your code with a ton of variables. So, here it is. My own take on sensor calibration library. You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":145,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1,18,2],"tags":[7,25,24],"class_list":["post-142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","category-featured","category-programming","tag-arduino","tag-c","tag-sensors"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/julianvidal.com\/blog\/wp-content\/uploads\/arduino.jpg?fit=600%2C600&quality=89&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/palO2H-2i","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":5,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions\/148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/media\/145"}],"wp:attachment":[{"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julianvidal.com\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}