Under The Hood

So you might have noticed we didn't use one of those CircuitPlayground.something() functions to get our analog value. Why not, and how did it work without doing so?

The answer to the second question is pretty easy - we just use the Arduino library. The Circuit Playground library is built (largely) on top of the Arduino library and 'hides' the details of using its various bells and whistles. However, you can still use the Arduino library if you want (and know what you're doing).

No real good answer to the other question. This could've been done, say with something like CircuitPlayground.analogRead() or CircuitPlayground.giveMeTheAnalogPlease() or whatever. But this would have likely done nothing more than just call the same Arduino function. So, guess it just never made the cut.

However, there are a few of the Circuit Playground sensors that are essentially the same as the potentiometer we used in this guide, namely the light sensor , sound sensor , and temperature sensor. They all output a voltage that varies with the physical quantity they sense. In order for this to be used in the Circuit Playground, those signals are fed into ADCs, just like we did with the potentiometer. And that's pretty much all there is to it.

Don't believe me? Well, take a look...

Light Sensor

Here's the circuit path for the output of the light sensor. The big red square that says 'THERMAL' is the brain of the Circuit Playground.

And here's the code for the CircuitPlayground.lightSensor() function. (direct link to repo)

Download: file
uint16_t Adafruit_CircuitPlayground::lightSensor(void) {
  return analogRead(CPLAY_LIGHTSENSOR);
}

Sound Sensor

Here's the circuit path for the output of the soud sensor (microphone off screen).

And here's the code for the CircuitPlayground.soundSensor() function. (direct link to repo)

Download: file
uint16_t Adafruit_CircuitPlayground::soundSensor(void) {
  return analogRead(CPLAY_SOUNDSENSOR);
}

Pretty boring. Nothing fancy or magical. Both the light sensor and sound sensor just call analogRead() for the pins they are attached to and return the value. And now you know why those functions return values from 0 to 1023. It's just the raw 10 bit analog to digital conversion.

Temperature Sensor

Here's the circuit path for the output of the temperature sensor.

And here's the code for the CircuitPlayground.temperature() function. (direct link to repo)

Download: file
float Adafruit_CircuitPlayground::temperature(void) {
   // Thermistor test
  float reading;

  reading = analogRead(CPLAY_THERMISTORPIN);

  //Serial.print("Thermistor reading: "); Serial.println(reading);

  // convert the value to resistance
  reading = ((1023.0 * SERIESRESISTOR) / reading);
  reading -= SERIESRESISTOR;

  //Serial.print("Thermistor resistance: "); Serial.println(reading);

  float steinhart;
  steinhart = reading / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C

  return steinhart;
}

OK, a bit more going on. But not really. Note that pretty much the first thing that happens is to read the analog value. The rest is just math to turn that into a temperature. In this case, that's more useful than the raw analog value. For the light and sound sensors, there's no really useful thing to convert to, so those just return the raw values.

This guide was first published on Feb 11, 2017. It was last updated on Feb 11, 2017. This page (Under The Hood) was last updated on Nov 16, 2019.