{"id":1069,"date":"2014-07-22T07:44:50","date_gmt":"2014-07-22T11:44:50","guid":{"rendered":"https:\/\/itp.nyu.edu\/physicalcomputing\/?page_id=1069"},"modified":"2022-06-23T08:00:52","modified_gmt":"2022-06-23T12:00:52","slug":"sensors-the-basics","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/lessons\/sensors-the-basics\/","title":{"rendered":"Sensors: the Basics"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Sensors<\/strong> convert various forms of physical energy into electrical energy, allowing microcontrollers to read changes in the physical world.<\/p>\n\n\n\n<p>The simplest sensors read changes in mechanical energy, usually by moving electrical contacts. For example the pushbutton or <a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380345304\" target=\"_blank\">switch (related video)<\/a> converts mechanical energy (e.g, your finger&#8217;s press) into electrical energy by closing a connection between two metal contacts. The <a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380345253\" target=\"_blank\">potentiometer (related video)<\/a>&nbsp;shown in Figure 1 and Figure 2 is another sensor that reads mechanical energy changes: a metal contact called a wiper slides along a resistor, effectively short circuiting the <a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380345253\" target=\"_blank\">resistor (related video)<\/a> into two halves and creating a voltage divider circuit.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistor_inside.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistor_inside-1024x1024.png\" alt=\"Photo showing the inside of a potentiometer\" class=\"wp-image-1833\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistor_inside-1024x1024.png 1024w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistor_inside-150x150.png 150w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistor_inside-300x300.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Figure 1. A potentiometer broken open, showing the carbon substrate and wiper.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/potentiometer_inside-e1408827451166.png\"><img loading=\"lazy\" decoding=\"async\" width=\"253\" height=\"359\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/potentiometer_inside-e1408827451166.png\" alt=\"Drawing of the inside of a potentiometer. The wiper and carbon resistor are shown, and a symbol of a variable resistor is shown at the bottom of the drawing.\" class=\"wp-image-1826\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/potentiometer_inside-e1408827451166.png 253w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/potentiometer_inside-e1408827451166-211x300.png 211w\" sizes=\"(max-width: 253px) 85vw, 253px\" \/><\/a><figcaption>Figure 2. Inside a potentiometer, the wiper, attached to the center contact, slides along the carbon resistor, forming effectively two resistors in series.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Although switches and pushbuttons typically only read an on state or an off state, most other sensors can read a wide range of possible states. In other words, they&#8217;re <strong>analog sensors<\/strong>, because they read a variable change. Whenever you use a new sensor, the first thing you need to understand is what the range is that it can read and deliver. When you&#8217;re using your sensor in an application, you need to know the range of possible values your application requires. Do you just need high, medium, and low (3 possible values), or a range from 0 to 10? Do you need a 100-point range? The range that you need depends on what your user can perceive. For example, if you&#8217;re making a lighting dimmer, and your user can only perceive about a dozen different light levels, then you don&#8217;t need to give her a sensor that can give her a 1000-point range of control.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Resistive_Sensors\"><\/span>Resistive&nbsp;Sensors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380354290\" target=\"_blank\">Related Video: Sensors &#8211; Survey 1<\/a><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistive_sensors.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1752\" height=\"1023\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistive_sensors.png\" alt=\"Resistive sensors. Stretch sensor, top; flex sensor, middle; force-sensing resistor, bottom\" class=\"wp-image-1837\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistive_sensors.png 1752w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistive_sensors-300x175.png 300w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/resistive_sensors-1024x597.png 1024w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Figure 3. Resistive sensors. Stretch sensor, top; flex sensor, middle and bottom; force-sensing resistor, bottom<\/figcaption><\/figure><\/div>\n\n\n\n<p>Many sensors work by converting the energy they read into a changing electrical resistance by using a variably resistive material at their heart. Examples of resistive sensors are shown in Figure 3. For example, force sensors and stretch sensors are made of a partially conductive rubber. When the rubber is stretched or compressed, the resistance change. Photoresistors or light dependent resistors change their resistance when exposed to a change in light energy. In order to read changes in resistance, you typically place these sensors in a voltage divider circuit , which converts the resistance change into a changing voltage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Optical_Sensors\"><\/span>Optical Sensors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380354290\" target=\"_blank\">Related video: Light Sensors<\/a><\/p>\n\n\n\n<p>Light&nbsp;is used in many sensors in a variety of ways. Light-emitting diodes, light-dependent resistors, and phototransistors &nbsp;can be combined to sense dust, measure distance, or determine reflected color. &nbsp;Light is also used in some ranging sensors to determine distance from a sensor to a target.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ranging_Sensors\"><\/span>Ranging Sensors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380353950\" target=\"_blank\">Related video: Ranging Sensors<\/a><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensors.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1172\" height=\"1023\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensors.png\" alt=\"Ranging sensors: ultrasonic ranger, top; infrared ranger, bottom.\" class=\"wp-image-1836\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensors.png 1172w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensors-300x261.png 300w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensors-1024x893.png 1024w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><figcaption>Figure 4. Ranging sensors: ultrasonic ranger, top; infrared ranger, bottom.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Many sensors measure movement or distance indirectly, by sending out a pulse of light or sound and reading the reflected signal when it bounces off a target as illustrated in Figure 5. These are called <strong>ranging sensors<\/strong>&nbsp;(Figure 4) because they read a range of distance.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensor.png\"><img loading=\"lazy\" decoding=\"async\" width=\"423\" height=\"228\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensor.png\" alt=\"Drawing of a ranging sensor, showing energy waves radiating out, then bouncing off a human figure back to the sensor.\" class=\"wp-image-1821\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensor.png 423w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/ranging_sensor-300x161.png 300w\" sizes=\"(max-width: 423px) 85vw, 423px\" \/><\/a><figcaption>Figure 5. Ranging sensors work by bouncing energy off the target and reading the reflection.<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"MEMS_Sensors\"><\/span>MEMS Sensors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380353950\" target=\"_blank\">Related video: MEMS Sensors<\/a><\/p>\n\n\n\n<p>Still other sensors work by converting the energy they read into a change in capacitance. For example, accelerometers and other miniaturized electromechanical (MEMS) sensors typically have a tiny moving conductive mass at their core, suspended on tiny springs and surrounded on both sides by electrical contacts. Because the conductive mass&nbsp;is parallel to the outer contacts, a capacitance builds up between the contact. When the mass&nbsp;is moved, the capacitance changes between the two sides, effectively creating two variable capacitors as illustrated in Figure 6. That variable capacitor is then placed in a resistor-capacitor circuit to convert the change in capacitance into a changing voltage.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/mems_sensor.png\"><img loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"300\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/mems_sensor-220x300.png\" alt=\"Diagram of a MEMS sensor, showing a moving mass that moves a capacitive plate in between two other capacitive surfaces.\" class=\"wp-image-1817\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/mems_sensor-220x300.png 220w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/mems_sensor.png 303w\" sizes=\"(max-width: 220px) 85vw, 220px\" \/><\/a><figcaption>Figure 6. MEMS sensors often form a variable capacitor by moving a capacitive plate in between two other capacitive surfaces.<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Digital_Interface_Sensors\"><\/span>Digital Interface Sensors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380351165\" target=\"_blank\">Related video: Sensors &#8211; Interfacing<\/a><\/p>\n\n\n\n<p>Other than switches and variable resistors, most sensors you buy are integrated circuits that include the resistance-to-voltage or capacitance-to-voltage circuit and provide you with an analog voltage output. Some will even include an analog-to-digital converter and provide you with a serial data interface, either <a rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-serial-communication\/i2c-communication-with-an-infrared-temperature-sensor\/\" target=\"_blank\">I2C<\/a>, <a rel=\"noopener noreferrer\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-serial-communication\/lab-spi-communication-aith-a-digital-potentiometer\/\" target=\"_blank\">SPI<\/a>, or <a rel=\"noreferrer noopener\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/serial-communication-the-basics\/\" target=\"_blank\">asynchronous serial<\/a>, so that you can connect the sensor to the serial ports of your microcontroller. Still others will provide a changing pulse output, or <a rel=\"noreferrer noopener\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/analog-output\/\" target=\"_blank\">pulse width modulation (PWM) output<\/a>, where the width of the pulse represents&nbsp;the sensor value.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-longdesc\"><img loading=\"lazy\" decoding=\"async\" width=\"2472\" height=\"1818\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/I2C_sensors.jpg\" alt=\"Photo of sensors with digital interfaces\" class=\"wp-image-9035\" srcset=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/I2C_sensors.jpg 2472w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/I2C_sensors-768x565.jpg 768w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/I2C_sensors-1536x1130.jpg 1536w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/I2C_sensors-2048x1506.jpg 2048w, https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/I2C_sensors-1200x883.jpg 1200w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption>Figure 7. A collection of sensors with digital interfaces. Clockwise, from top: AS7341 spectrometer; LIS3DH accelerometer; VL53L0X Time-of-flight distance sensor; Max3010x particle detector. These sensors are on small breakout boards which can connect to a solderless breadboard.  They all connect to a microcontroller using SPI or I2C.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Data_Sheets\"><\/span>Data Sheets<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380351140\" target=\"_blank\">Related Video: Datasheets<\/a><\/p>\n\n\n\n<p>When you shop for sensors, you need to look at the data sheet to learn how they operate. The data sheet will usually include the following essential facts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a text description of the sensor and its operation;<\/li><li>a pin diagram to tell you what pins perform what functions;<\/li><li>a table of electrical characteristics that tells you what the supply voltage is, what the operating current is, and what the output is, along with other electrical and physical properties;<\/li><li>a graph or conversion formula that relates the input energy to the output energy;<\/li><li>a mechanical description of the sensor itself.<\/li><\/ul>\n\n\n\n<p>Some datasheets will include other information as well, such as application circuits, reliability data, and so forth.<\/p>\n\n\n\n<p>You should follow the datasheet&#8217;s guideline for sensor operation. Don&#8217;t exceed the maximum or minimum operating voltage, and make sure to supply adequate current to operate the sensor. Avoid supplying too much current, because the excess current will get turned into heat, which often changes the sensor&#8217;s operating characteristics. Make sure you understand the sensor&#8217;s interface. Be clear on whether it&#8217;s an analog electrical property like resistance, capacitance or voltage, or a digital serial data interface. Look for typical application circuits and microcontroller code samples online if you can find them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_a_Sensor_With_a_Microcontroller\"><\/span>Using a Sensor With a Microcontroller<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380354933\" target=\"_blank\">Related video: Sensors &#8211; Testing<\/a><\/p>\n\n\n\n<p>Once you understand how a sensor works and what its output will be, you should connect it to your microcontroller and write a simple program to read the output. Don&#8217;t jump right into writing a complex program. Write the smallest program necessary to read the sensor&#8217;s changing values and output them for debugging purposes. Then put the sensor in the physical context in which you plan to use it and read the output values in action. The range that a sensor can read will change depending on the specific conditions or actions that you plan to read. So make sure the physical setting of the sensor is as close to reality as possible in order to test it. Once you know the range of values that it&#8217;s going to output in those conditions, then you can write a function to convert the sensor&#8217;s raw output range into a range you can use. Depending on the context, you probably won&#8217;t get the full output range that the sensor is capable of delivering. So your conversion function should be based on the range you&#8217;re actually seeing, not the total range.<\/p>\n\n\n\n<p>You probably don&#8217;t need to convert the sensor&#8217;s readings into its output voltage or its physical property. For example, if you&#8217;re using a force sensing resistor, you probably don&#8217;t need to know how many Newtons of force are being exerted on the sensor, or what the output voltage is. Instead, you probably just need to know whether someone is pressing gently, firmly, or really firmly against the sensor. Perhaps you just need a range from 0 to 10. When you write your conversion function, consider what the relevant result is for you, and write a function that delivers that result.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Sensors convert various forms of physical energy into electrical energy, allowing microcontrollers to read changes in the physical world. The simplest sensors read changes in mechanical energy, usually by moving electrical contacts. For example the pushbutton or switch (related video) converts mechanical energy (e.g, your finger&#8217;s press) into electrical energy by closing a connection &hellip; <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/sensors-the-basics\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sensors: the Basics&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":13,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[20,21,25,5,7,19],"tags":[],"class_list":["post-1069","page","type-page","status-publish","hentry","category-analog","category-components","category-digital","category-electronics","category-sensors","category-switches"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1069"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=1069"}],"version-history":[{"count":25,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1069\/revisions"}],"predecessor-version":[{"id":9848,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/1069\/revisions\/9848"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=1069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=1069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=1069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}