Analog Sensors

The buttons and toggle switch are considered binary inputs, because they can only communicate one of two states (up/down for the buttons, open/closed for the switch). There exists a different class of inputs called analog inputs. Unlike the buttons and switch, these analog inputs can communicate a range of values.

Light, Sound, and Temperature

The Circuit Playground has three basic analog sensors, each of which takes an analog input and converts it into a value that is usable by the computer.

analog sensors

  1. The Light Sensor (represented in code as lightSensor) is used to measure the amount of ambient light. When it is completely dark, lightSensor.value will read as 0, and when it is fully bright it will read 1023.
  2. The Temperature Sensor (represented in code as tempSensor) measures the temperature. Instead of providing a raw value (like the other two sensors do), this sensor takes advantage of the fact the we already have commonly understood ways to convert temperature into a computable value. The tempSensor.F and tempSensor.C provide the temperature reading in either Fahrenheit or Celsius, respectively.
  3. The Sound Sensor (represented in code as soundSensor) is used to measure noise. It's important to note that this is not really a microphone; don't expect to record sound with the sound sensor, it's only able to measure the volume of sound it detects. Like the Light Sensor, this provides a value ranging from 0 to 1023.

Reading Sensor Data

The sound and light sensors each have a property called value that allows you to read the current input value of a sensor (such as with soundSensor.value). If the full input range of 0-1023 doesn't meet the needs of your program, you can change the range of numbers returned using the setScale() method. For example, soundSensor.setScale(-50, 50) will change the range of the sound sensor so that the lowest number is -50 and the highest is 50. This doesn't apply to the temperature sensor, which has two properties, F and C that report the temperature in a format that is both computable and human readable.

Sensor Sensitivity

The sensors on your boards are relatively blunt instruments. While more expensive and consistent sensors exist for applications that require high fidelity values (such as medical or scientific tools), these sensors just provide a rough approximation.

You may find that your programs work a bit differently across different boards, because the sensors on each board are giving different readings. One way to account for this is to write code that allows you calibrate your sensors for different boards. If your app has code that should be run if the sensor reading goes above a certain value, consider making that value a variable that can be changed from the user interface. This way the user can modify the variable so that it works best with their board.

Found a bug in the documentation? Let us know at documentation@code.org